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)
        ])
    ])
])

Cases

  • 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])
  • Navigate an immutable n-ary Tree structure.

    See more

    Declaration

    Swift

    public struct Zipper

Instance Properties

  • 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.

Instance Methods

  • Replace the subtree at the given index for the given tree.

    Throws

    TreeError if self is a leaf.

    Declaration

    Swift

    public func replacingTree(at index: Int, with tree: Tree) throws -> Tree
  • Replace the subtree at the given path.

    Throws

    TreeError if the given path is valid.

    Declaration

    Swift

    public func replacingTree(through path: [Int], with tree: Tree) throws -> Tree
  • Throws

    TreeError in 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 -> Tree

    Return Value

    A new Tree with the given tree inserted at the given index, through the given path.

  • Declaration

    Swift

    @inlinable
    public func mapLeaves<T>(_ transform: @escaping (Leaf) -> T) -> Tree<Branch, T>

    Return Value

    A Tree with leaves updated by the given transform.

  • Declaration

    Swift

    @inlinable
    public func zipLeaves <C: RangeReplaceableCollection> (_ collection: C)
        -> Tree<Branch, C.Element>

    Return Value

    A Tree with its leaves replaced by the elements in the given collection.

  • 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 Tree with its branches and leaves modified by the given transform.

Errors

  • Things that can go wrong when doing things to a Tree.

    See more

    Declaration

    Swift

    public enum Error : Swift.Error

Assosciated Types

  • Transforms for branch and leaf cases.

    See more

    Declaration

    Swift

    public struct Transform<B, L>

CustomStringConvertible

  • Printed description.

    Declaration

    Swift

    public var description: String { get }

Single-Typed Tree

  • The payload of a given Tree.

    Declaration

    Swift

    @inlinable
    public var value: Leaf { get }
  • Create a single-depth TreeNode.branch with leaves defined by a given Sequence parameretized over T.

    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) -> Tree

    Return Value

    A new Tree with the given value as payload.

  • Apply a given transform to all nodes in a Tree.

    Declaration

    Swift

    @inlinable
    public func map<Result>(_ transform: (Leaf) -> Result) -> Tree<Result, Result>