* Inserts a node into the tree with the specified key and value if * the tree does not already contain a node with the specified key. If * the value is inserted, it becomes the root of the tree. * * @param {number} key Key to insert into the tree. * @param {*} value Value to insert in
(key, value)
| 61 | * @param {*} value Value to insert into the tree. |
| 62 | */ |
| 63 | insert(key, value) { |
| 64 | if (this.isEmpty()) { |
| 65 | this.root_ = new SplayTreeNode(key, value); |
| 66 | return; |
| 67 | } |
| 68 | // Splay on the key to move the last node on the search path for |
| 69 | // the key to the root of the tree. |
| 70 | this.splay_(key); |
| 71 | if (this.root_.key == key) return; |
| 72 | |
| 73 | const node = new SplayTreeNode(key, value); |
| 74 | if (key > this.root_.key) { |
| 75 | node.left = this.root_; |
| 76 | node.right = this.root_.right; |
| 77 | this.root_.right = null; |
| 78 | } else { |
| 79 | node.right = this.root_; |
| 80 | node.left = this.root_.left; |
| 81 | this.root_.left = null; |
| 82 | } |
| 83 | this.root_ = node; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Removes a node with the specified key from the tree if the tree |
no test coverage detected