@itpropro/tree-structure-ts / Exports / TreeNode
Represents a node in a tree structure.
Name |
---|
T |
• new TreeNode<T
>(value
, parent?
)
Creates a new TreeNode instance.
Name |
---|
T |
Name | Type | Default value | Description |
---|---|---|---|
value |
T |
undefined |
The value of the node. |
parent |
null | TreeNode <T > |
null |
The parent of the node. |
• children: TreeNode
<T
>[]
The children of the node.
• parent: null
| TreeNode
<T
>
The parent of the node.
• value: T
The value of the node.
▸ addChild(value
): TreeNode
<T
>
Adds a child node to the current node.
Name | Type | Description |
---|---|---|
value |
T |
The value of the child node. |
TreeNode
<T
>
The new child node.
▸ all(): TreeNode
<T
>[]
Gets all nodes in the tree below the current node.
TreeNode
<T
>[]
An array of TreeNode instances.
▸ getPath(): TreeNode
<T
>[]
Gets the path from the root node to the current node.
TreeNode
<T
>[]
An array of TreeNode instances.
▸ hasChildren(): boolean
Checks if the current node has any child nodes.
boolean
true
if the node has children, false
otherwise.
▸ hasSiblings(): boolean
Checks if the current node has any siblings.
boolean
true
if the node has siblings, false
otherwise.
▸ isRoot(): boolean
Checks if the current node is the root node.
boolean
true
if the node is the root node, false
otherwise.
▸ remove(): null
| TreeNode
<T
>
Removes the current node from the tree.
null
| TreeNode
<T
>
The new current node after removing the current node.
▸ traverse(callback
, traversal
): void
Traverses the tree starting from the current node.
Name | Type | Description |
---|---|---|
callback |
(node : TreeNode <T >) => void |
A function to be called for each visited node. |
traversal |
"breadthFirst" | "depthFirst" | "preOrder" | "postOrder" |
true to traverse the tree in depth-first order, false for breadth-first order. |
void