Tree
public enum Tree<Branch, Leaf>
extension Tree: CustomStringConvertible
extension Tree: Equatable where Leaf: Equatable, Branch: Equatable
extension Tree: Hashable where Leaf: Hashable, Branch: Hashable
A value-semantic, immutable Tree structure with two generic types for the branches and leaves.
Example Usage
A Tree can be used in pretty simple cases:
let justALeaf: Tree<(),Double> = .leaf(3.14159265359)
Or more nested cases:
let happyTree = Tree.branch("alpha", [
.leaf(1),
.branch("beta", [
.leaf(2),
.leaf(3),
.leaf(4)
]),
.leaf(5),
.branch("gamma", [
.leaf(6),
.branch("delta", [
.leaf(7),
.leaf(8)
])
])
])
-
The leaf case.
Declaration
Swift
case leaf(Leaf) -
The branch case, containing the value for this node, as well as all of its children nodes.
Declaration
Swift
indirect case branch(Branch, [Tree])
-
Declaration
Swift
@inlinable public var leaves: [Leaf] { get }Return Value
The values contained by the leaves of this
Tree.let tree = Tree.branch(“root”, [ .leaf(0), .branch(“internal”, [ .leaf(1) ]), .leaf(2) ]) tree.leaves // => [0,1,2]
-
Declaration
Swift
@inlinable public var height: Int { get }Return Value
The number of edges on the longest path between the root and a leaf.
let tree = Tree.branch(0, [ .leaf(1), .branch(2, [ .leaf(1), .leaf(2) ]), .leaf(3) ]) tree.height // => 2
-
Declaration
Swift
public var paths: [[Either<Branch, Leaf>]] { get }Return Value
All of the values along the paths from this node to each leaf.
-
Replace the subtree at the given
indexfor the giventree.Throws
TreeErrorifselfis aleaf.Declaration
Swift
public func replacingTree(at index: Int, with tree: Tree) throws -> Tree -
Replace the subtree at the given
path.Throws
TreeErrorif the givenpathis valid.Declaration
Swift
public func replacingTree(through path: [Int], with tree: Tree) throws -> Tree -
Throws
TreeErrorin the case of ill-formed index paths and indexes out-of-range.Declaration
Swift
public func inserting(_ tree: Tree, through path: [Int] = [], at index: Int) throws -> TreeReturn Value
A new
Treewith the giventreeinserted at the givenindex, through the givenpath. -
Declaration
Swift
@inlinable public func mapLeaves<T>(_ transform: @escaping (Leaf) -> T) -> Tree<Branch, T>Return Value
A
Treewith leaves updated by the giventransform. -
Declaration
Swift
@inlinable public func zipLeaves <C: RangeReplaceableCollection> (_ collection: C) -> Tree<Branch, C.Element>Return Value
A
Treewith its leaves replaced by the elements in the givencollection. -
Undocumented
Declaration
Swift
@inlinable public func zipLeaves <C: RangeReplaceableCollection, T> ( _ collection: C, _ transform: @escaping (Leaf, C.Element) -> T ) -> Tree<Branch,T> -
Declaration
Swift
@inlinable public func map<B, L>(_ transform: Transform<B, L>) -> Tree<B, L>Return Value
A
Treewith its branches and leaves modified by the giventransform.
-
Transforms for
See morebranchandleafcases.Declaration
Swift
public struct Transform<B, L>
-
Printed description.
Declaration
Swift
public var description: String { get }
-
The payload of a given
Tree.Declaration
Swift
@inlinable public var value: Leaf { get } -
Create a single-depth
TreeNode.branchwith leaves defined by a givenSequenceparameretized overT.In the case of initializing with an empty array:
let tree = Tree(1, [])A branch is created, populated with a single value matching the given
value:self = .branch(value, [.leaf(value)])Declaration
Swift
@inlinable public init<S>(_ value: Leaf, _ sequence: S) where Leaf == S.Element, S : Sequence -
Declaration
Swift
@inlinable public func updating(value: Leaf) -> TreeReturn Value
A new
Treewith the givenvalueas payload. -
Apply a given
transformto all nodes in aTree.Declaration
Swift
@inlinable public func map<Result>(_ transform: (Leaf) -> Result) -> Tree<Result, Result>
View on GitHub
Install in Dash
Tree Enumeration Reference