-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDataStructures.js
5449 lines (5053 loc) · 142 KB
/
DataStructures.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
/**
* Interface for managing a generic data structure.
* @constructor
* @interface
*/
function Aggregate() {
}
/**
* Returns the iterator relative to the aggregate.
* @abstract
* @return {Iterator} The iterator.
*/
Aggregate.prototype.getIterator = function () {
};
/**
* Interface for managing an iterator for an aggregate.
* @constructor
* @interface
*/
function Iterator() {
}
/**
* Moves the iterator to the first position of the aggregate.
* @abstract
* @return {void}
*/
Iterator.prototype.first = function () {
};
/**
* Moves the iterator to the next item.
* @abstract
* @return {void}
*/
Iterator.prototype.next = function () {
};
/**
* Moves the iterator to the last position of the aggregate.
* @abstract
* @return {void}
*/
Iterator.prototype.last = function () {
};
/**
* Moves the iterator to the previous item.
* @abstract
* @return {void}
*/
Iterator.prototype.previous = function () {
};
/**
* Checks if the iterator is out of the bounds of the aggregate.
* @abstract
* @return {boolean} It return true if the iterator is out of the bounds of the aggregate, otherwise false.
*/
Iterator.prototype.isDone = function () {
};
/**
* Returns the item stored at the position pointed by the iterator.
* @abstract
* @return {*} The item stored or undefined if it's out of the bounds.
*/
Iterator.prototype.getItem = function () {
};
/**
* Class for managing a binary search tree.
* @constructor
*/
function BSTree() {
/**
* The root of the tree.
* @type {BSNode|null}
*/
this.root = null;
}
/**
* Returns the iterator relative to the aggregate.
* @return {Iterator} The iterator.
*/
BSTree.prototype.getIterator = function () {
return new BSTreeIterator(this);
};
/**
* Inserts the item relatives to the key value in the tree.
* @param key {number} The key to store.
* @param item {*} The item to store.
* @return {void}
*/
BSTree.prototype.insert = function (key, item) {
var node = new BSNode(key, item);
var p = this.root;
for (var n = this.root; n;) {
p = n;
if (key < n.key)
n = n.left;
else
n = n.right;
}
node.parent = p;
if (!p)
this.root = node;
else if (key < p.key)
p.left = node;
else
p.right = node;
};
/**
* Searches the item relatives to the key.
* @param key {Number} The key to find.
* @param [node = root] {BSNode} The node from which start the search.
* @return {*} The item found or undefined if there isn't the key in the tree.
*/
BSTree.prototype.search = function (key, node) {
node = node || this.root;
while (node && key !== node.key)
if (key < node.key)
node = node.left;
else
node = node.right;
if (node)
return node.item;
return undefined;
};
/**
* Gets the item relatives to the minimum key stored in the tree.
* @param [node = root] {Node} The node from which start the search.
* @return {BSNode} The node found.
*/
BSTree.prototype.minimum = function (node) {
node = node || this.root;
while (node && node.left)
node = node.left;
return node;
};
/**
* Gets the item relatives to the maximum key stored in the tree.
* @param [node = root] {Node} The node from which start the search.
* @return {BSNode} The node found.
*/
BSTree.prototype.maximum = function (node) {
node = node || this.root;
while (node && node.right)
node = node.right;
return node;
};
/**
* Gets the node with the key next to the param node key.
* @param node {BSNode} The node of which search the successor.
* @return {BSNode} The node found.
*/
BSTree.prototype.successor = function (node) {
if (node.right)
return this.minimum(node.right);
var parent = node.parent;
while (parent && node === parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
};
/**
* Gets the node with the key previous to the param node key.
* @param node {BSNode} The node of which search the predecessor.
* @return {BSNode} The node found.
*/
BSTree.prototype.predecessor = function (node) {
if (node.left)
return this.maximum(node.left);
var parent = node.parent;
while (parent && node === parent.left) {
node = parent;
parent = parent.parent;
}
return parent;
};
/**
* Deletes the node from the tree.
* @param node {BSNode} The node to delete.
* @return {void}
*/
BSTree.prototype.deleteNode = function (node) {
if (!node.left && !node.right) {
if (node === this.root)
this.root = null;
else if (node.parent.left === node)
node.parent.left = null;
else
node.parent.right = null;
} else if (node.left && node.right) {
var next = this.successor(node);
node.key = next.key;
node.item = next.item;
if (next.parent.left === next)
next.parent.left = null;
else
next.parent.right = null;
} else {
if (node.right) {
if (node === this.root) {
this.root = node.right;
node.right.parent = null;
} else {
node.parent.right = node.right;
node.right.parent = node.parent;
}
} else {
if (node === this.root) {
this.root = node.left;
node.left.parent = null;
} else {
node.parent.left = node.left;
node.left.parent = node.parent;
}
}
}
};
/**
* Class that implements the iterator for a binary search tree.
* @param aggregate {BSTree} The aggregate to scan.
* @constructor
*/
function BSTreeIterator(aggregate) {
/**
* The aggregate relates to this iterator.
* @type {BSTree}
*/
this.aggregate = aggregate;
/**
* The pointer to the position.
* @type {BSNode|null}
*/
this.pointer = null;
}
/**
* Moves the iterator to the first position of the aggregate.
* @return {void}
*/
BSTreeIterator.prototype.first = function () {
this.pointer = this.aggregate.minimum();
};
/**
* Moves the iterator to the next item.
* @return {void}
*/
BSTreeIterator.prototype.next = function () {
this.pointer = this.aggregate.successor(this.pointer);
};
/**
* Moves the iterator to the last position of the aggregate.
* @return {void}
*/
BSTreeIterator.prototype.last = function () {
this.pointer = this.aggregate.maximum();
};
/**
* Moves the iterator to the previous item.
* @return {void}
*/
BSTreeIterator.prototype.previous = function () {
this.pointer = this.aggregate.predecessor(this.pointer);
};
/**
* Checks if the iterator is out of the bounds of the aggregate.
* @return {boolean} It return true if the iterator is out of the bounds of the aggregate, otherwise false.
*/
BSTreeIterator.prototype.isDone = function () {
return !this.pointer;
};
/**
* Returns the item stored at the position pointed by the iterator.
* @return {*} The item stored or undefined if it's out of the bounds.
*/
BSTreeIterator.prototype.getItem = function () {
return this.pointer.item;
};
/**
* Returns the node stored at the position pointed by the iterator.
* @return {BSNode|null} The node stored or null if it's out of the bounds.
*/
BSTreeIterator.prototype.getNode = function () {
return this.pointer;
};
/**
* Class for managing a B-Tree.
* @param minimumDegree {number} The minimum number of keys of a node.
* @constructor
*/
function BTree(minimumDegree) {
/**
* The root of the tree.
* @type {BNode}
*/
this.root = new BNode();
/**
* The minimum number of the keys of a node.
* @type {number}
*/
this.t = minimumDegree;
/**
* The number of items stored in the tree.
* @type {number}
*/
this.size = 0;
}
/**
* Returns the iterator relative to the aggregate.
* @return {Iterator} The iterator.
*/
BTree.prototype.getIterator = function () {
return new BTreeIterator(this);
};
/**
* Inserts the item relatives to the key value in the tree.
* @param key {number} The key to store.
* @param item {*} The item to store.
* @return {void}
*/
BTree.prototype.insert = function (key, item) {
var node = this.root;
if (node.keys.length === 2 * this.t - 1) {
var newNode = new BNode();
newNode.childs.push(node);
this.root = newNode;
this.splitChild(newNode, 0);
node = newNode;
}
this.size++;
this.insertNonFull(node, key, item);
};
/**
* Inserts the new node in the right position if the node is not full.
* @param node {BNode} The node from which start to check the insertion.
* @param key {number} The key to store.
* @param item {*} The item to store.
* @return {void}
*/
BTree.prototype.insertNonFull = function (node, key, item) {
while (node) {
var i = node.keys.length - 1;
if (!node.childs.length) {
for (; i > -1 && key < node.keys[i]; i--) {
node.keys[i + 1] = node.keys[i];
node.items[i + 1] = node.items[i];
}
node.keys[i + 1] = key;
node.items[i + 1] = item;
return;
} else {
var j = 0;
i++;
while (j < i) {
var m = Math.floor((j + i) / 2);
if (key <= node.keys[m])
i = m;
else
j = m + 1;
}
if (node.childs[j].keys.length === 2 * this.t - 1) {
this.splitChild(node, j);
if (key > node.keys[j])
j++;
}
node = node.childs[j];
}
}
};
/**
* Searches the item relatives to the key that satisfy the condition represented by the callback function.
* @param key {Number} The key to find.
* @param [node = root] {RBNode} The node from which start the search.
* @param [callback = function(node,index){return(node.keys[index]===key);}] The condition to satisfy. The callback must accept the current node to check and optionally the position of the key.
* @return {*} The item found or undefined if there isn't the key in the tree.
*/
BTree.prototype.search = function (key, node, callback) {
node = node || this.root;
callback = callback || function (node, index) {
return node.keys[index] === key;
};
while (node) {
var n = node.keys.length;
var i = 0, j = n;
while (i < j) {
var m = Math.floor((i + j) / 2);
if (key <= node.keys[m])
j = m;
else
i = m + 1;
}
if (i < n && callback(node, i))
return node.items[i];
else if (!node.childs.length)
return undefined;
else
node = node.childs[i];
}
};
/**
* Splits the child of the node at the position index.
* @param node {BNode} The parent of the child to split.
* @param index {number} The position of the child to split.
* @return {void}
*/
BTree.prototype.splitChild = function (node, index) {
var newNode = new BNode();
var child = node.childs[index];
//copy of the last t - 1 keys and items in the new node
for (var i = 0; i < this.t - 1; i++) {
newNode.keys[i] = child.keys[i + this.t];
newNode.items[i] = child.items[i + this.t];
}
if (child.childs.length)
//copy of the last t child in the new node
for (var j = 0; j < this.t; j++)
newNode.childs[j] = child.childs[j + this.t];
//shift the children from index + 1 position
for (var l = node.keys.length; l > index; l--)
node.childs[l + 1] = node.childs[l];
//set the index position as the position t of the child
node.childs[index + 1] = newNode;
//shift the keys and the items from index position
for (var k = node.keys.length - 1; k > index - 1; k--) {
node.keys[k + 1] = node.keys[k];
node.items[k + 1] = node.items[k];
}
node.keys[index] = child.keys[this.t - 1];
node.items[index] = child.items[this.t - 1];
//remove keys, items and child in the old node.
child.keys.splice(child.keys.length - this.t);
child.items.splice(child.items.length - this.t);
child.childs.splice(child.childs.length - this.t);
};
/**
* Deletes the key from the tree.
* @param key {*} The key to delete.
* @return {void}
*/
BTree.prototype.deleteKey = function (key) {
if (this.root.keys.length) {
this.deleteNonMin(this.root, key);
if (!this.root.keys.length && this.root.childs.length)
this.root = this.root.childs[0];
this.size--;
}
};
/**
* Deletes a node that's a number of keys greater than the minimum for a node.
* @param node {BNode} The node to delete.
* @param key {number} The key to delete.
* @return {void}
*/
BTree.prototype.deleteNonMin = function (node, key) {
var i = 0, j = node.keys.length;
while (i < j) {
var m = Math.floor((i + j) / 2);
if (key <= node.keys[m])
j = m;
else
i = m + 1;
}
//key is in the node
if (i < node.keys.length && key === node.keys[i]) {
//the node is a leaf
if (!node.childs.length) {
//remove the key
for (j = i + 1; j < node.keys.length; j++) {
node.keys[j - 1] = node.keys[j];
node.items[j - 1] = node.items[j];
}
node.keys.pop();
node.items.pop();
} else {
//the node is not a leaf
//the node has the minimum number of keys
if (node.childs[i].length === this.t - 1) {
//increase the number of the keys of the node
this.augmentChild(node, i);
if (i === node.keys.length + 1)
i--;
}
//check if the key is moved in the child
if (node.keys[i] !== key)
this.deleteNonMin(node.childs[i], key);
else
this.deleteMax(node, i);
}
//the key is not in the node
} else {
//check if the child i has the minimum number of keys
if (node.childs[i].keys.length === this.t - 1) {
this.augmentChild(node, i);
if (i === node.keys.length + 2)
i--;
}
this.deleteNonMin(node.childs[i], key);
}
};
/**
* Deletes a node that have the maximum number of keys for node.
* @param node {BNode} The node to delete.
* @param index {number} The key to delete in the node.
* @return {void}
*/
BTree.prototype.deleteMax = function (node, index) {
var child = node.childs[index];
var goAhead = true;
while (goAhead) {
if (!child.childs.length) {
node.keys[index] = child.keys[child.keys.length - 1];
node.items[index] = child.items[child.items.length - 1];
child.keys.pop();
child.items.pop();
goAhead = false;
} else {
var last = child.childs[child.keys.length];
if (last.keys.length === this.t - 1)
this.augmentChild(child, child.keys.length);
child = child.childs[child.keys.length];
}
}
};
/**
* Augments the number of keys stored in the node preserving the order.
* @param node {BNode} The node to delete.
* @param index {number} The index of the position to augment.
* @return {void}
*/
BTree.prototype.augmentChild = function (node, index) {
var child = node.childs[index];
var brother;
if (index)
brother = node.childs[index - 1];
if (index && brother.keys.length > this.t - 1) {
if (child.childs.length) {
for (var j = this.keys.length + 1; j > 0; j--)
child.childs[j] = child.childs[j - 1];
child.childs[0] = brother.childs[brother.keys.length];
for (var i = child.keys.length; i > 0; i--) {
child.keys[i] = child.keys[i - 1];
child.items[i] = child.items[i - 1];
}
child.keys[0] = node.keys[index - 1];
child.items[0] = node.items[index - 1];
node.keys[index - 1] = brother.keys[brother.keys.length - 1];
node.items[index - 1] = brother.items[brother.items.length - 1];
}
} else {
if (index < node.keys.length)
brother = node.childs[index + 1];
if (index < node.keys.length && brother.keys.length > this.t - 1) {
if (brother.childs.length) {
child.childs[child.keys.length + 1] = brother.childs[0];
for (var l = 1; l < brother.keys.length + 1; l++)
brother.childs[l - 1] = brother.childs[l];
brother.childs.pop();
}
child.keys[child.keys.length] = node.keys[index];
child.items[child.items.length] = node.items[index];
node.keys[index] = brother.keys[0];
node.items[index] = brother.items[0];
for (var k = 1; k < brother.keys.length; k++) {
brother.keys[k - 1] = brother.keys[k];
brother.items[k - 1] = brother.items[k];
}
brother.keys.pop();
brother.items.pop();
} else {
if (index < node.keys.length) {
child.keys[this.t - 1] = node.keys[index];
child.items[this.t - 1] = node.items[index];
for (var m = index + 2; m < node.keys.length + 1; m++)
node.childs[m - 1] = node.childs[m];
node.childs.pop();
for (var n = index + 1; n < node.keys.length; n++) {
node.keys[n - 1] = node.keys[n];
node.items[n - 1] = node.items[n];
}
node.keys.pop();
node.items.pop();
if (brother.childs.length)
for (var y = 0; y < brother.keys.length + 1; y++)
child.childs[this.t + y] = brother.childs[y];
for (var x = 0; x < brother.keys.length; x++) {
child.keys[x + this.t] = brother.keys[x];
child.items[x + this.t] = brother.items[x];
}
} else {
if (brother.childs.length)
for (var w = 0; w < child.keys.length + 1; w++)
brother.childs[this.t + w] = child.childs[w];
brother.keys[this.t - 1] = node.keys[node.keys.length - 1];
brother.items[this.t - 1] = node.items[node.keys.length - 1];
for (var z = 0; z < child.keys.length; z++) {
brother.keys[z + this.t] = child.keys[z];
brother.items[z + this.t] = child.items[z];
}
}
}
}
};
/**
* Checks if the tree contains the key.
* @param key {number} The key to find.
* @param [callback = function(node,index){return(node.keys[index]===key);}] The condition to satisfy. The callback must accept the current node to check and optionally the position of the key.
* @return {boolean} True if the tree contains the key.
*/
BTree.prototype.contains = function (key, callback) {
return this.search(key, null, callback) !== undefined;
};
/**
* Checks if the tree contains a node that satisfy the condition represented by the callback function.
* This method check all the tree avoiding the binary search.
* @param callback {function} The condition to satisfy. The callback must accept the current node to check.
* @return {boolean} True if the tree contains the node that satisfy the condition, false otherwise.
*/
BTree.prototype.fullContains = function (callback) {
var key = this.minimumKey();
while (key !== null && !callback(this.search(key)))
key = this.successor(key);
return key !== null;
};
/**
* Gets the key next to the param node key.
* @param key {number} The key of which search the successor.
* @param [node = root] The node from start the search of the successor.
* @return {number} The key found.
*/
BTree.prototype.successor = function (key, node) {
node = node || this.root;
var i = 0, j = node.keys.length;
//search the key in the node
while (i < j) {
var m = Math.floor((i + j) / 2);
if (key <= node.keys[m])
j = m;
else
i = m + 1;
}
//check if the key has been found
if (node.keys[i] === key)
//in this case the successor is the next key
i++;
//if it's a leaf
if (!node.childs.length) {
//check if the key hasn't been found
if (i > node.keys.length - 1)
return null;
else
return node.keys[i];
}
//if it's not a leaf check if the successor is in the i-child
var successor = this.successor(key, node.childs[i]);
//if it's not in the child and has been found a key then return it
if (successor === null && i < node.keys.length)
return node.keys[i];
//return the value of the successor even if it's null
return successor;
};
/**
* Gets the key previous to the param key.
* @param key {number} The key of which search the predecessor.
* @param [node = root] The node from start the search of the predecessor.
* @return {number} The key found.
*/
BTree.prototype.predecessor = function (key, node) {
node = node || this.root;
var i = 0, j = node.keys.length;
//search the key in the node
while (i < j) {
var m = Math.floor((i + j) / 2);
if (key <= node.keys[m])
j = m;
else
i = m + 1;
}
i--;
//check if the node is a leaf
if (!node.childs.length) {
//check if a predecessor has been found
if (i < 0)
return null;
else
return node.keys[i];
}
var predecessor = this.predecessor(key, node.childs[i + 1]);
if (predecessor === null && key > node.keys[0]) {
return node.keys[i];
}
return predecessor;
};
/**
* Gets the minimum key stored in the tree.
* @return {number} The key found.
*/
BTree.prototype.minimumKey = function () {
var node = this.root;
while (node.childs.length)
node = node.childs[0];
if (node)
return node.keys[0];
return null;
};
/**
* Gets the maximum key stored in the tree.
* @return {number} The key found.
*/
BTree.prototype.maximumKey = function () {
var node = this.root;
while (node.childs.length)
node = node.childs[node.childs.length - 1];
if (node)
return node.keys[node.keys.length - 1];
return null;
};
/**
* Gets the item relatives to the minimum key stored in the tree.
* @return {number} The item found.
*/
BTree.prototype.minimum = function () {
var node = this.root;
while (node.childs.length)
node = node.childs[0];
return node.items[0];
};
/**
* Gets the item relatives to the maximum key stored in the tree.
* @return {node} The item found.
*/
BTree.prototype.maximum = function () {
var node = this.root;
while (node.childs.length)
node = node.childs[node.childs.length - 1];
return node.items[node.items.length - 1];
};
/**
* Returns the size of the tree.
* @return {number} The size of the tree.
*/
BTree.prototype.getSize = function () {
return this.size;
};
/**
* Checks if the tree is empty.
* @return {boolean} True if the tree is empty, false otherwise.
*/
BTree.prototype.isEmpty = function () {
return !this.size;
};
/**
* Executes the callback function for each item of the tree.
* This method modifies the tree so if you don't need to modify it you must return the same item of the array.
* @param callback {function} The function to execute for each item. The function must accept the current item on which execute the function.
* @return {void}
*/
BTree.prototype.execute = function (callback) {
var node = arguments[1] || this.root;
for (var i = 0; i < node.items.length; i++)
node.items[i] = callback(node.items[i]);
for (var j = 0; j < node.childs.length; j++)
this.execute(callback, node.childs[j]);
};
/**
* Removes all the items stored in the tree.
* @return {void}
*/
BTree.prototype.clear = function () {
this.root = null;
this.size = 0;
};
/**
* Returns the items that satisfy the condition determined by the callback.
* @param callback {function} The function that implements the condition.
* @return {Array<*>} The array that contains the items that satisfy the condition.
*/
BTree.prototype.filter = function (callback) {
var result = [];
var node = arguments[1] || this.root;
for (var i = 0; i < node.items.length; i++) {
if (node.childs.length)
result = result.concat(this.filter(callback, node.childs[i]));
if (callback(node.items[i]))
result.push(node.items[i]);
}
if (node.childs.length)
result = result.concat(this.filter(callback, node.childs[node.childs.length - 1]));
return result;
};
/**
* Clones the tree into a new tree.
* @return {BTree} The tree cloned from this tree.
*/
BTree.prototype.clone = function () {
var tree = new BTree(this.t);
var it = this.getIterator();
for (it.first(); !it.isDone(); it.next()) {
var item = it.getItem();
if (item.clone)
item = item.clone();
tree.insert(it.getKey(), item);
}
return tree;
};
/**
* Clones the tree into a new tree without cloning duplicated items.
* @return {BTree} The tree cloned from this tree.
*/
BTree.prototype.cloneDistinct = function () {
var tree = new BTree(this.t);
var it = this.getIterator();
for (it.first(); !it.isDone(); it.next()) {
var callback = function (item) {
return item === it.getItem();
};
if (!tree.fullContains(callback)) {
if (it.getItem().cloneDistinct)
tree.insert(it.getKey(), it.getItem().cloneDistinct());
else if (it.getItem().clone)
tree.insert(it.getKey(), it.getItem().clone());
else
tree.insert(it.getKey(), it.getItem());
}
}
return tree;
};
/**
* Transforms the tree into an array without preserving keys.
* @return {Array<*>} The array that represents the tree.
*/
BTree.prototype.toArray = function () {
var result = [];
var it = this.getIterator();
for (it.first(); !it.isDone(); it.next())
result.push(it.getItem());
return result;
};
/**
* Returns the first position of the item in the tree.
* @param item {*} The item to search.
* @param [callback = function(item){return(it===item);}] The condition to satisfy. The callback must accept the current item to check.
* @return {number} The first position of the item.
*/
BTree.prototype.indexOf = function (item, callback) {
callback = callback || function (it) {
return it === item;
};
var i = 0, key = this.minimumKey();
while (key !== null) {
if (callback(this.search(key)))
return i;
key = this.successor(key);
i++;
}
return -1;
};
/**
* Returns the last position of the item in the tree.
* @param item {*} The item to search.
* @param [callback = function(item){return(it===item);}] The condition to satisfy. The callback must accept the current item to check.
* @return {number} The last position of the item.
*/
BTree.prototype.lastIndexOf = function (item, callback) {
callback = callback || function (it) {
return it === item;
};
var i = this.size - 1, key = this.maximumKey();
while (key !== null) {
if (callback(this.search(key)))
return i;
i--;
key = this.predecessor(key);
}
return -1;
};
/**
* Returns all the position in which the item has been found in the tree.
* @param item {*} The item to search.
* @param [callback = function(item){return(it===item);}] The condition to satisfy. The callback must accept the current item to check.
* @return {Array<number>} The positions in which the item has been found.
*/
BTree.prototype.allIndexesOf = function (item, callback) {
callback = callback || function (it) {
return it === item;
};
var i = 0, key = this.minimumKey();
var indexes = [];
while (key !== null) {
if (callback(this.search(key)))
indexes.push(i);
i++;
key = this.successor(key);
}
return indexes;
};
/**
* Returns the item at the position index.
* @param index {number} The position of the item.
* @return {*} The item at the position. It's undefined if index isn't in the tree bounds.
*/
BTree.prototype.getItem = function (index) {
if (index < 0 || index > this.size - 1)
return undefined;
var key = this.minimum();
for (var i = 0; i < index; i++)
key = this.successor(key);
return this.search(key);
};
/**
* Class that implements the iterator for a binary search tree.
* @param aggregate {BTree} The aggregate to scan.
* @constructor
*/
function BTreeIterator(aggregate) {
/**
* The aggregate relates to this iterator.
* @type {BTree}
*/
this.aggregate = aggregate;
/**
* The pointer to the position.
* @type {number}
*/
this.pointer = null;
}
/**
* Moves the iterator to the first position of the aggregate.
* @return {void}
*/
BTreeIterator.prototype.first = function () {
this.pointer = this.aggregate.minimumKey();
};
/**
* Moves the iterator to the next item.