forked from ONLYOFFICE/sdkjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervalTree.js
1522 lines (1488 loc) · 61.9 KB
/
intervalTree.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
/*
* (c) Copyright Ascensio System SIA 2010-2024
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
"use strict";
(function(window) {
// An augmented AVL Tree where each node maintains a list of records and their search intervals.
// Record is composed of an interval and its underlying data, sent by a client. This allows the
// interval tree to have the same interval inserted multiple times, as long its data is different.
// Both insertion and deletion require O(log n) time. Searching requires O(k*logn) time, where `k`
// is the number of intervals in the output list.
var isSame = function shallowEqual(objA, objB, compare, compareContext) {
var ret = compare ? compare.call(compareContext, objA, objB) : void 0;
if (ret !== void 0) {
return !!ret;
}
if (objA === objB) {
return true;
}
if (typeof objA !== "object" || !objA || typeof objB !== "object" || !objB) {
return false;
}
var keysA = Object.keys(objA);
var keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
// Test for A's keys different from B.
for (var idx = 0; idx < keysA.length; idx++) {
var key = keysA[idx];
if (!bHasOwnProperty(key)) {
return false;
}
var valueA = objA[key];
var valueB = objB[key];
ret = compare ? compare.call(compareContext, valueA, valueB, key) : void 0;
if (ret === false || (ret === void 0 && valueA !== valueB)) {
return false;
}
}
return true;
};
function height(node) {
if (node === undefined) {
return -1;
}
else {
return node.height;
}
}
var Node = /** @class */ (function () {
function Node(intervalTree, record) {
this.intervalTree = intervalTree;
this.records = [];
this.height = 0;
this.key = record.low;
this.max = record.high;
// Save the array of all records with the same key for this node
this.records.push(record);
}
// Gets the highest record.high value for this node
Node.prototype.getNodeHigh = function () {
var high = this.records[0].high;
for (var i = 1; i < this.records.length; i++) {
if (this.records[i].high > high) {
high = this.records[i].high;
}
}
return high;
};
// Updates height value of the node. Called during insertion, rebalance, removal
Node.prototype.updateHeight = function () {
this.height = Math.max(height(this.left), height(this.right)) + 1;
};
// Updates the max value of all the parents after inserting into already existing node, as well as
// removing the node completely or removing the record of an already existing node. Starts with
// the parent of an affected node and bubbles up to root
Node.prototype.updateMaxOfParents = function () {
if (this === undefined) {
return;
}
var thisHigh = this.getNodeHigh();
if (this.left !== undefined && this.right !== undefined) {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(this.left.max, thisHigh);
}
else if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(this.right.max, thisHigh);
}
else {
this.max = thisHigh;
}
if (this.parent) {
this.parent.updateMaxOfParents();
}
};
/*
Left-Left case:
z y
/ \ / \
y T4 Right Rotate (z) x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
Left-Right case:
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
*/
// Handles Left-Left case and Left-Right case after rebalancing AVL tree
Node.prototype._updateMaxAfterRightRotate = function () {
var parent = this.parent;
var left = parent.left;
// Update max of left sibling (x in first case, y in second)
var thisParentLeftHigh = left.getNodeHigh();
if (left.left === undefined && left.right !== undefined) {
left.max = Math.max(thisParentLeftHigh, left.right.max);
}
else if (left.left !== undefined && left.right === undefined) {
left.max = Math.max(thisParentLeftHigh, left.left.max);
}
else if (left.left === undefined && left.right === undefined) {
left.max = thisParentLeftHigh;
}
else {
left.max = Math.max(Math.max(left.left.max, left.right.max), thisParentLeftHigh);
}
// Update max of itself (z)
var thisHigh = this.getNodeHigh();
if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(thisHigh, this.right.max);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(thisHigh, this.left.max);
}
else if (this.left === undefined && this.right === undefined) {
this.max = thisHigh;
}
else {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
// Update max of parent (y in first case, x in second)
parent.max = Math.max(Math.max(parent.left.max, parent.right.max), parent.getNodeHigh());
};
/*
Right-Right case:
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/ \
T3 T4
Right-Left case:
z z x
/ \ / \ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4
*/
// Handles Right-Right case and Right-Left case in rebalancing AVL tree
Node.prototype._updateMaxAfterLeftRotate = function () {
var parent = this.parent;
var right = parent.right;
// Update max of right sibling (x in first case, y in second)
var thisParentRightHigh = right.getNodeHigh();
if (right.left === undefined && right.right !== undefined) {
right.max = Math.max(thisParentRightHigh, right.right.max);
}
else if (right.left !== undefined && right.right === undefined) {
right.max = Math.max(thisParentRightHigh, right.left.max);
}
else if (right.left === undefined && right.right === undefined) {
right.max = thisParentRightHigh;
}
else {
right.max = Math.max(Math.max(right.left.max, right.right.max), thisParentRightHigh);
}
// Update max of itself (z)
var thisHigh = this.getNodeHigh();
if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(thisHigh, this.right.max);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(thisHigh, this.left.max);
}
else if (this.left === undefined && this.right === undefined) {
this.max = thisHigh;
}
else {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
// Update max of parent (y in first case, x in second)
parent.max = Math.max(Math.max(parent.left.max, right.max), parent.getNodeHigh());
};
Node.prototype._leftRotate = function () {
var rightChild = this.right;
rightChild.parent = this.parent;
if (rightChild.parent === undefined) {
this.intervalTree.root = rightChild;
}
else {
if (rightChild.parent.left === this) {
rightChild.parent.left = rightChild;
}
else if (rightChild.parent.right === this) {
rightChild.parent.right = rightChild;
}
}
this.right = rightChild.left;
if (this.right !== undefined) {
this.right.parent = this;
}
rightChild.left = this;
this.parent = rightChild;
this.updateHeight();
rightChild.updateHeight();
};
Node.prototype._rightRotate = function () {
var leftChild = this.left;
leftChild.parent = this.parent;
if (leftChild.parent === undefined) {
this.intervalTree.root = leftChild;
}
else {
if (leftChild.parent.left === this) {
leftChild.parent.left = leftChild;
}
else if (leftChild.parent.right === this) {
leftChild.parent.right = leftChild;
}
}
this.left = leftChild.right;
if (this.left !== undefined) {
this.left.parent = this;
}
leftChild.right = this;
this.parent = leftChild;
this.updateHeight();
leftChild.updateHeight();
};
// Rebalances the tree if the height value between two nodes of the same parent is greater than
// two. There are 4 cases that can happen which are outlined in the graphics above
Node.prototype._rebalance = function () {
if (height(this.left) >= 2 + height(this.right)) {
var left = this.left;
if (height(left.left) >= height(left.right)) {
// Left-Left case
this._rightRotate();
this._updateMaxAfterRightRotate();
}
else {
// Left-Right case
left._leftRotate();
this._rightRotate();
this._updateMaxAfterRightRotate();
}
}
else if (height(this.right) >= 2 + height(this.left)) {
var right = this.right;
if (height(right.right) >= height(right.left)) {
// Right-Right case
this._leftRotate();
this._updateMaxAfterLeftRotate();
}
else {
// Right-Left case
right._rightRotate();
this._leftRotate();
this._updateMaxAfterLeftRotate();
}
}
};
Node.prototype.insert = function (record) {
if (record.low < this.key) {
// Insert into left subtree
if (this.left === undefined) {
this.left = new Node(this.intervalTree, record);
this.left.parent = this;
}
else {
this.left.insert(record);
}
}
else {
// Insert into right subtree
if (this.right === undefined) {
this.right = new Node(this.intervalTree, record);
this.right.parent = this;
}
else {
this.right.insert(record);
}
}
// Update the max value of this ancestor if needed
if (this.max < record.high) {
this.max = record.high;
}
// Update height of each node
this.updateHeight();
// Rebalance the tree to ensure all operations are executed in O(logn) time. This is especially
// important in searching, as the tree has a high chance of degenerating without the rebalancing
this._rebalance();
};
Node.prototype._getOverlappingRecords = function (currentNode, low, high, output) {
if (currentNode.key <= high && low <= currentNode.getNodeHigh()) {
// Nodes are overlapping, check if individual records in the node are overlapping
for (var i = 0; i < currentNode.records.length; i++) {
if (currentNode.records[i].high >= low) {
output.push(currentNode.records[i]);
}
}
}
};
Node.prototype.search = function (low, high, output) {
// Don't search nodes that don't exist
if (this === undefined) {
return;
}
// If interval is to the right of the rightmost point of any interval in this node and all its
// children, there won't be any matches
if (low > this.max) {
return;
}
// Search left children
if (this.left !== undefined && this.left.max >= low) {
this.left.search(low, high, output);
}
// Check this node
this._getOverlappingRecords(this, low, high, output);
// If interval is to the left of the start of this interval, then it can't be in any child to
// the right
if (high < this.key) {
return;
}
// Otherwise, search right children
if (this.right !== undefined) {
this.right.search(low, high, output);
}
};
// Searches for any overlapping node
Node.prototype.searchAny = function(low, high) {
// Don't search nodes that don't exist
if (this === undefined) {
return;
}
// If interval is to the right of the rightmost point of any interval in this node and all its
// children, there won't be any matches
if (low > this.max) {
return;
}
if (this.key <= high) {
// Nodes are overlapping, check if individual records in the node are overlapping
for (var i = 0; i < this.records.length; i++) {
if (this.records[i].high >= low) {
return this.records[i];
}
}
}
// Search left children
if (this.left !== undefined && this.left.max >= low) {
return this.left.searchAny(low, high);
} else if (this.right !== undefined && this.key <= high) {
return this.right.searchAny(low, high);
}
};
// Searches for a node by a `key` value
Node.prototype.searchExisting = function (low) {
if (this === undefined) {
return undefined;
}
if (this.key === low) {
return this;
}
else if (low < this.key) {
if (this.left !== undefined) {
return this.left.searchExisting(low);
}
}
else {
if (this.right !== undefined) {
return this.right.searchExisting(low);
}
}
return undefined;
};
// Returns the smallest node of the subtree
Node.prototype._minValue = function () {
if (this.left === undefined) {
return this;
}
else {
return this.left._minValue();
}
};
Node.prototype.remove = function (node) {
var parent = this.parent;
if (node.key < this.key) {
// Node to be removed is on the left side
if (this.left !== undefined) {
return this.left.remove(node);
}
else {
return undefined;
}
}
else if (node.key > this.key) {
// Node to be removed is on the right side
if (this.right !== undefined) {
return this.right.remove(node);
}
else {
return undefined;
}
}
else {
if (this.left !== undefined && this.right !== undefined) {
// Node has two children
var minValue = this.right._minValue();
this.key = minValue.key;
this.records = minValue.records;
return this.right.remove(this);
}
else if (parent.left === this) {
// One child or no child case on left side
if (this.right !== undefined) {
parent.left = this.right;
this.right.parent = parent;
}
else {
parent.left = this.left;
if (this.left !== undefined) {
this.left.parent = parent;
}
}
parent.updateMaxOfParents();
parent.updateHeight();
parent._rebalance();
return this;
}
else if (parent.right === this) {
// One child or no child case on right side
if (this.right !== undefined) {
parent.right = this.right;
this.right.parent = parent;
}
else {
parent.right = this.left;
if (this.left !== undefined) {
this.left.parent = parent;
}
}
parent.updateMaxOfParents();
parent.updateHeight();
parent._rebalance();
return this;
}
}
};
return Node;
}());
var NodeWithId = /** @class */ (function () {
function NodeWithId(intervalTree, record) {
this.intervalTree = intervalTree;
this.records = {};
this.recordsCount = 0;
this.height = 0;
this.key = record.low;
this.max = record.high;
// Save the array of all records with the same key for this node
this.records[record.id] = record;
this.recordsCount = 1;
}
// Gets the highest record.high value for this node
NodeWithId.prototype.getNodeHigh = function () {
var high = Number.NEGATIVE_INFINITY;
for (var i in this.records) {
if (this.records.hasOwnProperty(i)) {
high = Math.max(high, this.records[i].high);
}
}
return high;
};
// Updates height value of the node. Called during insertion, rebalance, removal
NodeWithId.prototype.updateHeight = function () {
this.height = Math.max(height(this.left), height(this.right)) + 1;
};
// Updates the max value of all the parents after inserting into already existing node, as well as
// removing the node completely or removing the record of an already existing node. Starts with
// the parent of an affected node and bubbles up to root
NodeWithId.prototype.updateMaxOfParents = function () {
if (this === undefined) {
return;
}
var thisHigh = this.getNodeHigh();
if (this.left !== undefined && this.right !== undefined) {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(this.left.max, thisHigh);
}
else if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(this.right.max, thisHigh);
}
else {
this.max = thisHigh;
}
if (this.parent) {
this.parent.updateMaxOfParents();
}
};
/*
Left-Left case:
z y
/ \ / \
y T4 Right Rotate (z) x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
Left-Right case:
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
*/
// Handles Left-Left case and Left-Right case after rebalancing AVL tree
NodeWithId.prototype._updateMaxAfterRightRotate = function () {
var parent = this.parent;
var left = parent.left;
// Update max of left sibling (x in first case, y in second)
var thisParentLeftHigh = left.getNodeHigh();
if (left.left === undefined && left.right !== undefined) {
left.max = Math.max(thisParentLeftHigh, left.right.max);
}
else if (left.left !== undefined && left.right === undefined) {
left.max = Math.max(thisParentLeftHigh, left.left.max);
}
else if (left.left === undefined && left.right === undefined) {
left.max = thisParentLeftHigh;
}
else {
left.max = Math.max(Math.max(left.left.max, left.right.max), thisParentLeftHigh);
}
// Update max of itself (z)
var thisHigh = this.getNodeHigh();
if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(thisHigh, this.right.max);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(thisHigh, this.left.max);
}
else if (this.left === undefined && this.right === undefined) {
this.max = thisHigh;
}
else {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
// Update max of parent (y in first case, x in second)
parent.max = Math.max(Math.max(parent.left.max, parent.right.max), parent.getNodeHigh());
};
/*
Right-Right case:
z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ / \
T2 x T1 T2 T3 T4
/ \
T3 T4
Right-Left case:
z z x
/ \ / \ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4
*/
// Handles Right-Right case and Right-Left case in rebalancing AVL tree
NodeWithId.prototype._updateMaxAfterLeftRotate = function () {
var parent = this.parent;
var right = parent.right;
// Update max of right sibling (x in first case, y in second)
var thisParentRightHigh = right.getNodeHigh();
if (right.left === undefined && right.right !== undefined) {
right.max = Math.max(thisParentRightHigh, right.right.max);
}
else if (right.left !== undefined && right.right === undefined) {
right.max = Math.max(thisParentRightHigh, right.left.max);
}
else if (right.left === undefined && right.right === undefined) {
right.max = thisParentRightHigh;
}
else {
right.max = Math.max(Math.max(right.left.max, right.right.max), thisParentRightHigh);
}
// Update max of itself (z)
var thisHigh = this.getNodeHigh();
if (this.left === undefined && this.right !== undefined) {
this.max = Math.max(thisHigh, this.right.max);
}
else if (this.left !== undefined && this.right === undefined) {
this.max = Math.max(thisHigh, this.left.max);
}
else if (this.left === undefined && this.right === undefined) {
this.max = thisHigh;
}
else {
this.max = Math.max(Math.max(this.left.max, this.right.max), thisHigh);
}
// Update max of parent (y in first case, x in second)
parent.max = Math.max(Math.max(parent.left.max, right.max), parent.getNodeHigh());
};
NodeWithId.prototype._leftRotate = function () {
var rightChild = this.right;
rightChild.parent = this.parent;
if (rightChild.parent === undefined) {
this.intervalTree.root = rightChild;
}
else {
if (rightChild.parent.left === this) {
rightChild.parent.left = rightChild;
}
else if (rightChild.parent.right === this) {
rightChild.parent.right = rightChild;
}
}
this.right = rightChild.left;
if (this.right !== undefined) {
this.right.parent = this;
}
rightChild.left = this;
this.parent = rightChild;
this.updateHeight();
rightChild.updateHeight();
};
NodeWithId.prototype._rightRotate = function () {
var leftChild = this.left;
leftChild.parent = this.parent;
if (leftChild.parent === undefined) {
this.intervalTree.root = leftChild;
}
else {
if (leftChild.parent.left === this) {
leftChild.parent.left = leftChild;
}
else if (leftChild.parent.right === this) {
leftChild.parent.right = leftChild;
}
}
this.left = leftChild.right;
if (this.left !== undefined) {
this.left.parent = this;
}
leftChild.right = this;
this.parent = leftChild;
this.updateHeight();
leftChild.updateHeight();
};
// Rebalances the tree if the height value between two nodes of the same parent is greater than
// two. There are 4 cases that can happen which are outlined in the graphics above
NodeWithId.prototype._rebalance = function () {
if (height(this.left) >= 2 + height(this.right)) {
var left = this.left;
if (height(left.left) >= height(left.right)) {
// Left-Left case
this._rightRotate();
this._updateMaxAfterRightRotate();
}
else {
// Left-Right case
left._leftRotate();
this._rightRotate();
this._updateMaxAfterRightRotate();
}
}
else if (height(this.right) >= 2 + height(this.left)) {
var right = this.right;
if (height(right.right) >= height(right.left)) {
// Right-Right case
this._leftRotate();
this._updateMaxAfterLeftRotate();
}
else {
// Right-Left case
right._rightRotate();
this._leftRotate();
this._updateMaxAfterLeftRotate();
}
}
};
NodeWithId.prototype.insert = function (record) {
if (record.low < this.key) {
// Insert into left subtree
if (this.left === undefined) {
this.left = new NodeWithId(this.intervalTree, record);
this.left.parent = this;
}
else {
this.left.insert(record);
}
}
else {
// Insert into right subtree
if (this.right === undefined) {
this.right = new NodeWithId(this.intervalTree, record);
this.right.parent = this;
}
else {
this.right.insert(record);
}
}
// Update the max value of this ancestor if needed
if (this.max < record.high) {
this.max = record.high;
}
// Update height of each node
this.updateHeight();
// Rebalance the tree to ensure all operations are executed in O(logn) time. This is especially
// important in searching, as the tree has a high chance of degenerating without the rebalancing
this._rebalance();
};
NodeWithId.prototype._getOverlappingRecords = function (currentNode, low, high, output) {
if (currentNode.key <= high && low <= currentNode.max) {
// Nodes are overlapping, check if individual records in the node are overlapping
for (var i in this.records) {
if (this.records.hasOwnProperty(i)) {
if (this.records[i].high >= low) {
output.push(this.records[i]);
}
}
}
}
};
NodeWithId.prototype.search = function (low, high, output) {
// Don't search nodes that don't exist
if (this === undefined) {
return;
}
// If interval is to the right of the rightmost point of any interval in this node and all its
// children, there won't be any matches
if (low > this.max) {
return;
}
// Search left children
if (this.left !== undefined && this.left.max >= low) {
this.left.search(low, high, output);
}
// Check this node
this._getOverlappingRecords(this, low, high, output);
// If interval is to the left of the start of this interval, then it can't be in any child to
// the right
if (high < this.key) {
return;
}
// Otherwise, search right children
if (this.right !== undefined) {
this.right.search(low, high, output);
}
};
// Searches for any overlapping node
NodeWithId.prototype.searchAny = function(low, high) {
// Don't search nodes that don't exist
if (this === undefined) {
return;
}
// If interval is to the right of the rightmost point of any interval in this node and all its
// children, there won't be any matches
if (low > this.max) {
return;
}
if (this.key <= high) {
// Nodes are overlapping, check if individual records in the node are overlapping
for (var i in this.records) {
if (this.records.hasOwnProperty(i)) {
if (this.records[i].high >= low) {
return this.records[i];
}
}
}
}
// Search left children
if (this.left !== undefined && this.left.max >= low) {
return this.left.searchAny(low, high);
} else if (this.right !== undefined && this.key <= high) {
return this.right.searchAny(low, high);
}
};
// Searches for a node by a `key` value
NodeWithId.prototype.searchExisting = function (low) {
if (this === undefined) {
return undefined;
}
if (this.key === low) {
return this;
}
else if (low < this.key) {
if (this.left !== undefined) {
return this.left.searchExisting(low);
}
}
else {
if (this.right !== undefined) {
return this.right.searchExisting(low);
}
}
return undefined;
};
// Returns the smallest node of the subtree
NodeWithId.prototype._minValue = function () {
if (this.left === undefined) {
return this;
}
else {
return this.left._minValue();
}
};
NodeWithId.prototype.remove = function (node) {
var parent = this.parent;
if (node.key < this.key) {
// NodeWithId to be removed is on the left side
if (this.left !== undefined) {
return this.left.remove(node);
}
else {
return undefined;
}
}
else if (node.key > this.key) {
// NodeWithId to be removed is on the right side
if (this.right !== undefined) {
return this.right.remove(node);
}
else {
return undefined;
}
}
else {
if (this.left !== undefined && this.right !== undefined) {
// NodeWithId has two children
var minValue = this.right._minValue();
this.key = minValue.key;
this.records = minValue.records;
this.recordsCount = minValue.recordsCount;
return this.right.remove(this);
}
else if (parent.left === this) {
// One child or no child case on left side
if (this.right !== undefined) {
parent.left = this.right;
this.right.parent = parent;
}
else {
parent.left = this.left;
if (this.left !== undefined) {
this.left.parent = parent;
}
}
parent.updateMaxOfParents();
parent.updateHeight();
parent._rebalance();
return this;
}
else if (parent.right === this) {
// One child or no child case on right side
if (this.right !== undefined) {
parent.right = this.right;
this.right.parent = parent;
}
else {
parent.right = this.left;
if (this.left !== undefined) {
this.left.parent = parent;
}
}
parent.updateMaxOfParents();
parent.updateHeight();
parent._rebalance();
return this;
}
}
};
return NodeWithId;
}());
var IntervalTree = /** @class */ (function () {
function IntervalTree() {
this.count = 0;
}
IntervalTree.prototype.insert = function (record) {
if (record.low > record.high) {
throw new Error('`low` value must be lower or equal to `high` value');
}
if (this.root === undefined) {
// Base case: Tree is empty, new node becomes root
this.root = new Node(this, record);
this.count++;
return true;
}
else {
// Otherwise, check if node already exists with the same key
var node = this.root.searchExisting(record.low);
if (node !== undefined) {
// Check the records in this node if there already is the one with same low, high, data
for (var i = 0; i < node.records.length; i++) {
if (isSame(node.records[i], record)) {
// This record is same as the one we're trying to insert; return false to indicate
// nothing has been inserted
return false;
}
}
// Add the record to the node
node.records.push(record);
// Update max of the node and its parents if necessary
if (record.high > node.max) {
node.max = record.high;
if (node.parent) {
node.parent.updateMaxOfParents();
}
}
this.count++;
return true;
}
else {
// Node with this key doesn't already exist. Call insert function on root's node
this.root.insert(record);
this.count++;
return true;
}
}
};
IntervalTree.prototype.search = function (low, high, opt_output) {