-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree.js
330 lines (289 loc) · 11.4 KB
/
binary-search-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2015 Kurt von Laven
/* BinarySearchTree maintains an arbitrary set of elements in sorted order.
* Performs in-order traversal in O(N) time; min, max, get, insertion, and
* removal in O(log N) time; and size in O(1) time. Nodes in the tree perform
* predecessor and successor in O(log N) time. The tree forbids repeated
* insertions with the same key. Assumes single-threaded execution. Many of
* these functions can also be implemented recursively, but their iterative
* implementations are favored for performance.
*/
// Creates a new binary search tree with no nodes.
var BinarySearchTree = function () {
this.root = null;
this.numNodes = 0;
};
/* BinarySearchTree.prototype.Node */
/* Creates a node in a binary search tree with a comparable key and an arbitrary
* value. A null parent indicates that this is the root of its tree. Nodes are
* sorted according to the key's natural order.
*/
BinarySearchTree.prototype.Node = function (key, value, parent) {
this.key = key;
this.value = value;
this.parent = parent;
this.leftChild = null;
this.rightChild = null;
};
/* Traverses the subtree rooted at this node in the sort order, meaning
* according to the key's natural order. Calls the given callback function once
* for each node with the current node as its only argument.
*/
BinarySearchTree.prototype.Node.prototype.traverse = function (callback) {
for (var ancestors = [this], currNode = this.leftChild; true; ) {
if (currNode === null) {
if (ancestors.length === 0) {
return;
}
currNode = ancestors.pop();
callback(currNode);
currNode = currNode.rightChild;
} else {
ancestors.push(currNode);
currNode = currNode.leftChild;
}
}
};
/* Returns the node in this node's tree that immediately precedes it in the sort
* order. The node returned has the greatest key of the nodes with keys less
* than this node's key. Returns null if this node's key is the min. Prefer
* BinarySearchTree.traverse when iterating over an entire tree and
* BinarySearchTree.prototype.Node.traverse when iterating over an entire
* subtree. The traverse methods run in O(N) time in the number of nodes being
* iterated over, while predecessor requires O(N * log N) time to traverse a
* subtree.
*/
BinarySearchTree.prototype.Node.prototype.predecessor = function () {
if (this.leftChild !== null) {
return this.leftChild.max();
}
for (var prevNode = this, currNode = this.parent; currNode !== null;
prevNode = currNode, currNode = currNode.parent) {
if (currNode.rightChild === prevNode) {
return currNode;
}
}
return null;
};
/* Returns the node in this node's tree that immediately succeeds it in the sort
* order. The node returned has the least key of the nodes with keys greater
* than this node's key. Returns null if this node's key is the max. Prefer
* BinarySearchTree.traverse when iterating over an entire tree and
* BinarySearchTree.prototype.Node.traverse when iterating over an entire
* subtree. The traverse methods run in O(N) time in the number of nodes being
* iterated over, while successor requires O(N * log N) time to traverse a
* subtree.
*/
BinarySearchTree.prototype.Node.prototype.successor = function () {
if (this.rightChild !== null) {
return this.rightChild.min();
}
for (var prevNode = this, currNode = this.parent; currNode !== null;
prevNode = currNode, currNode = currNode.parent) {
if (currNode.leftChild === prevNode) {
return currNode;
}
}
return null;
};
// Returns the node with the minimum key in the subtree rooted at this node.
BinarySearchTree.prototype.Node.prototype.min = function () {
var currNode = this;
for (; currNode.leftChild !== null; currNode = currNode.leftChild) {}
return currNode;
};
// Returns the node with the maximum key in the subtree rooted at this node.
BinarySearchTree.prototype.Node.prototype.max = function () {
var currNode = this;
for (; currNode.rightChild !== null; currNode = currNode.rightChild) {}
return currNode;
};
/* Returns the node with the given key in the subtree rooted at this node.
* Returns null if there is no such node.
*/
BinarySearchTree.prototype.Node.prototype.get = function (key) {
var currNode = this;
do {
if (key < currNode.key) {
currNode = currNode.leftChild;
} else if (key > currNode.key) {
currNode = currNode.rightChild;
} else {
return currNode;
}
} while (currNode !== null);
return null;
};
/* Inserts the given key, value pair in the subtree rooted at this node. Creates
* a new node with the given key and value and inserts it in the appropriate
* position. Returns the created node. Throws an exception if the given key is
* already in this subtree.
*/
BinarySearchTree.prototype.Node.prototype.insert = function (key, value) {
for (var currNode = this; true; ) {
if (key < currNode.key) {
if (currNode.leftChild === null) {
return currNode.leftChild =
new BinarySearchTree.prototype.Node(key, value, currNode);
}
currNode = currNode.leftChild;
} else if (key > currNode.key) {
if (currNode.rightChild === null) {
return currNode.rightChild =
new BinarySearchTree.prototype.Node(key, value, currNode);
}
currNode = currNode.rightChild;
} else {
throw 'Tried to insert key ' + key.toString() + ' twice.';
}
}
};
/* Places the given node at the position currently occupied by this node. Does
* not modify this node, but effectively removes it from its tree. All pointers
* in the tree to this node are replaced with pointers to the replacement node.
* Does not modify the given node's children.
*/
BinarySearchTree.prototype.Node.prototype.replaceWith = function (node) {
var parent = this.parent;
if (node !== null) {
node.parent = parent;
}
if (parent === null) {
return;
}
if (parent.leftChild === this) {
parent.leftChild = node;
return;
}
parent.rightChild = node;
};
/* Makes the left child of the given node the left child of this node. Does not
* modify the given node. Assumes the given node has a left child.
*/
BinarySearchTree.prototype.Node.prototype.adoptLeftChild = function (node) {
node.leftChild.parent = this;
this.leftChild = node.leftChild;
};
/* Makes the right child of the given node the right child of this node. Does
* not modify the given node. Assumes the given node has a right child.
*/
BinarySearchTree.prototype.Node.prototype.adoptRightChild = function (node) {
node.rightChild.parent = this;
this.rightChild = node.rightChild;
};
/* Removes this node from its tree. Returns the node that now occupies this
* node's position or null if there is no such node. Does not modify the removed
* node. References to nodes other than the removed node are guaranteed to
* remain valid.
*/
BinarySearchTree.prototype.Node.prototype.remove = function () {
var replacementNode;
if (this.rightChild === null) {
replacementNode = this.leftChild; // possibly null
} else if (this.leftChild === null) {
replacementNode = this.rightChild;
} else if (this.parent === null || this.parent.leftChild === this) {
replacementNode = this.leftChild.max(); // predecessor of this node
/* In the more common implementation of this case, the node being
* removed remains in place, but its key and value are overwritten by
* its predecessor's. However, that implementation invalidates
* references to the predecessor, so using it would violate this
* method's contract.
*/
if (replacementNode !== this.leftChild) {
replacementNode.replaceWith(replacementNode.leftChild);
replacementNode.adoptLeftChild(this);
}
replacementNode.adoptRightChild(this);
} else {
/* When the node being removed is a right child, replace it with its
* successor rather than its predecessor. The binary search tree's
* invariants would still be maintained if the successor were used
* instead, but using both approaches helps keep the tree balanced.
*/
replacementNode = this.rightChild.min(); // successor of this node
/* In the more common implementation of this case, the node being
* removed remains in place, but its key and value are overwritten by
* its successor's. However, that implementation invalidates references
* to the successor, so using it would violate this method's contract.
*/
if (replacementNode !== this.rightChild) {
replacementNode.replaceWith(replacementNode.rightChild);
replacementNode.adoptRightChild(this);
}
replacementNode.adoptLeftChild(this);
}
this.replaceWith(replacementNode);
return replacementNode;
};
/* BinarySearchTree */
/* Traverses this tree in the sort order, meaning according to the key's
* natural order. Calls the given callback function once for each node with the
* current node as its only argument.
*/
BinarySearchTree.prototype.traverse = function (callback) {
if (this.root !== null) {
this.root.traverse(callback);
}
};
/* Returns the node in this tree with the minimum key. Returns null if this tree
* is empty.
*/
BinarySearchTree.prototype.min = function () {
if (this.root === null) {
return null;
}
return this.root.min();
};
/* Returns the node in this tree with the maximum key. Returns null if this tree
* is empty.
*/
BinarySearchTree.prototype.max = function () {
if (this.root === null) {
return null;
}
return this.root.max();
};
/* Returns the node in this tree with the given key. Returns null if the given
* key is not present in this tree.
*/
BinarySearchTree.prototype.get = function (key) {
if (this.root === null) {
return null;
}
return this.root.get(key);
};
/* Inserts the given key, value pair in this tree. Creates a new node with the
* given key and value and inserts it in the appropriate position. Returns the
* created node. Throws an exception if the given key is already present in this
* tree.
*/
BinarySearchTree.prototype.insert = function (key, value) {
if (this.root === null) {
this.root = new BinarySearchTree.prototype.Node(key, value, null);
} else {
this.root.insert(key, value);
}
this.numNodes++;
};
/* Removes the node with the given key from this tree. Throws an exception if
* the given key is not present in this tree. Does not modify the removed node.
* References to nodes other than the removed node are guaranteed to remain
* valid.
*/
BinarySearchTree.prototype.remove = function (key) {
var nodeToRemove = this.get(key);
if (nodeToRemove === null) {
throw 'Attempted to remove a key that is not in this tree.';
}
var replacementNode = nodeToRemove.remove();
if (this.root === nodeToRemove) {
this.root = replacementNode;
}
this.numNodes--;
};
/* Returns the number of nodes in this tree, which is equivalent to the number
* of key, value pairs.
*/
BinarySearchTree.prototype.size = function () {
return this.numNodes;
};