Skip to content

Latest commit

 

History

History
250 lines (136 loc) · 4.66 KB

TreeNode.md

File metadata and controls

250 lines (136 loc) · 4.66 KB

@itpropro/tree-structure-ts / Exports / TreeNode

Class: TreeNode<T>

Represents a node in a tree structure.

Type parameters

Name
T

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new TreeNode<T>(value, parent?)

Creates a new TreeNode instance.

Type parameters

Name
T

Parameters

Name Type Default value Description
value T undefined The value of the node.
parent null | TreeNode<T> null The parent of the node.

Defined in

TreeNode.ts:25

Properties

children

children: TreeNode<T>[]

The children of the node.

Defined in

TreeNode.ts:13


parent

parent: null | TreeNode<T>

The parent of the node.

Defined in

TreeNode.ts:18


value

value: T

The value of the node.

Defined in

TreeNode.ts:8

Methods

addChild

addChild(value): TreeNode<T>

Adds a child node to the current node.

Parameters

Name Type Description
value T The value of the child node.

Returns

TreeNode<T>

The new child node.

Defined in

TreeNode.ts:36


all

all(): TreeNode<T>[]

Gets all nodes in the tree below the current node.

Returns

TreeNode<T>[]

An array of TreeNode instances.

Defined in

TreeNode.ts:46


getPath

getPath(): TreeNode<T>[]

Gets the path from the root node to the current node.

Returns

TreeNode<T>[]

An array of TreeNode instances.

Defined in

TreeNode.ts:59


hasChildren

hasChildren(): boolean

Checks if the current node has any child nodes.

Returns

boolean

true if the node has children, false otherwise.

Defined in

TreeNode.ts:75


hasSiblings

hasSiblings(): boolean

Checks if the current node has any siblings.

Returns

boolean

true if the node has siblings, false otherwise.

Defined in

TreeNode.ts:83


isRoot

isRoot(): boolean

Checks if the current node is the root node.

Returns

boolean

true if the node is the root node, false otherwise.

Defined in

TreeNode.ts:91


remove

remove(): null | TreeNode<T>

Removes the current node from the tree.

Returns

null | TreeNode<T>

The new current node after removing the current node.

Defined in

TreeNode.ts:99


traverse

traverse(callback, traversal): void

Traverses the tree starting from the current node.

Parameters

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.

Returns

void

Defined in

TreeNode.ts:119