blob: d272a9e18279bd5add5a95c0a33d784eb8a2cef7 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
Steve Blocka7e24c12009-10-30 11:49:00 +000029/**
30 * Constructs a Splay tree. A splay tree is a self-balancing binary
31 * search tree with the additional property that recently accessed
32 * elements are quick to access again. It performs basic operations
33 * such as insertion, look-up and removal in O(log(n)) amortized time.
34 *
35 * @constructor
36 */
Steve Block1e0659c2011-05-24 12:43:12 +010037function SplayTree() {
Steve Blocka7e24c12009-10-30 11:49:00 +000038};
39
40
41/**
42 * Pointer to the root node of the tree.
43 *
Steve Block1e0659c2011-05-24 12:43:12 +010044 * @type {SplayTree.Node}
Steve Blocka7e24c12009-10-30 11:49:00 +000045 * @private
46 */
Steve Block1e0659c2011-05-24 12:43:12 +010047SplayTree.prototype.root_ = null;
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49
50/**
51 * @return {boolean} Whether the tree is empty.
52 */
Steve Block1e0659c2011-05-24 12:43:12 +010053SplayTree.prototype.isEmpty = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +000054 return !this.root_;
55};
56
57
58
59/**
60 * Inserts a node into the tree with the specified key and value if
61 * the tree does not already contain a node with the specified key. If
62 * the value is inserted, it becomes the root of the tree.
63 *
64 * @param {number} key Key to insert into the tree.
65 * @param {*} value Value to insert into the tree.
66 */
Steve Block1e0659c2011-05-24 12:43:12 +010067SplayTree.prototype.insert = function(key, value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000068 if (this.isEmpty()) {
Steve Block1e0659c2011-05-24 12:43:12 +010069 this.root_ = new SplayTree.Node(key, value);
Steve Blocka7e24c12009-10-30 11:49:00 +000070 return;
71 }
72 // Splay on the key to move the last node on the search path for
73 // the key to the root of the tree.
74 this.splay_(key);
75 if (this.root_.key == key) {
76 return;
77 }
Steve Block1e0659c2011-05-24 12:43:12 +010078 var node = new SplayTree.Node(key, value);
Steve Blocka7e24c12009-10-30 11:49:00 +000079 if (key > this.root_.key) {
80 node.left = this.root_;
81 node.right = this.root_.right;
82 this.root_.right = null;
83 } else {
84 node.right = this.root_;
85 node.left = this.root_.left;
86 this.root_.left = null;
87 }
88 this.root_ = node;
89};
90
91
92/**
93 * Removes a node with the specified key from the tree if the tree
94 * contains a node with this key. The removed node is returned. If the
95 * key is not found, an exception is thrown.
96 *
97 * @param {number} key Key to find and remove from the tree.
Steve Block1e0659c2011-05-24 12:43:12 +010098 * @return {SplayTree.Node} The removed node.
Steve Blocka7e24c12009-10-30 11:49:00 +000099 */
Steve Block1e0659c2011-05-24 12:43:12 +0100100SplayTree.prototype.remove = function(key) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 if (this.isEmpty()) {
102 throw Error('Key not found: ' + key);
103 }
104 this.splay_(key);
105 if (this.root_.key != key) {
106 throw Error('Key not found: ' + key);
107 }
108 var removed = this.root_;
109 if (!this.root_.left) {
110 this.root_ = this.root_.right;
111 } else {
112 var right = this.root_.right;
113 this.root_ = this.root_.left;
114 // Splay to make sure that the new root has an empty right child.
115 this.splay_(key);
116 // Insert the original right child as the right child of the new
117 // root.
118 this.root_.right = right;
119 }
120 return removed;
121};
122
123
124/**
125 * Returns the node having the specified key or null if the tree doesn't contain
126 * a node with the specified key.
127 *
128 * @param {number} key Key to find in the tree.
Steve Block1e0659c2011-05-24 12:43:12 +0100129 * @return {SplayTree.Node} Node having the specified key.
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 */
Steve Block1e0659c2011-05-24 12:43:12 +0100131SplayTree.prototype.find = function(key) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 if (this.isEmpty()) {
133 return null;
134 }
135 this.splay_(key);
136 return this.root_.key == key ? this.root_ : null;
137};
138
139
140/**
Steve Block1e0659c2011-05-24 12:43:12 +0100141 * @return {SplayTree.Node} Node having the minimum key value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 */
Steve Block1e0659c2011-05-24 12:43:12 +0100143SplayTree.prototype.findMin = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 if (this.isEmpty()) {
145 return null;
146 }
147 var current = this.root_;
148 while (current.left) {
149 current = current.left;
150 }
151 return current;
152};
153
154
155/**
Steve Block1e0659c2011-05-24 12:43:12 +0100156 * @return {SplayTree.Node} Node having the maximum key value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 */
Steve Block1e0659c2011-05-24 12:43:12 +0100158SplayTree.prototype.findMax = function(opt_startNode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 if (this.isEmpty()) {
160 return null;
161 }
162 var current = opt_startNode || this.root_;
163 while (current.right) {
164 current = current.right;
165 }
166 return current;
167};
168
169
170/**
Steve Block1e0659c2011-05-24 12:43:12 +0100171 * @return {SplayTree.Node} Node having the maximum key value that
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 * is less or equal to the specified key value.
173 */
Steve Block1e0659c2011-05-24 12:43:12 +0100174SplayTree.prototype.findGreatestLessThan = function(key) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 if (this.isEmpty()) {
176 return null;
177 }
178 // Splay on the key to move the node with the given key or the last
179 // node on the search path to the top of the tree.
180 this.splay_(key);
181 // Now the result is either the root node or the greatest node in
182 // the left subtree.
183 if (this.root_.key <= key) {
184 return this.root_;
185 } else if (this.root_.left) {
186 return this.findMax(this.root_.left);
187 } else {
188 return null;
189 }
190};
191
192
193/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000194 * @return {Array<*>} An array containing all the values of tree's nodes paired
195 * with keys.
196 */
197SplayTree.prototype.exportKeysAndValues = function() {
198 var result = [];
199 this.traverse_(function(node) { result.push([node.key, node.value]); });
200 return result;
201};
202
203
204/**
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 * @return {Array<*>} An array containing all the values of tree's nodes.
206 */
Steve Block1e0659c2011-05-24 12:43:12 +0100207SplayTree.prototype.exportValues = function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 var result = [];
209 this.traverse_(function(node) { result.push(node.value); });
210 return result;
211};
212
213
214/**
215 * Perform the splay operation for the given key. Moves the node with
216 * the given key to the top of the tree. If no node has the given
217 * key, the last node on the search path is moved to the top of the
218 * tree. This is the simplified top-down splaying algorithm from:
219 * "Self-adjusting Binary Search Trees" by Sleator and Tarjan
220 *
221 * @param {number} key Key to splay the tree on.
222 * @private
223 */
Steve Block1e0659c2011-05-24 12:43:12 +0100224SplayTree.prototype.splay_ = function(key) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 if (this.isEmpty()) {
226 return;
227 }
228 // Create a dummy node. The use of the dummy node is a bit
229 // counter-intuitive: The right child of the dummy node will hold
230 // the L tree of the algorithm. The left child of the dummy node
231 // will hold the R tree of the algorithm. Using a dummy node, left
232 // and right will always be nodes and we avoid special cases.
233 var dummy, left, right;
Steve Block1e0659c2011-05-24 12:43:12 +0100234 dummy = left = right = new SplayTree.Node(null, null);
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 var current = this.root_;
236 while (true) {
237 if (key < current.key) {
238 if (!current.left) {
239 break;
240 }
241 if (key < current.left.key) {
242 // Rotate right.
243 var tmp = current.left;
244 current.left = tmp.right;
245 tmp.right = current;
246 current = tmp;
247 if (!current.left) {
248 break;
249 }
250 }
251 // Link right.
252 right.left = current;
253 right = current;
254 current = current.left;
255 } else if (key > current.key) {
256 if (!current.right) {
257 break;
258 }
259 if (key > current.right.key) {
260 // Rotate left.
261 var tmp = current.right;
262 current.right = tmp.left;
263 tmp.left = current;
264 current = tmp;
265 if (!current.right) {
266 break;
267 }
268 }
269 // Link left.
270 left.right = current;
271 left = current;
272 current = current.right;
273 } else {
274 break;
275 }
276 }
277 // Assemble.
278 left.right = current.left;
279 right.left = current.right;
280 current.left = dummy.right;
281 current.right = dummy.left;
282 this.root_ = current;
283};
284
285
286/**
287 * Performs a preorder traversal of the tree.
288 *
Steve Block1e0659c2011-05-24 12:43:12 +0100289 * @param {function(SplayTree.Node)} f Visitor function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 * @private
291 */
Steve Block1e0659c2011-05-24 12:43:12 +0100292SplayTree.prototype.traverse_ = function(f) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 var nodesToVisit = [this.root_];
294 while (nodesToVisit.length > 0) {
295 var node = nodesToVisit.shift();
296 if (node == null) {
297 continue;
298 }
299 f(node);
300 nodesToVisit.push(node.left);
301 nodesToVisit.push(node.right);
302 }
303};
304
305
306/**
307 * Constructs a Splay tree node.
308 *
309 * @param {number} key Key.
310 * @param {*} value Value.
311 */
Steve Block1e0659c2011-05-24 12:43:12 +0100312SplayTree.Node = function(key, value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 this.key = key;
314 this.value = value;
315};
316
317
318/**
Steve Block1e0659c2011-05-24 12:43:12 +0100319 * @type {SplayTree.Node}
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 */
Steve Block1e0659c2011-05-24 12:43:12 +0100321SplayTree.Node.prototype.left = null;
Steve Blocka7e24c12009-10-30 11:49:00 +0000322
323
324/**
Steve Block1e0659c2011-05-24 12:43:12 +0100325 * @type {SplayTree.Node}
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 */
Steve Block1e0659c2011-05-24 12:43:12 +0100327SplayTree.Node.prototype.right = null;