TreeNode
A node in a generic, mutable n-ary tree. Each node holds a value, a reference to its parent and an ordered list of children.
Iterating a node (via iterator, or the lazy asSequence/preOrderSequence extensions) visits the node and all of its descendants. Traversal and the height/nodeCount/clear helpers are implemented iteratively, so they are safe on arbitrarily deep trees.
Not thread-safe. Nodes are mutable (addChild/removeChild/clear mutate the structure and parent pointers). Sharing a tree across threads requires external synchronization, and the tree must not be modified while it is being iterated.
Equality is by reference (identity); use the structurallyEquals extension to compare two trees by value and shape.
Parameters
the value stored in this node.
the default traversal order used by iterator. Prefer the asSequence(order) / preOrderSequence() extensions to choose an order without mutating state.
Functions
Returns an iterator over this node and its descendants using the default treeIterator order. Use iterator with an explicit order, or the asSequence(order) extension, to traverse in a different order without changing this node's default.
Returns an iterator over this node and its descendants in the given order.
Returns the chain of nodes from descendant up to and including this node, or null if descendant is not a strict descendant of this node.
Renders this subtree as a multi-line string, one node per line, with branch connectors.
Removes the direct child at the given index, detaching it (its parent becomes null).
Sorts this node's direct children in place according to the given comparator. Only the immediate children are reordered; their subtrees are left untouched.