avl-tree-typed 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1537 -74
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1536 -72
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1537 -74
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1536 -72
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/avl-tree-typed.js +1612 -149
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +299 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
- package/src/data-structures/binary-tree/binary-tree.ts +606 -6
- package/src/data-structures/binary-tree/bst.ts +946 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
- package/src/data-structures/binary-tree/segment-tree.ts +145 -2
- package/src/data-structures/binary-tree/tree-map.ts +3423 -499
- package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
- package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
- package/src/data-structures/binary-tree/tree-set.ts +3209 -413
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +240 -0
- package/src/data-structures/graph/undirected-graph.ts +216 -0
- package/src/data-structures/hash/hash-map.ts +281 -19
- package/src/data-structures/heap/heap.ts +340 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
- package/src/data-structures/matrix/matrix.ts +202 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +365 -5
- package/src/data-structures/queue/queue.ts +288 -0
- package/src/data-structures/stack/stack.ts +240 -0
- package/src/data-structures/trie/trie.ts +295 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
package/dist/cjs/index.cjs
CHANGED
|
@@ -59,6 +59,56 @@ function makeTrampoline(fn) {
|
|
|
59
59
|
}
|
|
60
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
61
|
|
|
62
|
+
// src/common/error.ts
|
|
63
|
+
function raise(ErrorClass, message) {
|
|
64
|
+
throw new ErrorClass(message);
|
|
65
|
+
}
|
|
66
|
+
__name(raise, "raise");
|
|
67
|
+
var ERR = {
|
|
68
|
+
// Range / index
|
|
69
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
70
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
71
|
+
// Type / argument
|
|
72
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
73
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
74
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
75
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
76
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
77
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
78
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
79
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
80
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
81
|
+
// State / operation
|
|
82
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
83
|
+
// Matrix
|
|
84
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
85
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
86
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
87
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
88
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch"),
|
|
89
|
+
// Order statistic
|
|
90
|
+
orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/common/index.ts
|
|
94
|
+
var Range = class {
|
|
95
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
96
|
+
this.low = low;
|
|
97
|
+
this.high = high;
|
|
98
|
+
this.includeLow = includeLow;
|
|
99
|
+
this.includeHigh = includeHigh;
|
|
100
|
+
}
|
|
101
|
+
static {
|
|
102
|
+
__name(this, "Range");
|
|
103
|
+
}
|
|
104
|
+
// Determine whether a key is within the range
|
|
105
|
+
isInRange(key, comparator) {
|
|
106
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
107
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
108
|
+
return lowCheck && highCheck;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
62
112
|
// src/data-structures/base/iterable-element-base.ts
|
|
63
113
|
var IterableElementBase = class {
|
|
64
114
|
static {
|
|
@@ -77,7 +127,7 @@ var IterableElementBase = class {
|
|
|
77
127
|
if (options) {
|
|
78
128
|
const { toElementFn } = options;
|
|
79
129
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
80
|
-
else if (toElementFn)
|
|
130
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
81
131
|
}
|
|
82
132
|
}
|
|
83
133
|
/**
|
|
@@ -240,7 +290,7 @@ var IterableElementBase = class {
|
|
|
240
290
|
acc = initialValue;
|
|
241
291
|
} else {
|
|
242
292
|
const first = iter.next();
|
|
243
|
-
if (first.done)
|
|
293
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
244
294
|
acc = first.value;
|
|
245
295
|
index = 1;
|
|
246
296
|
}
|
|
@@ -475,50 +525,6 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
475
525
|
}
|
|
476
526
|
};
|
|
477
527
|
|
|
478
|
-
// src/common/error.ts
|
|
479
|
-
var ERR = {
|
|
480
|
-
// Range / index
|
|
481
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
482
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
483
|
-
// Type / argument
|
|
484
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
485
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
486
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
487
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
488
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
489
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
490
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
491
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
492
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
493
|
-
// State / operation
|
|
494
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
495
|
-
// Matrix
|
|
496
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
497
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
498
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
499
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
500
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
// src/common/index.ts
|
|
504
|
-
var Range = class {
|
|
505
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
506
|
-
this.low = low;
|
|
507
|
-
this.high = high;
|
|
508
|
-
this.includeLow = includeLow;
|
|
509
|
-
this.includeHigh = includeHigh;
|
|
510
|
-
}
|
|
511
|
-
static {
|
|
512
|
-
__name(this, "Range");
|
|
513
|
-
}
|
|
514
|
-
// Determine whether a key is within the range
|
|
515
|
-
isInRange(key, comparator) {
|
|
516
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
517
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
518
|
-
return lowCheck && highCheck;
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
|
|
522
528
|
// src/data-structures/base/iterable-entry-base.ts
|
|
523
529
|
var IterableEntryBase = class {
|
|
524
530
|
static {
|
|
@@ -772,6 +778,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
772
778
|
|
|
773
779
|
|
|
774
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
|
+
|
|
775
805
|
* @example
|
|
776
806
|
* // Track queue length
|
|
777
807
|
* const q = new Queue<number>();
|
|
@@ -798,6 +828,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
798
828
|
|
|
799
829
|
|
|
800
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
|
+
|
|
801
855
|
* @example
|
|
802
856
|
* // View the front element
|
|
803
857
|
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
@@ -840,6 +894,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
840
894
|
|
|
841
895
|
|
|
842
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
|
+
|
|
843
921
|
* @example
|
|
844
922
|
* // Queue for...of iteration and isEmpty check
|
|
845
923
|
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
@@ -878,6 +956,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
878
956
|
|
|
879
957
|
|
|
880
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
|
+
|
|
881
983
|
* @example
|
|
882
984
|
* // basic Queue creation and push operation
|
|
883
985
|
* // Create a simple Queue with initial values
|
|
@@ -923,6 +1025,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
923
1025
|
|
|
924
1026
|
|
|
925
1027
|
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
926
1052
|
* @example
|
|
927
1053
|
* // Queue shift and peek operations
|
|
928
1054
|
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
@@ -958,6 +1084,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
958
1084
|
|
|
959
1085
|
|
|
960
1086
|
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
961
1111
|
* @example
|
|
962
1112
|
* // Remove specific element
|
|
963
1113
|
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
@@ -986,6 +1136,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
986
1136
|
|
|
987
1137
|
|
|
988
1138
|
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
989
1163
|
* @example
|
|
990
1164
|
* // Access element by index
|
|
991
1165
|
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
@@ -1055,7 +1229,31 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1055
1229
|
|
|
1056
1230
|
|
|
1057
1231
|
|
|
1058
|
-
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
* @example
|
|
1059
1257
|
* // Remove all elements
|
|
1060
1258
|
* const q = new Queue<number>([1, 2, 3]);
|
|
1061
1259
|
* q.clear();
|
|
@@ -1077,6 +1275,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1077
1275
|
|
|
1078
1276
|
|
|
1079
1277
|
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1080
1302
|
* @example
|
|
1081
1303
|
* // Reclaim unused memory
|
|
1082
1304
|
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
@@ -1122,6 +1344,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1122
1344
|
|
|
1123
1345
|
|
|
1124
1346
|
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1125
1371
|
* @example
|
|
1126
1372
|
* // Create independent copy
|
|
1127
1373
|
* const q = new Queue<number>([1, 2, 3]);
|
|
@@ -1151,6 +1397,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1151
1397
|
|
|
1152
1398
|
|
|
1153
1399
|
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
|
|
1405
|
+
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1154
1424
|
* @example
|
|
1155
1425
|
* // Filter elements
|
|
1156
1426
|
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
@@ -1184,6 +1454,30 @@ var Queue = class _Queue extends LinearBase {
|
|
|
1184
1454
|
|
|
1185
1455
|
|
|
1186
1456
|
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1476
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
|
|
1480
|
+
|
|
1187
1481
|
* @example
|
|
1188
1482
|
* // Transform elements
|
|
1189
1483
|
* const q = new Queue<number>([1, 2, 3]);
|
|
@@ -1434,7 +1728,7 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1434
1728
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1435
1729
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1436
1730
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1437
|
-
else if (toEntryFn)
|
|
1731
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1438
1732
|
}
|
|
1439
1733
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1440
1734
|
}
|
|
@@ -1660,6 +1954,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1660
1954
|
|
|
1661
1955
|
|
|
1662
1956
|
|
|
1957
|
+
|
|
1958
|
+
|
|
1959
|
+
|
|
1960
|
+
|
|
1961
|
+
|
|
1962
|
+
|
|
1963
|
+
|
|
1964
|
+
|
|
1965
|
+
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
|
|
1969
|
+
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
|
|
1974
|
+
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1663
1981
|
* @example
|
|
1664
1982
|
* // Add a single node
|
|
1665
1983
|
* const tree = new BinaryTree<number>();
|
|
@@ -1690,6 +2008,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1690
2008
|
|
|
1691
2009
|
|
|
1692
2010
|
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
2018
|
+
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
|
|
2025
|
+
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
|
|
2034
|
+
|
|
1693
2035
|
* @example
|
|
1694
2036
|
* // basic BinaryTree creation and insertion
|
|
1695
2037
|
* // Create a BinaryTree with entries
|
|
@@ -1772,6 +2114,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1772
2114
|
|
|
1773
2115
|
|
|
1774
2116
|
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
|
|
2135
|
+
|
|
2136
|
+
|
|
2137
|
+
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
|
|
1775
2141
|
* @example
|
|
1776
2142
|
* // Bulk add
|
|
1777
2143
|
* const tree = new BinaryTree<number>();
|
|
@@ -1790,6 +2156,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1790
2156
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1791
2157
|
|
|
1792
2158
|
|
|
2159
|
+
|
|
2160
|
+
|
|
2161
|
+
|
|
2162
|
+
|
|
2163
|
+
|
|
2164
|
+
|
|
2165
|
+
|
|
2166
|
+
|
|
2167
|
+
|
|
2168
|
+
|
|
2169
|
+
|
|
2170
|
+
|
|
2171
|
+
|
|
2172
|
+
|
|
2173
|
+
|
|
2174
|
+
|
|
2175
|
+
|
|
2176
|
+
|
|
2177
|
+
|
|
2178
|
+
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
1793
2183
|
* @example
|
|
1794
2184
|
* // Set multiple entries
|
|
1795
2185
|
* const tree = new BinaryTree<number, string>();
|
|
@@ -1829,6 +2219,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1829
2219
|
|
|
1830
2220
|
|
|
1831
2221
|
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
1832
2246
|
* @example
|
|
1833
2247
|
* // Combine trees
|
|
1834
2248
|
* const t1 = new BinaryTree<number>([1, 2]);
|
|
@@ -1867,21 +2281,45 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1867
2281
|
|
|
1868
2282
|
|
|
1869
2283
|
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
2284
|
+
|
|
2285
|
+
|
|
2286
|
+
|
|
2287
|
+
|
|
2288
|
+
|
|
2289
|
+
|
|
2290
|
+
|
|
2291
|
+
|
|
2292
|
+
|
|
2293
|
+
|
|
2294
|
+
|
|
2295
|
+
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
* @example
|
|
2309
|
+
* // Remove a node
|
|
2310
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2311
|
+
* tree.delete(3);
|
|
2312
|
+
* console.log(tree.has(3)); // false;
|
|
2313
|
+
* console.log(tree.size); // 4;
|
|
2314
|
+
*/
|
|
2315
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
2316
|
+
const deletedResult = [];
|
|
2317
|
+
if (!this._root) return deletedResult;
|
|
2318
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
2319
|
+
if (!curr) return deletedResult;
|
|
2320
|
+
const parent = curr?.parent;
|
|
2321
|
+
let needBalanced;
|
|
2322
|
+
let orgCurrent = curr;
|
|
1885
2323
|
if (!curr.left && !curr.right && !parent) {
|
|
1886
2324
|
this._setRoot(void 0);
|
|
1887
2325
|
} else if (curr.left) {
|
|
@@ -1983,6 +2421,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1983
2421
|
|
|
1984
2422
|
|
|
1985
2423
|
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
|
|
2427
|
+
|
|
2428
|
+
|
|
2429
|
+
|
|
2430
|
+
|
|
2431
|
+
|
|
2432
|
+
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
1986
2448
|
* @example
|
|
1987
2449
|
* // Get node by key
|
|
1988
2450
|
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
@@ -2017,6 +2479,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2017
2479
|
|
|
2018
2480
|
|
|
2019
2481
|
|
|
2482
|
+
|
|
2483
|
+
|
|
2484
|
+
|
|
2485
|
+
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
|
|
2489
|
+
|
|
2490
|
+
|
|
2491
|
+
|
|
2492
|
+
|
|
2493
|
+
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
|
|
2020
2506
|
* @example
|
|
2021
2507
|
* // Retrieve value by key
|
|
2022
2508
|
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
@@ -2053,6 +2539,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2053
2539
|
|
|
2054
2540
|
|
|
2055
2541
|
|
|
2542
|
+
|
|
2543
|
+
|
|
2544
|
+
|
|
2545
|
+
|
|
2546
|
+
|
|
2547
|
+
|
|
2548
|
+
|
|
2549
|
+
|
|
2550
|
+
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
|
|
2554
|
+
|
|
2555
|
+
|
|
2556
|
+
|
|
2557
|
+
|
|
2558
|
+
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
|
|
2056
2566
|
* @example
|
|
2057
2567
|
* // Remove all nodes
|
|
2058
2568
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2077,6 +2587,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2077
2587
|
|
|
2078
2588
|
|
|
2079
2589
|
|
|
2590
|
+
|
|
2591
|
+
|
|
2592
|
+
|
|
2593
|
+
|
|
2594
|
+
|
|
2595
|
+
|
|
2596
|
+
|
|
2597
|
+
|
|
2598
|
+
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2080
2614
|
* @example
|
|
2081
2615
|
* // Check empty
|
|
2082
2616
|
* console.log(new BinaryTree().isEmpty()); // true;
|
|
@@ -2110,6 +2644,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2110
2644
|
|
|
2111
2645
|
|
|
2112
2646
|
|
|
2647
|
+
|
|
2648
|
+
|
|
2649
|
+
|
|
2650
|
+
|
|
2651
|
+
|
|
2652
|
+
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
|
|
2656
|
+
|
|
2657
|
+
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2113
2671
|
* @example
|
|
2114
2672
|
* // Check BST property
|
|
2115
2673
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2170,6 +2728,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2170
2728
|
|
|
2171
2729
|
|
|
2172
2730
|
|
|
2731
|
+
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
|
|
2740
|
+
|
|
2741
|
+
|
|
2742
|
+
|
|
2743
|
+
|
|
2744
|
+
|
|
2745
|
+
|
|
2746
|
+
|
|
2747
|
+
|
|
2748
|
+
|
|
2749
|
+
|
|
2750
|
+
|
|
2751
|
+
|
|
2752
|
+
|
|
2753
|
+
|
|
2754
|
+
|
|
2173
2755
|
* @example
|
|
2174
2756
|
* // Get depth of a node
|
|
2175
2757
|
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
@@ -2207,6 +2789,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2207
2789
|
|
|
2208
2790
|
|
|
2209
2791
|
|
|
2792
|
+
|
|
2793
|
+
|
|
2794
|
+
|
|
2795
|
+
|
|
2796
|
+
|
|
2797
|
+
|
|
2798
|
+
|
|
2799
|
+
|
|
2800
|
+
|
|
2801
|
+
|
|
2802
|
+
|
|
2803
|
+
|
|
2804
|
+
|
|
2805
|
+
|
|
2806
|
+
|
|
2807
|
+
|
|
2808
|
+
|
|
2809
|
+
|
|
2810
|
+
|
|
2811
|
+
|
|
2812
|
+
|
|
2813
|
+
|
|
2814
|
+
|
|
2815
|
+
|
|
2210
2816
|
* @example
|
|
2211
2817
|
* // Get tree height
|
|
2212
2818
|
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
@@ -2660,6 +3266,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2660
3266
|
|
|
2661
3267
|
|
|
2662
3268
|
|
|
3269
|
+
|
|
3270
|
+
|
|
3271
|
+
|
|
3272
|
+
|
|
3273
|
+
|
|
3274
|
+
|
|
3275
|
+
|
|
3276
|
+
|
|
3277
|
+
|
|
3278
|
+
|
|
3279
|
+
|
|
3280
|
+
|
|
3281
|
+
|
|
3282
|
+
|
|
3283
|
+
|
|
3284
|
+
|
|
3285
|
+
|
|
3286
|
+
|
|
3287
|
+
|
|
3288
|
+
|
|
3289
|
+
|
|
3290
|
+
|
|
3291
|
+
|
|
3292
|
+
|
|
2663
3293
|
* @example
|
|
2664
3294
|
* // Deep copy
|
|
2665
3295
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2688,6 +3318,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2688
3318
|
|
|
2689
3319
|
|
|
2690
3320
|
|
|
3321
|
+
|
|
3322
|
+
|
|
3323
|
+
|
|
3324
|
+
|
|
3325
|
+
|
|
3326
|
+
|
|
3327
|
+
|
|
3328
|
+
|
|
3329
|
+
|
|
3330
|
+
|
|
3331
|
+
|
|
3332
|
+
|
|
3333
|
+
|
|
3334
|
+
|
|
3335
|
+
|
|
3336
|
+
|
|
3337
|
+
|
|
3338
|
+
|
|
3339
|
+
|
|
3340
|
+
|
|
3341
|
+
|
|
3342
|
+
|
|
3343
|
+
|
|
3344
|
+
|
|
2691
3345
|
* @example
|
|
2692
3346
|
* // Filter nodes by condition
|
|
2693
3347
|
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
@@ -2720,7 +3374,31 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2720
3374
|
|
|
2721
3375
|
|
|
2722
3376
|
|
|
2723
|
-
|
|
3377
|
+
|
|
3378
|
+
|
|
3379
|
+
|
|
3380
|
+
|
|
3381
|
+
|
|
3382
|
+
|
|
3383
|
+
|
|
3384
|
+
|
|
3385
|
+
|
|
3386
|
+
|
|
3387
|
+
|
|
3388
|
+
|
|
3389
|
+
|
|
3390
|
+
|
|
3391
|
+
|
|
3392
|
+
|
|
3393
|
+
|
|
3394
|
+
|
|
3395
|
+
|
|
3396
|
+
|
|
3397
|
+
|
|
3398
|
+
|
|
3399
|
+
|
|
3400
|
+
|
|
3401
|
+
* @example
|
|
2724
3402
|
* // Transform to new tree
|
|
2725
3403
|
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
2726
3404
|
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
@@ -2777,6 +3455,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2777
3455
|
|
|
2778
3456
|
|
|
2779
3457
|
|
|
3458
|
+
|
|
3459
|
+
|
|
3460
|
+
|
|
3461
|
+
|
|
3462
|
+
|
|
3463
|
+
|
|
3464
|
+
|
|
3465
|
+
|
|
3466
|
+
|
|
3467
|
+
|
|
3468
|
+
|
|
3469
|
+
|
|
3470
|
+
|
|
3471
|
+
|
|
3472
|
+
|
|
3473
|
+
|
|
3474
|
+
|
|
3475
|
+
|
|
3476
|
+
|
|
3477
|
+
|
|
3478
|
+
|
|
3479
|
+
|
|
3480
|
+
|
|
3481
|
+
|
|
2780
3482
|
* @example
|
|
2781
3483
|
* // Display tree
|
|
2782
3484
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -3408,12 +4110,16 @@ var BST = class extends BinaryTree {
|
|
|
3408
4110
|
} else {
|
|
3409
4111
|
this._comparator = this._createDefaultComparator();
|
|
3410
4112
|
}
|
|
4113
|
+
if (options.enableOrderStatistic) {
|
|
4114
|
+
this._enableOrderStatistic = true;
|
|
4115
|
+
}
|
|
3411
4116
|
} else {
|
|
3412
4117
|
this._comparator = this._createDefaultComparator();
|
|
3413
4118
|
}
|
|
3414
4119
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
3415
4120
|
}
|
|
3416
4121
|
_root = void 0;
|
|
4122
|
+
_enableOrderStatistic = false;
|
|
3417
4123
|
/**
|
|
3418
4124
|
* Gets the root node of the tree.
|
|
3419
4125
|
* @remarks Time O(1)
|
|
@@ -3548,6 +4254,54 @@ var BST = class extends BinaryTree {
|
|
|
3548
4254
|
|
|
3549
4255
|
|
|
3550
4256
|
|
|
4257
|
+
|
|
4258
|
+
|
|
4259
|
+
|
|
4260
|
+
|
|
4261
|
+
|
|
4262
|
+
|
|
4263
|
+
|
|
4264
|
+
|
|
4265
|
+
|
|
4266
|
+
|
|
4267
|
+
|
|
4268
|
+
|
|
4269
|
+
|
|
4270
|
+
|
|
4271
|
+
|
|
4272
|
+
|
|
4273
|
+
|
|
4274
|
+
|
|
4275
|
+
|
|
4276
|
+
|
|
4277
|
+
|
|
4278
|
+
|
|
4279
|
+
|
|
4280
|
+
|
|
4281
|
+
|
|
4282
|
+
|
|
4283
|
+
|
|
4284
|
+
|
|
4285
|
+
|
|
4286
|
+
|
|
4287
|
+
|
|
4288
|
+
|
|
4289
|
+
|
|
4290
|
+
|
|
4291
|
+
|
|
4292
|
+
|
|
4293
|
+
|
|
4294
|
+
|
|
4295
|
+
|
|
4296
|
+
|
|
4297
|
+
|
|
4298
|
+
|
|
4299
|
+
|
|
4300
|
+
|
|
4301
|
+
|
|
4302
|
+
|
|
4303
|
+
|
|
4304
|
+
|
|
3551
4305
|
* @example
|
|
3552
4306
|
* // Get node object by key
|
|
3553
4307
|
* const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
|
|
@@ -3702,6 +4456,84 @@ var BST = class extends BinaryTree {
|
|
|
3702
4456
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
3703
4457
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
3704
4458
|
}
|
|
4459
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4460
|
+
if (!this._enableOrderStatistic) {
|
|
4461
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4462
|
+
}
|
|
4463
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4464
|
+
let actualCallback = void 0;
|
|
4465
|
+
let actualIterationType = this.iterationType;
|
|
4466
|
+
if (typeof callback === "string") {
|
|
4467
|
+
actualIterationType = callback;
|
|
4468
|
+
} else if (callback) {
|
|
4469
|
+
actualCallback = callback;
|
|
4470
|
+
if (iterationType) {
|
|
4471
|
+
actualIterationType = iterationType;
|
|
4472
|
+
}
|
|
4473
|
+
}
|
|
4474
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4475
|
+
if (!node) return void 0;
|
|
4476
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4477
|
+
}
|
|
4478
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4479
|
+
if (!this._enableOrderStatistic) {
|
|
4480
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4481
|
+
}
|
|
4482
|
+
if (!this._root || this._size === 0) return -1;
|
|
4483
|
+
let actualIterationType = this.iterationType;
|
|
4484
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4485
|
+
let key;
|
|
4486
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4487
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4488
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4489
|
+
key = results[0];
|
|
4490
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4491
|
+
return -1;
|
|
4492
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4493
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4494
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4495
|
+
key = keyNodeEntryOrPredicate[0] ?? void 0;
|
|
4496
|
+
if (key === void 0 || key === null) return -1;
|
|
4497
|
+
} else {
|
|
4498
|
+
key = keyNodeEntryOrPredicate;
|
|
4499
|
+
}
|
|
4500
|
+
if (key === void 0) return -1;
|
|
4501
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4502
|
+
}
|
|
4503
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4504
|
+
if (!this._enableOrderStatistic) {
|
|
4505
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4506
|
+
}
|
|
4507
|
+
if (this._size === 0) return [];
|
|
4508
|
+
const lo = Math.max(0, start);
|
|
4509
|
+
const hi = Math.min(this._size - 1, end);
|
|
4510
|
+
if (lo > hi) return [];
|
|
4511
|
+
let actualCallback = void 0;
|
|
4512
|
+
let actualIterationType = this.iterationType;
|
|
4513
|
+
if (typeof callback === "string") {
|
|
4514
|
+
actualIterationType = callback;
|
|
4515
|
+
} else if (callback) {
|
|
4516
|
+
actualCallback = callback;
|
|
4517
|
+
if (iterationType) {
|
|
4518
|
+
actualIterationType = iterationType;
|
|
4519
|
+
}
|
|
4520
|
+
}
|
|
4521
|
+
const results = [];
|
|
4522
|
+
const count = hi - lo + 1;
|
|
4523
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4524
|
+
if (!startNode) return [];
|
|
4525
|
+
let collected = 0;
|
|
4526
|
+
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK;
|
|
4527
|
+
let current = startNode;
|
|
4528
|
+
while (current && collected < count) {
|
|
4529
|
+
results.push(cb(current));
|
|
4530
|
+
collected++;
|
|
4531
|
+
if (collected < count) {
|
|
4532
|
+
current = this._next(current);
|
|
4533
|
+
}
|
|
4534
|
+
}
|
|
4535
|
+
return results;
|
|
4536
|
+
}
|
|
3705
4537
|
/**
|
|
3706
4538
|
* Adds a new node to the BST based on key comparison.
|
|
3707
4539
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -3729,6 +4561,78 @@ var BST = class extends BinaryTree {
|
|
|
3729
4561
|
|
|
3730
4562
|
|
|
3731
4563
|
|
|
4564
|
+
|
|
4565
|
+
|
|
4566
|
+
|
|
4567
|
+
|
|
4568
|
+
|
|
4569
|
+
|
|
4570
|
+
|
|
4571
|
+
|
|
4572
|
+
|
|
4573
|
+
|
|
4574
|
+
|
|
4575
|
+
|
|
4576
|
+
|
|
4577
|
+
|
|
4578
|
+
|
|
4579
|
+
|
|
4580
|
+
|
|
4581
|
+
|
|
4582
|
+
|
|
4583
|
+
|
|
4584
|
+
|
|
4585
|
+
|
|
4586
|
+
|
|
4587
|
+
|
|
4588
|
+
|
|
4589
|
+
|
|
4590
|
+
|
|
4591
|
+
|
|
4592
|
+
|
|
4593
|
+
|
|
4594
|
+
|
|
4595
|
+
|
|
4596
|
+
|
|
4597
|
+
|
|
4598
|
+
|
|
4599
|
+
|
|
4600
|
+
|
|
4601
|
+
|
|
4602
|
+
|
|
4603
|
+
|
|
4604
|
+
|
|
4605
|
+
|
|
4606
|
+
|
|
4607
|
+
|
|
4608
|
+
|
|
4609
|
+
|
|
4610
|
+
|
|
4611
|
+
|
|
4612
|
+
|
|
4613
|
+
|
|
4614
|
+
|
|
4615
|
+
|
|
4616
|
+
|
|
4617
|
+
|
|
4618
|
+
|
|
4619
|
+
|
|
4620
|
+
|
|
4621
|
+
|
|
4622
|
+
|
|
4623
|
+
|
|
4624
|
+
|
|
4625
|
+
|
|
4626
|
+
|
|
4627
|
+
|
|
4628
|
+
|
|
4629
|
+
|
|
4630
|
+
|
|
4631
|
+
|
|
4632
|
+
|
|
4633
|
+
|
|
4634
|
+
|
|
4635
|
+
|
|
3732
4636
|
|
|
3733
4637
|
|
|
3734
4638
|
|
|
@@ -3748,6 +4652,7 @@ var BST = class extends BinaryTree {
|
|
|
3748
4652
|
this._setRoot(newNode);
|
|
3749
4653
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3750
4654
|
this._size++;
|
|
4655
|
+
this._updateCount(newNode);
|
|
3751
4656
|
return true;
|
|
3752
4657
|
}
|
|
3753
4658
|
let current = this._root;
|
|
@@ -3761,6 +4666,7 @@ var BST = class extends BinaryTree {
|
|
|
3761
4666
|
current.left = newNode;
|
|
3762
4667
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3763
4668
|
this._size++;
|
|
4669
|
+
this._updateCountAlongPath(newNode);
|
|
3764
4670
|
return true;
|
|
3765
4671
|
}
|
|
3766
4672
|
if (current.left !== null) current = current.left;
|
|
@@ -3769,6 +4675,7 @@ var BST = class extends BinaryTree {
|
|
|
3769
4675
|
current.right = newNode;
|
|
3770
4676
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3771
4677
|
this._size++;
|
|
4678
|
+
this._updateCountAlongPath(newNode);
|
|
3772
4679
|
return true;
|
|
3773
4680
|
}
|
|
3774
4681
|
if (current.right !== null) current = current.right;
|
|
@@ -3798,6 +4705,54 @@ var BST = class extends BinaryTree {
|
|
|
3798
4705
|
|
|
3799
4706
|
|
|
3800
4707
|
|
|
4708
|
+
|
|
4709
|
+
|
|
4710
|
+
|
|
4711
|
+
|
|
4712
|
+
|
|
4713
|
+
|
|
4714
|
+
|
|
4715
|
+
|
|
4716
|
+
|
|
4717
|
+
|
|
4718
|
+
|
|
4719
|
+
|
|
4720
|
+
|
|
4721
|
+
|
|
4722
|
+
|
|
4723
|
+
|
|
4724
|
+
|
|
4725
|
+
|
|
4726
|
+
|
|
4727
|
+
|
|
4728
|
+
|
|
4729
|
+
|
|
4730
|
+
|
|
4731
|
+
|
|
4732
|
+
|
|
4733
|
+
|
|
4734
|
+
|
|
4735
|
+
|
|
4736
|
+
|
|
4737
|
+
|
|
4738
|
+
|
|
4739
|
+
|
|
4740
|
+
|
|
4741
|
+
|
|
4742
|
+
|
|
4743
|
+
|
|
4744
|
+
|
|
4745
|
+
|
|
4746
|
+
|
|
4747
|
+
|
|
4748
|
+
|
|
4749
|
+
|
|
4750
|
+
|
|
4751
|
+
|
|
4752
|
+
|
|
4753
|
+
|
|
4754
|
+
|
|
4755
|
+
|
|
3801
4756
|
* @example
|
|
3802
4757
|
* // Set multiple key-value pairs
|
|
3803
4758
|
* const bst = new BST<number, string>();
|
|
@@ -4061,11 +5016,35 @@ var BST = class extends BinaryTree {
|
|
|
4061
5016
|
|
|
4062
5017
|
|
|
4063
5018
|
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
5019
|
+
|
|
5020
|
+
|
|
5021
|
+
|
|
5022
|
+
|
|
5023
|
+
|
|
5024
|
+
|
|
5025
|
+
|
|
5026
|
+
|
|
5027
|
+
|
|
5028
|
+
|
|
5029
|
+
|
|
5030
|
+
|
|
5031
|
+
|
|
5032
|
+
|
|
5033
|
+
|
|
5034
|
+
|
|
5035
|
+
|
|
5036
|
+
|
|
5037
|
+
|
|
5038
|
+
|
|
5039
|
+
|
|
5040
|
+
|
|
5041
|
+
|
|
5042
|
+
|
|
5043
|
+
* @example
|
|
5044
|
+
* // Rebalance the tree
|
|
5045
|
+
* const bst = new BST<number>();
|
|
5046
|
+
* // Insert in sorted order (worst case for BST)
|
|
5047
|
+
* for (let i = 1; i <= 7; i++) bst.add(i);
|
|
4069
5048
|
* console.log(bst.isAVLBalanced()); // false;
|
|
4070
5049
|
* bst.perfectlyBalance();
|
|
4071
5050
|
* console.log(bst.isAVLBalanced()); // true;
|
|
@@ -4106,6 +5085,30 @@ var BST = class extends BinaryTree {
|
|
|
4106
5085
|
|
|
4107
5086
|
|
|
4108
5087
|
|
|
5088
|
+
|
|
5089
|
+
|
|
5090
|
+
|
|
5091
|
+
|
|
5092
|
+
|
|
5093
|
+
|
|
5094
|
+
|
|
5095
|
+
|
|
5096
|
+
|
|
5097
|
+
|
|
5098
|
+
|
|
5099
|
+
|
|
5100
|
+
|
|
5101
|
+
|
|
5102
|
+
|
|
5103
|
+
|
|
5104
|
+
|
|
5105
|
+
|
|
5106
|
+
|
|
5107
|
+
|
|
5108
|
+
|
|
5109
|
+
|
|
5110
|
+
|
|
5111
|
+
|
|
4109
5112
|
* @example
|
|
4110
5113
|
* // Check if tree is height-balanced
|
|
4111
5114
|
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
@@ -4180,6 +5183,54 @@ var BST = class extends BinaryTree {
|
|
|
4180
5183
|
|
|
4181
5184
|
|
|
4182
5185
|
|
|
5186
|
+
|
|
5187
|
+
|
|
5188
|
+
|
|
5189
|
+
|
|
5190
|
+
|
|
5191
|
+
|
|
5192
|
+
|
|
5193
|
+
|
|
5194
|
+
|
|
5195
|
+
|
|
5196
|
+
|
|
5197
|
+
|
|
5198
|
+
|
|
5199
|
+
|
|
5200
|
+
|
|
5201
|
+
|
|
5202
|
+
|
|
5203
|
+
|
|
5204
|
+
|
|
5205
|
+
|
|
5206
|
+
|
|
5207
|
+
|
|
5208
|
+
|
|
5209
|
+
|
|
5210
|
+
|
|
5211
|
+
|
|
5212
|
+
|
|
5213
|
+
|
|
5214
|
+
|
|
5215
|
+
|
|
5216
|
+
|
|
5217
|
+
|
|
5218
|
+
|
|
5219
|
+
|
|
5220
|
+
|
|
5221
|
+
|
|
5222
|
+
|
|
5223
|
+
|
|
5224
|
+
|
|
5225
|
+
|
|
5226
|
+
|
|
5227
|
+
|
|
5228
|
+
|
|
5229
|
+
|
|
5230
|
+
|
|
5231
|
+
|
|
5232
|
+
|
|
5233
|
+
|
|
4183
5234
|
* @example
|
|
4184
5235
|
* // Transform to new tree
|
|
4185
5236
|
* const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
@@ -4254,13 +5305,11 @@ var BST = class extends BinaryTree {
|
|
|
4254
5305
|
if (a instanceof Date && b instanceof Date) {
|
|
4255
5306
|
const ta = a.getTime();
|
|
4256
5307
|
const tb = b.getTime();
|
|
4257
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5308
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
4258
5309
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
4259
5310
|
}
|
|
4260
5311
|
if (typeof a === "object" || typeof b === "object") {
|
|
4261
|
-
|
|
4262
|
-
ERR.comparatorRequired("BST")
|
|
4263
|
-
);
|
|
5312
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
4264
5313
|
}
|
|
4265
5314
|
return 0;
|
|
4266
5315
|
};
|
|
@@ -4579,7 +5628,8 @@ var BST = class extends BinaryTree {
|
|
|
4579
5628
|
_snapshotOptions() {
|
|
4580
5629
|
return {
|
|
4581
5630
|
...super._snapshotOptions(),
|
|
4582
|
-
comparator: this._comparator
|
|
5631
|
+
comparator: this._comparator,
|
|
5632
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
4583
5633
|
};
|
|
4584
5634
|
}
|
|
4585
5635
|
/**
|
|
@@ -4601,6 +5651,113 @@ var BST = class extends BinaryTree {
|
|
|
4601
5651
|
*
|
|
4602
5652
|
* @param v - The node to set as root.
|
|
4603
5653
|
*/
|
|
5654
|
+
/**
|
|
5655
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5656
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5657
|
+
*/
|
|
5658
|
+
_updateCount(node) {
|
|
5659
|
+
if (!this._enableOrderStatistic) return;
|
|
5660
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5661
|
+
}
|
|
5662
|
+
/**
|
|
5663
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5664
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5665
|
+
*/
|
|
5666
|
+
_updateCountAlongPath(node) {
|
|
5667
|
+
if (!this._enableOrderStatistic) return;
|
|
5668
|
+
let current = node;
|
|
5669
|
+
while (current) {
|
|
5670
|
+
this._updateCount(current);
|
|
5671
|
+
current = current.parent;
|
|
5672
|
+
}
|
|
5673
|
+
}
|
|
5674
|
+
/**
|
|
5675
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5676
|
+
* @remarks Time O(log n), Space O(1)
|
|
5677
|
+
*/
|
|
5678
|
+
_getByRankIterative(node, k) {
|
|
5679
|
+
let current = node;
|
|
5680
|
+
let remaining = k;
|
|
5681
|
+
while (current) {
|
|
5682
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5683
|
+
if (remaining < leftCount) {
|
|
5684
|
+
current = current.left;
|
|
5685
|
+
} else if (remaining === leftCount) {
|
|
5686
|
+
return current;
|
|
5687
|
+
} else {
|
|
5688
|
+
remaining = remaining - leftCount - 1;
|
|
5689
|
+
current = current.right;
|
|
5690
|
+
}
|
|
5691
|
+
}
|
|
5692
|
+
return void 0;
|
|
5693
|
+
}
|
|
5694
|
+
/**
|
|
5695
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5696
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5697
|
+
*/
|
|
5698
|
+
_getByRankRecursive(node, k) {
|
|
5699
|
+
if (!node) return void 0;
|
|
5700
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5701
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5702
|
+
if (k === leftCount) return node;
|
|
5703
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5704
|
+
}
|
|
5705
|
+
/**
|
|
5706
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5707
|
+
* @remarks Time O(log n), Space O(1)
|
|
5708
|
+
*/
|
|
5709
|
+
_getRankIterative(node, key) {
|
|
5710
|
+
let rank = 0;
|
|
5711
|
+
let current = node;
|
|
5712
|
+
while (this.isRealNode(current)) {
|
|
5713
|
+
const cmp = this._compare(current.key, key);
|
|
5714
|
+
if (cmp > 0) {
|
|
5715
|
+
current = current.left;
|
|
5716
|
+
} else if (cmp < 0) {
|
|
5717
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5718
|
+
current = current.right;
|
|
5719
|
+
} else {
|
|
5720
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5721
|
+
return rank;
|
|
5722
|
+
}
|
|
5723
|
+
}
|
|
5724
|
+
return rank;
|
|
5725
|
+
}
|
|
5726
|
+
/**
|
|
5727
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5728
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5729
|
+
*/
|
|
5730
|
+
_getRankRecursive(node, key) {
|
|
5731
|
+
if (!node) return 0;
|
|
5732
|
+
const cmp = this._compare(node.key, key);
|
|
5733
|
+
if (cmp > 0) {
|
|
5734
|
+
return this._getRankRecursive(node.left, key);
|
|
5735
|
+
} else if (cmp < 0) {
|
|
5736
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5737
|
+
} else {
|
|
5738
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5739
|
+
}
|
|
5740
|
+
}
|
|
5741
|
+
/**
|
|
5742
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5743
|
+
* @remarks Time O(log n), Space O(1)
|
|
5744
|
+
*/
|
|
5745
|
+
_next(node) {
|
|
5746
|
+
if (this.isRealNode(node.right)) {
|
|
5747
|
+
let current2 = node.right;
|
|
5748
|
+
while (this.isRealNode(current2.left)) {
|
|
5749
|
+
current2 = current2.left;
|
|
5750
|
+
}
|
|
5751
|
+
return current2;
|
|
5752
|
+
}
|
|
5753
|
+
let current = node;
|
|
5754
|
+
let parent = current.parent;
|
|
5755
|
+
while (parent && current === parent.right) {
|
|
5756
|
+
current = parent;
|
|
5757
|
+
parent = parent.parent;
|
|
5758
|
+
}
|
|
5759
|
+
return parent;
|
|
5760
|
+
}
|
|
4604
5761
|
_setRoot(v) {
|
|
4605
5762
|
if (v) v.parent = void 0;
|
|
4606
5763
|
this._root = v;
|
|
@@ -4647,21 +5804,28 @@ var BST = class extends BinaryTree {
|
|
|
4647
5804
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
4648
5805
|
return x;
|
|
4649
5806
|
}, "minNode");
|
|
5807
|
+
let countUpdateStart;
|
|
4650
5808
|
if (node.left === void 0) {
|
|
5809
|
+
countUpdateStart = node.parent;
|
|
4651
5810
|
transplant(node, node.right);
|
|
4652
5811
|
} else if (node.right === void 0) {
|
|
5812
|
+
countUpdateStart = node.parent;
|
|
4653
5813
|
transplant(node, node.left);
|
|
4654
5814
|
} else {
|
|
4655
5815
|
const succ = minNode(node.right);
|
|
4656
5816
|
if (succ.parent !== node) {
|
|
5817
|
+
countUpdateStart = succ.parent;
|
|
4657
5818
|
transplant(succ, succ.right);
|
|
4658
5819
|
succ.right = node.right;
|
|
4659
5820
|
if (succ.right) succ.right.parent = succ;
|
|
5821
|
+
} else {
|
|
5822
|
+
countUpdateStart = succ;
|
|
4660
5823
|
}
|
|
4661
5824
|
transplant(node, succ);
|
|
4662
5825
|
succ.left = node.left;
|
|
4663
5826
|
if (succ.left) succ.left.parent = succ;
|
|
4664
5827
|
}
|
|
5828
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
4665
5829
|
this._size = Math.max(0, this._size - 1);
|
|
4666
5830
|
return true;
|
|
4667
5831
|
}
|
|
@@ -4856,6 +6020,102 @@ var AVLTree = class extends BST {
|
|
|
4856
6020
|
|
|
4857
6021
|
|
|
4858
6022
|
|
|
6023
|
+
|
|
6024
|
+
|
|
6025
|
+
|
|
6026
|
+
|
|
6027
|
+
|
|
6028
|
+
|
|
6029
|
+
|
|
6030
|
+
|
|
6031
|
+
|
|
6032
|
+
|
|
6033
|
+
|
|
6034
|
+
|
|
6035
|
+
|
|
6036
|
+
|
|
6037
|
+
|
|
6038
|
+
|
|
6039
|
+
|
|
6040
|
+
|
|
6041
|
+
|
|
6042
|
+
|
|
6043
|
+
|
|
6044
|
+
|
|
6045
|
+
|
|
6046
|
+
|
|
6047
|
+
|
|
6048
|
+
|
|
6049
|
+
|
|
6050
|
+
|
|
6051
|
+
|
|
6052
|
+
|
|
6053
|
+
|
|
6054
|
+
|
|
6055
|
+
|
|
6056
|
+
|
|
6057
|
+
|
|
6058
|
+
|
|
6059
|
+
|
|
6060
|
+
|
|
6061
|
+
|
|
6062
|
+
|
|
6063
|
+
|
|
6064
|
+
|
|
6065
|
+
|
|
6066
|
+
|
|
6067
|
+
|
|
6068
|
+
|
|
6069
|
+
|
|
6070
|
+
|
|
6071
|
+
|
|
6072
|
+
|
|
6073
|
+
|
|
6074
|
+
|
|
6075
|
+
|
|
6076
|
+
|
|
6077
|
+
|
|
6078
|
+
|
|
6079
|
+
|
|
6080
|
+
|
|
6081
|
+
|
|
6082
|
+
|
|
6083
|
+
|
|
6084
|
+
|
|
6085
|
+
|
|
6086
|
+
|
|
6087
|
+
|
|
6088
|
+
|
|
6089
|
+
|
|
6090
|
+
|
|
6091
|
+
|
|
6092
|
+
|
|
6093
|
+
|
|
6094
|
+
|
|
6095
|
+
|
|
6096
|
+
|
|
6097
|
+
|
|
6098
|
+
|
|
6099
|
+
|
|
6100
|
+
|
|
6101
|
+
|
|
6102
|
+
|
|
6103
|
+
|
|
6104
|
+
|
|
6105
|
+
|
|
6106
|
+
|
|
6107
|
+
|
|
6108
|
+
|
|
6109
|
+
|
|
6110
|
+
|
|
6111
|
+
|
|
6112
|
+
|
|
6113
|
+
|
|
6114
|
+
|
|
6115
|
+
|
|
6116
|
+
|
|
6117
|
+
|
|
6118
|
+
|
|
4859
6119
|
|
|
4860
6120
|
|
|
4861
6121
|
|
|
@@ -4908,6 +6168,78 @@ var AVLTree = class extends BST {
|
|
|
4908
6168
|
|
|
4909
6169
|
|
|
4910
6170
|
|
|
6171
|
+
|
|
6172
|
+
|
|
6173
|
+
|
|
6174
|
+
|
|
6175
|
+
|
|
6176
|
+
|
|
6177
|
+
|
|
6178
|
+
|
|
6179
|
+
|
|
6180
|
+
|
|
6181
|
+
|
|
6182
|
+
|
|
6183
|
+
|
|
6184
|
+
|
|
6185
|
+
|
|
6186
|
+
|
|
6187
|
+
|
|
6188
|
+
|
|
6189
|
+
|
|
6190
|
+
|
|
6191
|
+
|
|
6192
|
+
|
|
6193
|
+
|
|
6194
|
+
|
|
6195
|
+
|
|
6196
|
+
|
|
6197
|
+
|
|
6198
|
+
|
|
6199
|
+
|
|
6200
|
+
|
|
6201
|
+
|
|
6202
|
+
|
|
6203
|
+
|
|
6204
|
+
|
|
6205
|
+
|
|
6206
|
+
|
|
6207
|
+
|
|
6208
|
+
|
|
6209
|
+
|
|
6210
|
+
|
|
6211
|
+
|
|
6212
|
+
|
|
6213
|
+
|
|
6214
|
+
|
|
6215
|
+
|
|
6216
|
+
|
|
6217
|
+
|
|
6218
|
+
|
|
6219
|
+
|
|
6220
|
+
|
|
6221
|
+
|
|
6222
|
+
|
|
6223
|
+
|
|
6224
|
+
|
|
6225
|
+
|
|
6226
|
+
|
|
6227
|
+
|
|
6228
|
+
|
|
6229
|
+
|
|
6230
|
+
|
|
6231
|
+
|
|
6232
|
+
|
|
6233
|
+
|
|
6234
|
+
|
|
6235
|
+
|
|
6236
|
+
|
|
6237
|
+
|
|
6238
|
+
|
|
6239
|
+
|
|
6240
|
+
|
|
6241
|
+
|
|
6242
|
+
|
|
4911
6243
|
|
|
4912
6244
|
|
|
4913
6245
|
|
|
@@ -4964,6 +6296,54 @@ var AVLTree = class extends BST {
|
|
|
4964
6296
|
|
|
4965
6297
|
|
|
4966
6298
|
|
|
6299
|
+
|
|
6300
|
+
|
|
6301
|
+
|
|
6302
|
+
|
|
6303
|
+
|
|
6304
|
+
|
|
6305
|
+
|
|
6306
|
+
|
|
6307
|
+
|
|
6308
|
+
|
|
6309
|
+
|
|
6310
|
+
|
|
6311
|
+
|
|
6312
|
+
|
|
6313
|
+
|
|
6314
|
+
|
|
6315
|
+
|
|
6316
|
+
|
|
6317
|
+
|
|
6318
|
+
|
|
6319
|
+
|
|
6320
|
+
|
|
6321
|
+
|
|
6322
|
+
|
|
6323
|
+
|
|
6324
|
+
|
|
6325
|
+
|
|
6326
|
+
|
|
6327
|
+
|
|
6328
|
+
|
|
6329
|
+
|
|
6330
|
+
|
|
6331
|
+
|
|
6332
|
+
|
|
6333
|
+
|
|
6334
|
+
|
|
6335
|
+
|
|
6336
|
+
|
|
6337
|
+
|
|
6338
|
+
|
|
6339
|
+
|
|
6340
|
+
|
|
6341
|
+
|
|
6342
|
+
|
|
6343
|
+
|
|
6344
|
+
|
|
6345
|
+
|
|
6346
|
+
|
|
4967
6347
|
* @example
|
|
4968
6348
|
* // Rebalance the tree
|
|
4969
6349
|
* const avl = new AVLTree<number>();
|
|
@@ -5026,6 +6406,78 @@ var AVLTree = class extends BST {
|
|
|
5026
6406
|
|
|
5027
6407
|
|
|
5028
6408
|
|
|
6409
|
+
|
|
6410
|
+
|
|
6411
|
+
|
|
6412
|
+
|
|
6413
|
+
|
|
6414
|
+
|
|
6415
|
+
|
|
6416
|
+
|
|
6417
|
+
|
|
6418
|
+
|
|
6419
|
+
|
|
6420
|
+
|
|
6421
|
+
|
|
6422
|
+
|
|
6423
|
+
|
|
6424
|
+
|
|
6425
|
+
|
|
6426
|
+
|
|
6427
|
+
|
|
6428
|
+
|
|
6429
|
+
|
|
6430
|
+
|
|
6431
|
+
|
|
6432
|
+
|
|
6433
|
+
|
|
6434
|
+
|
|
6435
|
+
|
|
6436
|
+
|
|
6437
|
+
|
|
6438
|
+
|
|
6439
|
+
|
|
6440
|
+
|
|
6441
|
+
|
|
6442
|
+
|
|
6443
|
+
|
|
6444
|
+
|
|
6445
|
+
|
|
6446
|
+
|
|
6447
|
+
|
|
6448
|
+
|
|
6449
|
+
|
|
6450
|
+
|
|
6451
|
+
|
|
6452
|
+
|
|
6453
|
+
|
|
6454
|
+
|
|
6455
|
+
|
|
6456
|
+
|
|
6457
|
+
|
|
6458
|
+
|
|
6459
|
+
|
|
6460
|
+
|
|
6461
|
+
|
|
6462
|
+
|
|
6463
|
+
|
|
6464
|
+
|
|
6465
|
+
|
|
6466
|
+
|
|
6467
|
+
|
|
6468
|
+
|
|
6469
|
+
|
|
6470
|
+
|
|
6471
|
+
|
|
6472
|
+
|
|
6473
|
+
|
|
6474
|
+
|
|
6475
|
+
|
|
6476
|
+
|
|
6477
|
+
|
|
6478
|
+
|
|
6479
|
+
|
|
6480
|
+
|
|
5029
6481
|
|
|
5030
6482
|
|
|
5031
6483
|
* @example
|
|
@@ -5146,6 +6598,8 @@ var AVLTree = class extends BST {
|
|
|
5146
6598
|
}
|
|
5147
6599
|
this._updateHeight(A);
|
|
5148
6600
|
if (B) this._updateHeight(B);
|
|
6601
|
+
this._updateCount(A);
|
|
6602
|
+
if (B) this._updateCount(B);
|
|
5149
6603
|
}
|
|
5150
6604
|
/**
|
|
5151
6605
|
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
@@ -5191,6 +6645,9 @@ var AVLTree = class extends BST {
|
|
|
5191
6645
|
this._updateHeight(A);
|
|
5192
6646
|
if (B) this._updateHeight(B);
|
|
5193
6647
|
if (C) this._updateHeight(C);
|
|
6648
|
+
this._updateCount(A);
|
|
6649
|
+
if (B) this._updateCount(B);
|
|
6650
|
+
if (C) this._updateCount(C);
|
|
5194
6651
|
}
|
|
5195
6652
|
/**
|
|
5196
6653
|
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
@@ -5225,6 +6682,8 @@ var AVLTree = class extends BST {
|
|
|
5225
6682
|
}
|
|
5226
6683
|
this._updateHeight(A);
|
|
5227
6684
|
if (B) this._updateHeight(B);
|
|
6685
|
+
this._updateCount(A);
|
|
6686
|
+
if (B) this._updateCount(B);
|
|
5228
6687
|
}
|
|
5229
6688
|
/**
|
|
5230
6689
|
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
@@ -5268,6 +6727,9 @@ var AVLTree = class extends BST {
|
|
|
5268
6727
|
this._updateHeight(A);
|
|
5269
6728
|
if (B) this._updateHeight(B);
|
|
5270
6729
|
if (C) this._updateHeight(C);
|
|
6730
|
+
this._updateCount(A);
|
|
6731
|
+
if (B) this._updateCount(B);
|
|
6732
|
+
if (C) this._updateCount(C);
|
|
5271
6733
|
}
|
|
5272
6734
|
/**
|
|
5273
6735
|
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
@@ -5282,6 +6744,7 @@ var AVLTree = class extends BST {
|
|
|
5282
6744
|
const A = path[i];
|
|
5283
6745
|
if (A) {
|
|
5284
6746
|
this._updateHeight(A);
|
|
6747
|
+
this._updateCount(A);
|
|
5285
6748
|
switch (this._balanceFactor(A)) {
|
|
5286
6749
|
case -2:
|
|
5287
6750
|
if (A && A.left) {
|