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:
The maximum node of the tree.
- Type
- Node
findMin() → {Node}
Finds the node with minimum value in the whole tree.
Returns:
The minimum node of the tree.
- Type
- Node
getDiameter() → {Number}
Finds the diameter of the binary tree.
Returns:
The longest path in the BST.
- Type
- Number
getHeight() → {Number}
Returns the height of the tree.
Returns:
The height of the tree.
- Type
- Number
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:
Whether the tree is balanced or not.
- Type
- Boolean
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:
The lowest common ancestor of the two nodes or null.
- Type
- Node
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:
True/false depending
on whether the given node is removed.
- Type
- Boolean