Methods
find(value)
Finds a node by it's value.
Average time complexity: O(log N).
Average time complexity: O(log N).
Parameters:
Name | Type | Description |
---|---|---|
value |
Number
|
String
|
of the node which should be found. |
findMax() → {Node}
Finds the node with maximum value in the whole tree.
Returns:
- Type:
-
Node
The maximum node of the tree.
findMin() → {Node}
Finds the node with minimum value in the whole tree.
Returns:
- Type:
-
Node
The minimum node of the tree.
getDiameter() → {Number}
Finds the diameter of the binary tree.
Returns:
- Type:
-
Number
The longest path in the BST.
getHeight() → {Number}
Returns the height of the tree.
Returns:
- Type:
-
Number
The height of the tree.
inorder(callback)
In-order traversal of the whole binary search tree.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function
|
Callback which will be called for each traversed node. |
insert(value, current)
Inserts a node into the binary search tree.
Time complexity: O(log N) in the average case and O(N) in the worst case.
Time complexity: O(log N) in the average case and O(N) in the worst case.
Parameters:
Name | Type | Description |
---|---|---|
value |
Number
|
String
|
Node value. |
current |
Node
|
Current node. |
isBalanced() → {Boolean}
Returns whether the BST is balanced.
Returns:
- Type:
-
Boolean
Whether the tree is balanced or not.
lowestCommonAncestor(firstNode, secondNode) → {Node}
Finds the lowest common ancestor of two nodes.
Parameters:
Name | Type | Description |
---|---|---|
firstNode |
Node
|
First node to be considered when checking for ancestor. |
secondNode |
Node
|
Second node to be considered when checking for ancestor. |
Returns:
- Type:
-
Node
The lowest common ancestor of the two nodes or null.
postorder(callback)
Post-order traversal of the whole tree.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function
|
Callback which will be called for each traversed node. |
preorder(callback)
Pre-order preorder traversal of the whole tree.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function
|
Callback which will be called for each traversed node. |
remove(node) → {Boolean}
Removes node from the tree.
Average runtime complexity: O(log N).
Average runtime complexity: O(log N).
Parameters:
Name | Type | Description |
---|---|---|
node |
Node
|
to be removed |
Returns:
- Type:
-
Boolean
True/false depending
on whether the given node is removed.