Nodes 是树形数据结构的类协议。遵循 Node
协议的 class
将获得一些有用的属性,以便轻松地在树中导航。
创建一个新的 class
并使其遵循 Node
协议
final class SimpleNode: Node {
typealias Value = String
var value: Value
weak var parent: SimpleNode?
var children: [SimpleNode]
init(value: Value) {
self.value = value
self.children = []
}
}
extension SimpleNode: Equatable, CustomStringConvertible {
static func == (lhs: SimpleNode, rhs: SimpleNode) -> Bool {
return lhs.value == rhs.value && lhs.parent == rhs.parent
}
public var description: String {
return "\(value)"
}
}
创建一个根节点
let root = SimpleNode(value: "Hand")
使用 addChild(node: Node)
添加子节点
root.addChild(node: SimpleNode(value: "Thumb"))
root.addChild(node: SimpleNode(value: "Index finger"))
root.addChild(node: SimpleNode(value: "Middle finger"))
root.addChild(node: SimpleNode(value: "Ring finger"))
root.addChild(node: SimpleNode(value: "Little finger"))
将树打印到控制台
print(root.lineBasedDescription)
结果
Hand
├── Thumb
├── Index finger
├── Middle finger
├── Ring finger
└── Little finger
/// Returns all parent nodes.
var ancestors: [Node]
/// Returns all parent nodes, including the current node.
var ancestorsIncludingSelf: [Node]
/// A Boolean value indicating whether the current node is the top node.
var isRoot: Bool
/// Returns the top node.
var root: Node
/// Adds a sub-node.
func addChild(node: Node)
/// Returns the number of children.
var degree: Int
/// Returns all descendants, traversing the entire tree.
var descendants: [Node]
/// A Boolean value indicating whether the node is without children.
var isLeaf: Bool
/// Returns all nodes with no children.
var leaves: [Node]
/// Returns the number of leaves.
var breadth: Int
/// A Boolean value indicating whether the node has children.
var isBranch: Bool
/// Returns all nodes with at least one child.
var branches: [Node]
/// Returns all other nodes with the same parent.
var siblings: [Node]
/// Returns all nodes (including the current node) with the same parent.
var siblingsIncludingSelf: [Node]
/// Returns the distance between a node and the root.
var depth: Int
/// The number of edges between the current node and the root.
var level: Int
var lineBasedDescription: String
let root = SimpleNode(value: "Apple")
let desktops = SimpleNode(value: "Desktops")
root.addChild(node: desktops)
let macPro = SimpleNode(value: "Mac Pro")
desktops.addChild(node: macPro)
let macMini = SimpleNode(value: "Mac Mini")
desktops.addChild(node: macMini)
let iMac = SimpleNode(value: "iMac")
desktops.addChild(node: iMac)
let notebooks = SimpleNode(value: "Notebooks")
root.addChild(node: notebooks)
let macBookPro = SimpleNode(value: "MacBook Pro")
notebooks.addChild(node: macBookPro)
let devices = SimpleNode(value: "Devices")
root.addChild(node: devices)
let handhelds = SimpleNode(value: "Handhelds")
devices.addChild(node: handhelds)
let ipod = SimpleNode(value: "iPod")
handhelds.addChild(node: ipod)
let iphone = SimpleNode(value: "iPhone")
handhelds.addChild(node: iphone)
let newton = SimpleNode(value: "Newton")
handhelds.addChild(node: newton)
let setTopBoxes = SimpleNode(value: "Set-top boxes")
devices.addChild(node: setTopBoxes)
let appleTV = SimpleNode(value: "Apple TV")
setTopBoxes.addChild(node: appleTV)
print(root.lineBasedDescription)
输出
Apple
├── Desktops
│ ├── Mac Pro
│ ├── Mac Mini
│ └── iMac
├── Notebooks
│ └── MacBook Pro
└── Devices
├── Handhelds
│ ├── iPod
│ ├── iPhone
│ └── Newton
└── Set-top boxes
└── Apple TV
Nodes 在 MIT 许可证下发布。