data-structure-typed 1.50.3 → 1.50.4
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/.github/ISSUE_TEMPLATE/bug_report.md +0 -13
- package/CHANGELOG.md +1 -1
- package/README.md +6 -6
- package/README_zh-CN.md +6 -6
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +15 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/bst.js +5 -7
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +17 -9
- 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 +200 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +400 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/{tree-multimap.js → tree-multi-map.js} +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +11 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +5 -7
- package/dist/mjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +17 -9
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +397 -0
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/umd/data-structure-typed.js +399 -29
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +20 -10
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/test/integration/index.html +2 -2
- package/test/unit/data-structures/binary-tree/{tree-multimap.test.ts → avl-tree-multi-map.test.ts} +55 -55
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -25
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +706 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +4 -4
- package/test/unit/unrestricted-interconversion.test.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/mjs/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
|
@@ -102,6 +102,8 @@ var dataStructureTyped = (() => {
|
|
|
102
102
|
var src_exports = {};
|
|
103
103
|
__export(src_exports, {
|
|
104
104
|
AVLTree: () => AVLTree,
|
|
105
|
+
AVLTreeMultiMap: () => AVLTreeMultiMap,
|
|
106
|
+
AVLTreeMultiMapNode: () => AVLTreeMultiMapNode,
|
|
105
107
|
AVLTreeNode: () => AVLTreeNode,
|
|
106
108
|
AbstractEdge: () => AbstractEdge,
|
|
107
109
|
AbstractGraph: () => AbstractGraph,
|
|
@@ -152,8 +154,8 @@ var dataStructureTyped = (() => {
|
|
|
152
154
|
SkipListNode: () => SkipListNode,
|
|
153
155
|
Stack: () => Stack,
|
|
154
156
|
THUNK_SYMBOL: () => THUNK_SYMBOL,
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
TreeMultiMap: () => TreeMultiMap,
|
|
158
|
+
TreeMultiMapNode: () => TreeMultiMapNode,
|
|
157
159
|
TreeNode: () => TreeNode,
|
|
158
160
|
Trie: () => Trie,
|
|
159
161
|
TrieNode: () => TrieNode,
|
|
@@ -10221,11 +10223,9 @@ var dataStructureTyped = (() => {
|
|
|
10221
10223
|
const compared = this._compare(cur.key, targetKey);
|
|
10222
10224
|
if (compared === lesserOrGreater)
|
|
10223
10225
|
ans.push(callback(cur));
|
|
10224
|
-
if (
|
|
10225
|
-
return;
|
|
10226
|
-
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
|
|
10226
|
+
if (this.isRealNode(cur.left))
|
|
10227
10227
|
_traverse(cur.left);
|
|
10228
|
-
if (
|
|
10228
|
+
if (this.isRealNode(cur.right))
|
|
10229
10229
|
_traverse(cur.right);
|
|
10230
10230
|
};
|
|
10231
10231
|
_traverse(this.root);
|
|
@@ -10234,13 +10234,13 @@ var dataStructureTyped = (() => {
|
|
|
10234
10234
|
const queue = new Queue([this.root]);
|
|
10235
10235
|
while (queue.size > 0) {
|
|
10236
10236
|
const cur = queue.shift();
|
|
10237
|
-
if (cur) {
|
|
10237
|
+
if (this.isRealNode(cur)) {
|
|
10238
10238
|
const compared = this._compare(cur.key, targetKey);
|
|
10239
10239
|
if (compared === lesserOrGreater)
|
|
10240
10240
|
ans.push(callback(cur));
|
|
10241
|
-
if (
|
|
10241
|
+
if (this.isRealNode(cur.left))
|
|
10242
10242
|
queue.push(cur.left);
|
|
10243
|
-
if (
|
|
10243
|
+
if (this.isRealNode(cur.right))
|
|
10244
10244
|
queue.push(cur.right);
|
|
10245
10245
|
}
|
|
10246
10246
|
}
|
|
@@ -11847,12 +11847,12 @@ var dataStructureTyped = (() => {
|
|
|
11847
11847
|
*/
|
|
11848
11848
|
_fixInsert(k) {
|
|
11849
11849
|
let u;
|
|
11850
|
-
while (k.parent && k.parent.color === 1) {
|
|
11850
|
+
while (k.parent && k.parent.color === 1 /* RED */) {
|
|
11851
11851
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
11852
11852
|
u = k.parent.parent.left;
|
|
11853
|
-
if (u && u.color === 1) {
|
|
11854
|
-
u.color = 0 /* BLACK */;
|
|
11853
|
+
if (u && u.color === 1 /* RED */) {
|
|
11855
11854
|
k.parent.color = 0 /* BLACK */;
|
|
11855
|
+
u.color = 0 /* BLACK */;
|
|
11856
11856
|
k.parent.parent.color = 1 /* RED */;
|
|
11857
11857
|
k = k.parent.parent;
|
|
11858
11858
|
} else {
|
|
@@ -11860,15 +11860,17 @@ var dataStructureTyped = (() => {
|
|
|
11860
11860
|
k = k.parent;
|
|
11861
11861
|
this._rightRotate(k);
|
|
11862
11862
|
}
|
|
11863
|
-
k.parent.color
|
|
11864
|
-
|
|
11863
|
+
if (k.parent.color === 1 /* RED */) {
|
|
11864
|
+
k.parent.color = 0 /* BLACK */;
|
|
11865
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11866
|
+
}
|
|
11865
11867
|
this._leftRotate(k.parent.parent);
|
|
11866
11868
|
}
|
|
11867
11869
|
} else {
|
|
11868
11870
|
u = k.parent.parent.right;
|
|
11869
|
-
if (u && u.color === 1) {
|
|
11870
|
-
u.color = 0 /* BLACK */;
|
|
11871
|
+
if (u && u.color === 1 /* RED */) {
|
|
11871
11872
|
k.parent.color = 0 /* BLACK */;
|
|
11873
|
+
u.color = 0 /* BLACK */;
|
|
11872
11874
|
k.parent.parent.color = 1 /* RED */;
|
|
11873
11875
|
k = k.parent.parent;
|
|
11874
11876
|
} else {
|
|
@@ -11876,8 +11878,10 @@ var dataStructureTyped = (() => {
|
|
|
11876
11878
|
k = k.parent;
|
|
11877
11879
|
this._leftRotate(k);
|
|
11878
11880
|
}
|
|
11879
|
-
k.parent.color
|
|
11880
|
-
|
|
11881
|
+
if (k.parent.color === 1 /* RED */) {
|
|
11882
|
+
k.parent.color = 0 /* BLACK */;
|
|
11883
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11884
|
+
}
|
|
11881
11885
|
this._rightRotate(k.parent.parent);
|
|
11882
11886
|
}
|
|
11883
11887
|
}
|
|
@@ -11996,8 +12000,8 @@ var dataStructureTyped = (() => {
|
|
|
11996
12000
|
}
|
|
11997
12001
|
};
|
|
11998
12002
|
|
|
11999
|
-
// src/data-structures/binary-tree/tree-
|
|
12000
|
-
var
|
|
12003
|
+
// src/data-structures/binary-tree/avl-tree-multi-map.ts
|
|
12004
|
+
var AVLTreeMultiMapNode = class extends AVLTreeNode {
|
|
12001
12005
|
/**
|
|
12002
12006
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
12003
12007
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -12029,7 +12033,7 @@ var dataStructureTyped = (() => {
|
|
|
12029
12033
|
this._count = value;
|
|
12030
12034
|
}
|
|
12031
12035
|
};
|
|
12032
|
-
var
|
|
12036
|
+
var AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
12033
12037
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
12034
12038
|
super([], options);
|
|
12035
12039
|
__publicField(this, "_count", 0);
|
|
@@ -12057,19 +12061,19 @@ var dataStructureTyped = (() => {
|
|
|
12057
12061
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
12058
12062
|
*/
|
|
12059
12063
|
createNode(key, value, count) {
|
|
12060
|
-
return new
|
|
12064
|
+
return new AVLTreeMultiMapNode(key, value, count);
|
|
12061
12065
|
}
|
|
12062
12066
|
/**
|
|
12063
|
-
* The function creates a new
|
|
12067
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
12064
12068
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
12065
|
-
* configuration options for creating the `
|
|
12069
|
+
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
12066
12070
|
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
12067
12071
|
* the tree, respectively. These properties can be
|
|
12068
|
-
* @returns a new instance of the `
|
|
12072
|
+
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
12069
12073
|
* default options. The returned value is casted as `TREE`.
|
|
12070
12074
|
*/
|
|
12071
12075
|
createTree(options) {
|
|
12072
|
-
return new
|
|
12076
|
+
return new _AVLTreeMultiMap([], __spreadValues({
|
|
12073
12077
|
iterationType: this.iterationType,
|
|
12074
12078
|
variant: this.variant
|
|
12075
12079
|
}, options));
|
|
@@ -12106,13 +12110,13 @@ var dataStructureTyped = (() => {
|
|
|
12106
12110
|
return node;
|
|
12107
12111
|
}
|
|
12108
12112
|
/**
|
|
12109
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the
|
|
12113
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
12110
12114
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
12111
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the
|
|
12115
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
12112
12116
|
* class.
|
|
12113
12117
|
*/
|
|
12114
12118
|
isNode(keyOrNodeOrEntry) {
|
|
12115
|
-
return keyOrNodeOrEntry instanceof
|
|
12119
|
+
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
12116
12120
|
}
|
|
12117
12121
|
/**
|
|
12118
12122
|
* Time Complexity: O(log n)
|
|
@@ -12344,6 +12348,372 @@ var dataStructureTyped = (() => {
|
|
|
12344
12348
|
}
|
|
12345
12349
|
};
|
|
12346
12350
|
|
|
12351
|
+
// src/data-structures/binary-tree/tree-multi-map.ts
|
|
12352
|
+
var TreeMultiMapNode = class extends RedBlackTreeNode {
|
|
12353
|
+
/**
|
|
12354
|
+
* The constructor function initializes an instance of a class with a key, value, and count.
|
|
12355
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the
|
|
12356
|
+
* constructor. It is required and must be provided when creating an instance of the class.
|
|
12357
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
12358
|
+
* value associated with the key in the constructor. If no value is provided, it will be `undefined`.
|
|
12359
|
+
* @param [count=1] - The "count" parameter is an optional parameter that specifies the number of
|
|
12360
|
+
* times the key-value pair should be repeated. If no value is provided for "count", it defaults to
|
|
12361
|
+
* 1.
|
|
12362
|
+
*/
|
|
12363
|
+
constructor(key, value, count = 1) {
|
|
12364
|
+
super(key, value);
|
|
12365
|
+
__publicField(this, "_count", 1);
|
|
12366
|
+
this.count = count;
|
|
12367
|
+
}
|
|
12368
|
+
/**
|
|
12369
|
+
* The function returns the value of the private variable _count.
|
|
12370
|
+
* @returns The count property of the object, which is of type number.
|
|
12371
|
+
*/
|
|
12372
|
+
get count() {
|
|
12373
|
+
return this._count;
|
|
12374
|
+
}
|
|
12375
|
+
/**
|
|
12376
|
+
* The above function sets the value of the count property.
|
|
12377
|
+
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
12378
|
+
* numeric value.
|
|
12379
|
+
*/
|
|
12380
|
+
set count(value) {
|
|
12381
|
+
this._count = value;
|
|
12382
|
+
}
|
|
12383
|
+
};
|
|
12384
|
+
var TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
12385
|
+
/**
|
|
12386
|
+
* The constructor function initializes a new instance of the TreeMultiMap class with optional
|
|
12387
|
+
* initial keys, nodes, or entries.
|
|
12388
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
12389
|
+
* contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
|
|
12390
|
+
* nodes, or entries.
|
|
12391
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
12392
|
+
* constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
|
|
12393
|
+
*/
|
|
12394
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
12395
|
+
super([], options);
|
|
12396
|
+
__publicField(this, "_count", 0);
|
|
12397
|
+
if (keysOrNodesOrEntries)
|
|
12398
|
+
this.addMany(keysOrNodesOrEntries);
|
|
12399
|
+
}
|
|
12400
|
+
// TODO the _count is not accurate after nodes count modified
|
|
12401
|
+
/**
|
|
12402
|
+
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
12403
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
12404
|
+
*/
|
|
12405
|
+
get count() {
|
|
12406
|
+
let sum = 0;
|
|
12407
|
+
this.dfs((node) => sum += node.count);
|
|
12408
|
+
return sum;
|
|
12409
|
+
}
|
|
12410
|
+
/**
|
|
12411
|
+
* The function creates a new TreeMultiMapNode object with the specified key, value, and count.
|
|
12412
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
12413
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
12414
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
12415
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
12416
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
12417
|
+
* key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
|
|
12418
|
+
* default to 1.
|
|
12419
|
+
* @returns a new instance of the TreeMultiMapNode class, casted as NODE.
|
|
12420
|
+
*/
|
|
12421
|
+
createNode(key, value, count) {
|
|
12422
|
+
return new TreeMultiMapNode(key, value, count);
|
|
12423
|
+
}
|
|
12424
|
+
/**
|
|
12425
|
+
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
12426
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
12427
|
+
* configuration options for creating the `TreeMultiMap`. It can include properties such as
|
|
12428
|
+
* `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
|
|
12429
|
+
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
12430
|
+
* existing `iterationType` option. The returned value is casted as `TREE`.
|
|
12431
|
+
*/
|
|
12432
|
+
createTree(options) {
|
|
12433
|
+
return new _TreeMultiMap([], __spreadValues({
|
|
12434
|
+
iterationType: this.iterationType
|
|
12435
|
+
}, options));
|
|
12436
|
+
}
|
|
12437
|
+
/**
|
|
12438
|
+
* The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
|
|
12439
|
+
* is valid.
|
|
12440
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
12441
|
+
* NODE>`. It can accept three types of values:
|
|
12442
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
12443
|
+
* value associated with a key in a key-value pair.
|
|
12444
|
+
* @param [count=1] - The count parameter is an optional parameter that specifies the number of times
|
|
12445
|
+
* the key-value pair should be added to the node. If not provided, it defaults to 1.
|
|
12446
|
+
* @returns a NODE object or undefined.
|
|
12447
|
+
*/
|
|
12448
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
12449
|
+
let node;
|
|
12450
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
12451
|
+
return;
|
|
12452
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
12453
|
+
node = keyOrNodeOrEntry;
|
|
12454
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
12455
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
12456
|
+
if (key === void 0 || key === null) {
|
|
12457
|
+
return;
|
|
12458
|
+
} else {
|
|
12459
|
+
node = this.createNode(key, value2, count);
|
|
12460
|
+
}
|
|
12461
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
12462
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
12463
|
+
} else {
|
|
12464
|
+
return;
|
|
12465
|
+
}
|
|
12466
|
+
return node;
|
|
12467
|
+
}
|
|
12468
|
+
/**
|
|
12469
|
+
* The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
|
|
12470
|
+
* class.
|
|
12471
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
12472
|
+
* NODE>`.
|
|
12473
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
|
|
12474
|
+
* of the `TreeMultiMapNode` class.
|
|
12475
|
+
*/
|
|
12476
|
+
isNode(keyOrNodeOrEntry) {
|
|
12477
|
+
return keyOrNodeOrEntry instanceof TreeMultiMapNode;
|
|
12478
|
+
}
|
|
12479
|
+
/**
|
|
12480
|
+
* Time Complexity: O(log n)
|
|
12481
|
+
* Space Complexity: O(1)
|
|
12482
|
+
*/
|
|
12483
|
+
/**
|
|
12484
|
+
* Time Complexity: O(log n)
|
|
12485
|
+
* Space Complexity: O(1)
|
|
12486
|
+
*
|
|
12487
|
+
* The function overrides the add method in TypeScript and adds a new node to the data structure.
|
|
12488
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
12489
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
12490
|
+
* data structure.
|
|
12491
|
+
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
12492
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
12493
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
12494
|
+
* @returns a boolean value.
|
|
12495
|
+
*/
|
|
12496
|
+
add(keyOrNodeOrEntry, value, count = 1) {
|
|
12497
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
|
|
12498
|
+
if (newNode === void 0)
|
|
12499
|
+
return false;
|
|
12500
|
+
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
12501
|
+
const inserted = super.add(newNode);
|
|
12502
|
+
if (inserted) {
|
|
12503
|
+
this._count += orgNodeCount;
|
|
12504
|
+
}
|
|
12505
|
+
return true;
|
|
12506
|
+
}
|
|
12507
|
+
/**
|
|
12508
|
+
* Time Complexity: O(log n)
|
|
12509
|
+
* Space Complexity: O(1)
|
|
12510
|
+
*/
|
|
12511
|
+
/**
|
|
12512
|
+
* Time Complexity: O(log n)
|
|
12513
|
+
* Space Complexity: O(1)
|
|
12514
|
+
*
|
|
12515
|
+
* The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
|
|
12516
|
+
* given identifier, and it returns an array of results containing information about the deleted
|
|
12517
|
+
* nodes.
|
|
12518
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
|
|
12519
|
+
* to identify the node to be deleted. It can be of any type that is returned by the callback
|
|
12520
|
+
* function. It can also be null or undefined if no node needs to be deleted.
|
|
12521
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
12522
|
+
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
12523
|
+
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is
|
|
12524
|
+
* used
|
|
12525
|
+
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
12526
|
+
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
12527
|
+
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
12528
|
+
* target node will be decremented
|
|
12529
|
+
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
12530
|
+
*/
|
|
12531
|
+
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
|
|
12532
|
+
const deleteResults = [];
|
|
12533
|
+
if (identifier === null)
|
|
12534
|
+
return deleteResults;
|
|
12535
|
+
const deleteHelper = (node) => {
|
|
12536
|
+
let targetNode = this._Sentinel;
|
|
12537
|
+
let currentNode;
|
|
12538
|
+
while (node !== this._Sentinel) {
|
|
12539
|
+
if (node && callback(node) === identifier) {
|
|
12540
|
+
targetNode = node;
|
|
12541
|
+
}
|
|
12542
|
+
if (node && identifier && callback(node) <= identifier) {
|
|
12543
|
+
node = node.right;
|
|
12544
|
+
} else {
|
|
12545
|
+
node = node == null ? void 0 : node.left;
|
|
12546
|
+
}
|
|
12547
|
+
}
|
|
12548
|
+
if (targetNode === this._Sentinel) {
|
|
12549
|
+
return;
|
|
12550
|
+
}
|
|
12551
|
+
if (ignoreCount || targetNode.count <= 1) {
|
|
12552
|
+
let parentNode = targetNode;
|
|
12553
|
+
let parentNodeOriginalColor = parentNode.color;
|
|
12554
|
+
if (targetNode.left === this._Sentinel) {
|
|
12555
|
+
currentNode = targetNode.right;
|
|
12556
|
+
this._rbTransplant(targetNode, targetNode.right);
|
|
12557
|
+
} else if (targetNode.right === this._Sentinel) {
|
|
12558
|
+
currentNode = targetNode.left;
|
|
12559
|
+
this._rbTransplant(targetNode, targetNode.left);
|
|
12560
|
+
} else {
|
|
12561
|
+
parentNode = this.getLeftMost(targetNode.right);
|
|
12562
|
+
parentNodeOriginalColor = parentNode.color;
|
|
12563
|
+
currentNode = parentNode.right;
|
|
12564
|
+
if (parentNode.parent === targetNode) {
|
|
12565
|
+
currentNode.parent = parentNode;
|
|
12566
|
+
} else {
|
|
12567
|
+
this._rbTransplant(parentNode, parentNode.right);
|
|
12568
|
+
parentNode.right = targetNode.right;
|
|
12569
|
+
parentNode.right.parent = parentNode;
|
|
12570
|
+
}
|
|
12571
|
+
this._rbTransplant(targetNode, parentNode);
|
|
12572
|
+
parentNode.left = targetNode.left;
|
|
12573
|
+
parentNode.left.parent = parentNode;
|
|
12574
|
+
parentNode.color = targetNode.color;
|
|
12575
|
+
}
|
|
12576
|
+
if (parentNodeOriginalColor === 0 /* BLACK */) {
|
|
12577
|
+
this._fixDelete(currentNode);
|
|
12578
|
+
}
|
|
12579
|
+
this._size--;
|
|
12580
|
+
this._count -= targetNode.count;
|
|
12581
|
+
deleteResults.push({ deleted: targetNode, needBalanced: void 0 });
|
|
12582
|
+
} else {
|
|
12583
|
+
targetNode.count--;
|
|
12584
|
+
this._count--;
|
|
12585
|
+
}
|
|
12586
|
+
};
|
|
12587
|
+
deleteHelper(this.root);
|
|
12588
|
+
return deleteResults;
|
|
12589
|
+
}
|
|
12590
|
+
/**
|
|
12591
|
+
* Time Complexity: O(1)
|
|
12592
|
+
* Space Complexity: O(1)
|
|
12593
|
+
*/
|
|
12594
|
+
/**
|
|
12595
|
+
* Time Complexity: O(1)
|
|
12596
|
+
* Space Complexity: O(1)
|
|
12597
|
+
*
|
|
12598
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
12599
|
+
* zero.
|
|
12600
|
+
*/
|
|
12601
|
+
clear() {
|
|
12602
|
+
super.clear();
|
|
12603
|
+
this._count = 0;
|
|
12604
|
+
}
|
|
12605
|
+
/**
|
|
12606
|
+
* Time Complexity: O(n log n)
|
|
12607
|
+
* Space Complexity: O(log n)
|
|
12608
|
+
*/
|
|
12609
|
+
/**
|
|
12610
|
+
* Time Complexity: O(n log n)
|
|
12611
|
+
* Space Complexity: O(log n)
|
|
12612
|
+
*
|
|
12613
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
12614
|
+
* tree using either a recursive or iterative approach.
|
|
12615
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
12616
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
12617
|
+
* values:
|
|
12618
|
+
* @returns a boolean value.
|
|
12619
|
+
*/
|
|
12620
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
12621
|
+
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
12622
|
+
if (sorted.length < 1)
|
|
12623
|
+
return false;
|
|
12624
|
+
this.clear();
|
|
12625
|
+
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
12626
|
+
const buildBalanceBST = (l, r) => {
|
|
12627
|
+
if (l > r)
|
|
12628
|
+
return;
|
|
12629
|
+
const m = l + Math.floor((r - l) / 2);
|
|
12630
|
+
const midNode = sorted[m];
|
|
12631
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
12632
|
+
buildBalanceBST(l, m - 1);
|
|
12633
|
+
buildBalanceBST(m + 1, r);
|
|
12634
|
+
};
|
|
12635
|
+
buildBalanceBST(0, n - 1);
|
|
12636
|
+
return true;
|
|
12637
|
+
} else {
|
|
12638
|
+
const stack = [[0, n - 1]];
|
|
12639
|
+
while (stack.length > 0) {
|
|
12640
|
+
const popped = stack.pop();
|
|
12641
|
+
if (popped) {
|
|
12642
|
+
const [l, r] = popped;
|
|
12643
|
+
if (l <= r) {
|
|
12644
|
+
const m = l + Math.floor((r - l) / 2);
|
|
12645
|
+
const midNode = sorted[m];
|
|
12646
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
12647
|
+
stack.push([m + 1, r]);
|
|
12648
|
+
stack.push([l, m - 1]);
|
|
12649
|
+
}
|
|
12650
|
+
}
|
|
12651
|
+
}
|
|
12652
|
+
return true;
|
|
12653
|
+
}
|
|
12654
|
+
}
|
|
12655
|
+
/**
|
|
12656
|
+
* Time complexity: O(n)
|
|
12657
|
+
* Space complexity: O(n)
|
|
12658
|
+
*/
|
|
12659
|
+
/**
|
|
12660
|
+
* Time complexity: O(n)
|
|
12661
|
+
* Space complexity: O(n)
|
|
12662
|
+
*
|
|
12663
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
12664
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
12665
|
+
*/
|
|
12666
|
+
clone() {
|
|
12667
|
+
const cloned = this.createTree();
|
|
12668
|
+
this.bfs((node) => cloned.add(node.key, node.value, node.count));
|
|
12669
|
+
return cloned;
|
|
12670
|
+
}
|
|
12671
|
+
/**
|
|
12672
|
+
* The function swaps the properties of two nodes in a binary search tree.
|
|
12673
|
+
* @param srcNode - The source node that needs to be swapped with the destination node. It can be
|
|
12674
|
+
* either a key or a node object.
|
|
12675
|
+
* @param destNode - The `destNode` parameter is the node in the binary search tree where the
|
|
12676
|
+
* properties will be swapped with the `srcNode`.
|
|
12677
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
12678
|
+
* If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
|
|
12679
|
+
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
12680
|
+
* If either `srcNode` or `destNode` is
|
|
12681
|
+
*/
|
|
12682
|
+
_swapProperties(srcNode, destNode) {
|
|
12683
|
+
srcNode = this.ensureNode(srcNode);
|
|
12684
|
+
destNode = this.ensureNode(destNode);
|
|
12685
|
+
if (srcNode && destNode) {
|
|
12686
|
+
const { key, value, count, color } = destNode;
|
|
12687
|
+
const tempNode = this.createNode(key, value, count);
|
|
12688
|
+
if (tempNode) {
|
|
12689
|
+
tempNode.color = color;
|
|
12690
|
+
destNode.key = srcNode.key;
|
|
12691
|
+
destNode.value = srcNode.value;
|
|
12692
|
+
destNode.count = srcNode.count;
|
|
12693
|
+
destNode.color = srcNode.color;
|
|
12694
|
+
srcNode.key = tempNode.key;
|
|
12695
|
+
srcNode.value = tempNode.value;
|
|
12696
|
+
srcNode.count = tempNode.count;
|
|
12697
|
+
srcNode.color = tempNode.color;
|
|
12698
|
+
}
|
|
12699
|
+
return destNode;
|
|
12700
|
+
}
|
|
12701
|
+
return void 0;
|
|
12702
|
+
}
|
|
12703
|
+
/**
|
|
12704
|
+
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
12705
|
+
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
|
|
12706
|
+
* needs to be replaced in the data structure.
|
|
12707
|
+
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
|
|
12708
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
12709
|
+
* superclass, after updating the `count` property of the `newNode` object.
|
|
12710
|
+
*/
|
|
12711
|
+
_replaceNode(oldNode, newNode) {
|
|
12712
|
+
newNode.count = oldNode.count + newNode.count;
|
|
12713
|
+
return super._replaceNode(oldNode, newNode);
|
|
12714
|
+
}
|
|
12715
|
+
};
|
|
12716
|
+
|
|
12347
12717
|
// src/data-structures/tree/tree.ts
|
|
12348
12718
|
var TreeNode = class _TreeNode {
|
|
12349
12719
|
/**
|