Members
count
Get the total number of nodes in the tree. May not be same as the depth or height of the tree.
- Source:
max
Returns the node having the "largest" value of data in the entire tree.
- Source:
min
Returns the node having the "smallest" value of data in the entire tree.
- Source:
Methods
add(data) → {BinaryTreeNode}
Inserts the a new node into the tree and attachs the given data
to it.
data
must be a type which supports logical operations of ==
, <=
, >=
.
- If the tree is empty, the newly created node is added as root.
- If the
data
is already in a tree node (== equality), simply increments thecopies
value of that BinaryTreeNode (node.metadata.copies
).
Returns the newly added node.
Parameters:
Name | Type | Description |
---|---|---|
data |
any |
- Source:
find(data) → (nullable) {BinaryTreeNode}
Returns the tree node containing data
(== equality) or null
if none.
Parameters:
Name | Type | Description |
---|---|---|
data |
BinaryTreeNode |
- Source:
remove(data, copies) → (nullable) {BinaryTreeNode}
Removes the node having data
if it's the only copy left (node.metadata.copies
is 1).
To delete some copies of data
in the node, simply set copies
param to
a numeric value. If this value is more than node.metadata.copies
than the
node is removed from the tree.
By default, deletes all copies of data
in the node and removes node from the tree.
If the removed node had children, they are assigned new parent
node(s).
If no node is found, returns null
. Otherwise, returns the removed/modified node.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
data |
any | ||
copies |
number | * |
number of copies to delete; deletes all by default |
- Source:
(generator) searchBreadthFirst()
Breadth-first searches the tree level by level starting at the root. It visits the root, then the children of the root, then their children and so on. So for a tree of shape:
10
/ \
5 30
/ / \
4 15 40
/
3
Breadth-first traversal would look like: 10, 5, 30, 4, 15, 40, 3
Use breadth-first search when the node you are looking for is likely to be nearby the root.
- Source:
(generator) searchDepthFirst()
Depth-first search starts from the root and goes as deep as it can until it finds a leaf node. Then it visits all the remaining nodes it encountered along the path, going as deep as possible in each branch.
For a tree of shape:
10
/ \
5 30
/ / \
4 15 40
/
3
Depth-first traversal would look like:
- In-order (left-root-right) -> 3, 4, 5, 10, 15, 30, 40 see traverseInOrderly
- Pre-order (root-left-right) -> 10, 5, 4, 3, 30, 15, 40 see traversePreOrderly
- Post-order (left-right-root) -> 3, 4, 5, 15, 40, 30, 10 see traversePostOrderly
This function returns result similar to a pre-order traversal but uses a Stack to achieve that.
Use depth-first search when the node you are looking for is likely to be far from the root.
- Source:
toString() → {string}
Returns a JSON string representation of the tree.
- Source:
(generator) traverseInOrderly(node)
For a binary tree, an in-order traversal returns values sorted in ascending order.
Parameters:
Name | Type | Description |
---|---|---|
node |
BinaryTreeNode |
- Source:
(generator) traversePostOrderly(node)
Post-order traversal is used to:
- delete the tree because it visits the children before removing the parent
- get the postfix expression of an expression tree, as in the reverse Polish notation
Parameters:
Name | Type | Description |
---|---|---|
node |
BinaryTreeNode |
- Source:
(generator) traversePreOrderly(node)
For a binary tree, a pre-order traversal creates a copy of the tree. If the tree is used as an expression tree, a pre-order traversal will yield a prefix expression of the tree (as used in the Polish notation).
Parameters:
Name | Type | Description |
---|---|---|
node |
BinaryTreeNode |
- Source: