linked-list-typed 1.47.6 → 1.47.7
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.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -5,16 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
IterableEntriesOrKeys,
|
|
15
|
-
IterationType,
|
|
16
|
-
TreeMultimapNested
|
|
8
|
+
import type {
|
|
9
|
+
BSTNodeKeyOrNode,
|
|
10
|
+
BTNKey,
|
|
11
|
+
BTNodeExemplar,
|
|
12
|
+
TreeMultimapNodeNested,
|
|
13
|
+
TreeMultimapOptions
|
|
17
14
|
} from '../../types';
|
|
15
|
+
import { BiTreeDeleteResult, BTNCallback, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
|
|
18
16
|
import { IBinaryTree } from '../../interfaces';
|
|
19
17
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
20
18
|
|
|
@@ -48,21 +46,18 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
48
46
|
extends AVLTree<V, N, TREE>
|
|
49
47
|
implements IBinaryTree<V, N, TREE> {
|
|
50
48
|
|
|
51
|
-
|
|
52
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
53
|
-
* merge duplicated values.
|
|
54
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
55
|
-
* TreeMultimap.
|
|
56
|
-
*/
|
|
57
|
-
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>) {
|
|
49
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>) {
|
|
58
50
|
super([], options);
|
|
59
|
-
if (elements) this.
|
|
51
|
+
if (elements) this.addMany(elements);
|
|
60
52
|
}
|
|
61
53
|
|
|
62
54
|
private _count = 0;
|
|
63
55
|
|
|
56
|
+
// TODO the _count is not accurate after nodes count modified
|
|
64
57
|
get count(): number {
|
|
65
|
-
|
|
58
|
+
let sum = 0;
|
|
59
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
60
|
+
return sum;
|
|
66
61
|
}
|
|
67
62
|
|
|
68
63
|
/**
|
|
@@ -85,126 +80,66 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
85
80
|
}) as TREE;
|
|
86
81
|
}
|
|
87
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
85
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
88
|
/**
|
|
89
89
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
90
90
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
91
91
|
*
|
|
92
|
-
* The `add` function
|
|
93
|
-
*
|
|
94
|
-
* @param
|
|
95
|
-
* following types:
|
|
96
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
97
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
92
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
93
|
+
* and update the count.
|
|
94
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
98
95
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
99
|
-
* times the key
|
|
100
|
-
*
|
|
96
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
97
|
+
* is 1.
|
|
98
|
+
* @returns either a node (`N`) or `undefined`.
|
|
101
99
|
*/
|
|
102
|
-
override add(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
newNode =
|
|
108
|
-
} else if (
|
|
109
|
-
newNode = undefined;
|
|
100
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
101
|
+
let newNode: N | undefined;
|
|
102
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
103
|
+
return;
|
|
104
|
+
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
105
|
+
newNode = keyOrNodeOrEntry;
|
|
106
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
107
|
+
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
108
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
+
if (key === undefined || key === null) {
|
|
111
|
+
return;
|
|
112
|
+
} else {
|
|
113
|
+
newNode = this.createNode(key, value, count);
|
|
114
|
+
}
|
|
110
115
|
} else {
|
|
111
|
-
|
|
116
|
+
return;
|
|
112
117
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
inserted = this.root;
|
|
118
|
-
} else {
|
|
119
|
-
let cur = this.root;
|
|
120
|
-
let traversing = true;
|
|
121
|
-
while (traversing) {
|
|
122
|
-
if (cur) {
|
|
123
|
-
if (newNode) {
|
|
124
|
-
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
125
|
-
cur.value = newNode.value;
|
|
126
|
-
cur.count += newNode.count;
|
|
127
|
-
this._count += newNode.count;
|
|
128
|
-
traversing = false;
|
|
129
|
-
inserted = cur;
|
|
130
|
-
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
131
|
-
// Traverse left of the node
|
|
132
|
-
if (cur.left === undefined) {
|
|
133
|
-
//Add to the left of the current node
|
|
134
|
-
cur.left = newNode;
|
|
135
|
-
this._size = this.size + 1;
|
|
136
|
-
this._count += newNode.count;
|
|
137
|
-
|
|
138
|
-
traversing = false;
|
|
139
|
-
inserted = cur.left;
|
|
140
|
-
} else {
|
|
141
|
-
//Traverse the left of the current node
|
|
142
|
-
if (cur.left) cur = cur.left;
|
|
143
|
-
}
|
|
144
|
-
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
145
|
-
// Traverse right of the node
|
|
146
|
-
if (cur.right === undefined) {
|
|
147
|
-
//Add to the right of the current node
|
|
148
|
-
cur.right = newNode;
|
|
149
|
-
this._size = this.size + 1;
|
|
150
|
-
this._count += newNode.count;
|
|
151
|
-
|
|
152
|
-
traversing = false;
|
|
153
|
-
inserted = cur.right;
|
|
154
|
-
} else {
|
|
155
|
-
//Traverse the left of the current node
|
|
156
|
-
if (cur.right) cur = cur.right;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
// TODO may need to support undefined inserted
|
|
161
|
-
}
|
|
162
|
-
} else {
|
|
163
|
-
traversing = false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
118
|
+
const orgNodeCount = newNode?.count || 0;
|
|
119
|
+
const inserted = super.add(newNode);
|
|
120
|
+
if (inserted) {
|
|
121
|
+
this._count += orgNodeCount;
|
|
166
122
|
}
|
|
167
|
-
if (inserted) this._balancePath(inserted);
|
|
168
123
|
return inserted;
|
|
169
124
|
}
|
|
170
125
|
|
|
171
126
|
/**
|
|
172
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
127
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
173
128
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
174
129
|
*/
|
|
175
130
|
|
|
176
131
|
/**
|
|
177
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
132
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
178
133
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
179
134
|
*
|
|
180
|
-
* The function
|
|
181
|
-
*
|
|
182
|
-
* @param
|
|
183
|
-
*
|
|
184
|
-
* @
|
|
185
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
186
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
187
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
135
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
136
|
+
* structure.
|
|
137
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
138
|
+
* either keys, nodes, or entries.
|
|
139
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
188
140
|
*/
|
|
189
|
-
override addMany(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
193
|
-
const keyOrNode = keysOrNodes[i];
|
|
194
|
-
|
|
195
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
196
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (keyOrNode === undefined) {
|
|
201
|
-
inserted.push(this.add(NaN, undefined, 0));
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
206
|
-
}
|
|
207
|
-
return inserted;
|
|
141
|
+
override addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[] {
|
|
142
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
208
143
|
}
|
|
209
144
|
|
|
210
145
|
/**
|
|
@@ -235,7 +170,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
235
170
|
if (l > r) return;
|
|
236
171
|
const m = l + Math.floor((r - l) / 2);
|
|
237
172
|
const midNode = sorted[m];
|
|
238
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
173
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
239
174
|
buildBalanceBST(l, m - 1);
|
|
240
175
|
buildBalanceBST(m + 1, r);
|
|
241
176
|
};
|
|
@@ -251,7 +186,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
251
186
|
if (l <= r) {
|
|
252
187
|
const m = l + Math.floor((r - l) / 2);
|
|
253
188
|
const midNode = sorted[m];
|
|
254
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
189
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
255
190
|
stack.push([m + 1, r]);
|
|
256
191
|
stack.push([l, m - 1]);
|
|
257
192
|
}
|
|
@@ -320,7 +255,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
320
255
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
321
256
|
if (leftSubTreeRightMost) {
|
|
322
257
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
323
|
-
orgCurrent = this.
|
|
258
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
324
259
|
if (parentOfLeftSubTreeMax) {
|
|
325
260
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
326
261
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -358,24 +293,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
358
293
|
this._count = 0;
|
|
359
294
|
}
|
|
360
295
|
|
|
361
|
-
/**
|
|
362
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
363
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
364
|
-
*/
|
|
365
|
-
|
|
366
|
-
init(elements: IterableEntriesOrKeys<V>): void {
|
|
367
|
-
if (elements) {
|
|
368
|
-
for (const entryOrKey of elements) {
|
|
369
|
-
if (Array.isArray(entryOrKey)) {
|
|
370
|
-
const [key, value] = entryOrKey;
|
|
371
|
-
this.add(key, value);
|
|
372
|
-
} else {
|
|
373
|
-
this.add(entryOrKey);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
296
|
/**
|
|
380
297
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
381
298
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -391,8 +308,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
391
308
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
392
309
|
* added, or `undefined` if no node was added.
|
|
393
310
|
*/
|
|
394
|
-
protected override _addTo(newNode: N | undefined, parent:
|
|
395
|
-
parent = this.
|
|
311
|
+
protected override _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined {
|
|
312
|
+
parent = this.ensureNode(parent);
|
|
396
313
|
if (parent) {
|
|
397
314
|
if (parent.left === undefined) {
|
|
398
315
|
parent.left = newNode;
|
|
@@ -418,7 +335,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
418
335
|
}
|
|
419
336
|
|
|
420
337
|
/**
|
|
421
|
-
* The `
|
|
338
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
422
339
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
423
340
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
424
341
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -426,9 +343,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
426
343
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
427
344
|
* if either `srcNode` or `destNode` is undefined.
|
|
428
345
|
*/
|
|
429
|
-
protected
|
|
430
|
-
srcNode = this.
|
|
431
|
-
destNode = this.
|
|
346
|
+
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
|
|
347
|
+
srcNode = this.ensureNode(srcNode);
|
|
348
|
+
destNode = this.ensureNode(destNode);
|
|
432
349
|
if (srcNode && destNode) {
|
|
433
350
|
const { key, value, count, height } = destNode;
|
|
434
351
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -450,4 +367,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
450
367
|
}
|
|
451
368
|
return undefined;
|
|
452
369
|
}
|
|
370
|
+
|
|
371
|
+
protected _replaceNode(oldNode: N, newNode: N): N {
|
|
372
|
+
newNode.count = oldNode.count + newNode.count
|
|
373
|
+
return super._replaceNode(oldNode, newNode);
|
|
374
|
+
}
|
|
453
375
|
}
|
|
@@ -19,20 +19,16 @@ export class HashMap<K = any, V = any> {
|
|
|
19
19
|
protected _hashFn: (key: K) => string;
|
|
20
20
|
protected _objHashFn: (key: K) => object;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
26
|
-
*/
|
|
27
|
-
constructor(options: HashMapOptions<K, V> = {
|
|
28
|
-
elements: [],
|
|
22
|
+
|
|
23
|
+
constructor(elements?: Iterable<[K, V]>, options: HashMapOptions<K> = {
|
|
24
|
+
|
|
29
25
|
hashFn: (key: K) => String(key),
|
|
30
26
|
objHashFn: (key: K) => (<object>key)
|
|
31
27
|
}) {
|
|
32
28
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
33
29
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
34
30
|
|
|
35
|
-
const {
|
|
31
|
+
const { hashFn, objHashFn } = options;
|
|
36
32
|
this._hashFn = hashFn;
|
|
37
33
|
this._objHashFn = objHashFn;
|
|
38
34
|
if (elements) {
|
|
@@ -379,6 +375,10 @@ export class HashMap<K = any, V = any> {
|
|
|
379
375
|
}
|
|
380
376
|
}
|
|
381
377
|
|
|
378
|
+
print() {
|
|
379
|
+
console.log([...this]);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
382
|
/**
|
|
383
383
|
* Time Complexity: O(1)
|
|
384
384
|
* Space Complexity: O(1)
|
|
@@ -29,13 +29,20 @@ export class Trie {
|
|
|
29
29
|
constructor(words?: string[], caseSensitive = true) {
|
|
30
30
|
this._root = new TrieNode('');
|
|
31
31
|
this._caseSensitive = caseSensitive;
|
|
32
|
+
this._size = 0;
|
|
32
33
|
if (words) {
|
|
33
|
-
for (const
|
|
34
|
-
this.add(
|
|
34
|
+
for (const word of words) {
|
|
35
|
+
this.add(word);
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
protected _size: number;
|
|
41
|
+
|
|
42
|
+
get size(): number {
|
|
43
|
+
return this._size;
|
|
44
|
+
}
|
|
45
|
+
|
|
39
46
|
protected _caseSensitive: boolean;
|
|
40
47
|
|
|
41
48
|
get caseSensitive(): boolean {
|
|
@@ -64,6 +71,7 @@ export class Trie {
|
|
|
64
71
|
add(word: string): boolean {
|
|
65
72
|
word = this._caseProcess(word);
|
|
66
73
|
let cur = this.root;
|
|
74
|
+
let isNewWord = false;
|
|
67
75
|
for (const c of word) {
|
|
68
76
|
let nodeC = cur.children.get(c);
|
|
69
77
|
if (!nodeC) {
|
|
@@ -72,8 +80,12 @@ export class Trie {
|
|
|
72
80
|
}
|
|
73
81
|
cur = nodeC;
|
|
74
82
|
}
|
|
75
|
-
cur.isEnd
|
|
76
|
-
|
|
83
|
+
if (!cur.isEnd) {
|
|
84
|
+
isNewWord = true;
|
|
85
|
+
cur.isEnd = true;
|
|
86
|
+
this._size++;
|
|
87
|
+
}
|
|
88
|
+
return isNewWord;
|
|
77
89
|
}
|
|
78
90
|
|
|
79
91
|
/**
|
|
@@ -143,6 +155,9 @@ export class Trie {
|
|
|
143
155
|
};
|
|
144
156
|
|
|
145
157
|
dfs(this.root, 0);
|
|
158
|
+
if (isDeleted) {
|
|
159
|
+
this._size--;
|
|
160
|
+
}
|
|
146
161
|
return isDeleted;
|
|
147
162
|
}
|
|
148
163
|
|
|
@@ -377,6 +392,10 @@ export class Trie {
|
|
|
377
392
|
return accumulator;
|
|
378
393
|
}
|
|
379
394
|
|
|
395
|
+
print() {
|
|
396
|
+
console.log([...this]);
|
|
397
|
+
}
|
|
398
|
+
|
|
380
399
|
/**
|
|
381
400
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
382
401
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
BiTreeDeleteResult,
|
|
7
7
|
BTNCallback,
|
|
8
8
|
BTNKey,
|
|
9
|
-
|
|
9
|
+
BTNodeExemplar,
|
|
10
10
|
} from '../types';
|
|
11
11
|
|
|
12
12
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
@@ -14,9 +14,9 @@ export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTre
|
|
|
14
14
|
|
|
15
15
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
|
|
20
20
|
|
|
21
21
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
22
22
|
}
|
package/src/types/common.ts
CHANGED
|
@@ -24,4 +24,14 @@ export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLeng
|
|
|
24
24
|
|
|
25
25
|
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
|
26
26
|
|
|
27
|
-
export type
|
|
27
|
+
export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
|
|
28
|
+
|
|
29
|
+
export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
|
|
30
|
+
|
|
31
|
+
export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>
|
|
32
|
+
|
|
33
|
+
export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>
|
|
34
|
+
|
|
35
|
+
export type BTNodePureKeyOrNode<N> = BTNKey | N;
|
|
36
|
+
|
|
37
|
+
export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
|