Members
isEmpty
Indicates if the BST is empty
max
Returns max element in the BST
min
Returns min element in the BST
size
Returns number of element in the BST
Methods
add(key, valueopt) → {boolean}
Insert a new element in the BST
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
number | Unique key for the element | ||
value |
any |
<optional> |
null | Value of the element, null if not provided |
Throws:
-
- If Element already exists in the BST
- Type
- Error
Returns:
- True - If element was added
- Type
- boolean
clear()
Clears the BST
get(keyopt) → {TreeElement}
Returns an existing element in the BST
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
number |
<optional> |
null | Unique key for the element, if null, return root of BST |
Throws:
-
- If element is not found
- Type
- Error
Returns:
- Requested element, root of key is not provided
- Type
- TreeElement
has(key) → {boolean}
Indicates if an element exists in the BST
Parameters:
Name | Type | Description |
---|---|---|
key |
number | Unique key for the element |
Returns:
- True if element exists, false if element does not exists
- Type
- boolean
remove(key, typeopt) → {boolean}
Removes an existing element in the BST
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
number | Unique key for the element | ||
type |
string |
<optional> |
predecessor | Specifies delete type, allows 'predecessor' (default) for in-order predecessor, and 'successor' for in-order successor |
Throws:
-
- If delete type is invalid
- Type
- Error
Returns:
- True - If element was added; False if element does not exists
- Type
- boolean
toArray(key, traversalopt) → {Array.<number>}
Returns an array containing the key of the elements in the BST
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
number | Unique key for the element | ||
traversal |
string |
<optional> |
"in-order" | Specifies Traversal type, allows 'in-order' (default), 'pre-order', 'post-order', and 'reverse-in-order |
Returns:
- Array of element keys
- Type
- Array.<number>
update(key, valueopt) → {boolean}
Updates an existing element in the BST
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
number | Unique key for the element | ||
value |
any |
<optional> |
null | Value of the element, null if not provided |
Throws:
-
- If element is not found
- Type
- Error
Returns:
- True - If element was edited
- Type
- boolean