-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance.js
1640 lines (1448 loc) · 46.8 KB
/
performance.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
/**
* Data structures to compare: Linked List, BST, AVL tree, Hash Table, Splay Tree, Working Set
* Interface/API that must be shared by all structures:
* insert(value) --> void
* delete(value) --> false, or void
* search(value) --> false, or value
*/
/**
* BST
* Adapted from: http://www.geeksforgeeks.org/implementation-binary-search-tree-javascript/
*/
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
// root of a binary seach tree
this.root = null;
}
// helper method which creates a new node to
// be inserted and calls insertNode
insert(data) {
// Creating a node and initailising
// with data
var newNode = new Node(data);
// root is null then node will
// be added to the tree and made root.
if (this.root === null)
this.root = newNode;
else
// find the correct position in the
// tree and add the node
this.insertNode(this.root, newNode);
}
// Method to insert a node in a tree
// it moves over the tree to find the location
// to insert a node with a given data
insertNode(node, newNode) {
// if the data is less than the node
// data move left of the tree
if (newNode.data < node.data) {
// if left is null insert node here
if (node.left === null)
node.left = newNode;
else
// if left is not null recurr until
// null is found
this.insertNode(node.left, newNode);
}
// if the data is more than the node
// data move right of the tree
else {
// if right is null insert node here
if (node.right === null)
node.right = newNode;
else
// if right is not null recurr until
// null is found
this.insertNode(node.right, newNode);
}
}
// helper method that calls the
// deleteNode with a given data
delete(data) {
// root is re-initialized with
// root of a modified tree.
this.root = this.deleteNode(this.root, data);
if (this.root == null) {
return false;
}
}
// Method to delete node with a
// given data
// it recurrs over the tree to find the
// data and deletes it
deleteNode(node, key) {
// if the root is null then tree is
// empty
if (node === null)
return null;
// if data to be delete is less than
// roots data then move to left subtree
else if (key < node.data) {
node.left = this.deleteNode(node.left, key);
return node;
}
// if data to be delete is greater than
// roots data then move to right subtree
else if (key > node.data) {
node.right = this.deleteNode(node.right, key);
return node;
}
// if data is similar to the root's data
// then delete this node
else {
// deleting node with no children
if (node.left === null && node.right === null) {
node = null;
return node;
}
// deleting node with one children
if (node.left === null) {
node = node.right;
return node;
}
else if (node.right === null) {
node = node.left;
return node;
}
// Deleting node with two children
// minumum node of the rigt subtree
// is stored in aux
var aux = this.findMinNode(node.right);
node.data = aux.data;
node.right = this.deleteNode(node.right, aux.data);
return node;
}
}
// Helper functions
// finds the minimum node in tree
// searching starts from given node
findMinNode(node) {
// if left of a node is null
// then it must be minimum node
if (node.left === null)
return node;
else
return this.findMinNode(node.left);
}
// returns root of the tree
getRootNode() {
return this.root;
}
// Performs inorder traversal of a tree
inorder(node) {
if (node !== null) {
this.inorder(node.left);
console.log(node.data);
this.inorder(node.right);
}
}
// Performs preorder traversal of a tree
preorder(node) {
if (node != null) {
console.log(node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}
// Performs postorder traversal of a tree
postorder(node) {
if (node != null) {
this.postorder(node.left);
this.postorder(node.right);
console.log(node.data);
}
}
// search for a node with given data
search(data) {
var node = this.root
// if tree is empty return null
if (node === null)
return false;
// if data is less than node's data
// move left
else if (data < node.data)
return this.search(node.left, data);
// if data is less than node's data
// move left
else if (data > node.data)
return this.search(node.right, data);
// if data is equal to the node data
// return node
else
return node;
}
}
/**
* Deque, AVL, Working Set
* Taken from: working-set.js
*/
class DequeNode {
constructor(value, prev, next) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
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;
}
/**
* Print the deque from front to back.
*/
showDeque() {
var currentNode = this.first;
while (currentNode) {
console.log(currentNode);
currentNode = currentNode.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;
}
toString() {
var total = '[';
var currentNode = this.first;
while (currentNode) {
total += currentNode.value + ', ';
currentNode = currentNode.next;
}
total += ']';
return total;
}
// Functions just for performance testing purposes (insert, search):
// Note: We are using implementation of deque for doubly-linked list.
insert(value) {
var node = new DequeNode(value);
this.pushToBack(node);
}
search(value) {
var currentNode = this.first;
while (currentNode) {
if (currentNode.value == value) {
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}
}
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;
}
////////////////////////////////////////////////
// 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.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;
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.
*/
insert(val) {
if (typeof val == 'number') {
this.insertHelper(val);
//this.rebalance();
} else {
for (var i = 0; i < val.length; i++) {
this.insert(val[i]);
}
}
}
/**
* Helper for insert - inserts the node, except for
* rebalancing.
*/
insertHelper(val) {
if (this.value == val) {
throw new Error("value " + val + " already exists in tree");
} else if (this.value < val) {
if (this.rightChild != null) {
this.rightChild.insertHelper(val);
} else {
this.rightChild = new AvlNode(val);
this.rightChild.parent = this;
this.updateToRoot();
//this.rebalancePath();
}
} else if (this.value > val) {
if (this.leftChild != null) {
this.leftChild.insertHelper(val);
} else {
this.leftChild = new AvlNode(val);
this.leftChild.parent = this;
this.updateToRoot();
//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 the new root of the tree if the value
* was in this node's subtree and false otherwise.
*/
delete(val) {
var r = this.deleteHelper(val);
this.rebalance();
return r;
}
/**
* Helper function for delete - carries out delete
* operation, besides rebalancing.
* Returns the new root of the tree, or null if
* the delete is unsuccessful.
*/
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;
} else {
return this;
}
}
/**
* 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;
} else {
this.rootNode.insert(value);
}
}
insert(value) {
if (typeof value == 'number') {
this.insertSingle(value);
} else {
for (var i = 0; i < value.length; i++) {
this.insertSingle(value[i]);
}
}
}
delete(value) {
if (!this.rootNode) {
return false;
} else {
var newRoot = this.rootNode.delete(value);
this.rootNode = newRoot;
}
}
search(value) {
if (!this.rootNode) {
return false;
} else {
return this.rootNode.search(value);
}
}
size() {
if (!this.rootNode) {
return 0;
} else {
return this.rootNode.size;
}
}
}
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();
this.deques[i + 1].pushToFront(new DequeNode(item.value));
// delete the item from T_i and insert into T_i+1
this.trees[i].delete(item.value);
this.trees[i + 1].insert(item.value);
}
} 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;
}
this.deques[i - 1].pushToBack(new DequeNode(item.value));
// delete the item from T_i and insert into T_i-1
this.trees[i].delete(item.value);
this.trees[i - 1].insert(item.value);
}
}
}
////////////////////////////////////////////////
// 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;