deque-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
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
|
-
|
|
3
|
+
BTNRep,
|
|
4
4
|
CRUD,
|
|
5
|
-
|
|
5
|
+
OptNode,
|
|
6
6
|
RBTNColor,
|
|
7
7
|
RBTreeOptions,
|
|
8
8
|
RedBlackTreeNested,
|
|
9
|
-
RedBlackTreeNodeNested
|
|
10
|
-
BTNEntry
|
|
9
|
+
RedBlackTreeNodeNested
|
|
11
10
|
} from '../../types';
|
|
12
11
|
import { BST, BSTNode } from './bst';
|
|
13
12
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -55,7 +54,7 @@ export class RedBlackTreeNode<
|
|
|
55
54
|
export class RedBlackTree<
|
|
56
55
|
K = any,
|
|
57
56
|
V = any,
|
|
58
|
-
R =
|
|
57
|
+
R = object,
|
|
59
58
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
60
59
|
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
61
60
|
>
|
|
@@ -64,7 +63,7 @@ export class RedBlackTree<
|
|
|
64
63
|
{
|
|
65
64
|
/**
|
|
66
65
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
67
|
-
* @param
|
|
66
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
68
67
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
69
68
|
* initialize the RBTree with the provided elements.
|
|
70
69
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -72,16 +71,13 @@ export class RedBlackTree<
|
|
|
72
71
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
73
72
|
* depend on the implementation
|
|
74
73
|
*/
|
|
75
|
-
constructor(
|
|
76
|
-
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
77
|
-
options?: RBTreeOptions<K, V, R>
|
|
78
|
-
) {
|
|
74
|
+
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RBTreeOptions<K, V, R>) {
|
|
79
75
|
super([], options);
|
|
80
76
|
|
|
81
77
|
this._root = this.NIL;
|
|
82
78
|
|
|
83
|
-
if (
|
|
84
|
-
this.addMany(
|
|
79
|
+
if (keysNodesEntriesOrRaws) {
|
|
80
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
85
81
|
}
|
|
86
82
|
}
|
|
87
83
|
|
|
@@ -122,6 +118,7 @@ export class RedBlackTree<
|
|
|
122
118
|
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
|
|
123
119
|
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
124
120
|
iterationType: this.iterationType,
|
|
121
|
+
isMapMode: this._isMapMode,
|
|
125
122
|
comparator: this._comparator,
|
|
126
123
|
toEntryFn: this._toEntryFn,
|
|
127
124
|
...options
|
|
@@ -133,13 +130,13 @@ export class RedBlackTree<
|
|
|
133
130
|
* Space Complexity: O(1)
|
|
134
131
|
*
|
|
135
132
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
136
|
-
* @param {
|
|
137
|
-
* `
|
|
138
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
133
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
134
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
135
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
139
136
|
* an instance of the `RedBlackTreeNode` class.
|
|
140
137
|
*/
|
|
141
|
-
override isNode(
|
|
142
|
-
return
|
|
138
|
+
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
139
|
+
return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
|
|
143
140
|
}
|
|
144
141
|
|
|
145
142
|
// /**
|
|
@@ -151,29 +148,29 @@ export class RedBlackTree<
|
|
|
151
148
|
// * Time Complexity: O(1)
|
|
152
149
|
// * Space Complexity: O(1)
|
|
153
150
|
// *
|
|
154
|
-
// * The function `
|
|
151
|
+
// * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
155
152
|
// * valid, otherwise it returns undefined.
|
|
156
|
-
// * @param {
|
|
157
|
-
// * @param {V} [value] - The value associated with the key (if `
|
|
153
|
+
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
154
|
+
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
158
155
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
159
156
|
// */
|
|
160
|
-
// override
|
|
157
|
+
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
161
158
|
//
|
|
162
|
-
// if (
|
|
163
|
-
// if (this.isNode(
|
|
159
|
+
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
160
|
+
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
164
161
|
//
|
|
165
162
|
// if (this._toEntryFn) {
|
|
166
|
-
// const [key, entryValue] = this._toEntryFn(
|
|
167
|
-
// if (this.isKey(key)) return this.createNode(key,
|
|
163
|
+
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
164
|
+
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
168
165
|
// }
|
|
169
166
|
//
|
|
170
|
-
// if (this.isEntry(
|
|
171
|
-
// const [key, value] =
|
|
167
|
+
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
168
|
+
// const [key, value] = keyNodeEntryOrRaw;
|
|
172
169
|
// if (key === undefined || key === null) return;
|
|
173
170
|
// else return this.createNode(key, value, 'RED');
|
|
174
171
|
// }
|
|
175
172
|
//
|
|
176
|
-
// if (this.isKey(
|
|
173
|
+
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
177
174
|
//
|
|
178
175
|
// return ;
|
|
179
176
|
// }
|
|
@@ -196,8 +193,8 @@ export class RedBlackTree<
|
|
|
196
193
|
*
|
|
197
194
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
198
195
|
* added.
|
|
199
|
-
* @param {
|
|
200
|
-
* `
|
|
196
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
197
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
201
198
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
202
199
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
203
200
|
* structure.
|
|
@@ -205,8 +202,8 @@ export class RedBlackTree<
|
|
|
205
202
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
206
203
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
207
204
|
*/
|
|
208
|
-
override add(
|
|
209
|
-
const newNode = this.
|
|
205
|
+
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
|
|
206
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
210
207
|
if (!this.isRealNode(newNode)) return false;
|
|
211
208
|
|
|
212
209
|
const insertStatus = this._insert(newNode);
|
|
@@ -218,6 +215,7 @@ export class RedBlackTree<
|
|
|
218
215
|
} else {
|
|
219
216
|
return false;
|
|
220
217
|
}
|
|
218
|
+
if (this._isMapMode) this._setValue(newNode.key, newValue);
|
|
221
219
|
this._size++;
|
|
222
220
|
return true;
|
|
223
221
|
} else return insertStatus === 'UPDATED';
|
|
@@ -229,7 +227,7 @@ export class RedBlackTree<
|
|
|
229
227
|
*
|
|
230
228
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
231
229
|
* a given predicate and maintain the binary search tree properties.
|
|
232
|
-
* @param {
|
|
230
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
233
231
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
234
232
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
235
233
|
* function that determines which node(s) should be deleted.
|
|
@@ -237,16 +235,13 @@ export class RedBlackTree<
|
|
|
237
235
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
238
236
|
* balancing is needed.
|
|
239
237
|
*/
|
|
240
|
-
override delete(
|
|
241
|
-
if (
|
|
238
|
+
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
239
|
+
if (keyNodeEntryOrRaw === null) return [];
|
|
242
240
|
|
|
243
241
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
244
|
-
let nodeToDelete:
|
|
245
|
-
if (this.
|
|
246
|
-
else
|
|
247
|
-
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
248
|
-
? keyOrNodeOrEntryOrRaw
|
|
249
|
-
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
242
|
+
let nodeToDelete: OptNode<NODE>;
|
|
243
|
+
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
|
|
244
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
|
|
250
245
|
|
|
251
246
|
if (!nodeToDelete) {
|
|
252
247
|
return results;
|
|
@@ -287,6 +282,7 @@ export class RedBlackTree<
|
|
|
287
282
|
successor.color = nodeToDelete.color;
|
|
288
283
|
}
|
|
289
284
|
}
|
|
285
|
+
if (this._isMapMode) this._store.delete(nodeToDelete.key);
|
|
290
286
|
this._size--;
|
|
291
287
|
|
|
292
288
|
// If the original color was black, fix the tree
|
|
@@ -7,15 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
BSTNOptKeyOrNode,
|
|
11
|
+
BTNRep,
|
|
12
12
|
IterationType,
|
|
13
|
-
|
|
13
|
+
OptNode,
|
|
14
14
|
RBTNColor,
|
|
15
15
|
TreeMultiMapNested,
|
|
16
16
|
TreeMultiMapNodeNested,
|
|
17
|
-
TreeMultiMapOptions
|
|
18
|
-
BTNEntry
|
|
17
|
+
TreeMultiMapOptions
|
|
19
18
|
} from '../../types';
|
|
20
19
|
import { IBinaryTree } from '../../interfaces';
|
|
21
20
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
@@ -65,7 +64,7 @@ export class TreeMultiMapNode<
|
|
|
65
64
|
export class TreeMultiMap<
|
|
66
65
|
K = any,
|
|
67
66
|
V = any,
|
|
68
|
-
R =
|
|
67
|
+
R = object,
|
|
69
68
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
70
69
|
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
71
70
|
>
|
|
@@ -74,19 +73,16 @@ export class TreeMultiMap<
|
|
|
74
73
|
{
|
|
75
74
|
/**
|
|
76
75
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
77
|
-
* @param
|
|
76
|
+
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
|
|
78
77
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
79
78
|
* TreeMultiMap with initial data.
|
|
80
79
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
81
80
|
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
82
81
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
83
82
|
*/
|
|
84
|
-
constructor(
|
|
85
|
-
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
86
|
-
options?: TreeMultiMapOptions<K, V, R>
|
|
87
|
-
) {
|
|
83
|
+
constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE>> = [], options?: TreeMultiMapOptions<K, V, R>) {
|
|
88
84
|
super([], options);
|
|
89
|
-
if (
|
|
85
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
90
86
|
}
|
|
91
87
|
|
|
92
88
|
protected _count = 0;
|
|
@@ -142,6 +138,7 @@ export class TreeMultiMap<
|
|
|
142
138
|
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
|
|
143
139
|
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
144
140
|
iterationType: this.iterationType,
|
|
141
|
+
isMapMode: this._isMapMode,
|
|
145
142
|
comparator: this._comparator,
|
|
146
143
|
toEntryFn: this._toEntryFn,
|
|
147
144
|
...options
|
|
@@ -149,10 +146,10 @@ export class TreeMultiMap<
|
|
|
149
146
|
}
|
|
150
147
|
|
|
151
148
|
/**
|
|
152
|
-
* The function `
|
|
149
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
153
150
|
* node based on the input.
|
|
154
|
-
* @param {
|
|
155
|
-
* `
|
|
151
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
152
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
156
153
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
157
154
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
158
155
|
* an existing node.
|
|
@@ -160,40 +157,42 @@ export class TreeMultiMap<
|
|
|
160
157
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
161
158
|
* @returns either a NODE object or undefined.
|
|
162
159
|
*/
|
|
163
|
-
override
|
|
164
|
-
|
|
160
|
+
override keyValueNodeEntryRawToNodeAndValue(
|
|
161
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
165
162
|
value?: V,
|
|
166
163
|
count = 1
|
|
167
|
-
): NODE | undefined {
|
|
168
|
-
if (
|
|
164
|
+
): [NODE | undefined, V | undefined] {
|
|
165
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
169
166
|
|
|
170
|
-
if (this.isNode(
|
|
167
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
171
168
|
|
|
172
|
-
if (this.isEntry(
|
|
173
|
-
const [key, entryValue] =
|
|
174
|
-
if (key === undefined || key === null) return;
|
|
175
|
-
|
|
169
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
170
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
171
|
+
if (key === undefined || key === null) return [undefined, undefined];
|
|
172
|
+
const finalValue = value ?? entryValue;
|
|
173
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
176
174
|
}
|
|
177
175
|
|
|
178
176
|
if (this._toEntryFn) {
|
|
179
|
-
const [key, entryValue] = this._toEntryFn(
|
|
180
|
-
|
|
177
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
178
|
+
const finalValue = value ?? entryValue;
|
|
179
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
181
180
|
}
|
|
182
181
|
|
|
183
|
-
if (this.isKey(
|
|
182
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
184
183
|
|
|
185
|
-
return;
|
|
184
|
+
return [undefined, undefined];
|
|
186
185
|
}
|
|
187
186
|
|
|
188
187
|
/**
|
|
189
188
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
190
|
-
* @param {
|
|
191
|
-
* `
|
|
192
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
189
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
190
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
191
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
193
192
|
* an instance of the `TreeMultiMapNode` class.
|
|
194
193
|
*/
|
|
195
|
-
override isNode(
|
|
196
|
-
return
|
|
194
|
+
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
195
|
+
return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
|
|
197
196
|
}
|
|
198
197
|
|
|
199
198
|
/**
|
|
@@ -202,8 +201,8 @@ export class TreeMultiMap<
|
|
|
202
201
|
*
|
|
203
202
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
204
203
|
* the count and returning a boolean indicating success.
|
|
205
|
-
* @param {
|
|
206
|
-
* `
|
|
204
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
205
|
+
* `keyNodeEntryOrRaw` parameter can accept one of the following types:
|
|
207
206
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
208
207
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
209
208
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -212,10 +211,10 @@ export class TreeMultiMap<
|
|
|
212
211
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
213
212
|
* was successful, and false otherwise.
|
|
214
213
|
*/
|
|
215
|
-
override add(
|
|
216
|
-
const newNode = this.
|
|
214
|
+
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
215
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
217
216
|
const orgCount = newNode?.count || 0;
|
|
218
|
-
const isSuccessAdded = super.add(newNode);
|
|
217
|
+
const isSuccessAdded = super.add(newNode, newValue);
|
|
219
218
|
|
|
220
219
|
if (isSuccessAdded) {
|
|
221
220
|
this._count += orgCount;
|
|
@@ -231,7 +230,7 @@ export class TreeMultiMap<
|
|
|
231
230
|
*
|
|
232
231
|
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
233
232
|
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
234
|
-
* @param {
|
|
233
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
235
234
|
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
236
235
|
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
237
236
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
@@ -240,20 +239,14 @@ export class TreeMultiMap<
|
|
|
240
239
|
* `ignoreCount` is `false
|
|
241
240
|
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
242
241
|
*/
|
|
243
|
-
override delete(
|
|
244
|
-
|
|
245
|
-
ignoreCount = false
|
|
246
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
247
|
-
if (keyOrNodeOrEntryOrRaw === null) return [];
|
|
242
|
+
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] {
|
|
243
|
+
if (keyNodeEntryOrRaw === null) return [];
|
|
248
244
|
|
|
249
245
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
250
246
|
|
|
251
|
-
let nodeToDelete:
|
|
252
|
-
if (this.
|
|
253
|
-
else
|
|
254
|
-
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
255
|
-
? keyOrNodeOrEntryOrRaw
|
|
256
|
-
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
247
|
+
let nodeToDelete: OptNode<NODE>;
|
|
248
|
+
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
|
|
249
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
|
|
257
250
|
|
|
258
251
|
if (!nodeToDelete) {
|
|
259
252
|
return results;
|
|
@@ -374,7 +367,8 @@ export class TreeMultiMap<
|
|
|
374
367
|
if (l > r) return;
|
|
375
368
|
const m = l + Math.floor((r - l) / 2);
|
|
376
369
|
const midNode = sorted[m];
|
|
377
|
-
this.add(midNode.key,
|
|
370
|
+
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
371
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
378
372
|
buildBalanceBST(l, m - 1);
|
|
379
373
|
buildBalanceBST(m + 1, r);
|
|
380
374
|
};
|
|
@@ -390,7 +384,8 @@ export class TreeMultiMap<
|
|
|
390
384
|
if (l <= r) {
|
|
391
385
|
const m = l + Math.floor((r - l) / 2);
|
|
392
386
|
const midNode = sorted[m];
|
|
393
|
-
this.add(midNode.key,
|
|
387
|
+
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
388
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
394
389
|
stack.push([m + 1, r]);
|
|
395
390
|
stack.push([l, m - 1]);
|
|
396
391
|
}
|
|
@@ -409,7 +404,8 @@ export class TreeMultiMap<
|
|
|
409
404
|
*/
|
|
410
405
|
override clone(): TREE {
|
|
411
406
|
const cloned = this.createTree();
|
|
412
|
-
this.bfs(node => cloned.add(node.key,
|
|
407
|
+
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
408
|
+
if (this._isMapMode) cloned._store = this._store;
|
|
413
409
|
return cloned;
|
|
414
410
|
}
|
|
415
411
|
|
|
@@ -419,17 +415,17 @@ export class TreeMultiMap<
|
|
|
419
415
|
*
|
|
420
416
|
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
421
417
|
* in a binary search tree.
|
|
422
|
-
* @param {R |
|
|
418
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
423
419
|
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
424
|
-
* instance of the `
|
|
425
|
-
* @param {R |
|
|
420
|
+
* instance of the `BSTNOptKeyOrNode<K, NODE>` class.
|
|
421
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
426
422
|
* node where the properties will be swapped with the source node.
|
|
427
423
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
428
424
|
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
429
425
|
*/
|
|
430
426
|
protected override _swapProperties(
|
|
431
|
-
srcNode: R |
|
|
432
|
-
destNode: R |
|
|
427
|
+
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
428
|
+
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
433
429
|
): NODE | undefined {
|
|
434
430
|
srcNode = this.ensureNode(srcNode);
|
|
435
431
|
destNode = this.ensureNode(destNode);
|
|
@@ -440,12 +436,12 @@ export class TreeMultiMap<
|
|
|
440
436
|
tempNode.color = color;
|
|
441
437
|
|
|
442
438
|
destNode.key = srcNode.key;
|
|
443
|
-
destNode.value = srcNode.value;
|
|
439
|
+
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
444
440
|
destNode.count = srcNode.count;
|
|
445
441
|
destNode.color = srcNode.color;
|
|
446
442
|
|
|
447
443
|
srcNode.key = tempNode.key;
|
|
448
|
-
srcNode.value = tempNode.value;
|
|
444
|
+
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
449
445
|
srcNode.count = tempNode.count;
|
|
450
446
|
srcNode.color = tempNode.color;
|
|
451
447
|
}
|
|
@@ -266,9 +266,9 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
266
266
|
*
|
|
267
267
|
*/
|
|
268
268
|
getHeight(): number {
|
|
269
|
-
const
|
|
269
|
+
const startNode = this.root;
|
|
270
270
|
let maxDepth = 0;
|
|
271
|
-
if (
|
|
271
|
+
if (startNode) {
|
|
272
272
|
const bfs = (node: TrieNode, level: number) => {
|
|
273
273
|
if (level > maxDepth) {
|
|
274
274
|
maxDepth = level;
|
|
@@ -280,7 +280,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
};
|
|
283
|
-
bfs(
|
|
283
|
+
bfs(startNode, 0);
|
|
284
284
|
}
|
|
285
285
|
return maxDepth;
|
|
286
286
|
}
|
|
@@ -4,14 +4,14 @@ import type {
|
|
|
4
4
|
BinaryTreeNested,
|
|
5
5
|
BinaryTreeNodeNested,
|
|
6
6
|
BinaryTreeOptions,
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
BTNRep,
|
|
8
|
+
NodePredicate
|
|
9
9
|
} from '../types';
|
|
10
10
|
|
|
11
11
|
export interface IBinaryTree<
|
|
12
12
|
K = any,
|
|
13
13
|
V = any,
|
|
14
|
-
R =
|
|
14
|
+
R = object,
|
|
15
15
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>,
|
|
16
16
|
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>
|
|
17
17
|
> {
|
|
@@ -19,9 +19,9 @@ export interface IBinaryTree<
|
|
|
19
19
|
|
|
20
20
|
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
|
|
21
21
|
|
|
22
|
-
add(keyOrNodeOrEntryOrRawElement:
|
|
22
|
+
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean;
|
|
23
23
|
|
|
24
|
-
addMany(nodes: Iterable<
|
|
24
|
+
addMany(nodes: Iterable<BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
|
|
25
25
|
|
|
26
|
-
delete(predicate: R |
|
|
26
|
+
delete(predicate: R | BTNRep<K, V, NODE> | NodePredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
|
|
27
27
|
}
|
|
@@ -6,31 +6,30 @@ export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K,
|
|
|
6
6
|
|
|
7
7
|
export type BinaryTreeNested<K, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
+
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;
|
|
10
|
+
|
|
9
11
|
export type BinaryTreeOptions<K, V, R> = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
iterationType?: IterationType;
|
|
13
|
+
toEntryFn?: ToEntryFn<K, V, R>;
|
|
14
|
+
isMapMode?: boolean;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
|
|
15
18
|
|
|
16
|
-
export type
|
|
17
|
-
|
|
18
|
-
export type OptBTNKeyOrNull<K> = K | null | undefined;
|
|
19
|
-
|
|
20
|
-
export type BTNEntry<K, V> = [OptBTNKeyOrNull<K>, OptValue<V>];
|
|
19
|
+
export type OptNodeOrNull<NODE> = NODE | null | undefined;
|
|
21
20
|
|
|
22
|
-
export type
|
|
21
|
+
export type BTNOptKeyOrNull<K> = K | null | undefined;
|
|
23
22
|
|
|
24
|
-
export type
|
|
23
|
+
export type BTNEntry<K, V> = [BTNOptKeyOrNull<K>, OptValue<V>];
|
|
25
24
|
|
|
26
|
-
export type
|
|
25
|
+
export type BTNOptKeyNodeOrNull<K, NODE> = BTNOptKeyOrNull<K> | NODE;
|
|
27
26
|
|
|
28
|
-
export type
|
|
27
|
+
export type BTNRep<K, V, NODE> = BTNEntry<K, V> | BTNOptKeyNodeOrNull<K, NODE>;
|
|
29
28
|
|
|
30
|
-
export type BinaryTreeDeleteResult<NODE> = { deleted:
|
|
29
|
+
export type BinaryTreeDeleteResult<NODE> = { deleted: OptNodeOrNull<NODE>; needBalanced: OptNodeOrNull<NODE> };
|
|
31
30
|
|
|
32
|
-
export type
|
|
31
|
+
export type NodeCallback<NODE, D = any> = (node: NODE) => D;
|
|
33
32
|
|
|
34
|
-
export type
|
|
33
|
+
export type NodePredicate<NODE> = (node: NODE) => boolean;
|
|
35
34
|
|
|
36
|
-
export type DFSStackItem<NODE> = { opt: DFSOperation; node:
|
|
35
|
+
export type DFSStackItem<NODE> = { opt: DFSOperation; node: OptNodeOrNull<NODE> }
|
|
@@ -7,12 +7,12 @@ export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTN
|
|
|
7
7
|
export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
9
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
10
|
-
|
|
10
|
+
comparator?: Comparator<K>
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export type
|
|
13
|
+
export type BSTNOptKey<K> = K | undefined;
|
|
14
14
|
|
|
15
|
-
export type
|
|
15
|
+
export type OptNode<NODE> = NODE | undefined;
|
|
16
16
|
|
|
17
|
-
export type
|
|
17
|
+
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
|
|
18
18
|
|