-
Notifications
You must be signed in to change notification settings - Fork 5
/
jstreemap.js
3829 lines (3547 loc) · 106 KB
/
jstreemap.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
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(typeof self !== 'undefined' ? self : this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 5);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/**
* @private
*/
const RED = 1;
/**
* @private
*/
const BLACK = 2;
/**
* @private
* A node for a red-black tree
*/
class TreeNode {
/**
* Default constructor
*/
constructor() {
/** left child */
this.left = null;
/** right child */
this.right = null;
/** parent node */
this.parent = null;
/** key object (additional 'value' data member is added in map-like classes) */
this.key = null;
/** by default new node is red */
this.color = RED;
}
/**
* @returns parent of parent
*/
grandparent() {
let p = this.parent;
if (p === null) {
return null;
} // No parent means no grandparent
return p.parent;
}
/**
* @returns the other child of the same parent
*/
sibling() {
let p = this.parent;
if (p === null) {
return null;
} // No parent means no sibling
if (this === p.left) {
return p.right;
}
else {
return p.left;
}
}
/**
* @returns another child of the grandparent
*/
uncle() {
let p = this.parent;
if (p === null) {
return null;
} // No parent means no uncle
let g = p.parent;
if (g === null) {
return null;
} // No grandparent means no uncle
return p.sibling();
}
}
module.exports = {
TreeNode: TreeNode,
BLACK: BLACK,
RED: RED
};
/***/ }),
/* 1 */
/***/ (function(module, exports) {
/**
* Used by sets
* @private
*/
class KeyOnlyPolicy {
/**
* Returns key data from the specified node
* @param {*} n
*/
fetch(n) {
return n.key;
}
/**
* Copies key data from one node to another
* @param {*} dst
* @param {*} src
*/
copy(dst, src) {
dst.key = src.key;
}
/**
* @returns string representation of the key
* @param {*} node
*/
toString(node) {
return String(node.key);
}
}
/**
* Used by maps
* @private
*/
class KeyValuePolicy {
/**
* Returns key-value data from the specified node
* @param {*} n
*/
fetch(n) {
return [n.key, n.value];
}
/**
* Copies key-value data from one node to another
* @param {*} dst
* @param {*} src
*/
copy(dst, src) {
dst.key = src.key;
dst.value = src.value;
}
/**
* @returns string representation of key-value pair
* @param {*} node
*/
toString(node) {
return String(node.key) + ':' + String(node.value);
}
}
/**
* Used for iteration through values of a map
* @private
*/
class ValueOnlyPolicy {
/**
* Returns data from the specified node
* @param {*} n
*/
fetch(n) {
return n.value;
}
/**
* Copies value data from one node to another
* @param {*} dst
* @param {*} src
*/
copy(dst, src) {
dst.value = src.value;
}
/**
* @returns string representation of node's value
* @param {*} node
*/
toString(node) {
return String(node.value);
}
}
module.exports = {
KeyOnlyPolicy: KeyOnlyPolicy,
ValueOnlyPolicy: ValueOnlyPolicy,
KeyValuePolicy: KeyValuePolicy
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/** @ignore */
const {TreeNode, RED, BLACK} = __webpack_require__(0);
/** @ignore */
const {JsIterator, JsReverseIterator} = __webpack_require__(3);
/** @ignore */
const {Iterator, ReverseIterator} = __webpack_require__(4);
/** @ignore */
const {KeyOnlyPolicy, ValueOnlyPolicy, KeyValuePolicy} = __webpack_require__(1);
/** @ignore */
const {InsertionResult} = __webpack_require__(8);
/** insertion mode of a multimap, nodes with the same keys can be added */
const INSERT_MULTI = 1;
/** if a node with the same key already exists then the subsequent attempts are ignored */
const INSERT_UNIQUE = 2;
/** if a node with the same key already exists then it's value is replaced on subsequent attempts */
const INSERT_REPLACE = 3;
/**
* @private
* Special node in a tree is created for performance reasons
*/
class Head {
/** default constructor */
constructor() {
/** node with the smallest key */
this.leftmost = this;
/** node with the largest key */
this.rightmost = this;
/** root node of the tree */
this.root = this;
/** number of nodes in the tree */
this.size = 0;
/** extra tag used in debuggin of unit tests */
this.id = 'HEAD';
}
}
/**
* @private
* 3-way comparison, similar to strcmp and memcp in C programming language
* @returns +1 if the value of rhs is greater than lhs
* -1 if the value of rhs is less than lhs
* 0 if values are the same
*/
function compare(lhs, rhs) {
if (lhs < rhs) {
return -1;
}
else if (lhs === rhs) {
return 0;
}
else {
return 1;
}
}
/**
* Red-black tree
* @access private
*/
class Tree {
/** default constructor of an empty tree */
constructor() {
/** head */
this.head = new Head();
/** 3-way comparison function */
this.compare = compare;
/** must be an instance of KeyOnlyPolicy for sets, or KeyValuePolicy for maps */
this.valuePolicy = new KeyOnlyPolicy();
}
/**
* Deletes all nodes in the tree
*/
clear() {
this.head = new Head();
}
/**
* @returns number of nodes in the tree
*/
size() {
return this.head.size;
}
/**
* @private
* A wrapper that calls 3-way comparison of node keys
* @param {*} lhs
* @param {*} rhs
*/
compareNodes(lhs, rhs) {
return this.compare(lhs.key, rhs.key);
}
/**
* @private
* used by rotation operations
*/
replaceNode(oldNode, newNode) {
if (oldNode === newNode) {
return;
}
if (oldNode.parent === null) {
this.head.root = newNode;
}
else {
if (oldNode === oldNode.parent.left) {
oldNode.parent.left = newNode;
}
else {
oldNode.parent.right = newNode;
}
}
if (!this.isLeaf(newNode)) {
newNode.parent = oldNode.parent;
}
}
/**
* Rebalances tree as described below
X Y
/ \ / \
Y c right rotate --> a X
/ \ <-- left rotate / \
a b b c
* @private
*/
rotateLeft(node) {
let right = node.right;
if (this.isLeaf(right)) {
throw new Error('rotateLeft can\'t be performed. The tree is corrupted');
}
this.replaceNode(node, right);
node.right = right.left;
if (right.left !== null) {
right.left.parent = node;
}
right.left = node;
node.parent = right;
}
/**
* Rebalances tree as described in rotateLeft
* @param {*} node - parent node
*/
rotateRight(node) {
let left = node.left;
if (this.isLeaf(left)) {
throw new Error('rotateRight can\'t be performed. The tree is corrupted');
}
this.replaceNode(node, left);
node.left = left.right;
if (left.right !== null) {
left.right.parent = node;
}
left.right = node;
node.parent = left;
}
/**
* @returns true - for null pointers and head node; false - for all other nodes
* @param {*} node
*/
isLeaf(node) {
if (node === null || node === this.head) {
return true;
}
return false;
}
/**
* Leaf nodes are considered 'black'. All real nodes contain 'color' data member
* @param {*} node
*/
fetchColor(node) {
if (this.isLeaf(node)) {
return BLACK;
}
else {
return node.color;
}
}
/**
* Tests a node for 'blackness'.
* @param {*} node
*/
isBlack(node) {
return (this.fetchColor(node) === BLACK);
}
/**
* Tests node for 'redness'.
* @param {*} node
*/
isRed(node) {
return (this.fetchColor(node) === RED);
}
/* ===========================
INSERT
=========================== */
/**
* A node will be inserted into the tree even if nodes with the same key already exist
* @param {*} node
* @returns {InsertionResult} - indicates whether a node was added and provides iterator to it.
*/
insertMulti(node) {
return this.insertNode(node, INSERT_MULTI);
}
/**
* The node is inserted into the tree only if nodes with the same key do not exist there
* @param {*} node
* @returns {InsertionResult} - indicates whether a node was added and provides iterator to it.
*/
insertUnique(node) {
return this.insertNode(node, INSERT_UNIQUE);
}
/**
* The node is inserted. If a node with the same key exists it's value will be replaced by the value of the new node
* @param {*} node
* @returns {InsertionResult} - indicates whether a node was added and provides iterator to it.
*/
insertOrReplace(node) {
return this.insertNode(node, INSERT_REPLACE);
}
/**
* @private
* Inserts node. Updates head node. Rebalances tree.
* @param {*} n - node
* @param {*} mode - one of INSERT_MULTI, INSERT_UNIQUE, INSERT_REPLACE
* @returns {InsertionResult} - indicates whether a node was added and provides iterator to it.
*/
insertNode(n, mode = INSERT_MULTI) {
let res = this.insertNodeInternal(this.head.root, n, mode);
if (res.wasAdded) {
if (this.head.size === 0) {
this.head.root = n;
this.head.leftmost = n;
this.head.rightmost = n;
n.left = this.head;
n.right = this.head;
}
else if (this.head.leftmost.left === n) {
this.head.leftmost = n;
n.left = this.head;
}
else if (this.head.rightmost.right === n) {
this.head.rightmost = n;
n.right = this.head;
}
this.insertRepairTree(n);
this.head.size = this.head.size + 1;
}
return res;
}
/**
* @private
* Inserts node according to the mode
* @param {*} root - root node of the tree
* @param {*} n - node to be inserted
* @param {*} mode - one of INSERT_MULTI, INSERT_UNIQUE, INSERT_REPLACE
* @returns {InsertionResult} - indicates whether a node was added and provides iterator to it.
*/
insertNodeInternal(root, n, mode) {
// recursively descend the tree until a leaf is found
let x = root;
let y = null;
let rc = -1;
// find matching node
while (!this.isLeaf(x)) {
y = x;
rc = this.compareNodes(n, y);
if (rc < 0) {
x = y.left;
}
else if (rc > 0) {
x = y.right;
}
else {
// node with the same key value
switch (mode) {
case INSERT_UNIQUE:
// it's a duplicate
return new InsertionResult(false, false, undefined);
case INSERT_REPLACE:
this.valuePolicy.copy(y, n);
return new InsertionResult(false, true, new Iterator(y, this));
default:
// INSERT_MULTI
x = y.right;
}
}
}
if (this.isLeaf(y)) {
n.parent = null;
n.left = this.head;
n.right = this.head;
}
else {
n.parent = y;
if (rc < 0) {
y.left = n;
}
else {
y.right = n;
}
}
return new InsertionResult(true, false, new Iterator(n, this));
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Insertion
* @param {*} n - node
*/
insertRepairTree(n) {
if (n.parent === null) {
this.repairCase1(n);
}
else if (this.isBlack(n.parent)) {
/* insert_case2(n);
// do nothing */
}
else if (this.isRed(n.uncle())) {
this.repairCase3(n);
}
else {
this.repairCase4(n);
}
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Insertion
* @param {*} n - node
*/
repairCase1(n) {
n.color = BLACK;
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Insertion
* @param {*} n - node
*/
repairCase3(n) {
n.parent.color = BLACK;
n.uncle().color = BLACK;
n.grandparent().color = RED;
this.insertRepairTree(n.grandparent());
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Insertion
* @param {*} n - node
*/
repairCase4(n) {
let p = n.parent;
let g = n.grandparent();
let nr = null;
if ((g.left !== null)
&& (n === g.left.right)) {
this.rotateLeft(p);
n = n.left;
}
else if ((g.right !== null)
&& (n === g.right.left)) {
this.rotateRight(p);
n = n.right;
}
p = n.parent;
g = n.grandparent();
if (n === p.left) {
this.rotateRight(g);
}
else {
this.rotateLeft(g);
}
p.color = BLACK;
g.color = RED;
}
/**
* @returns the node with the highest key for the subtree of the specified root node
* @param {*} node - root node of the subtree to be evaluated
*/
fetchMaximum(node) {
while (!this.isLeaf(node.right)) {
node = node.right;
}
return node;
}
/**
* @returns the node with the lowest key for the subtree of the specified root node
* @param {*} node - root node of the subtree to be evaluated
*/
fetchMinimum(node) {
while (!this.isLeaf(node.left)) {
node = node.left;
}
return node;
}
/* ===========================
ERASE
=========================== */
/**
* Removes node from the tree
* @param {*} node
*/
erase(node) {
if (this.isLeaf(node)) {
return;
}
this.eraseInternal(node);
let h = this.head;
h.size = h.size - 1;
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node - node
*/
eraseInternal(node) {
if (!this.isLeaf(node.left)
&& !this.isLeaf(node.right)) {
let pred = this.fetchMaximum(node.left);
this.valuePolicy.copy(node, pred);
node = pred;
}
let child = (this.isLeaf(node.right)) ? node.left : node.right;
if (this.isBlack(node)) {
this.eraseCase1(node);
}
this.replaceNode(node, child);
if (this.head.size === 2) {
if (!this.isLeaf(child)) {
// Root node must be BLACK
child.color = BLACK;
}
}
let h = this.head;
if (this.isLeaf(child)) {
/* The node didn't have children and it was removed
the head needs to update leftmost, rightmost pointers */
if (h.leftmost === node) {
let p = node.parent;
if (p !== null) {
h.leftmost = p;
p.left = h;
}
else {
h.leftmost = h;
}
}
if (h.rightmost === node) {
let p = node.parent;
if (p !== null) {
h.rightmost = p;
p.right = h;
}
else {
h.rightmost = h;
}
}
}
else {
// the node had a child. Now node is removed. Any references should point to the child now
if (h.leftmost === node) {
h.leftmost = child;
child.left = h;
}
if (h.rightmost === node) {
h.rightmost = child;
child.right = h;
}
}
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase1(node) {
if (node.parent === null) {
return;
}
else {
this.eraseCase2(node);
}
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase2(node) {
let s = node.sibling();
if (this.isRed(s)) {
node.parent.color = RED;
s.color = BLACK;
if (node === node.parent.left) {
this.rotateLeft(node.parent);
}
else {
this.rotateRight(node.parent);
}
}
this.eraseCase3(node);
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase3(node) {
let s = node.sibling();
let p = node.parent;
if (this.isBlack(p)
&& this.isBlack(s)
&& this.isBlack(s.left)
&& this.isBlack(s.right)) {
s.color = RED;
this.eraseCase1(p);
}
else {
this.eraseCase4(node);
}
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase4(node) {
let s = node.sibling();
let p = node.parent;
if (this.isRed(p)
&& this.isBlack(s)
&& this.isBlack(s.left)
&& this.isBlack(s.right)) {
s.color = RED;
p.color = BLACK;
}
else {
this.eraseCase5(node);
}
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase5(node) {
let s = node.sibling();
let p = node.parent;
/* The check below is unnecessary
due to case 2 (even though case 2 changed the sibling to a sibling's child,
the sibling's child can't be red, since no red parent can have a red child). */
/* if ((!this.isLeaf(s))
&& this.isBlack(s)) { */
/* the following statements just force the red to be on the left of the left of the parent,
or right of the right, so case six will rotate correctly. */
if (node === p.left
&& this.isRed(s.left)
&& this.isBlack(s.right)) {
s.color = RED;
s.left.color = BLACK;
this.rotateRight(s);
}
else if (node === p.right
&& this.isBlack(s.left)
&& this.isRed(s.right)) {
s.color = RED;
s.right.color = BLACK;
this.rotateLeft(s);
}
//}
this.eraseCase6(node);
}
/**
* @private
* The method is decribed at: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
* @param {*} node
*/
eraseCase6(node) {
let s = node.sibling();
let p = node.parent;
s.color = this.fetchColor(p);
p.color = BLACK;
if (node === p.left) {
s.right.color = BLACK;
this.rotateLeft(p);
}
else {
s.left.color = BLACK;
this.rotateRight(p);
}
}
/* ===========================
SEARCH BY KEY
=========================== */
/**
* @returns an iterator pointin to a node with matching key value. If node is not found then end() iterator is returned.
* @param {*} k - key value
*/
find(k) {
let y = this.head;
let x = y.root;
while (!this.isLeaf(x)) {
let rc = this.compare(x.key, k);
if (rc > 0) {
y = x;
x = x.left;
}
else if (rc < 0) {
y = x;
x = x.right;
}
else {
return new Iterator(x, this);
}
}
return new Iterator(this.head, this);
}
/**
* @returns an iterator pointing to the first node in the tree that is not less than
* (i.e. greater or equal to) the specified key value, or end() if no such node is found.
* @param {*} k - key value
*/
lowerBound(k) {
let y = this.head;
let x = y.root;
while (!this.isLeaf(x)) {
let rc = this.compare(x.key, k);
if (rc >= 0) {
y = x;
x = x.left;
}
else {
x = x.right;
}
}
return new Iterator(y, this);
}
/**
* @returns an iterator pointing to the first node in the tree that is greater than
* the specified key value, or end() if no such node is found.
* @param {*} k - key value
*/
upperBound(k) {
let y = this.head;
let x = y.root;
while (!this.isLeaf(x)) {
let rc = this.compare(x.key, k);
if (rc > 0) {
y = x;
x = x.left;
}
else {
x = x.right;
}
}
return new Iterator(y, this);
}
/* ===========================
ITERATORS
=========================== */
/**
* @returns iterator pointing to the node with the lowest key
*/
begin() {
return new Iterator(this.head.leftmost, this);
}
/**
* @returns iterator pointing to the node following the node with the highest key
*/
end() {
return new Iterator(this.head, this);
}