Implement a BinarySearchTree class that stores unique numbers. For every node, all values in its left subtree are smaller and all values in its right subtree are larger. Think of it as the index behind a timeline component that needs to insert timestamps, remove them, and list them in order.
const timeline = new BinarySearchTree();
timeline.insert(1700); // true
timeline.insert(1200); // true
timeline.insert(2400); // true
timeline.insert(1200); // false (already present)
timeline.has(2400); // true
timeline.min(); // 1200
timeline.remove(1700); // true (removing a node with two children)
timeline.inOrder(); // [1200, 2400]
timeline.size(); // 2
| Method | Returns | Description |
|---|---|---|
insert(value) | boolean | Adds value; returns false if it was already present. |
has(value) | boolean | Whether value is in the tree. |
remove(value) | boolean | Removes value; returns false if it wasn't present. |
min() / max() | number | undefined | Smallest / largest value, undefined when empty. |
inOrder() | number[] | All values in ascending order. |
size() | number | Number of values. |
insert, has, remove, min and max should be O(h), where h is the tree height. You don't need to keep the tree balanced.≥ x (a "ceiling" query)?Spotted a mistake or a rights concern? Report an issue