data-structure-typed 1.51.7 → 1.51.8
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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +156 -156
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -10
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +12 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -12
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +22 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +32 -77
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -136
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +3 -20
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/cjs/types/utils/utils.d.ts +10 -1
- package/dist/cjs/utils/utils.d.ts +2 -1
- package/dist/cjs/utils/utils.js +29 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +12 -12
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -12
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +22 -32
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +32 -77
- package/dist/mjs/data-structures/binary-tree/bst.js +70 -139
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +3 -20
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/mjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/mjs/types/utils/utils.d.ts +10 -1
- package/dist/mjs/utils/utils.d.ts +2 -1
- package/dist/mjs/utils/utils.js +27 -0
- package/dist/umd/data-structure-typed.js +133 -208
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +4 -12
- package/src/data-structures/binary-tree/avl-tree.ts +15 -14
- package/src/data-structures/binary-tree/binary-tree.ts +29 -38
- package/src/data-structures/binary-tree/bst.ts +78 -148
- package/src/data-structures/binary-tree/rb-tree.ts +8 -22
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -3
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/index.html +70 -0
- package/test/performance/reportor.ts +2 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
- package/test/unit/utils/utils.test.ts +5 -0
- package/test/utils/console.ts +1 -1
|
@@ -5,4 +5,13 @@ export type Thunk = () => ReturnType<ToThunkFn> & {
|
|
|
5
5
|
export type TrlFn = (...args: any[]) => any;
|
|
6
6
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
7
7
|
export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
8
|
-
export type Any = string | number |
|
|
8
|
+
export type Any = string | number | bigint | boolean | symbol | undefined | object;
|
|
9
|
+
export type Comparable = number | string | bigint | boolean | ({
|
|
10
|
+
[key in string]: any;
|
|
11
|
+
} & {
|
|
12
|
+
valueOf(): Comparable;
|
|
13
|
+
}) | ({
|
|
14
|
+
[key in string]: any;
|
|
15
|
+
} & {
|
|
16
|
+
toString(): Comparable;
|
|
17
|
+
}) | (() => Comparable);
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
|
|
8
|
+
import type { Comparable, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
|
|
9
9
|
export declare const uuidV4: () => string;
|
|
10
10
|
export declare const arrayRemove: <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean) => T[];
|
|
11
11
|
export declare const THUNK_SYMBOL: unique symbol;
|
|
@@ -23,3 +23,4 @@ export declare const throwRangeError: (message?: string) => void;
|
|
|
23
23
|
export declare const isWeakKey: (input: unknown) => input is object;
|
|
24
24
|
export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
|
|
25
25
|
export declare const roundFixed: (num: number, digit?: number) => number;
|
|
26
|
+
export declare function isComparable(key: any): key is Comparable;
|
package/dist/mjs/utils/utils.js
CHANGED
|
@@ -68,3 +68,30 @@ export const roundFixed = (num, digit = 10) => {
|
|
|
68
68
|
const multiplier = Math.pow(10, digit);
|
|
69
69
|
return Math.round(num * multiplier) / multiplier;
|
|
70
70
|
};
|
|
71
|
+
export function isComparable(key) {
|
|
72
|
+
const keyType = typeof key;
|
|
73
|
+
if (keyType === 'number')
|
|
74
|
+
return isNaN(key);
|
|
75
|
+
if (keyType === 'string')
|
|
76
|
+
return true;
|
|
77
|
+
if (keyType === 'bigint')
|
|
78
|
+
return true;
|
|
79
|
+
if (keyType === 'boolean')
|
|
80
|
+
return true;
|
|
81
|
+
if (keyType === 'symbol')
|
|
82
|
+
return false;
|
|
83
|
+
if (keyType === 'undefined')
|
|
84
|
+
return false;
|
|
85
|
+
if (keyType === 'function')
|
|
86
|
+
return isComparable(key());
|
|
87
|
+
if (keyType === 'object') {
|
|
88
|
+
if (key === null)
|
|
89
|
+
return true;
|
|
90
|
+
if (typeof key.valueOf === 'function')
|
|
91
|
+
return isComparable(key.valueOf());
|
|
92
|
+
if (typeof key.toString === 'function')
|
|
93
|
+
return isComparable(key.toString());
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
@@ -160,6 +160,7 @@ var dataStructureTyped = (() => {
|
|
|
160
160
|
arrayRemove: () => arrayRemove,
|
|
161
161
|
calcMinUnitsRequired: () => calcMinUnitsRequired,
|
|
162
162
|
getMSB: () => getMSB,
|
|
163
|
+
isComparable: () => isComparable,
|
|
163
164
|
isThunk: () => isThunk,
|
|
164
165
|
isWeakKey: () => isWeakKey,
|
|
165
166
|
rangeCheck: () => rangeCheck,
|
|
@@ -711,6 +712,33 @@ var dataStructureTyped = (() => {
|
|
|
711
712
|
const multiplier = Math.pow(10, digit);
|
|
712
713
|
return Math.round(num * multiplier) / multiplier;
|
|
713
714
|
};
|
|
715
|
+
function isComparable(key) {
|
|
716
|
+
const keyType = typeof key;
|
|
717
|
+
if (keyType === "number")
|
|
718
|
+
return isNaN(key);
|
|
719
|
+
if (keyType === "string")
|
|
720
|
+
return true;
|
|
721
|
+
if (keyType === "bigint")
|
|
722
|
+
return true;
|
|
723
|
+
if (keyType === "boolean")
|
|
724
|
+
return true;
|
|
725
|
+
if (keyType === "symbol")
|
|
726
|
+
return false;
|
|
727
|
+
if (keyType === "undefined")
|
|
728
|
+
return false;
|
|
729
|
+
if (keyType === "function")
|
|
730
|
+
return isComparable(key());
|
|
731
|
+
if (keyType === "object") {
|
|
732
|
+
if (key === null)
|
|
733
|
+
return true;
|
|
734
|
+
if (typeof key.valueOf === "function")
|
|
735
|
+
return isComparable(key.valueOf());
|
|
736
|
+
if (typeof key.toString === "function")
|
|
737
|
+
return isComparable(key.toString());
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
714
742
|
|
|
715
743
|
// src/utils/number.ts
|
|
716
744
|
function toBinaryString(num, digit = 32) {
|
|
@@ -7745,29 +7773,19 @@ var dataStructureTyped = (() => {
|
|
|
7745
7773
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
7746
7774
|
super();
|
|
7747
7775
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
7748
|
-
__publicField(this, "_extractor", (key) => typeof key === "number" ? key : Number(key));
|
|
7749
7776
|
__publicField(this, "_root");
|
|
7750
7777
|
__publicField(this, "_size");
|
|
7751
7778
|
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
7752
7779
|
__publicField(this, "_DEFAULT_CALLBACK", (node) => node ? node.key : void 0);
|
|
7753
7780
|
if (options) {
|
|
7754
|
-
const { iterationType
|
|
7781
|
+
const { iterationType } = options;
|
|
7755
7782
|
if (iterationType)
|
|
7756
7783
|
this.iterationType = iterationType;
|
|
7757
|
-
if (extractor)
|
|
7758
|
-
this._extractor = extractor;
|
|
7759
7784
|
}
|
|
7760
7785
|
this._size = 0;
|
|
7761
7786
|
if (keysOrNodesOrEntries)
|
|
7762
7787
|
this.addMany(keysOrNodesOrEntries);
|
|
7763
7788
|
}
|
|
7764
|
-
/**
|
|
7765
|
-
* The function returns the value of the `_extractor` property.
|
|
7766
|
-
* @returns The `_extractor` property is being returned.
|
|
7767
|
-
*/
|
|
7768
|
-
get extractor() {
|
|
7769
|
-
return this._extractor;
|
|
7770
|
-
}
|
|
7771
7789
|
/**
|
|
7772
7790
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
7773
7791
|
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
@@ -8337,7 +8355,7 @@ var dataStructureTyped = (() => {
|
|
|
8337
8355
|
const dfs = (cur, min, max) => {
|
|
8338
8356
|
if (!this.isRealNode(cur))
|
|
8339
8357
|
return true;
|
|
8340
|
-
const numKey =
|
|
8358
|
+
const numKey = Number(cur.key);
|
|
8341
8359
|
if (numKey <= min || numKey >= max)
|
|
8342
8360
|
return false;
|
|
8343
8361
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -8356,7 +8374,7 @@ var dataStructureTyped = (() => {
|
|
|
8356
8374
|
curr = curr.left;
|
|
8357
8375
|
}
|
|
8358
8376
|
curr = stack.pop();
|
|
8359
|
-
const numKey =
|
|
8377
|
+
const numKey = Number(curr.key);
|
|
8360
8378
|
if (!this.isRealNode(curr) || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
8361
8379
|
return false;
|
|
8362
8380
|
prev = numKey;
|
|
@@ -8386,15 +8404,15 @@ var dataStructureTyped = (() => {
|
|
|
8386
8404
|
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
8387
8405
|
*/
|
|
8388
8406
|
getDepth(dist, beginRoot = this.root) {
|
|
8389
|
-
|
|
8390
|
-
|
|
8407
|
+
let distEnsured = this.ensureNode(dist);
|
|
8408
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
8391
8409
|
let depth = 0;
|
|
8392
|
-
while (
|
|
8393
|
-
if (
|
|
8410
|
+
while (distEnsured == null ? void 0 : distEnsured.parent) {
|
|
8411
|
+
if (distEnsured === beginRootEnsured) {
|
|
8394
8412
|
return depth;
|
|
8395
8413
|
}
|
|
8396
8414
|
depth++;
|
|
8397
|
-
|
|
8415
|
+
distEnsured = distEnsured.parent;
|
|
8398
8416
|
}
|
|
8399
8417
|
return depth;
|
|
8400
8418
|
}
|
|
@@ -8523,14 +8541,14 @@ var dataStructureTyped = (() => {
|
|
|
8523
8541
|
*/
|
|
8524
8542
|
getPathToRoot(beginNode, isReverse = true) {
|
|
8525
8543
|
const result = [];
|
|
8526
|
-
|
|
8527
|
-
if (!
|
|
8544
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
8545
|
+
if (!beginNodeEnsured)
|
|
8528
8546
|
return result;
|
|
8529
|
-
while (
|
|
8530
|
-
result.push(
|
|
8531
|
-
|
|
8547
|
+
while (beginNodeEnsured.parent) {
|
|
8548
|
+
result.push(beginNodeEnsured);
|
|
8549
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
8532
8550
|
}
|
|
8533
|
-
result.push(
|
|
8551
|
+
result.push(beginNodeEnsured);
|
|
8534
8552
|
return isReverse ? result.reverse() : result;
|
|
8535
8553
|
}
|
|
8536
8554
|
/**
|
|
@@ -9158,10 +9176,10 @@ var dataStructureTyped = (() => {
|
|
|
9158
9176
|
console.log(`U for undefined
|
|
9159
9177
|
`);
|
|
9160
9178
|
if (opts.isShowNull)
|
|
9161
|
-
console.log(`
|
|
9179
|
+
console.log(`N for null
|
|
9162
9180
|
`);
|
|
9163
9181
|
if (opts.isShowRedBlackNIL)
|
|
9164
|
-
console.log(`S for Sentinel Node
|
|
9182
|
+
console.log(`S for Sentinel Node(NIL)
|
|
9165
9183
|
`);
|
|
9166
9184
|
const display = (root) => {
|
|
9167
9185
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -9187,22 +9205,22 @@ var dataStructureTyped = (() => {
|
|
|
9187
9205
|
const stack = [];
|
|
9188
9206
|
let current = node;
|
|
9189
9207
|
while (current || stack.length > 0) {
|
|
9190
|
-
while (
|
|
9208
|
+
while (this.isRealNode(current)) {
|
|
9191
9209
|
stack.push(current);
|
|
9192
9210
|
current = current.left;
|
|
9193
9211
|
}
|
|
9194
9212
|
current = stack.pop();
|
|
9195
|
-
if (
|
|
9213
|
+
if (this.isRealNode(current)) {
|
|
9196
9214
|
yield [current.key, current.value];
|
|
9197
9215
|
current = current.right;
|
|
9198
9216
|
}
|
|
9199
9217
|
}
|
|
9200
9218
|
} else {
|
|
9201
|
-
if (node.left &&
|
|
9219
|
+
if (node.left && this.isRealNode(node)) {
|
|
9202
9220
|
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
9203
9221
|
}
|
|
9204
9222
|
yield [node.key, node.value];
|
|
9205
|
-
if (node.right &&
|
|
9223
|
+
if (node.right && this.isRealNode(node)) {
|
|
9206
9224
|
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
9207
9225
|
}
|
|
9208
9226
|
}
|
|
@@ -9228,10 +9246,10 @@ var dataStructureTyped = (() => {
|
|
|
9228
9246
|
return emptyDisplayLayout;
|
|
9229
9247
|
} else if (node === void 0 && !isShowUndefined) {
|
|
9230
9248
|
return emptyDisplayLayout;
|
|
9231
|
-
} else if (
|
|
9249
|
+
} else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
9232
9250
|
return emptyDisplayLayout;
|
|
9233
9251
|
} else if (node !== null && node !== void 0) {
|
|
9234
|
-
const key = node.key, line =
|
|
9252
|
+
const key = node.key, line = this.isNIL(node) ? "S" : key.toString(), width = line.length;
|
|
9235
9253
|
return _buildNodeDisplay(
|
|
9236
9254
|
line,
|
|
9237
9255
|
width,
|
|
@@ -9378,23 +9396,29 @@ var dataStructureTyped = (() => {
|
|
|
9378
9396
|
};
|
|
9379
9397
|
var BST = class _BST extends BinaryTree {
|
|
9380
9398
|
/**
|
|
9381
|
-
* This is the constructor function for a
|
|
9382
|
-
*
|
|
9383
|
-
* @param keysOrNodesOrEntries -
|
|
9384
|
-
* to initialize the binary search tree with the provided
|
|
9399
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
|
|
9400
|
+
* the tree with keys, nodes, or entries and optional options.
|
|
9401
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
9402
|
+
* contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
|
|
9403
|
+
* keys, nodes, or entries.
|
|
9385
9404
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
9386
9405
|
* configuration options for the binary search tree. It can have the following properties:
|
|
9387
9406
|
*/
|
|
9388
9407
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
9389
9408
|
super([], options);
|
|
9390
9409
|
__publicField(this, "_root");
|
|
9391
|
-
__publicField(this, "
|
|
9410
|
+
__publicField(this, "_comparator", (a, b) => {
|
|
9411
|
+
if (a > b)
|
|
9412
|
+
return 1;
|
|
9413
|
+
if (a < b)
|
|
9414
|
+
return -1;
|
|
9415
|
+
return 0;
|
|
9416
|
+
});
|
|
9392
9417
|
if (options) {
|
|
9393
|
-
const {
|
|
9394
|
-
if (
|
|
9395
|
-
this.
|
|
9418
|
+
const { comparator } = options;
|
|
9419
|
+
if (comparator)
|
|
9420
|
+
this._comparator = comparator;
|
|
9396
9421
|
}
|
|
9397
|
-
this._root = void 0;
|
|
9398
9422
|
if (keysOrNodesOrEntries)
|
|
9399
9423
|
this.addMany(keysOrNodesOrEntries);
|
|
9400
9424
|
}
|
|
@@ -9406,11 +9430,11 @@ var dataStructureTyped = (() => {
|
|
|
9406
9430
|
return this._root;
|
|
9407
9431
|
}
|
|
9408
9432
|
/**
|
|
9409
|
-
* The function returns the value of the
|
|
9410
|
-
* @returns The
|
|
9433
|
+
* The function returns the value of the _comparator property.
|
|
9434
|
+
* @returns The `_comparator` property is being returned.
|
|
9411
9435
|
*/
|
|
9412
|
-
get
|
|
9413
|
-
return this.
|
|
9436
|
+
get comparator() {
|
|
9437
|
+
return this._comparator;
|
|
9414
9438
|
}
|
|
9415
9439
|
/**
|
|
9416
9440
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
@@ -9434,7 +9458,7 @@ var dataStructureTyped = (() => {
|
|
|
9434
9458
|
createTree(options) {
|
|
9435
9459
|
return new _BST([], __spreadValues({
|
|
9436
9460
|
iterationType: this.iterationType,
|
|
9437
|
-
|
|
9461
|
+
comparator: this.comparator
|
|
9438
9462
|
}, options));
|
|
9439
9463
|
}
|
|
9440
9464
|
/**
|
|
@@ -9514,13 +9538,11 @@ var dataStructureTyped = (() => {
|
|
|
9514
9538
|
* Time Complexity: O(log n)
|
|
9515
9539
|
* Space Complexity: O(1)
|
|
9516
9540
|
*
|
|
9517
|
-
* The `add` function adds a new node to a binary tree
|
|
9518
|
-
*
|
|
9519
|
-
* @param keyOrNodeOrEntry -
|
|
9520
|
-
* @param {V} [value] - The
|
|
9521
|
-
*
|
|
9522
|
-
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
9523
|
-
* node was not added.
|
|
9541
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
|
|
9542
|
+
* updating the value if the key already exists.
|
|
9543
|
+
* @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
|
|
9544
|
+
* @param {V} [value] - The value to be added to the binary search tree.
|
|
9545
|
+
* @returns The method returns a boolean value.
|
|
9524
9546
|
*/
|
|
9525
9547
|
add(keyOrNodeOrEntry, value) {
|
|
9526
9548
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
@@ -9533,10 +9555,10 @@ var dataStructureTyped = (() => {
|
|
|
9533
9555
|
}
|
|
9534
9556
|
let current = this.root;
|
|
9535
9557
|
while (current !== void 0) {
|
|
9536
|
-
if (this.
|
|
9558
|
+
if (this.comparator(current.key, newNode.key) === 0) {
|
|
9537
9559
|
this._replaceNode(current, newNode);
|
|
9538
9560
|
return true;
|
|
9539
|
-
} else if (this.
|
|
9561
|
+
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
9540
9562
|
if (current.left === void 0) {
|
|
9541
9563
|
current.left = newNode;
|
|
9542
9564
|
this._size++;
|
|
@@ -9562,21 +9584,24 @@ var dataStructureTyped = (() => {
|
|
|
9562
9584
|
* Time Complexity: O(k log n)
|
|
9563
9585
|
* Space Complexity: O(k + log n)
|
|
9564
9586
|
*
|
|
9565
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
9566
|
-
*
|
|
9567
|
-
*
|
|
9568
|
-
*
|
|
9587
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
|
|
9588
|
+
* the structure if specified, and returns an array indicating whether each key or node was
|
|
9589
|
+
* successfully inserted.
|
|
9590
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
9591
|
+
* data structure.
|
|
9569
9592
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
9570
9593
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
9571
9594
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
9572
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
9573
|
-
*
|
|
9574
|
-
* algorithm. If set to false, the
|
|
9575
|
-
*
|
|
9576
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
9577
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
9578
|
-
* `this.iterationType`, which
|
|
9579
|
-
*
|
|
9595
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
9596
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
9597
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
9598
|
+
* value is true.
|
|
9599
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
9600
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
|
|
9601
|
+
* has a default value of `this.iterationType`, which means it will use the iteration type specified
|
|
9602
|
+
* in the binary tree instance.
|
|
9603
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each key or node
|
|
9604
|
+
* or entry was successfully inserted into the data structure.
|
|
9580
9605
|
*/
|
|
9581
9606
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
9582
9607
|
const inserted = [];
|
|
@@ -9603,20 +9628,23 @@ var dataStructureTyped = (() => {
|
|
|
9603
9628
|
}
|
|
9604
9629
|
let sorted = [];
|
|
9605
9630
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
9606
|
-
let
|
|
9607
|
-
if (this.isEntry(a))
|
|
9608
|
-
|
|
9609
|
-
else if (this.isRealNode(a))
|
|
9610
|
-
|
|
9631
|
+
let keyA, keyB;
|
|
9632
|
+
if (this.isEntry(a)) {
|
|
9633
|
+
keyA = a[0];
|
|
9634
|
+
} else if (this.isRealNode(a))
|
|
9635
|
+
keyA = a.key;
|
|
9611
9636
|
else
|
|
9612
|
-
|
|
9637
|
+
keyA = a;
|
|
9613
9638
|
if (this.isEntry(b))
|
|
9614
|
-
|
|
9639
|
+
keyB = b[0];
|
|
9615
9640
|
else if (this.isRealNode(b))
|
|
9616
|
-
|
|
9641
|
+
keyB = b.key;
|
|
9617
9642
|
else
|
|
9618
|
-
|
|
9619
|
-
|
|
9643
|
+
keyB = b;
|
|
9644
|
+
if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
|
|
9645
|
+
return this.comparator(keyA, keyB);
|
|
9646
|
+
}
|
|
9647
|
+
return 0;
|
|
9620
9648
|
});
|
|
9621
9649
|
const _dfs = (arr) => {
|
|
9622
9650
|
if (arr.length === 0)
|
|
@@ -9696,9 +9724,9 @@ var dataStructureTyped = (() => {
|
|
|
9696
9724
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
9697
9725
|
return;
|
|
9698
9726
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
9699
|
-
if (this.isRealNode(cur.left) && this.
|
|
9727
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
9700
9728
|
dfs(cur.left);
|
|
9701
|
-
if (this.isRealNode(cur.right) && this.
|
|
9729
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
9702
9730
|
dfs(cur.right);
|
|
9703
9731
|
} else {
|
|
9704
9732
|
this.isRealNode(cur.left) && dfs(cur.left);
|
|
@@ -9717,9 +9745,9 @@ var dataStructureTyped = (() => {
|
|
|
9717
9745
|
return ans;
|
|
9718
9746
|
}
|
|
9719
9747
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
9720
|
-
if (this.isRealNode(cur.right) && this.
|
|
9748
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
9721
9749
|
stack.push(cur.right);
|
|
9722
|
-
if (this.isRealNode(cur.left) && this.
|
|
9750
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
9723
9751
|
stack.push(cur.left);
|
|
9724
9752
|
} else {
|
|
9725
9753
|
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
@@ -9854,38 +9882,6 @@ var dataStructureTyped = (() => {
|
|
|
9854
9882
|
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9855
9883
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9856
9884
|
}
|
|
9857
|
-
/**
|
|
9858
|
-
* Time Complexity: O(log n)
|
|
9859
|
-
* Space Complexity: O(1)
|
|
9860
|
-
*/
|
|
9861
|
-
/**
|
|
9862
|
-
* Time Complexity: O(log n)
|
|
9863
|
-
* Space Complexity: O(1)
|
|
9864
|
-
*
|
|
9865
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9866
|
-
* leftmost node if the comparison result is greater than.
|
|
9867
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9868
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
9869
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9870
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9871
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9872
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
9873
|
-
*/
|
|
9874
|
-
lastKey(beginRoot = this.root) {
|
|
9875
|
-
let current = this.ensureNode(beginRoot);
|
|
9876
|
-
if (!current)
|
|
9877
|
-
return void 0;
|
|
9878
|
-
if (this._variant === "STANDARD") {
|
|
9879
|
-
while (current.right !== void 0) {
|
|
9880
|
-
current = current.right;
|
|
9881
|
-
}
|
|
9882
|
-
} else {
|
|
9883
|
-
while (current.left !== void 0) {
|
|
9884
|
-
current = current.left;
|
|
9885
|
-
}
|
|
9886
|
-
}
|
|
9887
|
-
return current.key;
|
|
9888
|
-
}
|
|
9889
9885
|
/**
|
|
9890
9886
|
* Time Complexity: O(log n)
|
|
9891
9887
|
* Space Complexity: O(log n)
|
|
@@ -9911,18 +9907,18 @@ var dataStructureTyped = (() => {
|
|
|
9911
9907
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
9912
9908
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
9913
9909
|
*/
|
|
9914
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater =
|
|
9915
|
-
|
|
9910
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
|
|
9911
|
+
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
9916
9912
|
const ans = [];
|
|
9917
|
-
if (!
|
|
9913
|
+
if (!targetNodeEnsured)
|
|
9918
9914
|
return ans;
|
|
9919
9915
|
if (!this.root)
|
|
9920
9916
|
return ans;
|
|
9921
|
-
const targetKey =
|
|
9917
|
+
const targetKey = targetNodeEnsured.key;
|
|
9922
9918
|
if (iterationType === "RECURSIVE") {
|
|
9923
9919
|
const dfs = (cur) => {
|
|
9924
|
-
const compared = this.
|
|
9925
|
-
if (compared === lesserOrGreater)
|
|
9920
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
9921
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
9926
9922
|
ans.push(callback(cur));
|
|
9927
9923
|
if (this.isRealNode(cur.left))
|
|
9928
9924
|
dfs(cur.left);
|
|
@@ -9936,8 +9932,8 @@ var dataStructureTyped = (() => {
|
|
|
9936
9932
|
while (queue.size > 0) {
|
|
9937
9933
|
const cur = queue.shift();
|
|
9938
9934
|
if (this.isRealNode(cur)) {
|
|
9939
|
-
const compared = this.
|
|
9940
|
-
if (compared === lesserOrGreater)
|
|
9935
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
9936
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
9941
9937
|
ans.push(callback(cur));
|
|
9942
9938
|
if (this.isRealNode(cur.left))
|
|
9943
9939
|
queue.push(cur.left);
|
|
@@ -10074,51 +10070,6 @@ var dataStructureTyped = (() => {
|
|
|
10074
10070
|
}
|
|
10075
10071
|
this._root = v;
|
|
10076
10072
|
}
|
|
10077
|
-
/**
|
|
10078
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
10079
|
-
* is greater than, less than, or equal to the second value.
|
|
10080
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
10081
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
10082
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
10083
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
10084
|
-
*/
|
|
10085
|
-
_compare(a, b) {
|
|
10086
|
-
const extractedA = this.extractor(a);
|
|
10087
|
-
const extractedB = this.extractor(b);
|
|
10088
|
-
const compared = this.variant === "STANDARD" ? extractedA - extractedB : extractedB - extractedA;
|
|
10089
|
-
if (compared > 0)
|
|
10090
|
-
return "GT";
|
|
10091
|
-
if (compared < 0)
|
|
10092
|
-
return "LT";
|
|
10093
|
-
return "EQ";
|
|
10094
|
-
}
|
|
10095
|
-
/**
|
|
10096
|
-
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
10097
|
-
* `a` is less than `b` based on the specified variant.
|
|
10098
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
10099
|
-
* first value to be compared in the function.
|
|
10100
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
10101
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
10102
|
-
* @returns a boolean value.
|
|
10103
|
-
*/
|
|
10104
|
-
_lt(a, b) {
|
|
10105
|
-
const extractedA = this.extractor(a);
|
|
10106
|
-
const extractedB = this.extractor(b);
|
|
10107
|
-
return this.variant === "STANDARD" ? extractedA < extractedB : extractedA > extractedB;
|
|
10108
|
-
}
|
|
10109
|
-
/**
|
|
10110
|
-
* The function compares two values using a custom extractor function and returns true if the first
|
|
10111
|
-
* value is greater than the second value.
|
|
10112
|
-
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
10113
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
10114
|
-
* of the arguments for the comparison in the function.
|
|
10115
|
-
* @returns a boolean value.
|
|
10116
|
-
*/
|
|
10117
|
-
_gt(a, b) {
|
|
10118
|
-
const extractedA = this.extractor(a);
|
|
10119
|
-
const extractedB = this.extractor(b);
|
|
10120
|
-
return this.variant === "STANDARD" ? extractedA > extractedB : extractedA < extractedB;
|
|
10121
|
-
}
|
|
10122
10073
|
};
|
|
10123
10074
|
|
|
10124
10075
|
// src/data-structures/binary-tree/binary-indexed-tree.ts
|
|
@@ -10754,7 +10705,7 @@ var dataStructureTyped = (() => {
|
|
|
10754
10705
|
createTree(options) {
|
|
10755
10706
|
return new _AVLTree([], __spreadValues({
|
|
10756
10707
|
iterationType: this.iterationType,
|
|
10757
|
-
|
|
10708
|
+
comparator: this.comparator
|
|
10758
10709
|
}, options));
|
|
10759
10710
|
}
|
|
10760
10711
|
/**
|
|
@@ -10829,21 +10780,21 @@ var dataStructureTyped = (() => {
|
|
|
10829
10780
|
* if either `srcNode` or `destNode` is undefined.
|
|
10830
10781
|
*/
|
|
10831
10782
|
_swapProperties(srcNode, destNode) {
|
|
10832
|
-
|
|
10833
|
-
|
|
10834
|
-
if (
|
|
10835
|
-
const { key, value, height } =
|
|
10783
|
+
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
10784
|
+
const destNodeEnsured = this.ensureNode(destNode);
|
|
10785
|
+
if (srcNodeEnsured && destNodeEnsured) {
|
|
10786
|
+
const { key, value, height } = destNodeEnsured;
|
|
10836
10787
|
const tempNode = this.createNode(key, value);
|
|
10837
10788
|
if (tempNode) {
|
|
10838
10789
|
tempNode.height = height;
|
|
10839
|
-
|
|
10840
|
-
|
|
10841
|
-
|
|
10842
|
-
|
|
10843
|
-
|
|
10844
|
-
|
|
10790
|
+
destNodeEnsured.key = srcNodeEnsured.key;
|
|
10791
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
10792
|
+
destNodeEnsured.height = srcNodeEnsured.height;
|
|
10793
|
+
srcNodeEnsured.key = tempNode.key;
|
|
10794
|
+
srcNodeEnsured.value = tempNode.value;
|
|
10795
|
+
srcNodeEnsured.height = tempNode.height;
|
|
10845
10796
|
}
|
|
10846
|
-
return
|
|
10797
|
+
return destNodeEnsured;
|
|
10847
10798
|
}
|
|
10848
10799
|
return void 0;
|
|
10849
10800
|
}
|
|
@@ -11438,9 +11389,10 @@ var dataStructureTyped = (() => {
|
|
|
11438
11389
|
let parent = void 0;
|
|
11439
11390
|
while (this.isRealNode(current)) {
|
|
11440
11391
|
parent = current;
|
|
11441
|
-
|
|
11392
|
+
const compared = this.comparator(node.key, current.key);
|
|
11393
|
+
if (compared < 0) {
|
|
11442
11394
|
current = (_a = current.left) != null ? _a : this.NIL;
|
|
11443
|
-
} else if (
|
|
11395
|
+
} else if (compared > 0) {
|
|
11444
11396
|
current = (_b = current.right) != null ? _b : this.NIL;
|
|
11445
11397
|
} else {
|
|
11446
11398
|
this._replaceNode(current, node);
|
|
@@ -11686,24 +11638,6 @@ var dataStructureTyped = (() => {
|
|
|
11686
11638
|
x.right = y;
|
|
11687
11639
|
y.parent = x;
|
|
11688
11640
|
}
|
|
11689
|
-
/**
|
|
11690
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
11691
|
-
* is greater than, less than, or equal to the second value.
|
|
11692
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
11693
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
11694
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
11695
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
11696
|
-
*/
|
|
11697
|
-
_compare(a, b) {
|
|
11698
|
-
const extractedA = this.extractor(a);
|
|
11699
|
-
const extractedB = this.extractor(b);
|
|
11700
|
-
const compared = extractedA - extractedB;
|
|
11701
|
-
if (compared > 0)
|
|
11702
|
-
return "GT";
|
|
11703
|
-
if (compared < 0)
|
|
11704
|
-
return "LT";
|
|
11705
|
-
return "EQ";
|
|
11706
|
-
}
|
|
11707
11641
|
};
|
|
11708
11642
|
|
|
11709
11643
|
// src/data-structures/binary-tree/avl-tree-multi-map.ts
|
|
@@ -11784,19 +11718,10 @@ var dataStructureTyped = (() => {
|
|
|
11784
11718
|
createNode(key, value, count) {
|
|
11785
11719
|
return new AVLTreeMultiMapNode(key, value, count);
|
|
11786
11720
|
}
|
|
11787
|
-
/**
|
|
11788
|
-
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
11789
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
11790
|
-
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
11791
|
-
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
11792
|
-
* the tree, respectively. These properties can be
|
|
11793
|
-
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
11794
|
-
* default options. The returned value is casted as `TREE`.
|
|
11795
|
-
*/
|
|
11796
11721
|
createTree(options) {
|
|
11797
11722
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
11798
11723
|
iterationType: this.iterationType,
|
|
11799
|
-
|
|
11724
|
+
comparator: this.comparator
|
|
11800
11725
|
}, options));
|
|
11801
11726
|
}
|
|
11802
11727
|
/**
|