Constructor
new BinarySearchTree(valueopt)
Parameters:
PropertiesName | Type | Attributes | Description |
---|---|---|---|
value | Array.<number> | | <optional> | The value to start the tree |
Name | Type | Description |
---|---|---|
root | BSTNode | | The root node of the tree |
count | number | The number of nodes in the tree |
Example
const bst = new BinarySearchTree();
bst.add(1);
bst.add(0);
bst.delete(0);
Methods
add(value)
Add a new value to the Tree
Parameters:
Name | Type | Description |
---|---|---|
value | number | a number to add |
Example
const bst = new BinarySearchTree();
bst.add(1);
bst.add(100);
delete(value, returnNodeopt) → {BSTNode|BinarySearchTree}
Delete the node with the given value
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
value | number | the value to delete | ||
returnNode | boolean | <optional> | false | if the node should be returned |
Returns:
if the value is in the Tree
- Type:
- BSTNode |
BinarySearchTree
larger(value) → {number|undefined|null}
Finds the largest value in the tree.
- If a value is passed it will look for the largest value from that branch;
- If no value is passed, it will start from the root;
Parameters:
Name | Type | Description |
---|---|---|
value | number | the value to start searching |
Returns:
largest value
- Type:
- number |
undefined | null
search(value, returnNodeopt) → {BSTNode|null}
- Returns
false
if the number does not exist or if there is an error - If the number exists, return its node/true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
value | number | the value to find | ||
returnNode | boolean | <optional> | true | if the node should be returned |
Returns:
if the value is in the Tree
- Type:
- BSTNode |
null
smaller(valueopt) → {number|undefined|null}
Finds the smallest value in the tree.
- If a value is passed it will look for the smallest value from that branch;
- If no value is passed, it will start from the root;
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
value | number | | <optional> | the value to start searching |
Returns:
smaller value
- Type:
- number |
undefined | null
toString() → {string}
Returns a string representation of the tree
Returns:
the string representation
- Type:
- string