-
Notifications
You must be signed in to change notification settings - Fork 0
/
working-set.js
1392 lines (1226 loc) · 36.6 KB
/
working-set.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* A node in a deque.
*/
class DequeNode {
constructor(value, prev, next) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
/**
* An implementation of deque.
*/
class Deque {
constructor() {
this.first = null;
this.last = null;
// If the deque has only 1 element,
// then this.first = this.last.
// If the deque has 0 elements,
// then this.first = this.last = null.
}
/**
* Push the given node to the front of the deque.
*/
pushToFront(node) {
if (!this.last) {
this.last = node;
this.first = node;
node.next = null;
node.prev = null;
} else if (this.first == this.last) {
this.first = node;
this.first.next = this.last;
this.last.prev = this.first;
} else {
this.first.prev = node;
node.next = this.first;
node.prev = null;
this.first = node;
}
}
/**
* Push the given node to the back of the deque.
*/
pushToBack(node) {
if (!this.last) {
this.last = node;
this.first = node;
node.next = null;
node.prev = null;
} else if (this.first == this.last) {
this.last = node;
this.last.prev = this.first;
this.first.next = this.last;
} else {
this.last.next = node;
node.next = null;
node.prev = this.last;
this.last = node;
}
}
/**
* Pop the node at the front of the deque,
* removing it from the structure.
* Returns the popped node.
*/
popFromFront() {
var nodeToReturn = this.first;
if (this.first == this.last) {
this.first = null;
this.last = null;
} else {
this.first = this.first.next;
this.first.prev = null;
}
return nodeToReturn;
}
/**
* Pop the node at the back of the deque,
* removing it from the structure.
* Returns the popped node.
*/
popFromBack() {
var nodeToReturn = this.last;
if (this.first == this.last) {
this.first = null;
this.last = null;
} else {
this.last = this.last.prev;
this.last.next = null;
}
return nodeToReturn;
}
/**
* Deletes {dequeNode} from the deque.
*/
deleteMe(dequeNode) {
if (dequeNode.next) {
dequeNode.next.prev = dequeNode.prev;
} else {
// It's the last one
this.last = dequeNode.prev;
}
if (dequeNode.prev) {
dequeNode.prev.next = dequeNode.next;
} else {
// It's the first one
this.first = dequeNode.next;
}
}
/**
* Finds value in the deque and removes it.
* Returns the node with that value, or null
* if the value was not in the deque.
*/
findAndPop(value) {
var currentNode = this.first;
while (currentNode) {
if (currentNode.value == value) {
if (currentNode.prev) {
currentNode.prev.next = currentNode.next;
} else {
this.first = currentNode.next;
}
if (currentNode.next) {
currentNode.next.prev = currentNode.prev;
} else {
this.last = currentNode.prev;
}
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}
/**
* Print the deque from front to back.
*/
showDeque() {
var currentNode = this.first;
while (currentNode) {
console.log(currentNode);
currentNode = currentNode.next;
}
}
toString() {
var total = '[';
var currentNode = this.first;
while (currentNode) {
total += currentNode.value + ', ';
currentNode = currentNode.next;
}
total += ']';
return total;
}
}
/** Test of deleteMe
// Delete in middle
var deque = new Deque();
var middleNode = new DequeNode(4);
deque.pushToFront(new DequeNode(5));
deque.pushToFront(middleNode);
deque.pushToFront(new DequeNode(3));
deque.deleteMe(middleNode);
console.log(deque);
// Delete beginning
var deque = new Deque();
var firstNode = new DequeNode(4);
deque.pushToBack(new DequeNode(5));
deque.pushToFront(firstNode);
deque.deleteMe(firstNode);
console.log(deque);
// Delete end
var deque = new Deque();
var lastNode = new DequeNode(4);
deque.pushToBack(new DequeNode(7));
deque.pushToBack(lastNode);
deque.deleteMe(lastNode);
console.log(deque);
*/
// Demonstration
/*
console.log("deque testing");
var dequeExample = new Deque();
// Test pushes
//dequeExample.pushToBack(new DequeNode(5));
//dequeExample.pushToFront(new DequeNode(3));
//dequeExample.pushToFront(new DequeNode(2));
//dequeExample.pushToBack(new DequeNode(15));
//dequeExample.showDeque();
// Test pops
//dequeExample.popFromBack();
//dequeExample.popFromFront();
//dequeExample.popFromFront();
//dequeExample.showDeque();
// Test find
//dequeExample.findAndPop(2);
//dequeExample.findAndPop(5);
//dequeExample.findAndPop(15);
//dequeExample.findAndPop(4);
//dequeExample.findAndPop(3);
//dequeExample.showDeque();
console.log("end of deque testing");
*/
/**
* A class that represents a node in an AVL tree.
*
* Reference: https://en.wikipedia.org/wiki/AVL_tree
*/
class AvlNode {
constructor(value) {
/** The value that this node stores */
this.value = value;
/** The node's parent*/
this.parent = null;
/** The node's left child */
this.leftChild = null;
/** The node's right child */
this.rightChild = null;
/** The node's height */
this.height = 1;
/** The size of this node's subtree, including itself */
this.size = 1;
/** Pointer to the deque node that also stores this value */
this.pointerToDequeNode = null;
}
////////////////////////////////////////////////
// Helper methods
//
/**
* Rebalances this node.
* If the node is already balanced, does nothing.
*/
rebalance() {
// Rebalance only while |balance factor| is not <= 1:
var factor = this.getBalanceFactor();
if (!(Math.abs(factor) <= 1)) {
// R and LR rotations
if (factor > 1) {
var heightA = (this.leftChild.leftChild != null) ? this.leftChild.leftChild.height : 0;
var heightB = (this.leftChild.rightChild != null) ? this.leftChild.rightChild.height : 0;
var heightC = (this.rightChild != null) ? this.rightChild.height : 0;
// R:
if (heightA >= heightB && heightA >= heightC) {
this.rotateR();
//this.parent = null;
}
// LR:
else if (heightB >= heightA && heightB >= heightC) {
this.rotateLR();
//this.parent = null;
}
}
// L and RL rotations
else if (factor < -1) {
var heightA = (this.rightChild.rightChild != null) ? this.rightChild.rightChild.height : 0;
var heightB = (this.rightChild.leftChild != null) ? this.rightChild.leftChild.height : 0;
var heightC = (this.leftChild != null) ? this.leftChild.height : 0;
// L:
if (heightA >= heightB && heightA >= heightC) {
this.rotateL();
//this.parent = null;
}
// RL:
else if (heightB >= heightA && heightB >= heightC) {
this.rotateRL();
//this.parent = null;
}
}
}
}
/**
* Helper functions (rotations and updating root) for rebalancing.
*/
rotateR() {
// rotation
var temp = this.copy();
this.updateRoot(temp.leftChild);
/*
if (this.parent != null) {
this.parent.rightChild = this
}
*/
temp.leftChild = this.rightChild;
if (temp.leftChild != null) {
temp.leftChild.parent = temp;
}
this.rightChild = temp;
this.rightChild.parent = this;
// update heights
var rightHeight = (this.rightChild.rightChild != null) ? this.rightChild.rightChild.height : 0;
var leftHeight = (this.rightChild.leftChild != null) ? this.rightChild.leftChild.height : 0;
this.rightChild.height = Math.max(rightHeight, leftHeight) + 1
rightHeight = (this.rightChild != null) ? this.rightChild.height : 0;
leftHeight = (this.leftChild != null) ? this.leftChild.height : 0;
this.height = Math.max(rightHeight, leftHeight) + 1
// update sizes
var rightSize = (this.rightChild.rightChild != null) ? this.rightChild.rightChild.size : 0;
var leftSize = (this.rightChild.leftChild != null) ? this.rightChild.leftChild.size : 0;
this.rightChild.size = rightSize + leftSize + 1;
rightSize = (this.rightChild != null) ? this.rightChild.size : 0;
leftSize = (this.leftChild != null) ? this.leftChild.size : 0;
this.size = rightSize + leftSize + 1;
// not sure if the following is necessary...
/*
if (this.parent != null) {
rightHeight = (this.parent.rightChild != null) ? this.parent.rightChild.height : 0;
leftHeight = (this.parent.leftChild != null) ? this.parent.leftChild.height : 0;
this.parent.height = Math.max(rightHeight, leftHeight) + 1
}
*/
}
rotateL() {
// rotation
var temp = this.copy();
this.updateRoot(temp.rightChild);
/*
if (this.parent != null) {
this.parent.leftChild = this
}
*/
temp.rightChild = this.leftChild;
if (temp.rightChild != null) {
temp.rightChild.parent = temp;
}
this.leftChild = temp;
this.leftChild.parent = this;
// update heights
var rightHeight = (this.leftChild.rightChild != null) ? this.leftChild.rightChild.height : 0;
var leftHeight = (this.leftChild.leftChild != null) ? this.leftChild.leftChild.height : 0;
this.leftChild.height = Math.max(rightHeight, leftHeight) + 1
rightHeight = (this.rightChild != null) ? this.rightChild.height : 0;
leftHeight = (this.leftChild != null) ? this.leftChild.height : 0;
this.height = Math.max(rightHeight, leftHeight) + 1
// update sizes
var rightSize = (this.leftChild.rightChild != null) ? this.leftChild.rightChild.size : 0;
var leftSize = (this.leftChild.leftChild != null) ? this.leftChild.leftChild.size : 0;
this.leftChild.size = rightSize + leftSize + 1;
rightSize = (this.rightChild != null) ? this.rightChild.size : 0;
leftSize = (this.leftChild != null) ? this.leftChild.size : 0;
this.size = rightSize + leftSize + 1;
// not sure if the following is necessary...
/*
if (this.parent != null) {
rightHeight = (this.parent.rightChild != null) ? this.parent.rightChild.height : 0;
leftHeight = (this.parent.leftChild != null) ? this.parent.leftChild.height : 0;
this.parent.height = Math.max(rightHeight, leftHeight) + 1
}
*/
}
rotateLR() {
this.leftChild.rotateL();
this.rotateR();
}
rotateRL() {
this.rightChild.rotateR();
this.rotateL();
}
updateRoot(newRoot) {
this.value = newRoot.value;
this.pointerToDequeNode = newRoot.pointerToDequeNode;
this.leftChild = newRoot.leftChild;
if (this.leftChild != null) {
this.leftChild.parent = this;
}
this.rightChild = newRoot.rightChild;
if (this.rightChild != null) {
this.rightChild.parent = this;
}
}
////////////////////////////////////////////////
// Public methods
//
/**
* Make a copy of node ('this'). Returns copy.
*/
copy() {
var newNode = new AvlNode(this.value);
newNode.leftChild = this.leftChild;
newNode.rightChild = this.rightChild;
if (newNode.leftChild != null) {
newNode.leftChild.parent = newNode;
}
if (newNode.rightChild != null) {
newNode.rightChild.parent = newNode;
}
newNode.height = this.height;
newNode.parent = this.parent;
newNode.pointerToDequeNode = this.pointerToDequeNode;
return newNode;
}
/**
* Overrides default toString() method.
* Returns a pretty-printed tree.
*/
toString() {
var s = "\n";
var q = [[this, 1]];
var i = 0;
var max_chars = 2;
var l = 0;
while (i < q.length) {
var node = q[i][0];
var level = q[i][1];
i++;
var value = "_".repeat(max_chars)+" ".repeat((Math.pow(2, this.height-level+1)-1)*max_chars);
if (node != null && node.value != null) {
// TODO: Don't think this can handle 3-digit numbers like 100
value = "_".repeat(max_chars - node.value.toString().length)+node.value.toString()+" ".repeat((Math.pow(2, this.height-level+1)-1)*max_chars);
}
if (l != level) {
s += "\n";
if (level < this.height){
s += " ".repeat((Math.pow(2, this.height-level)-1)*max_chars);
}
l = level;
}
s += value;
var rightChild = [null, level+1];
var leftChild = [null, level+1];
if (node != null) {rightChild = [node.rightChild, level+1]};
if (node != null) {leftChild = [node.leftChild, level+1]};
if (l < this.height) {q.push(leftChild, rightChild);}
}
return s;
}
/**
* Returns the balance factor of this node.
* AVL invariant requires that the balance
* factor is -1, 0, or 1.
*/
getBalanceFactor() {
var leftHeight = (this.leftChild != null) ? this.leftChild.height : 0;
var rightHeight = (this.rightChild != null) ? this.rightChild.height : 0;
return leftHeight - rightHeight;
}
/**
* Insert the given node into this node's subtree.
* Returns the inserted AvlNode, or a list of inserted AvlNodes.
*/
insert(val) {
if (typeof val == 'number') {
return this.insertHelper(val);
//this.rebalance();
} else {
var allInserts = [];
for (var i = 0; i < val.length; i ++) {
allInserts.push(this.insert(val[i]));
}
return allInserts;
}
}
/**
* Helper for insert - inserts the node, except for
* rebalancing.
* Returns the inserted AvlNode.
*/
insertHelper(val) {
if (this.value == val) {
throw new Error("value " + val + " already exists in tree");
} else if (this.value < val) {
if (this.rightChild != null) {
return this.rightChild.insertHelper(val);
} else {
this.rightChild = new AvlNode(val);
this.rightChild.parent = this;
this.updateToRoot();
return this.rightChild;
//this.rebalancePath();
}
} else if (this.value > val) {
if (this.leftChild != null) {
return this.leftChild.insertHelper(val);
} else {
this.leftChild = new AvlNode(val);
this.leftChild.parent = this;
this.updateToRoot();
return this.leftChild;
//this.rebalancePath();
}
}
//this.rebalance();
}
/**
* Starting at this node, rebalances all nodes
* on the path to the root. Used after an insert/delete operation
* to account for those operations modifying the heights of
* subtrees.
*/
rebalancePath() {
return;
/*
var x = this;
while (x != null) {
x.rebalance();
x = x.parent;
}
*/
}
/**
* Starting at this node, updates the heights and sizes of all nodes
* on the path to the root. Used after an insert/delete operation
* to account for those operations modifying the heights of
* subtrees.
*/
updateToRoot() {
var x = this;
while (x != null) {
var rightHeight = (x.rightChild != null) ? x.rightChild.height : 0;
var leftHeight = (x.leftChild != null) ? x.leftChild.height : 0;
var rightSize = (x.rightChild != null) ? x.rightChild.size : 0;
var leftSize = (x.leftChild != null) ? x.leftChild.size : 0;
x.height = Math.max(rightHeight, leftHeight) + 1;
x.size = rightSize + leftSize + 1;
x.rebalance();
x = x.parent;
}
}
/**
* Delete the given value from this node's subtree.
* Returns a two element list. The first element is
* the new root of the tree and the second element is
* the node that was deleted.
* If {val} is not in the tree, returns null.
*/
delete(val) {
var r = this.deleteHelper(val);
this.rebalance();
return r;
}
/**
* Helper function for delete - carries out delete
* operation, besides rebalancing.
* Returns a two element list. The first element is
* the new root of the tree and the second element is
* the node that was deleted.
* If {val} is not in the tree, returns null.
*/
deleteHelper(val) {
var node = this.search(val);
var deletingRoot = false;
var returnNode;
if (node == null) {
return null;
}
if (node == this) {
deletingRoot = true;
}
var parent = node.parent;
if (node.leftChild != null && node.rightChild != null) {
var succ = node.successor();
node.value = succ.value;
succ.deleteHelper(succ.value);
returnNode = node;
} else if (node.leftChild != null) {
returnNode = node.replaceWith(node.leftChild);
} else if (node.rightChild != null) {
returnNode = node.replaceWith(node.rightChild);
} else {
returnNode = node.replaceWith(null);
}
if (parent != null) {
parent.updateToRoot();
//parent.rebalancePath();
}
if (deletingRoot == true) {
return [returnNode, node];
} else {
return [this, node];
}
}
/**
* Finds the immediate successor of this node.
*/
successor() {
var current = null;
if (this.rightChild != null) {
current = this.rightChild;
}
while (current.leftChild){
current = current.leftChild;
}
return current;
}
/**
* Replace this node with a new node (one of its child nodes)
* or null, effectively erasing it. Used in delete, in the
* case of the deleted node having 0 or 1 children.
*/
replaceWith(newNode) {
if (newNode != null) {
newNode.parent = this.parent;
}
if (this.parent != null) {
var left = false
var right = false
if (this != null && this.parent.leftChild != null) {
if (this.value == this.parent.leftChild.value) {
left = true
}
}
if (this != null && this.parent.rightChild != null) {
if (this.value == this.parent.rightChild.value) {
right = true
}
}
if (this == this.parent.leftChild || left) {
this.parent.leftChild = newNode;
this.parent = null
} else if (this == this.parent.rightChild || right) {
this.parent.rightChild = newNode;
this.parent = null;
}
}
return newNode;
}
/**
* Search for the given value in this node's subtree.
* Returns the AvlNode that contains that value.
* Returns null if the value is not in this subtree.
*/
search(value) {
if (this.value == value) {
return this;
} else if (value < this.value && this.leftChild) {
return this.leftChild.search(value);
} else if (value > this.value && this.rightChild) {
return this.rightChild.search(value);
} else {
return null;
}
}
toStringJustMe() {
var totalString = 'node toStringJustMe;';
totalString += 'val:' + this.value;
if (this.parent) {
totalString += 'parentVal:' + this.parent.value + ';';
} else {
totalString += "no parent;";
}
if (this.leftChild) {
totalString += 'leftChildVal:' + this.leftChild.value + ';';
} else {
totalString += "no left child;";
}
if (this.rightChild) {
totalString += 'rightChildVal:' + this.rightChild.value + ';';
} else {
totalString += "no right child;";
}
return totalString;
}
}
class AvlTree {
constructor() {
this.rootNode = null;
}
insertSingle(value) {
if (!this.rootNode) {
var rootNode = new AvlNode(value);
this.rootNode = rootNode;
return rootNode;
} else {
return this.rootNode.insert(value);
}
}
/**
* Inserts {value} and returns the AvlNode with that value.
*/
insert(value) {
if (typeof value == 'number') {
return this.insertSingle(value);
} else {
var allInserts = [];
for (var i = 0; i < value.length; i ++) {
allInserts.push(this.insertSingle(value[i]));
}
return allInserts;
}
}
/* Deletes {value} from the tree.
* Returns the node that was deleted.
* If {val} is not in the tree, returns null.
*/
delete(value) {
if (!this.rootNode) {
return false;
} else {
var returnVal = this.rootNode.delete(value);
if (returnVal) {
this.rootNode = returnVal[0];
return returnVal[1];
} else {
return null;
}
}
}
search(value) {
if (!this.rootNode) {
return false;
} else {
return this.rootNode.search(value);
}
}
size() {
if (!this.rootNode) {
return 0;
} else {
return this.rootNode.size;
}
}
}
// Demonstrate basic functions
/*
var n = new AvlNode(9);
n.insert([4, 15, 3, 6, 12, 2]);
console.log(n.toString());
n.delete(6);
console.log(n.toString());
n.insert(11);
console.log(n.toString());
n.insert(1);
console.log(n.toString());
n.delete(11);
console.log(n.toString());
n.delete(15);
console.log(n.toString());
*/
/*
var n = new AvlNode(10);
//n.insert(10);
n.insert([5,1,7,15,12,18]);
console.log(n.toString());
nn = n.delete(10);
console.log(nn.toString());
*/
/*
var tree = new AvlTree();
var toInsert = [5, 4, 3, 2, 1, 6, 9, 15, 12, 13, 14, 20, 25, 30, 28, 31, 29];
tree.insert(toInsert);
console.log("did the insert");
console.log(tree.rootNode.size);
console.log(tree.rootNode.toString());
console.log("done");
// TODO: Doesn't look balanced here
*/
/*
tree.delete(2);
tree.delete(5);
console.log(tree.rootNode.size);
console.log(tree.rootNode.toString());
tree.delete(15);
tree.delete(13);
tree.delete(14);
console.log(tree.rootNode.size);
console.log(tree.rootNode.toString());
tree.delete(30);
tree.delete(25);
console.log(tree.rootNode.toString());
tree.delete(31);
tree.delete(28);
console.log(tree.rootNode.toString());
*/
// Demonstrate search
/**
console.log(rootNode.search(5));
console.log(rootNode.search(3));
console.log(rootNode.search(25));
console.log(rootNode.search(40));
console.log(rootNode.search(-2));
*/
//////////////////////////////////////
/**
* A class for a structure that maintains the working set invariant.
*
* The working set invariant:
* (2) Every element in deque i has a smaller working
* set than every element in deque i+1.
* (1) Element x lies after y in some deque i iff
* x has a smaller working set than y.
*
* Reference: https://en.wikipedia.org/wiki/Iacono%27s_working_set_structure
*/
class WorkingSetStructure {
constructor() {
/**
A list of AVL trees to store the elements.
this.trees[i] has size 2^(2^i)
(except the last one, which might be smaller).
*/
this.trees = [];
var firstTree = new AvlTree();
this.trees.push(firstTree);
/**
A list of deques to store the elements.
this.deques[i] has size 2^(2^i)
(except the last one, which might be smaller).
*/
this.deques = [];
var firstDeque = new Deque();
this.deques.push(firstDeque);
// For all i, all elements in this.trees[i]
// are in all elemetns in this.deques[i] and
// vice versa.
// Each element is in exactly one tree and
// and exactly one deque.
}
////////////////////////////////////////////////
// Helper methods
//
/**
* Shift from h to j.
* Both h and j are indices of some tree/deque in
* our structure.
* The shift will decrease the size of trees[h] and deques[h]
* by 1 and increase the size of trees[j] and deques[j]
* by 1, maintaining the working set invariant.
*/
shift(h, j) {
if (h < j) {
for (var i = h; i < j; i++) {
// deque and item from Q_i, and enqueue the item into Q_i+1
var item = this.deques[i].popFromBack();
var insertedDequeNode = new DequeNode(item.value);
this.deques[i + 1].pushToFront(insertedDequeNode);
// delete the item from T_i and insert into T_i+1
this.trees[i].delete(item.value);
var insertedNode = this.trees[i + 1].insert(item.value);
insertedNode.pointerToDequeNode = insertedDequeNode;
}
} else if (j < h) {
for (var i = h; i > j; i--) {
// deque and item from Q_i, and enqueue the item into Q_i-1
var item = this.deques[i].popFromFront();
if (!item) {
// This deque is empty, so just go to the previous deque
continue;
}
var insertedDequeNode = new DequeNode(item.value);
this.deques[i - 1].pushToBack(insertedDequeNode);
// delete the item from T_i and insert into T_i-1
this.trees[i].delete(item.value);
var insertedNode = this.trees[i - 1].insert(item.value);
insertedNode.pointerToDequeNode = insertedDequeNode;
}
}
}
////////////////////////////////////////////////
// Public methods
//
/**
* Insert each value in the values array into the structure.
* The first item in the array will be the earliest-accessed
* item, and the last item will be the most recently
* accessed item.
*/
insertAll(values) {
var that = this;
values.forEach(function(value) {
that.insert(value);
})
}
/**
* Insert value into the structure. Does not insert
* the value if it's already in the structure.
*/
insert(value) {
// Don't allow insertion of duplicate values.
for (var i = 0; i < this.trees.length; i++) {
var foundNode = this.trees[i].search(value);
if (foundNode) {
return;
}
}
var k = this.trees.length;
if (k == 0 || this.trees[k-1].size() >= Math.pow(2, Math.pow(2, k))) {
// Need to add a new tree to the end to fit this element
this.trees.push(new AvlTree());
this.deques.push(new Deque());
k += 1;
}
var avlNode = this.trees[0].insert(value);
var dequeNode = new DequeNode(value);
this.deques[0].pushToFront(dequeNode);
avlNode.pointerToDequeNode = dequeNode;
this.shift(0, k-1);
}
/**
* Delete value from the structure.
*/
delete(value) {
var foundIndex = null;
for (var i = 0; i < this.trees.length; i++) {
var tree = this.trees[i];
var exists = tree.search(value);
if (exists != null) {
foundIndex = i;
var deletedAvlNode = tree.delete(value);
var deletedDequeNode = deletedAvlNode.pointerToDequeNode;
this.deques[foundIndex].deleteMe(deletedDequeNode);