3 Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/base.html">
12 * @fileoverview Splay tree used by CodeMap.
14 tv.exportTo('tracing.importer.v8', function() {
16 * Constructs a Splay tree. A splay tree is a self-balancing binary
17 * search tree with the additional property that recently accessed
18 * elements are quick to access again. It performs basic operations
19 * such as insertion, look-up and removal in O(log(n)) amortized time.
23 function SplayTree() { };
26 * Pointer to the root node of the tree.
28 * @type {SplayTree.Node}
31 SplayTree.prototype.root_ = null;
34 * @return {boolean} Whether the tree is empty.
36 SplayTree.prototype.isEmpty = function() {
41 * Inserts a node into the tree with the specified key and value if
42 * the tree does not already contain a node with the specified key. If
43 * the value is inserted, it becomes the root of the tree.
45 * @param {number} key Key to insert into the tree.
46 * @param {*} value Value to insert into the tree.
48 SplayTree.prototype.insert = function(key, value) {
50 this.root_ = new SplayTree.Node(key, value);
53 // Splay on the key to move the last node on the search path for
54 // the key to the root of the tree.
56 if (this.root_.key == key) {
59 var node = new SplayTree.Node(key, value);
60 if (key > this.root_.key) {
61 node.left = this.root_;
62 node.right = this.root_.right;
63 this.root_.right = null;
65 node.right = this.root_;
66 node.left = this.root_.left;
67 this.root_.left = null;
74 * Removes a node with the specified key from the tree if the tree
75 * contains a node with this key. The removed node is returned. If the
76 * key is not found, an exception is thrown.
78 * @param {number} key Key to find and remove from the tree.
79 * @return {SplayTree.Node} The removed node.
81 SplayTree.prototype.remove = function(key) {
83 throw Error('Key not found: ' + key);
86 if (this.root_.key != key) {
87 throw Error('Key not found: ' + key);
89 var removed = this.root_;
90 if (!this.root_.left) {
91 this.root_ = this.root_.right;
93 var right = this.root_.right;
94 this.root_ = this.root_.left;
95 // Splay to make sure that the new root has an empty right child.
97 // Insert the original right child as the right child of the new
99 this.root_.right = right;
106 * Returns the node having the specified key or null if the tree doesn't
107 * contain a node with the specified key.
110 * @param {number} key Key to find in the tree.
111 * @return {SplayTree.Node} Node having the specified key.
113 SplayTree.prototype.find = function(key) {
114 if (this.isEmpty()) {
118 return this.root_.key == key ? this.root_ : null;
122 * @return {SplayTree.Node} Node having the minimum key value.
124 SplayTree.prototype.findMin = function() {
125 if (this.isEmpty()) {
128 var current = this.root_;
129 while (current.left) {
130 current = current.left;
136 * @return {SplayTree.Node} Node having the maximum key value.
138 SplayTree.prototype.findMax = function(opt_startNode) {
139 if (this.isEmpty()) {
142 var current = opt_startNode || this.root_;
143 while (current.right) {
144 current = current.right;
150 * @return {SplayTree.Node} Node having the maximum key value that
151 * is less or equal to the specified key value.
153 SplayTree.prototype.findGreatestLessThan = function(key) {
154 if (this.isEmpty()) {
157 // Splay on the key to move the node with the given key or the last
158 // node on the search path to the top of the tree.
160 // Now the result is either the root node or the greatest node in
162 if (this.root_.key <= key) {
164 } else if (this.root_.left) {
165 return this.findMax(this.root_.left);
172 * @return {Array<*>} An array containing all the values of tree's nodes
176 SplayTree.prototype.exportKeysAndValues = function() {
178 this.traverse_(function(node) { result.push([node.key, node.value]); });
183 * @return {Array<*>} An array containing all the values of tree's nodes.
185 SplayTree.prototype.exportValues = function() {
187 this.traverse_(function(node) { result.push(node.value); });
192 * Perform the splay operation for the given key. Moves the node with
193 * the given key to the top of the tree. If no node has the given
194 * key, the last node on the search path is moved to the top of the
195 * tree. This is the simplified top-down splaying algorithm from:
196 * "Self-adjusting Binary Search Trees" by Sleator and Tarjan
198 * @param {number} key Key to splay the tree on.
201 SplayTree.prototype.splay_ = function(key) {
202 if (this.isEmpty()) {
205 // Create a dummy node. The use of the dummy node is a bit
206 // counter-intuitive: The right child of the dummy node will hold
207 // the L tree of the algorithm. The left child of the dummy node
208 // will hold the R tree of the algorithm. Using a dummy node, left
209 // and right will always be nodes and we avoid special cases.
210 var dummy, left, right;
211 dummy = left = right = new SplayTree.Node(null, null);
212 var current = this.root_;
214 if (key < current.key) {
218 if (key < current.left.key) {
220 var tmp = current.left;
221 current.left = tmp.right;
229 right.left = current;
231 current = current.left;
232 } else if (key > current.key) {
233 if (!current.right) {
236 if (key > current.right.key) {
238 var tmp = current.right;
239 current.right = tmp.left;
242 if (!current.right) {
247 left.right = current;
249 current = current.right;
255 left.right = current.left;
256 right.left = current.right;
257 current.left = dummy.right;
258 current.right = dummy.left;
259 this.root_ = current;
263 * Performs a preorder traversal of the tree.
265 * @param {function(SplayTree.Node)} f Visitor function.
268 SplayTree.prototype.traverse_ = function(f) {
269 var nodesToVisit = [this.root_];
270 while (nodesToVisit.length > 0) {
271 var node = nodesToVisit.shift();
276 nodesToVisit.push(node.left);
277 nodesToVisit.push(node.right);
282 * Constructs a Splay tree node.
284 * @param {number} key Key.
285 * @param {*} value Value.
287 SplayTree.Node = function(key, value) {
293 * @type {SplayTree.Node}
295 SplayTree.Node.prototype.left = null;
298 * @type {SplayTree.Node}
300 SplayTree.Node.prototype.right = null;