Node
A node in a generic tree structure.
Each node holds an arbitrary value, an optional parent link, and a list of child nodes (stored in the node's array part, sized by width).
Nodes track their own depth (distance from the root) and expose helpers to link/unlink from a parent, traverse the subtree, and measure its size.
Constructor
Node(value: T, parent: ( Node<T> | nil )): Node<T>Fields
| Name | Type | Description |
|---|---|---|
value | T | The value stored in the node. |
parent? | Node<T> | The parent node, or nil if this node is a root (not currently linked to anything). |
depth | number | How many links deep this node is from its root (root has depth 0). |
width | number | The number of direct children this node has. |
Methods
link | Links this node to a parent node, appending it to the parent's child list and updating this node's depth to parent.dept… |
size | Returns the size of the node tree. |
traverse | Walks this node and every descendant in depth-first, parent-before-child order, calling callback once for each node vis… |
unlink | Unlinks this node from its parent, removing it from the parent's child list and resetting this node's depth to 0. |
#Node:link
function Node:link(parent: Node<T>)
Links this node to a parent node, appending it to the parent's child list and updating this node's depth to parent.depth + 1.
Throws if parent is (or is a descendant of) this node, since that would create a cycle. Existing children of this node are re-linked afterward so their depths stay consistent with the change.
| Name | Type | Description |
|---|---|---|
parent | Node<T> | The parent node to link to. |
#Node:size
function Node:size(): integer
Returns the size of the node tree.
| Type | Name | Description |
|---|---|---|
integer | size | The size of the node tree. |
#Node:traverse
function Node:traverse(callback: fun( node: Node<T> ))
Walks this node and every descendant in depth-first, parent-before-child order, calling callback once for each node visited.
| Name | Type | Description |
|---|---|---|
callback | fun( node: Node<T> ) | The callback invoked for each node in the subtree. |
#Node:unlink
function Node:unlink()
Unlinks this node from its parent, removing it from the parent's child list and resetting this node's depth to 0.
Any children of this node are then re-linked to it so their depths stay consistent with the change.