ReferenceTreeProtocol

public protocol ReferenceTreeProtocol : AnyObject

Interface for nodes in tree structures.

Provides default implementations for many tree structure operations.

Useable only by final class types.

Instance Properties

  • Parent node.

    Declaration

    Swift

    var parent: Self? { get set }
  • Child nodes.

    Declaration

    Swift

    var children: [Self] { get set }
  • isLeaf Default implementation

    Default Implementation

    Declaration

    Swift

    var isLeaf: Bool { get }

    Return Value

    true if there are no children. Otherwise, false.

  • isContainer Default implementation

    Default Implementation

    Declaration

    Swift

    var isContainer: Bool { get }

    Return Value

    true if there is at least one child. Otherwise, false.

  • isRoot Default implementation

    Default Implementation

    Declaration

    Swift

    var isRoot: Bool { get }

    Return Value

    true if there is no parent. Otherwise, false.

  • leaves Default implementation

    All leaves.

    Default Implementation

    Declaration

    Swift

    var leaves: [Self] { get }
  • root Default implementation

    Root.

    Default Implementation

    Declaration

    Swift

    var root: Self { get }
  • pathToRoot Default implementation

    Array of all Node objects between (and including) self up to root.

    Default Implementation

    Array of all Node objects between (and including) self up to root.

    Declaration

    Swift

    var pathToRoot: [Self] { get }
  • depth Default implementation

    Depth of node.

    Default Implementation

    Depth of node.

    Declaration

    Swift

    var depth: Int { get }
  • height Default implementation

    Height of node.

    Default Implementation

    Height of node.

    Declaration

    Swift

    var height: Int { get }
  • heightOfTree Default implementation

    Height of containing tree.

    Default Implementation

    Height of containing tree.

    Declaration

    Swift

    var heightOfTree: Int { get }

Instance Methods

  • addChild(_:) Default implementation

    Append a child node.

    Default Implementation

    Add the given node to children.

    Declaration

    Swift

    func addChild(_ node: Self)
  • addChildren(_:) Default implementation

    Append an array of Child nodes.

    Default Implementation

    Append the given nodes to children.

    Declaration

    Swift

    func addChildren(_ nodes: [Self])
  • insertChild(_:at:) Default implementation

    Insert the given child node at the given index.

    Throws

    ReferenceTreeError.Error.insertionError if the given index is out of bounds.

    Default Implementation

    Insert the given node at the given index of children.

    Throws

    ReferenceTreeError.insertionError if index is out of bounds.

    Declaration

    Swift

    func insertChild(_ node: Self, at index: Int) throws
  • removeChild(_:) Default implementation

    Remove the given child node.

    Throws

    ReferenceTreeError.Error.removalError if the given node is not contained herein.

    Default Implementation

    Remove the given node from children.

    Throws

    ReferenceTreeError.removalError if the given node is not held in children.

    Declaration

    Swift

    func removeChild(_ node: Self) throws
  • removeChild(at:) Default implementation

    Remove the child node at the given index.

    Throws

    ReferenceTreeError.Error.removalError if the given index is out of bounds.

    Default Implementation

    Remove the node at the given index.

    Throws

    ReferenceTreeError.removalError if index is out of bounds.

    Declaration

    Swift

    func removeChild(at index: Int) throws
  • child(at:) Default implementation

    Default Implementation

    Declaration

    Swift

    func child(at index: Int) -> `Self`?

    Return Value

    Child node at the given index, if present. Otherwise, nil.

  • leaf(at:) Default implementation

    Default Implementation

    Declaration

    Swift

    func leaf(at index: Int) -> `Self`?

    Return Value

    Returns the leaf node at the given index, if present. Otherwise, nil.

  • hasChild(_:) Default implementation

    Default Implementation

    Declaration

    Swift

    func hasChild(_ child: Self) -> Bool

    Return Value

    true if the given node is contained herein. Otherwise, false.

  • hasLeaf(_:) Default implementation

    Default Implementation

    Declaration

    Swift

    func hasLeaf(_ node: Self) -> Bool

    Return Value

    true if the given node is a leaf. Otherwise, false.

  • hasAncestor(_:) Default implementation

    Default Implementation

    Declaration

    Swift

    func hasAncestor(_ node: Self) -> Bool

    Return Value

    true if the given node is an ancestor. Otherwise, false.

  • ancestor(at:) Default implementation

    Default Implementation

    Declaration

    Swift

    func ancestor(at distance: Int) -> `Self`?

    Return Value

    Ancestor at the given distance, if present. Otherwise, nil.

  • hasDescendent(_:) Default implementation

    Default Implementation

    Declaration

    Swift

    func hasDescendent(_ node: Self) -> Bool

    Return Value

    true if the given node is a descendent. Otherwise, false.