-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrealAvl.js
417 lines (378 loc) · 10.3 KB
/
realAvl.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* @license
* Copyright Daniel Imms <http://www.growingwiththeweb.com>
* Released under MIT license. See LICENSE in the project root for details.
*/
/**
* Creates a new AVL Tree node.
*
* @private
* @param {Object} key The key of the new node.
* @param {Object} value The value of the new node.
*/
var Node = function (key, value) {
this.left = null;
this.right = null;
this.height = null;
this.key = key;
this.value = value;
};
/**
* Performs a right rotate on this node.
*
* b a
* / \ / \
* a e -> b.rotateRight() -> c b
* / \ / \
* c d d e
*
* @return {Node} The root of the sub-tree; the node where this node used to be.
*/
Node.prototype.rotateRight = function () {
var other = this.left;
this.left = other.right;
other.right = this;
this.height = Math.max(this.leftHeight(), this.rightHeight()) + 1;
other.height = Math.max(other.leftHeight(), this.height) + 1;
return other;
};
/**
* Performs a left rotate on this node.
*
* a b
* / \ / \
* c b -> a.rotateLeft() -> a e
* / \ / \
* d e c d
*
* @return {Node} The root of the sub-tree; the node where this node used to be.
*/
Node.prototype.rotateLeft = function () {
var other = this.right;
this.right = other.left;
other.left = this;
this.height = Math.max(this.leftHeight(), this.rightHeight()) + 1;
other.height = Math.max(other.rightHeight(), this.height) + 1;
return other;
};
/**
* Convenience function to get the height of the left child of the node,
* returning -1 if the node is null.
*
* @return {number} The height of the left child, or -1 if it doesn't exist.
*/
Node.prototype.leftHeight = function () {
if (!this.left) {
return -1;
}
return this.left.height;
};
/**
* Convenience function to get the height of the right child of the node,
* returning -1 if the node is null.
*
* @return {number} The height of the right child, or -1 if it doesn't exist.
*/
Node.prototype.rightHeight = function () {
if (!this.right) {
return -1;
}
return this.right.height;
};
/**
* Creates a new AVL Tree.
*
* @param {function} customCompare An optional custom compare function.
*/
var AvlTree = function (customCompare) {
this._root = null;
this._size = 0;
if (customCompare) {
this._compare = customCompare;
}
};
/**
* Compares two keys with each other.
*
* @private
* @param {Object} a The first key to compare.
* @param {Object} b The second key to compare.
* @return {number} -1, 0 or 1 if a < b, a == b or a > b respectively.
*/
AvlTree.prototype._compare = function (a, b) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
};
/**
* Inserts a new node with a specific key into the tree.
*
* @param {Object} key The key being inserted.
* @param {Object} value The value being inserted.
*/
AvlTree.prototype.insert = function (key, value) {
this._root = this._insert(key, value, this._root);
this._size++;
};
/**
* Inserts a new node with a specific key into the tree.
*
* @private
* @param {Object} key The key being inserted.
* @param {Object} value The value being inserted.
* @param {Node} root The root of the tree to insert in.
* @return {Node} The new tree root.
*/
AvlTree.prototype._insert = function (key, value, root) {
// Perform regular BST insertion
if (root === null) {
return new Node(key, value);
}
if (this._compare(key, root.key) < 0) {
root.left = this._insert(key, value, root.left);
} else if (this._compare(key, root.key) > 0) {
root.right = this._insert(key, value, root.right);
} else {
// It's a duplicate so insertion failed, decrement size to make up for it
this._size--;
return root;
}
// Update height and rebalance tree
root.height = Math.max(root.leftHeight(), root.rightHeight()) + 1;
var balanceState = getBalanceState(root);
if (balanceState === BalanceState.UNBALANCED_LEFT) {
if (this._compare(key, root.left.key) < 0) {
// Left left case
root = root.rotateRight();
} else {
// Left right case
root.left = root.left.rotateLeft();
return root.rotateRight();
}
}
if (balanceState === BalanceState.UNBALANCED_RIGHT) {
if (this._compare(key, root.right.key) > 0) {
// Right right case
root = root.rotateLeft();
} else {
// Right left case
root.right = root.right.rotateRight();
return root.rotateLeft();
}
}
return root;
};
/**
* Deletes a node with a specific key from the tree.
*
* @param {Object} key The key being deleted.
*/
AvlTree.prototype.delete = function (key) {
this._root = this._delete(key, this._root);
this._size--;
};
/**
* Deletes a node with a specific key from the tree.
*
* @private
* @param {Object} key The key being deleted.
* @param {Node} root The root of the tree to delete from.
* @return {Node} The new tree root.
*/
AvlTree.prototype._delete = function (key, root) {
// Perform regular BST deletion
if (root === null) {
this._size++;
return root;
}
if (this._compare(key, root.key) < 0) {
// The key to be deleted is in the left sub-tree
root.left = this._delete(key, root.left);
} else if (this._compare(key, root.key) > 0) {
// The key to be deleted is in the right sub-tree
root.right = this._delete(key, root.right);
} else {
// root is the node to be deleted
if (!root.left && !root.right) {
root = null;
} else if (!root.left && root.right) {
root = root.right;
} else if (root.left && !root.right) {
root = root.left;
} else {
// Node has 2 children, get the in-order successor
var inOrderSuccessor = minValueNode(root.right);
root.key = inOrderSuccessor.key;
root.value = inOrderSuccessor.value;
root.right = this._delete(inOrderSuccessor.key, root.right);
}
}
if (root === null) {
return root;
}
// Update height and rebalance tree
root.height = Math.max(root.leftHeight(), root.rightHeight()) + 1;
var balanceState = getBalanceState(root);
if (balanceState === BalanceState.UNBALANCED_LEFT) {
// Left left case
if (getBalanceState(root.left) === BalanceState.BALANCED ||
getBalanceState(root.left) === BalanceState.SLIGHTLY_UNBALANCED_LEFT) {
return root.rotateRight();
}
// Left right case
if (getBalanceState(root.left) === BalanceState.SLIGHTLY_UNBALANCED_RIGHT) {
root.left = root.left.rotateLeft();
return root.rotateRight();
}
}
if (balanceState === BalanceState.UNBALANCED_RIGHT) {
// Right right case
if (getBalanceState(root.right) === BalanceState.BALANCED ||
getBalanceState(root.right) === BalanceState.SLIGHTLY_UNBALANCED_RIGHT) {
return root.rotateLeft();
}
// Right left case
if (getBalanceState(root.right) === BalanceState.SLIGHTLY_UNBALANCED_LEFT) {
root.right = root.right.rotateRight();
return root.rotateLeft();
}
}
return root;
};
/**
* Gets the value of a node within the tree with a specific key.
*
* @param {Object} key The key being searched for.
* @return {Object} The value of the node or null if it doesn't exist.
*/
AvlTree.prototype.get = function (key) {
if (this._root === null) {
return null;
}
return this._get(key, this._root).value;
};
/**
* Gets the value of a node within the tree with a specific key.
*
* @private
* @param {Object} key The key being searched for.
* @param {Node} root The root of the tree to search in.
* @return {Object} The node or null if it doesn't exist.
*/
AvlTree.prototype._get = function (key, root) {
var result = this._compare(key, root.key);
if (result === 0) {
return root;
}
if (result < 0) {
if (!root.left) {
return null;
}
return this._get(key, root.left);
}
if (!root.right) {
return null;
}
return this._get(key, root.right);
};
/**
* Gets whether a node with a specific key is within the tree.
*
* @param {Object} key The key being searched for.
* @return {boolean} Whether a node with the key exists.
*/
AvlTree.prototype.contains = function (key) {
if (this._root === null) {
return false;
}
return !!this._get(key, this._root);
};
/**
* @return {Object} The minimum key in the tree.
*/
AvlTree.prototype.findMinimum = function () {
if (!this._root) {
return null;
}
return minValueNode(this._root).key;
};
/**
* Gets the minimum value node, rooted in a particular node.
*
* @private
* @param {Node} root The node to search.
* @return {Node} The node with the minimum key in the tree.
*/
function minValueNode(root) {
var current = root;
while (current.left) {
current = current.left;
}
return current;
}
/**
* @return {Object} The maximum key in the tree.
*/
AvlTree.prototype.findMaximum = function () {
return maxValueNode(this._root).key;
};
/**
* Gets the maximum value node, rooted in a particular node.
*
* @private
* @param {Node} root The node to search.
* @return {Node} The node with the maximum key in the tree.
*/
function maxValueNode(root) {
var current = root;
while (current.right) {
current = current.right;
}
return current;
}
/**
* @return {number} The size of the tree.
*/
AvlTree.prototype.size = function () {
return this._size;
};
/**
* @return {boolean} Whether the tree is empty.
*/
AvlTree.prototype.isEmpty = function () {
return this._size === 0;
};
/**
* Represents how balanced a node's left and right children are.
*
* @private
*/
var BalanceState = {
UNBALANCED_RIGHT: 1,
SLIGHTLY_UNBALANCED_RIGHT: 2,
BALANCED: 3,
SLIGHTLY_UNBALANCED_LEFT: 4,
UNBALANCED_LEFT: 5
};
/**
* Gets the balance state of a node, indicating whether the left or right
* sub-trees are unbalanced.
*
* @private
* @param {Node} node The node to get the difference from.
* @return {BalanceState} The BalanceState of the node.
*/
function getBalanceState(node) {
var heightDifference = node.leftHeight() - node.rightHeight();
switch (heightDifference) {
case -2: return BalanceState.UNBALANCED_RIGHT;
case -1: return BalanceState.SLIGHTLY_UNBALANCED_RIGHT;
case 1: return BalanceState.SLIGHTLY_UNBALANCED_LEFT;
case 2: return BalanceState.UNBALANCED_LEFT;
default: return BalanceState.BALANCED;
}
}