bst-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 +1222 -57
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1222 -56
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1222 -58
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1222 -57
- 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/bst-typed.js +1220 -55
- package/dist/umd/bst-typed.js.map +1 -1
- package/dist/umd/bst-typed.min.js +3 -3
- package/dist/umd/bst-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/umd/bst-typed.js
CHANGED
|
@@ -29,7 +29,8 @@ var bstTyped = (() => {
|
|
|
29
29
|
BinaryTreeNode: () => BinaryTreeNode,
|
|
30
30
|
DFSOperation: () => DFSOperation,
|
|
31
31
|
ERR: () => ERR,
|
|
32
|
-
Range: () => Range
|
|
32
|
+
Range: () => Range,
|
|
33
|
+
raise: () => raise
|
|
33
34
|
});
|
|
34
35
|
|
|
35
36
|
// src/utils/utils.ts
|
|
@@ -83,6 +84,57 @@ var bstTyped = (() => {
|
|
|
83
84
|
return (...args) => trampoline(fn(...args));
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
// src/common/error.ts
|
|
88
|
+
function raise(ErrorClass, message) {
|
|
89
|
+
throw new ErrorClass(message);
|
|
90
|
+
}
|
|
91
|
+
var ERR = {
|
|
92
|
+
// Range / index
|
|
93
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
94
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
95
|
+
// Type / argument
|
|
96
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
97
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
98
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
99
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
100
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
101
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
102
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
103
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
104
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
105
|
+
// State / operation
|
|
106
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
107
|
+
// Matrix
|
|
108
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
109
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
110
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
111
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
112
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`,
|
|
113
|
+
// Order statistic
|
|
114
|
+
orderStatisticNotEnabled: (method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/common/index.ts
|
|
118
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
119
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
120
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
121
|
+
return DFSOperation2;
|
|
122
|
+
})(DFSOperation || {});
|
|
123
|
+
var Range = class {
|
|
124
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
125
|
+
this.low = low;
|
|
126
|
+
this.high = high;
|
|
127
|
+
this.includeLow = includeLow;
|
|
128
|
+
this.includeHigh = includeHigh;
|
|
129
|
+
}
|
|
130
|
+
// Determine whether a key is within the range
|
|
131
|
+
isInRange(key, comparator) {
|
|
132
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
133
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
134
|
+
return lowCheck && highCheck;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
86
138
|
// src/data-structures/base/iterable-element-base.ts
|
|
87
139
|
var IterableElementBase = class {
|
|
88
140
|
/**
|
|
@@ -105,7 +157,7 @@ var bstTyped = (() => {
|
|
|
105
157
|
if (options) {
|
|
106
158
|
const { toElementFn } = options;
|
|
107
159
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
108
|
-
else if (toElementFn)
|
|
160
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
109
161
|
}
|
|
110
162
|
}
|
|
111
163
|
/**
|
|
@@ -261,7 +313,7 @@ var bstTyped = (() => {
|
|
|
261
313
|
acc = initialValue;
|
|
262
314
|
} else {
|
|
263
315
|
const first = iter.next();
|
|
264
|
-
if (first.done)
|
|
316
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
265
317
|
acc = first.value;
|
|
266
318
|
index = 1;
|
|
267
319
|
}
|
|
@@ -493,52 +545,6 @@ var bstTyped = (() => {
|
|
|
493
545
|
}
|
|
494
546
|
};
|
|
495
547
|
|
|
496
|
-
// src/common/error.ts
|
|
497
|
-
var ERR = {
|
|
498
|
-
// Range / index
|
|
499
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
500
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
501
|
-
// Type / argument
|
|
502
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
503
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
504
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
505
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
506
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
507
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
508
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
509
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
510
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
511
|
-
// State / operation
|
|
512
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
513
|
-
// Matrix
|
|
514
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
515
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
516
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
517
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
518
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
// src/common/index.ts
|
|
522
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
523
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
524
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
525
|
-
return DFSOperation2;
|
|
526
|
-
})(DFSOperation || {});
|
|
527
|
-
var Range = class {
|
|
528
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
529
|
-
this.low = low;
|
|
530
|
-
this.high = high;
|
|
531
|
-
this.includeLow = includeLow;
|
|
532
|
-
this.includeHigh = includeHigh;
|
|
533
|
-
}
|
|
534
|
-
// Determine whether a key is within the range
|
|
535
|
-
isInRange(key, comparator) {
|
|
536
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
537
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
538
|
-
return lowCheck && highCheck;
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
|
|
542
548
|
// src/data-structures/base/iterable-entry-base.ts
|
|
543
549
|
var IterableEntryBase = class {
|
|
544
550
|
/**
|
|
@@ -786,6 +792,30 @@ var bstTyped = (() => {
|
|
|
786
792
|
|
|
787
793
|
|
|
788
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
|
+
|
|
789
819
|
* @example
|
|
790
820
|
* // Track queue length
|
|
791
821
|
* const q = new Queue<number>();
|
|
@@ -812,6 +842,30 @@ var bstTyped = (() => {
|
|
|
812
842
|
|
|
813
843
|
|
|
814
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
|
+
|
|
815
869
|
* @example
|
|
816
870
|
* // View the front element
|
|
817
871
|
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
@@ -854,6 +908,30 @@ var bstTyped = (() => {
|
|
|
854
908
|
|
|
855
909
|
|
|
856
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
|
+
|
|
857
935
|
* @example
|
|
858
936
|
* // Queue for...of iteration and isEmpty check
|
|
859
937
|
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
@@ -892,6 +970,30 @@ var bstTyped = (() => {
|
|
|
892
970
|
|
|
893
971
|
|
|
894
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
|
+
|
|
895
997
|
* @example
|
|
896
998
|
* // basic Queue creation and push operation
|
|
897
999
|
* // Create a simple Queue with initial values
|
|
@@ -937,6 +1039,30 @@ var bstTyped = (() => {
|
|
|
937
1039
|
|
|
938
1040
|
|
|
939
1041
|
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
940
1066
|
* @example
|
|
941
1067
|
* // Queue shift and peek operations
|
|
942
1068
|
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
@@ -972,6 +1098,30 @@ var bstTyped = (() => {
|
|
|
972
1098
|
|
|
973
1099
|
|
|
974
1100
|
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
975
1125
|
* @example
|
|
976
1126
|
* // Remove specific element
|
|
977
1127
|
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
@@ -1000,6 +1150,30 @@ var bstTyped = (() => {
|
|
|
1000
1150
|
|
|
1001
1151
|
|
|
1002
1152
|
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1003
1177
|
* @example
|
|
1004
1178
|
* // Access element by index
|
|
1005
1179
|
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
@@ -1069,6 +1243,30 @@ var bstTyped = (() => {
|
|
|
1069
1243
|
|
|
1070
1244
|
|
|
1071
1245
|
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1072
1270
|
* @example
|
|
1073
1271
|
* // Remove all elements
|
|
1074
1272
|
* const q = new Queue<number>([1, 2, 3]);
|
|
@@ -1091,6 +1289,30 @@ var bstTyped = (() => {
|
|
|
1091
1289
|
|
|
1092
1290
|
|
|
1093
1291
|
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1094
1316
|
* @example
|
|
1095
1317
|
* // Reclaim unused memory
|
|
1096
1318
|
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
@@ -1136,6 +1358,30 @@ var bstTyped = (() => {
|
|
|
1136
1358
|
|
|
1137
1359
|
|
|
1138
1360
|
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1139
1385
|
* @example
|
|
1140
1386
|
* // Create independent copy
|
|
1141
1387
|
* const q = new Queue<number>([1, 2, 3]);
|
|
@@ -1165,6 +1411,30 @@ var bstTyped = (() => {
|
|
|
1165
1411
|
|
|
1166
1412
|
|
|
1167
1413
|
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1168
1438
|
* @example
|
|
1169
1439
|
* // Filter elements
|
|
1170
1440
|
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
@@ -1198,6 +1468,30 @@ var bstTyped = (() => {
|
|
|
1198
1468
|
|
|
1199
1469
|
|
|
1200
1470
|
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1476
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1201
1495
|
* @example
|
|
1202
1496
|
* // Transform elements
|
|
1203
1497
|
* const q = new Queue<number>([1, 2, 3]);
|
|
@@ -1462,7 +1756,7 @@ var bstTyped = (() => {
|
|
|
1462
1756
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1463
1757
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1464
1758
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1465
|
-
else if (toEntryFn)
|
|
1759
|
+
else if (toEntryFn) raise(TypeError, ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1466
1760
|
}
|
|
1467
1761
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1468
1762
|
}
|
|
@@ -1678,6 +1972,30 @@ var bstTyped = (() => {
|
|
|
1678
1972
|
|
|
1679
1973
|
|
|
1680
1974
|
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1681
1999
|
* @example
|
|
1682
2000
|
* // Add a single node
|
|
1683
2001
|
* const tree = new BinaryTree<number>();
|
|
@@ -1708,6 +2026,30 @@ var bstTyped = (() => {
|
|
|
1708
2026
|
|
|
1709
2027
|
|
|
1710
2028
|
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
|
|
2034
|
+
|
|
2035
|
+
|
|
2036
|
+
|
|
2037
|
+
|
|
2038
|
+
|
|
2039
|
+
|
|
2040
|
+
|
|
2041
|
+
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
|
|
2052
|
+
|
|
1711
2053
|
* @example
|
|
1712
2054
|
* // basic BinaryTree creation and insertion
|
|
1713
2055
|
* // Create a BinaryTree with entries
|
|
@@ -1790,6 +2132,30 @@ var bstTyped = (() => {
|
|
|
1790
2132
|
|
|
1791
2133
|
|
|
1792
2134
|
|
|
2135
|
+
|
|
2136
|
+
|
|
2137
|
+
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
|
|
2141
|
+
|
|
2142
|
+
|
|
2143
|
+
|
|
2144
|
+
|
|
2145
|
+
|
|
2146
|
+
|
|
2147
|
+
|
|
2148
|
+
|
|
2149
|
+
|
|
2150
|
+
|
|
2151
|
+
|
|
2152
|
+
|
|
2153
|
+
|
|
2154
|
+
|
|
2155
|
+
|
|
2156
|
+
|
|
2157
|
+
|
|
2158
|
+
|
|
1793
2159
|
* @example
|
|
1794
2160
|
* // Bulk add
|
|
1795
2161
|
* const tree = new BinaryTree<number>();
|
|
@@ -1808,6 +2174,30 @@ var bstTyped = (() => {
|
|
|
1808
2174
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1809
2175
|
|
|
1810
2176
|
|
|
2177
|
+
|
|
2178
|
+
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
|
|
2192
|
+
|
|
2193
|
+
|
|
2194
|
+
|
|
2195
|
+
|
|
2196
|
+
|
|
2197
|
+
|
|
2198
|
+
|
|
2199
|
+
|
|
2200
|
+
|
|
1811
2201
|
* @example
|
|
1812
2202
|
* // Set multiple entries
|
|
1813
2203
|
* const tree = new BinaryTree<number, string>();
|
|
@@ -1847,6 +2237,30 @@ var bstTyped = (() => {
|
|
|
1847
2237
|
|
|
1848
2238
|
|
|
1849
2239
|
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2252
|
+
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
|
|
2257
|
+
|
|
2258
|
+
|
|
2259
|
+
|
|
2260
|
+
|
|
2261
|
+
|
|
2262
|
+
|
|
2263
|
+
|
|
1850
2264
|
* @example
|
|
1851
2265
|
* // Combine trees
|
|
1852
2266
|
* const t1 = new BinaryTree<number>([1, 2]);
|
|
@@ -1885,6 +2299,30 @@ var bstTyped = (() => {
|
|
|
1885
2299
|
|
|
1886
2300
|
|
|
1887
2301
|
|
|
2302
|
+
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
|
|
2311
|
+
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
|
|
2325
|
+
|
|
1888
2326
|
* @example
|
|
1889
2327
|
* // Remove a node
|
|
1890
2328
|
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
@@ -2001,6 +2439,30 @@ var bstTyped = (() => {
|
|
|
2001
2439
|
|
|
2002
2440
|
|
|
2003
2441
|
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2449
|
+
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
|
|
2462
|
+
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
|
|
2004
2466
|
* @example
|
|
2005
2467
|
* // Get node by key
|
|
2006
2468
|
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
@@ -2035,6 +2497,30 @@ var bstTyped = (() => {
|
|
|
2035
2497
|
|
|
2036
2498
|
|
|
2037
2499
|
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
|
|
2506
|
+
|
|
2507
|
+
|
|
2508
|
+
|
|
2509
|
+
|
|
2510
|
+
|
|
2511
|
+
|
|
2512
|
+
|
|
2513
|
+
|
|
2514
|
+
|
|
2515
|
+
|
|
2516
|
+
|
|
2517
|
+
|
|
2518
|
+
|
|
2519
|
+
|
|
2520
|
+
|
|
2521
|
+
|
|
2522
|
+
|
|
2523
|
+
|
|
2038
2524
|
* @example
|
|
2039
2525
|
* // Retrieve value by key
|
|
2040
2526
|
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
@@ -2072,6 +2558,30 @@ var bstTyped = (() => {
|
|
|
2072
2558
|
|
|
2073
2559
|
|
|
2074
2560
|
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
|
|
2564
|
+
|
|
2565
|
+
|
|
2566
|
+
|
|
2567
|
+
|
|
2568
|
+
|
|
2569
|
+
|
|
2570
|
+
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
|
|
2578
|
+
|
|
2579
|
+
|
|
2580
|
+
|
|
2581
|
+
|
|
2582
|
+
|
|
2583
|
+
|
|
2584
|
+
|
|
2075
2585
|
* @example
|
|
2076
2586
|
* // Remove all nodes
|
|
2077
2587
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2096,6 +2606,30 @@ var bstTyped = (() => {
|
|
|
2096
2606
|
|
|
2097
2607
|
|
|
2098
2608
|
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2617
|
+
|
|
2618
|
+
|
|
2619
|
+
|
|
2620
|
+
|
|
2621
|
+
|
|
2622
|
+
|
|
2623
|
+
|
|
2624
|
+
|
|
2625
|
+
|
|
2626
|
+
|
|
2627
|
+
|
|
2628
|
+
|
|
2629
|
+
|
|
2630
|
+
|
|
2631
|
+
|
|
2632
|
+
|
|
2099
2633
|
* @example
|
|
2100
2634
|
* // Check empty
|
|
2101
2635
|
* console.log(new BinaryTree().isEmpty()); // true;
|
|
@@ -2129,6 +2663,30 @@ var bstTyped = (() => {
|
|
|
2129
2663
|
|
|
2130
2664
|
|
|
2131
2665
|
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
|
|
2683
|
+
|
|
2684
|
+
|
|
2685
|
+
|
|
2686
|
+
|
|
2687
|
+
|
|
2688
|
+
|
|
2689
|
+
|
|
2132
2690
|
* @example
|
|
2133
2691
|
* // Check BST property
|
|
2134
2692
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2189,6 +2747,30 @@ var bstTyped = (() => {
|
|
|
2189
2747
|
|
|
2190
2748
|
|
|
2191
2749
|
|
|
2750
|
+
|
|
2751
|
+
|
|
2752
|
+
|
|
2753
|
+
|
|
2754
|
+
|
|
2755
|
+
|
|
2756
|
+
|
|
2757
|
+
|
|
2758
|
+
|
|
2759
|
+
|
|
2760
|
+
|
|
2761
|
+
|
|
2762
|
+
|
|
2763
|
+
|
|
2764
|
+
|
|
2765
|
+
|
|
2766
|
+
|
|
2767
|
+
|
|
2768
|
+
|
|
2769
|
+
|
|
2770
|
+
|
|
2771
|
+
|
|
2772
|
+
|
|
2773
|
+
|
|
2192
2774
|
* @example
|
|
2193
2775
|
* // Get depth of a node
|
|
2194
2776
|
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
@@ -2226,6 +2808,30 @@ var bstTyped = (() => {
|
|
|
2226
2808
|
|
|
2227
2809
|
|
|
2228
2810
|
|
|
2811
|
+
|
|
2812
|
+
|
|
2813
|
+
|
|
2814
|
+
|
|
2815
|
+
|
|
2816
|
+
|
|
2817
|
+
|
|
2818
|
+
|
|
2819
|
+
|
|
2820
|
+
|
|
2821
|
+
|
|
2822
|
+
|
|
2823
|
+
|
|
2824
|
+
|
|
2825
|
+
|
|
2826
|
+
|
|
2827
|
+
|
|
2828
|
+
|
|
2829
|
+
|
|
2830
|
+
|
|
2831
|
+
|
|
2832
|
+
|
|
2833
|
+
|
|
2834
|
+
|
|
2229
2835
|
* @example
|
|
2230
2836
|
* // Get tree height
|
|
2231
2837
|
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
@@ -2679,6 +3285,30 @@ var bstTyped = (() => {
|
|
|
2679
3285
|
|
|
2680
3286
|
|
|
2681
3287
|
|
|
3288
|
+
|
|
3289
|
+
|
|
3290
|
+
|
|
3291
|
+
|
|
3292
|
+
|
|
3293
|
+
|
|
3294
|
+
|
|
3295
|
+
|
|
3296
|
+
|
|
3297
|
+
|
|
3298
|
+
|
|
3299
|
+
|
|
3300
|
+
|
|
3301
|
+
|
|
3302
|
+
|
|
3303
|
+
|
|
3304
|
+
|
|
3305
|
+
|
|
3306
|
+
|
|
3307
|
+
|
|
3308
|
+
|
|
3309
|
+
|
|
3310
|
+
|
|
3311
|
+
|
|
2682
3312
|
* @example
|
|
2683
3313
|
* // Deep copy
|
|
2684
3314
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -2707,6 +3337,30 @@ var bstTyped = (() => {
|
|
|
2707
3337
|
|
|
2708
3338
|
|
|
2709
3339
|
|
|
3340
|
+
|
|
3341
|
+
|
|
3342
|
+
|
|
3343
|
+
|
|
3344
|
+
|
|
3345
|
+
|
|
3346
|
+
|
|
3347
|
+
|
|
3348
|
+
|
|
3349
|
+
|
|
3350
|
+
|
|
3351
|
+
|
|
3352
|
+
|
|
3353
|
+
|
|
3354
|
+
|
|
3355
|
+
|
|
3356
|
+
|
|
3357
|
+
|
|
3358
|
+
|
|
3359
|
+
|
|
3360
|
+
|
|
3361
|
+
|
|
3362
|
+
|
|
3363
|
+
|
|
2710
3364
|
* @example
|
|
2711
3365
|
* // Filter nodes by condition
|
|
2712
3366
|
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
@@ -2739,6 +3393,30 @@ var bstTyped = (() => {
|
|
|
2739
3393
|
|
|
2740
3394
|
|
|
2741
3395
|
|
|
3396
|
+
|
|
3397
|
+
|
|
3398
|
+
|
|
3399
|
+
|
|
3400
|
+
|
|
3401
|
+
|
|
3402
|
+
|
|
3403
|
+
|
|
3404
|
+
|
|
3405
|
+
|
|
3406
|
+
|
|
3407
|
+
|
|
3408
|
+
|
|
3409
|
+
|
|
3410
|
+
|
|
3411
|
+
|
|
3412
|
+
|
|
3413
|
+
|
|
3414
|
+
|
|
3415
|
+
|
|
3416
|
+
|
|
3417
|
+
|
|
3418
|
+
|
|
3419
|
+
|
|
2742
3420
|
* @example
|
|
2743
3421
|
* // Transform to new tree
|
|
2744
3422
|
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
@@ -2796,6 +3474,30 @@ var bstTyped = (() => {
|
|
|
2796
3474
|
|
|
2797
3475
|
|
|
2798
3476
|
|
|
3477
|
+
|
|
3478
|
+
|
|
3479
|
+
|
|
3480
|
+
|
|
3481
|
+
|
|
3482
|
+
|
|
3483
|
+
|
|
3484
|
+
|
|
3485
|
+
|
|
3486
|
+
|
|
3487
|
+
|
|
3488
|
+
|
|
3489
|
+
|
|
3490
|
+
|
|
3491
|
+
|
|
3492
|
+
|
|
3493
|
+
|
|
3494
|
+
|
|
3495
|
+
|
|
3496
|
+
|
|
3497
|
+
|
|
3498
|
+
|
|
3499
|
+
|
|
3500
|
+
|
|
2799
3501
|
* @example
|
|
2800
3502
|
* // Display tree
|
|
2801
3503
|
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
@@ -3410,6 +4112,7 @@ var bstTyped = (() => {
|
|
|
3410
4112
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
3411
4113
|
super([], options);
|
|
3412
4114
|
__publicField(this, "_root");
|
|
4115
|
+
__publicField(this, "_enableOrderStatistic", false);
|
|
3413
4116
|
/**
|
|
3414
4117
|
* The comparator function used to determine the order of keys in the tree.
|
|
3415
4118
|
|
|
@@ -3422,6 +4125,9 @@ var bstTyped = (() => {
|
|
|
3422
4125
|
} else {
|
|
3423
4126
|
this._comparator = this._createDefaultComparator();
|
|
3424
4127
|
}
|
|
4128
|
+
if (options.enableOrderStatistic) {
|
|
4129
|
+
this._enableOrderStatistic = true;
|
|
4130
|
+
}
|
|
3425
4131
|
} else {
|
|
3426
4132
|
this._comparator = this._createDefaultComparator();
|
|
3427
4133
|
}
|
|
@@ -3556,6 +4262,54 @@ var bstTyped = (() => {
|
|
|
3556
4262
|
|
|
3557
4263
|
|
|
3558
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
|
+
|
|
4305
|
+
|
|
4306
|
+
|
|
4307
|
+
|
|
4308
|
+
|
|
4309
|
+
|
|
4310
|
+
|
|
4311
|
+
|
|
4312
|
+
|
|
3559
4313
|
* @example
|
|
3560
4314
|
* // Get node object by key
|
|
3561
4315
|
* const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
|
|
@@ -3711,6 +4465,85 @@ var bstTyped = (() => {
|
|
|
3711
4465
|
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
3712
4466
|
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
3713
4467
|
}
|
|
4468
|
+
getByRank(k, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4469
|
+
if (!this._enableOrderStatistic) {
|
|
4470
|
+
raise(Error, ERR.orderStatisticNotEnabled("getByRank"));
|
|
4471
|
+
}
|
|
4472
|
+
if (k < 0 || k >= this._size) return void 0;
|
|
4473
|
+
let actualCallback = void 0;
|
|
4474
|
+
let actualIterationType = this.iterationType;
|
|
4475
|
+
if (typeof callback === "string") {
|
|
4476
|
+
actualIterationType = callback;
|
|
4477
|
+
} else if (callback) {
|
|
4478
|
+
actualCallback = callback;
|
|
4479
|
+
if (iterationType) {
|
|
4480
|
+
actualIterationType = iterationType;
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
const node = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, k) : this._getByRankIterative(this._root, k);
|
|
4484
|
+
if (!node) return void 0;
|
|
4485
|
+
return actualCallback ? actualCallback(node) : node.key;
|
|
4486
|
+
}
|
|
4487
|
+
getRank(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
4488
|
+
var _a;
|
|
4489
|
+
if (!this._enableOrderStatistic) {
|
|
4490
|
+
raise(Error, ERR.orderStatisticNotEnabled("getRank"));
|
|
4491
|
+
}
|
|
4492
|
+
if (!this._root || this._size === 0) return -1;
|
|
4493
|
+
let actualIterationType = this.iterationType;
|
|
4494
|
+
if (iterationType) actualIterationType = iterationType;
|
|
4495
|
+
let key;
|
|
4496
|
+
if (typeof keyNodeEntryOrPredicate === "function") {
|
|
4497
|
+
const results = this.search(keyNodeEntryOrPredicate, true);
|
|
4498
|
+
if (results.length === 0 || results[0] === void 0) return -1;
|
|
4499
|
+
key = results[0];
|
|
4500
|
+
} else if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
4501
|
+
return -1;
|
|
4502
|
+
} else if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
4503
|
+
key = keyNodeEntryOrPredicate.key;
|
|
4504
|
+
} else if (Array.isArray(keyNodeEntryOrPredicate)) {
|
|
4505
|
+
key = (_a = keyNodeEntryOrPredicate[0]) != null ? _a : void 0;
|
|
4506
|
+
if (key === void 0 || key === null) return -1;
|
|
4507
|
+
} else {
|
|
4508
|
+
key = keyNodeEntryOrPredicate;
|
|
4509
|
+
}
|
|
4510
|
+
if (key === void 0) return -1;
|
|
4511
|
+
return actualIterationType === "RECURSIVE" ? this._getRankRecursive(this._root, key) : this._getRankIterative(this._root, key);
|
|
4512
|
+
}
|
|
4513
|
+
rangeByRank(start, end, callback = this._DEFAULT_NODE_CALLBACK, iterationType = this.iterationType) {
|
|
4514
|
+
if (!this._enableOrderStatistic) {
|
|
4515
|
+
raise(Error, ERR.orderStatisticNotEnabled("rangeByRank"));
|
|
4516
|
+
}
|
|
4517
|
+
if (this._size === 0) return [];
|
|
4518
|
+
const lo = Math.max(0, start);
|
|
4519
|
+
const hi = Math.min(this._size - 1, end);
|
|
4520
|
+
if (lo > hi) return [];
|
|
4521
|
+
let actualCallback = void 0;
|
|
4522
|
+
let actualIterationType = this.iterationType;
|
|
4523
|
+
if (typeof callback === "string") {
|
|
4524
|
+
actualIterationType = callback;
|
|
4525
|
+
} else if (callback) {
|
|
4526
|
+
actualCallback = callback;
|
|
4527
|
+
if (iterationType) {
|
|
4528
|
+
actualIterationType = iterationType;
|
|
4529
|
+
}
|
|
4530
|
+
}
|
|
4531
|
+
const results = [];
|
|
4532
|
+
const count = hi - lo + 1;
|
|
4533
|
+
const startNode = actualIterationType === "RECURSIVE" ? this._getByRankRecursive(this._root, lo) : this._getByRankIterative(this._root, lo);
|
|
4534
|
+
if (!startNode) return [];
|
|
4535
|
+
let collected = 0;
|
|
4536
|
+
const cb = actualCallback != null ? actualCallback : this._DEFAULT_NODE_CALLBACK;
|
|
4537
|
+
let current = startNode;
|
|
4538
|
+
while (current && collected < count) {
|
|
4539
|
+
results.push(cb(current));
|
|
4540
|
+
collected++;
|
|
4541
|
+
if (collected < count) {
|
|
4542
|
+
current = this._next(current);
|
|
4543
|
+
}
|
|
4544
|
+
}
|
|
4545
|
+
return results;
|
|
4546
|
+
}
|
|
3714
4547
|
/**
|
|
3715
4548
|
* Adds a new node to the BST based on key comparison.
|
|
3716
4549
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -3738,6 +4571,78 @@ var bstTyped = (() => {
|
|
|
3738
4571
|
|
|
3739
4572
|
|
|
3740
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
|
+
|
|
4636
|
+
|
|
4637
|
+
|
|
4638
|
+
|
|
4639
|
+
|
|
4640
|
+
|
|
4641
|
+
|
|
4642
|
+
|
|
4643
|
+
|
|
4644
|
+
|
|
4645
|
+
|
|
3741
4646
|
|
|
3742
4647
|
|
|
3743
4648
|
|
|
@@ -3757,6 +4662,7 @@ var bstTyped = (() => {
|
|
|
3757
4662
|
this._setRoot(newNode);
|
|
3758
4663
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3759
4664
|
this._size++;
|
|
4665
|
+
this._updateCount(newNode);
|
|
3760
4666
|
return true;
|
|
3761
4667
|
}
|
|
3762
4668
|
let current = this._root;
|
|
@@ -3770,6 +4676,7 @@ var bstTyped = (() => {
|
|
|
3770
4676
|
current.left = newNode;
|
|
3771
4677
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3772
4678
|
this._size++;
|
|
4679
|
+
this._updateCountAlongPath(newNode);
|
|
3773
4680
|
return true;
|
|
3774
4681
|
}
|
|
3775
4682
|
if (current.left !== null) current = current.left;
|
|
@@ -3778,6 +4685,7 @@ var bstTyped = (() => {
|
|
|
3778
4685
|
current.right = newNode;
|
|
3779
4686
|
if (this._isMapMode && this.isRealNode(newNode)) this._store.set(newNode.key, newNode);
|
|
3780
4687
|
this._size++;
|
|
4688
|
+
this._updateCountAlongPath(newNode);
|
|
3781
4689
|
return true;
|
|
3782
4690
|
}
|
|
3783
4691
|
if (current.right !== null) current = current.right;
|
|
@@ -3807,6 +4715,54 @@ var bstTyped = (() => {
|
|
|
3807
4715
|
|
|
3808
4716
|
|
|
3809
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
|
+
|
|
4756
|
+
|
|
4757
|
+
|
|
4758
|
+
|
|
4759
|
+
|
|
4760
|
+
|
|
4761
|
+
|
|
4762
|
+
|
|
4763
|
+
|
|
4764
|
+
|
|
4765
|
+
|
|
3810
4766
|
* @example
|
|
3811
4767
|
* // Set multiple key-value pairs
|
|
3812
4768
|
* const bst = new BST<number, string>();
|
|
@@ -4070,6 +5026,30 @@ var bstTyped = (() => {
|
|
|
4070
5026
|
|
|
4071
5027
|
|
|
4072
5028
|
|
|
5029
|
+
|
|
5030
|
+
|
|
5031
|
+
|
|
5032
|
+
|
|
5033
|
+
|
|
5034
|
+
|
|
5035
|
+
|
|
5036
|
+
|
|
5037
|
+
|
|
5038
|
+
|
|
5039
|
+
|
|
5040
|
+
|
|
5041
|
+
|
|
5042
|
+
|
|
5043
|
+
|
|
5044
|
+
|
|
5045
|
+
|
|
5046
|
+
|
|
5047
|
+
|
|
5048
|
+
|
|
5049
|
+
|
|
5050
|
+
|
|
5051
|
+
|
|
5052
|
+
|
|
4073
5053
|
* @example
|
|
4074
5054
|
* // Rebalance the tree
|
|
4075
5055
|
* const bst = new BST<number>();
|
|
@@ -4115,6 +5095,30 @@ var bstTyped = (() => {
|
|
|
4115
5095
|
|
|
4116
5096
|
|
|
4117
5097
|
|
|
5098
|
+
|
|
5099
|
+
|
|
5100
|
+
|
|
5101
|
+
|
|
5102
|
+
|
|
5103
|
+
|
|
5104
|
+
|
|
5105
|
+
|
|
5106
|
+
|
|
5107
|
+
|
|
5108
|
+
|
|
5109
|
+
|
|
5110
|
+
|
|
5111
|
+
|
|
5112
|
+
|
|
5113
|
+
|
|
5114
|
+
|
|
5115
|
+
|
|
5116
|
+
|
|
5117
|
+
|
|
5118
|
+
|
|
5119
|
+
|
|
5120
|
+
|
|
5121
|
+
|
|
4118
5122
|
* @example
|
|
4119
5123
|
* // Check if tree is height-balanced
|
|
4120
5124
|
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
@@ -4189,6 +5193,54 @@ var bstTyped = (() => {
|
|
|
4189
5193
|
|
|
4190
5194
|
|
|
4191
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
|
+
|
|
5234
|
+
|
|
5235
|
+
|
|
5236
|
+
|
|
5237
|
+
|
|
5238
|
+
|
|
5239
|
+
|
|
5240
|
+
|
|
5241
|
+
|
|
5242
|
+
|
|
5243
|
+
|
|
4192
5244
|
* @example
|
|
4193
5245
|
* // Transform to new tree
|
|
4194
5246
|
* const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
@@ -4263,13 +5315,11 @@ var bstTyped = (() => {
|
|
|
4263
5315
|
if (a instanceof Date && b instanceof Date) {
|
|
4264
5316
|
const ta = a.getTime();
|
|
4265
5317
|
const tb = b.getTime();
|
|
4266
|
-
if (Number.isNaN(ta) || Number.isNaN(tb))
|
|
5318
|
+
if (Number.isNaN(ta) || Number.isNaN(tb)) raise(TypeError, ERR.invalidDate("BST"));
|
|
4267
5319
|
return ta > tb ? 1 : ta < tb ? -1 : 0;
|
|
4268
5320
|
}
|
|
4269
5321
|
if (typeof a === "object" || typeof b === "object") {
|
|
4270
|
-
|
|
4271
|
-
ERR.comparatorRequired("BST")
|
|
4272
|
-
);
|
|
5322
|
+
raise(TypeError, ERR.comparatorRequired("BST"));
|
|
4273
5323
|
}
|
|
4274
5324
|
return 0;
|
|
4275
5325
|
};
|
|
@@ -4591,7 +5641,8 @@ var bstTyped = (() => {
|
|
|
4591
5641
|
_snapshotOptions() {
|
|
4592
5642
|
return {
|
|
4593
5643
|
...super._snapshotOptions(),
|
|
4594
|
-
comparator: this._comparator
|
|
5644
|
+
comparator: this._comparator,
|
|
5645
|
+
enableOrderStatistic: this._enableOrderStatistic
|
|
4595
5646
|
};
|
|
4596
5647
|
}
|
|
4597
5648
|
/**
|
|
@@ -4613,6 +5664,113 @@ var bstTyped = (() => {
|
|
|
4613
5664
|
*
|
|
4614
5665
|
* @param v - The node to set as root.
|
|
4615
5666
|
*/
|
|
5667
|
+
/**
|
|
5668
|
+
* (Protected) Recalculates the subtree count for a single node.
|
|
5669
|
+
* @remarks Time O(1). Only active when enableOrderStatistic is true.
|
|
5670
|
+
*/
|
|
5671
|
+
_updateCount(node) {
|
|
5672
|
+
if (!this._enableOrderStatistic) return;
|
|
5673
|
+
node._count = 1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
5674
|
+
}
|
|
5675
|
+
/**
|
|
5676
|
+
* (Protected) Updates subtree counts from a node up to the root.
|
|
5677
|
+
* @remarks Time O(log n). Only active when enableOrderStatistic is true.
|
|
5678
|
+
*/
|
|
5679
|
+
_updateCountAlongPath(node) {
|
|
5680
|
+
if (!this._enableOrderStatistic) return;
|
|
5681
|
+
let current = node;
|
|
5682
|
+
while (current) {
|
|
5683
|
+
this._updateCount(current);
|
|
5684
|
+
current = current.parent;
|
|
5685
|
+
}
|
|
5686
|
+
}
|
|
5687
|
+
/**
|
|
5688
|
+
* (Protected) Finds the node at position k in tree order (iterative).
|
|
5689
|
+
* @remarks Time O(log n), Space O(1)
|
|
5690
|
+
*/
|
|
5691
|
+
_getByRankIterative(node, k) {
|
|
5692
|
+
let current = node;
|
|
5693
|
+
let remaining = k;
|
|
5694
|
+
while (current) {
|
|
5695
|
+
const leftCount = this.isRealNode(current.left) ? current.left._count : 0;
|
|
5696
|
+
if (remaining < leftCount) {
|
|
5697
|
+
current = current.left;
|
|
5698
|
+
} else if (remaining === leftCount) {
|
|
5699
|
+
return current;
|
|
5700
|
+
} else {
|
|
5701
|
+
remaining = remaining - leftCount - 1;
|
|
5702
|
+
current = current.right;
|
|
5703
|
+
}
|
|
5704
|
+
}
|
|
5705
|
+
return void 0;
|
|
5706
|
+
}
|
|
5707
|
+
/**
|
|
5708
|
+
* (Protected) Finds the node at position k in tree order (recursive).
|
|
5709
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5710
|
+
*/
|
|
5711
|
+
_getByRankRecursive(node, k) {
|
|
5712
|
+
if (!node) return void 0;
|
|
5713
|
+
const leftCount = this.isRealNode(node.left) ? node.left._count : 0;
|
|
5714
|
+
if (k < leftCount) return this._getByRankRecursive(node.left, k);
|
|
5715
|
+
if (k === leftCount) return node;
|
|
5716
|
+
return this._getByRankRecursive(node.right, k - leftCount - 1);
|
|
5717
|
+
}
|
|
5718
|
+
/**
|
|
5719
|
+
* (Protected) Computes the rank of a key iteratively.
|
|
5720
|
+
* @remarks Time O(log n), Space O(1)
|
|
5721
|
+
*/
|
|
5722
|
+
_getRankIterative(node, key) {
|
|
5723
|
+
let rank = 0;
|
|
5724
|
+
let current = node;
|
|
5725
|
+
while (this.isRealNode(current)) {
|
|
5726
|
+
const cmp = this._compare(current.key, key);
|
|
5727
|
+
if (cmp > 0) {
|
|
5728
|
+
current = current.left;
|
|
5729
|
+
} else if (cmp < 0) {
|
|
5730
|
+
rank += (this.isRealNode(current.left) ? current.left._count : 0) + 1;
|
|
5731
|
+
current = current.right;
|
|
5732
|
+
} else {
|
|
5733
|
+
rank += this.isRealNode(current.left) ? current.left._count : 0;
|
|
5734
|
+
return rank;
|
|
5735
|
+
}
|
|
5736
|
+
}
|
|
5737
|
+
return rank;
|
|
5738
|
+
}
|
|
5739
|
+
/**
|
|
5740
|
+
* (Protected) Computes the rank of a key recursively.
|
|
5741
|
+
* @remarks Time O(log n), Space O(log n) call stack
|
|
5742
|
+
*/
|
|
5743
|
+
_getRankRecursive(node, key) {
|
|
5744
|
+
if (!node) return 0;
|
|
5745
|
+
const cmp = this._compare(node.key, key);
|
|
5746
|
+
if (cmp > 0) {
|
|
5747
|
+
return this._getRankRecursive(node.left, key);
|
|
5748
|
+
} else if (cmp < 0) {
|
|
5749
|
+
return (this.isRealNode(node.left) ? node.left._count : 0) + 1 + this._getRankRecursive(node.right, key);
|
|
5750
|
+
} else {
|
|
5751
|
+
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
5752
|
+
}
|
|
5753
|
+
}
|
|
5754
|
+
/**
|
|
5755
|
+
* (Protected) Finds the in-order successor of a node.
|
|
5756
|
+
* @remarks Time O(log n), Space O(1)
|
|
5757
|
+
*/
|
|
5758
|
+
_next(node) {
|
|
5759
|
+
if (this.isRealNode(node.right)) {
|
|
5760
|
+
let current2 = node.right;
|
|
5761
|
+
while (this.isRealNode(current2.left)) {
|
|
5762
|
+
current2 = current2.left;
|
|
5763
|
+
}
|
|
5764
|
+
return current2;
|
|
5765
|
+
}
|
|
5766
|
+
let current = node;
|
|
5767
|
+
let parent = current.parent;
|
|
5768
|
+
while (parent && current === parent.right) {
|
|
5769
|
+
current = parent;
|
|
5770
|
+
parent = parent.parent;
|
|
5771
|
+
}
|
|
5772
|
+
return parent;
|
|
5773
|
+
}
|
|
4616
5774
|
_setRoot(v) {
|
|
4617
5775
|
if (v) v.parent = void 0;
|
|
4618
5776
|
this._root = v;
|
|
@@ -4659,21 +5817,28 @@ var bstTyped = (() => {
|
|
|
4659
5817
|
while (x.left !== void 0 && x.left !== null) x = x.left;
|
|
4660
5818
|
return x;
|
|
4661
5819
|
};
|
|
5820
|
+
let countUpdateStart;
|
|
4662
5821
|
if (node.left === void 0) {
|
|
5822
|
+
countUpdateStart = node.parent;
|
|
4663
5823
|
transplant(node, node.right);
|
|
4664
5824
|
} else if (node.right === void 0) {
|
|
5825
|
+
countUpdateStart = node.parent;
|
|
4665
5826
|
transplant(node, node.left);
|
|
4666
5827
|
} else {
|
|
4667
5828
|
const succ = minNode(node.right);
|
|
4668
5829
|
if (succ.parent !== node) {
|
|
5830
|
+
countUpdateStart = succ.parent;
|
|
4669
5831
|
transplant(succ, succ.right);
|
|
4670
5832
|
succ.right = node.right;
|
|
4671
5833
|
if (succ.right) succ.right.parent = succ;
|
|
5834
|
+
} else {
|
|
5835
|
+
countUpdateStart = succ;
|
|
4672
5836
|
}
|
|
4673
5837
|
transplant(node, succ);
|
|
4674
5838
|
succ.left = node.left;
|
|
4675
5839
|
if (succ.left) succ.left.parent = succ;
|
|
4676
5840
|
}
|
|
5841
|
+
this._updateCountAlongPath(countUpdateStart);
|
|
4677
5842
|
this._size = Math.max(0, this._size - 1);
|
|
4678
5843
|
return true;
|
|
4679
5844
|
}
|