graph-typed 1.52.9 → 1.53.0
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/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -46
- package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/data-structures/binary-tree/binary-tree.js +375 -264
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +105 -77
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +35 -33
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/tree-multi-map.js +58 -48
- package/dist/data-structures/trie/trie.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +59 -53
- package/src/data-structures/binary-tree/avl-tree.ts +31 -34
- package/src/data-structures/binary-tree/binary-tree.ts +439 -359
- package/src/data-structures/binary-tree/bst.ts +142 -112
- package/src/data-structures/binary-tree/rb-tree.ts +37 -41
- package/src/data-structures/binary-tree/tree-multi-map.ts +56 -60
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult,
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, IterationType } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -36,16 +36,16 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
36
36
|
/**
|
|
37
37
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
38
38
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K = any, V = any, R =
|
|
39
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMapNested<K, V, R, NODE>>> extends AVLTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
40
40
|
/**
|
|
41
41
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
42
|
-
* @param
|
|
42
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
43
43
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
44
44
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
45
45
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
46
46
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
47
47
|
*/
|
|
48
|
-
constructor(
|
|
48
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: AVLTreeMultiMapOptions<K, V, R>);
|
|
49
49
|
protected _count: number;
|
|
50
50
|
/**
|
|
51
51
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
@@ -84,17 +84,17 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE
|
|
|
84
84
|
createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE;
|
|
85
85
|
/**
|
|
86
86
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
87
|
-
* @param {
|
|
88
|
-
* `
|
|
89
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
87
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
88
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
89
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
90
90
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
91
91
|
*/
|
|
92
|
-
isNode(
|
|
92
|
+
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
93
93
|
/**
|
|
94
|
-
* The function `
|
|
94
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
95
95
|
* a node object.
|
|
96
|
-
* @param {
|
|
97
|
-
* `
|
|
96
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
97
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
98
98
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
99
99
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
100
100
|
* value is provided, it will default to `undefined`.
|
|
@@ -102,16 +102,16 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE
|
|
|
102
102
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
103
103
|
* @returns either a NODE object or undefined.
|
|
104
104
|
*/
|
|
105
|
-
|
|
105
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(log n)
|
|
108
108
|
* Space Complexity: O(1)
|
|
109
109
|
*
|
|
110
110
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
111
111
|
* and update the count.
|
|
112
|
-
* @param {
|
|
113
|
-
* `
|
|
114
|
-
* can also accept a value of type `
|
|
112
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
113
|
+
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
114
|
+
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
|
|
115
115
|
* entry, or raw element
|
|
116
116
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
117
117
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -120,14 +120,14 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE
|
|
|
120
120
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
121
121
|
* @returns a boolean value.
|
|
122
122
|
*/
|
|
123
|
-
add(
|
|
123
|
+
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): boolean;
|
|
124
124
|
/**
|
|
125
125
|
* Time Complexity: O(log n)
|
|
126
126
|
* Space Complexity: O(1)
|
|
127
127
|
*
|
|
128
128
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
129
129
|
* nodes and maintaining balance in the tree.
|
|
130
|
-
* @param {
|
|
130
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
131
131
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
132
132
|
* binary tree. It can be a key, node, or entry that determines which
|
|
133
133
|
* node(s) should be deleted.
|
|
@@ -140,7 +140,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE
|
|
|
140
140
|
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
141
141
|
* deleted node and whether balancing is needed in the tree.
|
|
142
142
|
*/
|
|
143
|
-
delete(
|
|
143
|
+
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
144
144
|
/**
|
|
145
145
|
* Time Complexity: O(1)
|
|
146
146
|
* Space Complexity: O(1)
|
|
@@ -176,14 +176,14 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE
|
|
|
176
176
|
*
|
|
177
177
|
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
178
178
|
* in a binary search tree.
|
|
179
|
-
* @param {R |
|
|
179
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
180
180
|
* that will be swapped with the `destNode`.
|
|
181
|
-
* @param {R |
|
|
181
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
182
182
|
* node where the properties will be swapped with the source node.
|
|
183
183
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
184
184
|
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
185
185
|
*/
|
|
186
|
-
protected _swapProperties(srcNode: R |
|
|
186
|
+
protected _swapProperties(srcNode: R | BSTNOptKeyOrNode<K, NODE>, destNode: R | BSTNOptKeyOrNode<K, NODE>): NODE | undefined;
|
|
187
187
|
/**
|
|
188
188
|
* Time Complexity: O(1)
|
|
189
189
|
* Space Complexity: O(1)
|
|
@@ -41,17 +41,17 @@ exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
|
|
|
41
41
|
class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
42
42
|
/**
|
|
43
43
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
44
|
-
* @param
|
|
44
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
45
45
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
46
46
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
47
47
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
48
48
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
49
49
|
*/
|
|
50
|
-
constructor(
|
|
50
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
51
51
|
super([], options);
|
|
52
52
|
this._count = 0;
|
|
53
|
-
if (
|
|
54
|
-
this.addMany(
|
|
53
|
+
if (keysNodesEntriesOrRaws)
|
|
54
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
57
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
@@ -96,23 +96,23 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
96
96
|
* object.
|
|
97
97
|
*/
|
|
98
98
|
createTree(options) {
|
|
99
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
99
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
102
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
103
|
-
* @param {
|
|
104
|
-
* `
|
|
105
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
103
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
104
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
105
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
106
106
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
107
107
|
*/
|
|
108
|
-
isNode(
|
|
109
|
-
return
|
|
108
|
+
isNode(keyNodeEntryOrRaw) {
|
|
109
|
+
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* The function `
|
|
112
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
113
113
|
* a node object.
|
|
114
|
-
* @param {
|
|
115
|
-
* `
|
|
114
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
115
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
116
116
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
117
117
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
118
118
|
* value is provided, it will default to `undefined`.
|
|
@@ -120,26 +120,30 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
120
120
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
121
121
|
* @returns either a NODE object or undefined.
|
|
122
122
|
*/
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
return;
|
|
126
|
-
if (this.isNode(
|
|
127
|
-
return
|
|
128
|
-
if (this.isEntry(
|
|
129
|
-
const [key, entryValue] =
|
|
123
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
124
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
125
|
+
return [undefined, undefined];
|
|
126
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
127
|
+
return [keyNodeEntryOrRaw, value];
|
|
128
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
129
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
130
130
|
if (key === undefined || key === null)
|
|
131
|
-
return;
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
return [undefined, undefined];
|
|
132
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
133
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
134
134
|
}
|
|
135
|
-
if (this.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
136
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
137
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
138
|
+
if (this._toEntryFn) {
|
|
139
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
140
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
141
|
+
if (this.isKey(key))
|
|
142
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
143
|
+
}
|
|
144
|
+
return [undefined, undefined];
|
|
139
145
|
}
|
|
140
|
-
|
|
141
|
-
return this.createNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
142
|
-
return;
|
|
146
|
+
return [undefined, undefined];
|
|
143
147
|
}
|
|
144
148
|
/**
|
|
145
149
|
* Time Complexity: O(log n)
|
|
@@ -147,9 +151,9 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
147
151
|
*
|
|
148
152
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
149
153
|
* and update the count.
|
|
150
|
-
* @param {
|
|
151
|
-
* `
|
|
152
|
-
* can also accept a value of type `
|
|
154
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
155
|
+
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
156
|
+
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
|
|
153
157
|
* entry, or raw element
|
|
154
158
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
155
159
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -158,12 +162,12 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
158
162
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
159
163
|
* @returns a boolean value.
|
|
160
164
|
*/
|
|
161
|
-
add(
|
|
162
|
-
const newNode = this.
|
|
165
|
+
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
166
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
163
167
|
if (newNode === undefined)
|
|
164
168
|
return false;
|
|
165
169
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
166
|
-
const inserted = super.add(newNode);
|
|
170
|
+
const inserted = super.add(newNode, newValue);
|
|
167
171
|
if (inserted) {
|
|
168
172
|
this._count += orgNodeCount;
|
|
169
173
|
}
|
|
@@ -175,7 +179,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
175
179
|
*
|
|
176
180
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
177
181
|
* nodes and maintaining balance in the tree.
|
|
178
|
-
* @param {
|
|
182
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
179
183
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
180
184
|
* binary tree. It can be a key, node, or entry that determines which
|
|
181
185
|
* node(s) should be deleted.
|
|
@@ -188,12 +192,12 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
188
192
|
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
189
193
|
* deleted node and whether balancing is needed in the tree.
|
|
190
194
|
*/
|
|
191
|
-
delete(
|
|
195
|
+
delete(keyNodeEntryOrRaw, ignoreCount = false) {
|
|
192
196
|
var _a;
|
|
193
197
|
const deletedResult = [];
|
|
194
198
|
if (!this.root)
|
|
195
199
|
return deletedResult;
|
|
196
|
-
const curr = (_a = this.getNode(
|
|
200
|
+
const curr = (_a = this.getNode(keyNodeEntryOrRaw)) !== null && _a !== void 0 ? _a : undefined;
|
|
197
201
|
if (!curr)
|
|
198
202
|
return deletedResult;
|
|
199
203
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
|
|
@@ -280,7 +284,10 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
280
284
|
return;
|
|
281
285
|
const m = l + Math.floor((r - l) / 2);
|
|
282
286
|
const midNode = sorted[m];
|
|
283
|
-
this.
|
|
287
|
+
if (this._isMapMode)
|
|
288
|
+
this.add(midNode.key, undefined, midNode.count);
|
|
289
|
+
else
|
|
290
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
284
291
|
buildBalanceBST(l, m - 1);
|
|
285
292
|
buildBalanceBST(m + 1, r);
|
|
286
293
|
};
|
|
@@ -296,7 +303,10 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
296
303
|
if (l <= r) {
|
|
297
304
|
const m = l + Math.floor((r - l) / 2);
|
|
298
305
|
const midNode = sorted[m];
|
|
299
|
-
this.
|
|
306
|
+
if (this._isMapMode)
|
|
307
|
+
this.add(midNode.key, undefined, midNode.count);
|
|
308
|
+
else
|
|
309
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
300
310
|
stack.push([m + 1, r]);
|
|
301
311
|
stack.push([l, m - 1]);
|
|
302
312
|
}
|
|
@@ -314,7 +324,12 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
314
324
|
*/
|
|
315
325
|
clone() {
|
|
316
326
|
const cloned = this.createTree();
|
|
317
|
-
|
|
327
|
+
if (this._isMapMode)
|
|
328
|
+
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
329
|
+
else
|
|
330
|
+
this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
331
|
+
if (this._isMapMode)
|
|
332
|
+
cloned._store = this._store;
|
|
318
333
|
return cloned;
|
|
319
334
|
}
|
|
320
335
|
/**
|
|
@@ -323,9 +338,9 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
323
338
|
*
|
|
324
339
|
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
325
340
|
* in a binary search tree.
|
|
326
|
-
* @param {R |
|
|
341
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
327
342
|
* that will be swapped with the `destNode`.
|
|
328
|
-
* @param {R |
|
|
343
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
329
344
|
* node where the properties will be swapped with the source node.
|
|
330
345
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
331
346
|
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
@@ -339,11 +354,13 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
339
354
|
if (tempNode) {
|
|
340
355
|
tempNode.height = height;
|
|
341
356
|
destNode.key = srcNode.key;
|
|
342
|
-
|
|
357
|
+
if (!this._isMapMode)
|
|
358
|
+
destNode.value = srcNode.value;
|
|
343
359
|
destNode.count = srcNode.count;
|
|
344
360
|
destNode.height = srcNode.height;
|
|
345
361
|
srcNode.key = tempNode.key;
|
|
346
|
-
|
|
362
|
+
if (!this._isMapMode)
|
|
363
|
+
srcNode.value = tempNode.value;
|
|
347
364
|
srcNode.count = tempNode.count;
|
|
348
365
|
srcNode.height = tempNode.height;
|
|
349
366
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult,
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
@@ -40,11 +40,11 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
40
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
41
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
42
42
|
*/
|
|
43
|
-
export declare class AVLTree<K = any, V = any, R =
|
|
43
|
+
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
44
44
|
/**
|
|
45
45
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
46
46
|
* entries, or raw elements.
|
|
47
|
-
* @param
|
|
47
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
48
48
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
49
49
|
* be used to initialize the AVLTree.
|
|
50
50
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -52,7 +52,7 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
52
52
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
53
53
|
* `nodeBuilder` (
|
|
54
54
|
*/
|
|
55
|
-
constructor(
|
|
55
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: AVLTreeOptions<K, V, R>);
|
|
56
56
|
/**
|
|
57
57
|
* The function creates a new AVL tree node with the given key and value.
|
|
58
58
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
@@ -73,54 +73,54 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
73
73
|
createTree(options?: AVLTreeOptions<K, V, R>): TREE;
|
|
74
74
|
/**
|
|
75
75
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
76
|
-
* @param {
|
|
77
|
-
* `
|
|
78
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
76
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
77
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
78
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
79
79
|
* an instance of the `AVLTreeNode` class.
|
|
80
80
|
*/
|
|
81
|
-
isNode(
|
|
81
|
+
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
82
82
|
/**
|
|
83
83
|
* Time Complexity: O(log n)
|
|
84
84
|
* Space Complexity: O(1)
|
|
85
85
|
*
|
|
86
86
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
87
87
|
* structure, then balances the path.
|
|
88
|
-
* @param {
|
|
89
|
-
* `
|
|
88
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
89
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
90
90
|
* `RawElement`.
|
|
91
91
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
92
92
|
* the key or node being added to the data structure.
|
|
93
93
|
* @returns The method is returning a boolean value.
|
|
94
94
|
*/
|
|
95
|
-
add(
|
|
95
|
+
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean;
|
|
96
96
|
/**
|
|
97
97
|
* Time Complexity: O(log n)
|
|
98
98
|
* Space Complexity: O(1)
|
|
99
99
|
*
|
|
100
100
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
101
101
|
* balances the tree if necessary.
|
|
102
|
-
* @param {
|
|
102
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
103
103
|
* parameter in the `override delete` method can be one of the following types:
|
|
104
104
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
105
105
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
106
106
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
107
107
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
108
108
|
*/
|
|
109
|
-
delete(
|
|
109
|
+
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
110
110
|
/**
|
|
111
111
|
* Time Complexity: O(1)
|
|
112
112
|
* Space Complexity: O(1)
|
|
113
113
|
*
|
|
114
114
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
115
115
|
* binary search tree.
|
|
116
|
-
* @param {R |
|
|
116
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
117
117
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
118
|
-
* @param {R |
|
|
119
|
-
* `R` or an instance of `
|
|
118
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
119
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
120
120
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
121
121
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
122
122
|
*/
|
|
123
|
-
protected _swapProperties(srcNode: R |
|
|
123
|
+
protected _swapProperties(srcNode: R | BSTNOptKeyOrNode<K, NODE>, destNode: R | BSTNOptKeyOrNode<K, NODE>): NODE | undefined;
|
|
124
124
|
/**
|
|
125
125
|
* Time Complexity: O(1)
|
|
126
126
|
* Space Complexity: O(1)
|
|
@@ -179,10 +179,10 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
179
179
|
*
|
|
180
180
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
181
181
|
* to restore balance in an AVL tree after inserting a node.
|
|
182
|
-
* @param {
|
|
183
|
-
* `
|
|
182
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
183
|
+
* `BTNRep<K, V, NODE>`.
|
|
184
184
|
*/
|
|
185
|
-
protected _balancePath(node:
|
|
185
|
+
protected _balancePath(node: BTNRep<K, V, NODE> | R): void;
|
|
186
186
|
/**
|
|
187
187
|
* Time Complexity: O(1)
|
|
188
188
|
* Space Complexity: O(1)
|
|
@@ -52,7 +52,7 @@ class AVLTree extends bst_1.BST {
|
|
|
52
52
|
/**
|
|
53
53
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
54
54
|
* entries, or raw elements.
|
|
55
|
-
* @param
|
|
55
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
56
56
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
57
57
|
* be used to initialize the AVLTree.
|
|
58
58
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -60,10 +60,10 @@ class AVLTree extends bst_1.BST {
|
|
|
60
60
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
61
61
|
* `nodeBuilder` (
|
|
62
62
|
*/
|
|
63
|
-
constructor(
|
|
63
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
64
64
|
super([], options);
|
|
65
|
-
if (
|
|
66
|
-
super.addMany(
|
|
65
|
+
if (keysNodesEntriesOrRaws)
|
|
66
|
+
super.addMany(keysNodesEntriesOrRaws);
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* The function creates a new AVL tree node with the given key and value.
|
|
@@ -85,17 +85,17 @@ class AVLTree extends bst_1.BST {
|
|
|
85
85
|
* @returns a new AVLTree object.
|
|
86
86
|
*/
|
|
87
87
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
88
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
92
|
-
* @param {
|
|
93
|
-
* `
|
|
94
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
92
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
93
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
94
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
95
95
|
* an instance of the `AVLTreeNode` class.
|
|
96
96
|
*/
|
|
97
|
-
isNode(
|
|
98
|
-
return
|
|
97
|
+
isNode(keyNodeEntryOrRaw) {
|
|
98
|
+
return keyNodeEntryOrRaw instanceof AVLTreeNode;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
101
|
* Time Complexity: O(log n)
|
|
@@ -103,19 +103,19 @@ class AVLTree extends bst_1.BST {
|
|
|
103
103
|
*
|
|
104
104
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
105
105
|
* structure, then balances the path.
|
|
106
|
-
* @param {
|
|
107
|
-
* `
|
|
106
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
107
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
108
108
|
* `RawElement`.
|
|
109
109
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
110
110
|
* the key or node being added to the data structure.
|
|
111
111
|
* @returns The method is returning a boolean value.
|
|
112
112
|
*/
|
|
113
|
-
add(
|
|
114
|
-
if (
|
|
113
|
+
add(keyNodeEntryOrRaw, value) {
|
|
114
|
+
if (keyNodeEntryOrRaw === null)
|
|
115
115
|
return false;
|
|
116
|
-
const inserted = super.add(
|
|
116
|
+
const inserted = super.add(keyNodeEntryOrRaw, value);
|
|
117
117
|
if (inserted)
|
|
118
|
-
this._balancePath(
|
|
118
|
+
this._balancePath(keyNodeEntryOrRaw);
|
|
119
119
|
return inserted;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
@@ -124,15 +124,15 @@ class AVLTree extends bst_1.BST {
|
|
|
124
124
|
*
|
|
125
125
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
126
126
|
* balances the tree if necessary.
|
|
127
|
-
* @param {
|
|
127
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
128
128
|
* parameter in the `override delete` method can be one of the following types:
|
|
129
129
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
130
130
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
131
131
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
132
132
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
133
133
|
*/
|
|
134
|
-
delete(
|
|
135
|
-
const deletedResults = super.delete(
|
|
134
|
+
delete(keyNodeEntryOrRaw) {
|
|
135
|
+
const deletedResults = super.delete(keyNodeEntryOrRaw);
|
|
136
136
|
for (const { needBalanced } of deletedResults) {
|
|
137
137
|
if (needBalanced) {
|
|
138
138
|
this._balancePath(needBalanced);
|
|
@@ -146,10 +146,10 @@ class AVLTree extends bst_1.BST {
|
|
|
146
146
|
*
|
|
147
147
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
148
148
|
* binary search tree.
|
|
149
|
-
* @param {R |
|
|
149
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
150
150
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
151
|
-
* @param {R |
|
|
152
|
-
* `R` or an instance of `
|
|
151
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
152
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
153
153
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
154
154
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
155
155
|
*/
|
|
@@ -162,10 +162,12 @@ class AVLTree extends bst_1.BST {
|
|
|
162
162
|
if (tempNode) {
|
|
163
163
|
tempNode.height = height;
|
|
164
164
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
165
|
-
|
|
165
|
+
if (!this._isMapMode)
|
|
166
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
166
167
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
167
168
|
srcNodeEnsured.key = tempNode.key;
|
|
168
|
-
|
|
169
|
+
if (!this._isMapMode)
|
|
170
|
+
srcNodeEnsured.value = tempNode.value;
|
|
169
171
|
srcNodeEnsured.height = tempNode.height;
|
|
170
172
|
}
|
|
171
173
|
return destNodeEnsured;
|
|
@@ -402,8 +404,8 @@ class AVLTree extends bst_1.BST {
|
|
|
402
404
|
*
|
|
403
405
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
404
406
|
* to restore balance in an AVL tree after inserting a node.
|
|
405
|
-
* @param {
|
|
406
|
-
* `
|
|
407
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
408
|
+
* `BTNRep<K, V, NODE>`.
|
|
407
409
|
*/
|
|
408
410
|
_balancePath(node) {
|
|
409
411
|
node = this.ensureNode(node);
|