-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplayTree.js
546 lines (476 loc) · 15.2 KB
/
splayTree.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
translated from: http://trac.lighttpd.net/trac/browser/trunk/src/splaytree.c
An implementation of top-down splaying with sizes
D. Sleator <[email protected]>, January 1994.
This extends top-down-splay.c to maintain a size field in each node.
This is the number of nodes in the subtree rooted there. This makes
it possible to efficiently compute the rank of a key. (The rank is
the number of nodes to the left of the given key.) It it also
possible to quickly find the node of a given rank. Both of these
operations are illustrated in the code below. The remainder of this
introduction is taken from top-down-splay.c.
"Splay trees", or "self-adjusting search trees" are a simple and
efficient data structure for storing an ordered set. The data
structure consists of a binary tree, without parent pointers, and no
additional fields. It allows searching, insertion, deletion,
deletemin, deletemax, splitting, joining, and many other operations,
all with amortized logarithmic performance. Since the trees adapt to
the sequence of requests, their performance on real access patterns is
typically even better. Splay trees are described in a number of texts
and papers [1,2,3,4,5].
The code here is adapted from simple top-down splay, at the bottom of
page 669 of [3]. It can be obtained via anonymous ftp from
spade.pc.cs.cmu.edu in directory /usr/sleator/public.
The chief modification here is that the splay operation works even if the
item being splayed is not in the tree, and even if the tree root of the
tree is NULL. So the line:
t = splay(i, t);
causes it to search for item with key i in the tree rooted at t. If it's
there, it is splayed to the root. If it isn't there, then the node put
at the root is the last one before NULL that would have been reached in a
normal binary search for i. (It's a neighbor of i in the tree.) This
allows many other operations to be easily implemented, as shown below.
[1] "Fundamentals of data structures in C", Horowitz, Sahni,
and Anderson-Freed, Computer Science Press, pp 542-547.
[2] "Data Structures and Their Algorithms", Lewis and Denenberg,
Harper Collins, 1991, pp 243-251.
[3] "Self-adjusting Binary Search Trees" Sleator and Tarjan,
JACM Volume 32, No 3, July 1985, pp 652-686.
[4] "Data Structure and Algorithm Analysis", Mark Weiss,
Benjamin Cummins, 1992, pp 119-130.
[5] "Data Structures, Algorithms, and Performance", Derick Wood,
Addison-Wesley, 1993, pp 367-375.
*/
SplayTree = function (cmp) {
this.cmp = cmp; // cmp is a function on items such that cmp( a, b ) is true IFF a <= b
};
SplayTree.prototype = {
root: null,
min: null,
max: null,
/**
* An iterator than can tolerate insertions and deletions
* @param {SplayTree} tree The splaytree to search.
* @param {Object} start An object that can be stored in the splaytree. The start of the interval.
* @param {Object} end An object that can be stored in the splaytree. The end of the interval.
*/
getIterator : function(start, end) {
var last = null; // the last value returned
var tree = this;
return {
next: function() {
var more;
if( last !== null ) {
more = tree.findLeastGT(last);
} else {
more = tree.findLeastGE(start);
}
if( ! more ) {
return null;
}
last = tree.get();
if (tree.cmp(last, end)) {
return last;
} else {
return null;
}
}
};
},
getIter: function( elem ) {
return this.getIterator( elem, elem );
},
applyAll: function( iter, fn, scope, except ) {
if( ! (iter.next && typeof iter.next == "function") ) {
iter = this.getIter( iter );
}
var elem;
while ((elem = iter.next()) !== null) {
if (elem !== except) {
fn.call(scope, elem);
}
}
},
walk: function( t, func ) {
if( t !== null ) {
this.walk( t.l, func );
func( t );
this.walk( t.r, func );
}
},
toString : function() {
var s = [];
var walker = function( t ) {
s.push( t.e.toString() );
};
this.walk( this.root, walker );
return s.join( ", " );
},
splay: function(e) {
this.root = this.doSplay(this.root, e);
return this.root;
},
get: function() {
return this.root ? this.root.e: null;
},
getSize: function() {
return this.nodeSize(this.root);
},
getRank: function() {
if (this.root) {
return this.root.size - this.nodeSize(this.root.r);
} else {
return 0;
}
},
equals: function(e) {
if (this.root === null || e === null) {
return false;
}
return this.cmp(this.root.e, e) && this.cmp(e, this.root.e);
},
countIntersections : function( elem ) {
return this.getCount( elem, elem );
},
getCount: function(startElem, endElem) {
var startFound = this.findLeastGE(startElem);
if (!startFound) {
return 0;
}
var startRank = this.getRank();
var endFound = this.findGreatestLE(endElem);
if (!endFound) {
return 0;
}
var endRank = this.getRank();
var count = endRank - startRank + 1;
return count;
},
checkCount: function(startElem, endElem){
var iter = this.getIterator( startElem, endElem );
var count = 0;
while (iter.next() !== null) {
count++;
}
var smartCount = this.getCount( startElem, endElem);
return smartCount === count;
},
nodeSize: function(node) {
if (node === null) {
return 0;
} else {
return node.size;
}
},
insertElem: function(e) {
var tmp;
var found = this.searchElem(e);
if (found) {
alert("cannot insert duplicate");
this.searchElem(e);
}
if (this.root === null) {
this.root = {
e: e,
l: null,
r: null,
size: 1
};
this.min = this.max = this.root;
return;
}
tmp = this.splay(e);
if (this.cmp(tmp.e, e)) { // e > tmp.e
this.root = {
e: e,
l: tmp,
r: tmp.r
};
if (this.root.r === null) {
this.max = this.root;
}
tmp.r = null;
tmp.size = 1 + this.nodeSize(tmp.l);
} else { // e < tmp.e
this.root = {
e: e,
l: tmp.l,
r: tmp
};
if( this.root.l === null ) {
this.min = this.root;
}
tmp.l = null;
tmp.size = 1 + this.nodeSize(tmp.r);
}
this.root.size = 1 + this.nodeSize(this.root.l) + this.nodeSize(this.root.r);
},
deleteElem: function(e) {
var tmp;
if (this.root === null) {
console.log("delete elem failed" );
return false;
}
var tsize = this.root.size;
tmp = this.splay(e);
if (this.cmp(tmp.e, e) && this.cmp(e, tmp.e)) {
if (tmp.l === null) {
this.root = tmp.r;
if( this.root ) {
var tmpMin = this.root;
while(tmpMin.l !== null) {
tmpMin = tmpMin.l;
}
this.min = tmpMin;
}
} else if (tmp.r === null) {
this.root = tmp.l;
if( this.root ) {
var tmpMax = this.root;
while(tmpMax.r !== null) {
tmpMax = tmpMax.r;
}
this.max = tmpMax;
}
} else {
this.root = this.doSplay(tmp.l, e);
this.root.r = tmp.r;
}
if (this.root !== null) {
this.root.size = tsize - 1;
} else {
this.min = this.max = null;
}
return true;
} else {
console.log("delete elem failed" );
return false;
}
},
/*
* findGreatestLE - set root to node with greatest key less than
* or equal to the given elem.
* if no such node exists, return false; else, return true;
*/
findGreatestLE: function(e) {
if ((this.root === null) || (!this.cmp(this.getMin().e, e)) ) {
return false;
}
this.splay(e);
if (!this.cmp(this.root.e, e)) {
// e > this.root.e
return this.findPrev();
} else {
// e >= this.root.e
for( var n = this.getNext(this.root); n !== null && this.cmp(n.e, e); n = this.getNext( this.root ) ) {
this.splay( n.e );
}
return true;
}
},
same: function( n1, n2 ) {
return this.cmp(n1, n2) && this.cmp(n2, n1);
},
findLeastGT: function(e) {
var found = this.findLeastGE(e);
if (!found) {
return false;
} else if (this.same(this.root.e, e)) {
for( var n = this.getNext(this.root); n !== null && this.same(n.e, e); n = this.getNext( this.root ) ) {
this.splay( n.e );
}
if (n !== null) {
this.splay(n.e);
}
return n;
} else {
return true;
}
},
/*
* findLeastGE - set root to node with least key greater than
* or equal to the given elem.
* if no such node exists, return false; else, return true;
*/
findLeastGE: function(e){
if ((this.root === null) || (!this.cmp(e, this.getMax().e))) {
return false;
}
this.splay(e);
if (!this.cmp(e, this.root.e)) {
// e > this.root.e
return this.findNext();
} else {
// this.root.e >= e
for( var p = this.getPrev(this.root); p !== null && this.cmp(e, p.e); p = this.getPrev(this.root) ) {
this.splay( p.e );
}
return true;
}
},
getPrev: function( node ){
if (node.l === null) {
return null;
}
node = node.l;
while (node.r !== null) {
node = node.r;
}
return node;
},
getNext: function( node ) {
if( node.r === null ) {
return null;
}
node = node.r;
while (node.l !== null) {
node = node.l;
}
return node;
},
findPrev: function() {
if (this.root === this.getMin()) {
return false;
}
var node = this.getPrev( this.root );
this.splay(node.e);
return true;
},
findNext: function() {
if (this.root === this.getMax()) {
return false;
}
var node = this.getNext( this.root );
this.splay(node.e);
return true;
},
findMax: function() {
var max = this.getMax();
if (max === null) {
return false;
} else {
this.splay(max.e);
return true;
}
},
getMin: function(){
return this.min;
},
getMax: function(){
return this.max;
},
findMin: function() {
var min = this.getMin();
if (min === null) {
return false;
} else {
this.splay(min.e);
return true;
}
},
searchElem: function(e) {
if ((this.root === null) || (!this.cmp(this.getMin().e, e)) || (!this.cmp(e, this.getMax().e))) {
return false;
}
var tmp = this.splay(e);
return this.cmp(tmp.e, e) && this.cmp(e, tmp.e);
},
doSplay: function(t, e) {
var tmp, l, r, n;
if (t === null) {
return null;
}
l = r = n = {
l: null,
r: null
};
var root_size = this.nodeSize(t);
var left_size = 0;
var right_size = 0;
while (1) {
if (this.cmp(e, t.e)) {
if (this.cmp(t.e, e)) {
break;
}
if (t.l !== null && !this.cmp(t.l.e, e)) {
// rotate right
tmp = t.l;
t.l = tmp.r;
tmp.r = t;
t.size = this.nodeSize(t.l) + this.nodeSize(t.r) + 1;
t = tmp;
}
if (t.l === null) {
break;
}
// link right
r.l = t;
r = t;
t = t.l;
right_size += (1 + this.nodeSize(r.r));
} else {
if (t.r !== null && !this.cmp(e, t.r.e)) {
// rotate left
tmp = t.r;
t.r = tmp.l;
tmp.l = t;
t.size = this.nodeSize(t.l) + this.nodeSize(t.r) + 1;
t = tmp;
}
if (t.r === null) {
break;
}
// link left
l.r = t;
l = t;
t = t.r;
left_size += (1 + this.nodeSize(l.l));
}
}
left_size += this.nodeSize(t.l);
right_size += this.nodeSize(t.r);
t.size = left_size + right_size + 1;
/* The following two loops correct the size fields of the
* right path from the left child of the root and the right
* path from the left child of the root.
*/
l.r = null;
r.l = null;
for (var y = n.r; y !== null; y = y.r) {
y.size = left_size;
left_size -= (1 + this.nodeSize(y.l));
}
for (var z = n.l; z !== null; z = z.l) {
z.size = right_size;
right_size -= (1 + this.nodeSize(z.r));
}
// assemble
l.r = t.l;
r.l = t.r;
t.l = n.r;
t.r = n.l;
return t;
},
checkSize: function(node) {
if (node === null) {
return true;
}
var size = this.computeSize(node);
if (node.size !== size) {
console.log("size bug");
return false;
} else {
return true;
}
},
computeSize: function(node) {
if (node === null) {
return 0;
} else {
return 1 + this.computeSize(node.l) + this.computeSize(node.r);
}
}
};
exports = {
SplayTree: SplayTree
};