Package-level declarations

Types

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
data class TreeConnectors(val branch: String, val vertical: String, val lastBranch: String, val empty: String)

The four glyph strings used to draw the tree branches in prettyString.

Link copied to clipboard
open class TreeNode<T>(val value: T, val treeIterator: TreeNodeIterators = PreOrder) : Iterable<TreeNode<T>> , ChildDeclarationInterface<T>

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.

Properties

Link copied to clipboard

The number of direct children of this node.

Link copied to clipboard

true when this node has no children.

Functions

Link copied to clipboard
fun <T> TreeNode<T>.allNodes(predicate: (T) -> Boolean): Boolean

true if every node's value matches predicate. Short-circuits.

Link copied to clipboard

The chain of ancestors from the immediate parent up to (and including) the root.

Link copied to clipboard
fun <T> TreeNode<T>.anyNode(predicate: (T) -> Boolean): Boolean

true if any node's value matches predicate. Short-circuits.

Link copied to clipboard
fun <T> TreeNode<T>.asSequence(order: TreeNodeIterators = TreeNodeIterators.PreOrder): Sequence<TreeNode<T>>

Lazily traverses this subtree in the given order as a Sequence, without forcing the whole traversal up front. Pairs with the Kotlin stdlib, e.g. root.asSequence().map { it.value }.firstOrNull { it == target }.

Link copied to clipboard
fun <T> TreeNode<T>.contains(value: T): Boolean

Returns true when this subtree contains a node whose value equals value, including the receiver itself. Values are compared with == (equals).

Link copied to clipboard
fun <T> TreeNode<T>.countNodes(predicate: (T) -> Boolean): Int

Counts nodes whose value matches predicate.

Link copied to clipboard

Returns an independent deep copy of this subtree (same values, same shape, new nodes).

Link copied to clipboard

All nodes in this subtree except this node, in pre-order.

Link copied to clipboard
fun <T> TreeNode<T>.distance(other: TreeNode<T>): Int?

The number of edges on the shortest path between this node and other.

Link copied to clipboard
fun <T> TreeNode<T>.filterNodes(predicate: (T) -> Boolean): List<TreeNode<T>>

All nodes (pre-order) whose value matches predicate.

Link copied to clipboard
fun <T> TreeNode<T>.findNode(predicate: (T) -> Boolean): TreeNode<T>?

Returns the first node (pre-order) whose value matches predicate, or null. Short-circuits.

Link copied to clipboard
fun <T, R> TreeNode<T>.foldNodes(initial: R, operation: (acc: R, node: TreeNode<T>) -> R): R

Folds operation over all nodes in pre-order, starting from initial.

Link copied to clipboard

All leaf nodes in this subtree, in pre-order.

Link copied to clipboard

Lazy level-order (breadth-first) traversal as a Sequence.

Link copied to clipboard

The lowest (deepest) node that is an ancestor of both this node and other, where every node is considered an ancestor of itself.

Link copied to clipboard
fun <T, R> TreeNode<T>.mapValues(transform: (T) -> R): TreeNode<R>

Returns a new tree with the same shape whose values are produced by transform. The original is left untouched. Stack-safe (iterative), so it handles arbitrarily deep trees.

Link copied to clipboard

The shortest path of nodes from this node to other, inclusive of both endpoints.

Link copied to clipboard

Lazy post-order traversal as a Sequence.

Link copied to clipboard

Lazy pre-order traversal as a Sequence.

Link copied to clipboard
fun <T> TreeNode<T>.prettyString(connectors: TreeConnectors = TreeConnectors.Default, render: (value: T, depth: Int, isLast: Boolean) -> String = { value, _, _ -> "$value" }): String

Renders this subtree as a multi-line string, one node per line, with branch connectors.

Link copied to clipboard
fun <T> TreeNode<T>.root(): TreeNode<T>

Walks up the parent chain and returns the topmost ancestor (the tree root).

Link copied to clipboard

The other children of this node's parent (excludes this node). Empty for the root.

Link copied to clipboard

Structural equality: true when other holds the same values in the same shape. Unlike TreeNode's reference equality, this compares the entire subtree. Stack-safe.

Link copied to clipboard
inline fun <T> tree(root: T, defaultIterator: TreeNodeIterators = TreeNodeIterators.PreOrder, childDeclaration: ChildDeclaration<T>): TreeNode<T>

This method can be used to initialize new tree.