data-structure-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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +422 -466
- 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/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
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultimapNode extends AVLTreeNode {
|
|
4
4
|
count;
|
|
@@ -21,20 +21,17 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
/**
|
|
25
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
26
|
-
* merge duplicated values.
|
|
27
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
28
|
-
* TreeMultimap.
|
|
29
|
-
*/
|
|
30
24
|
constructor(elements, options) {
|
|
31
25
|
super([], options);
|
|
32
26
|
if (elements)
|
|
33
|
-
this.
|
|
27
|
+
this.addMany(elements);
|
|
34
28
|
}
|
|
35
29
|
_count = 0;
|
|
30
|
+
// TODO the _count is not accurate after nodes count modified
|
|
36
31
|
get count() {
|
|
37
|
-
|
|
32
|
+
let sum = 0;
|
|
33
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
34
|
+
return sum;
|
|
38
35
|
}
|
|
39
36
|
/**
|
|
40
37
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -54,131 +51,68 @@ export class TreeMultimap extends AVLTree {
|
|
|
54
51
|
comparator: this.comparator, ...options
|
|
55
52
|
});
|
|
56
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* 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.
|
|
56
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
57
|
+
*/
|
|
57
58
|
/**
|
|
58
59
|
* 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.
|
|
59
60
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
60
61
|
*
|
|
61
|
-
* The `add` function
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
* following types:
|
|
65
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
66
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
62
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
63
|
+
* and update the count.
|
|
64
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
67
65
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
68
|
-
* times the key
|
|
69
|
-
*
|
|
66
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
67
|
+
* is 1.
|
|
68
|
+
* @returns either a node (`N`) or `undefined`.
|
|
70
69
|
*/
|
|
71
|
-
add(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
76
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
70
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
71
|
+
let newNode;
|
|
72
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
73
|
+
return;
|
|
77
74
|
}
|
|
78
|
-
else if (
|
|
79
|
-
newNode =
|
|
75
|
+
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
76
|
+
newNode = keyOrNodeOrEntry;
|
|
80
77
|
}
|
|
81
|
-
else {
|
|
82
|
-
newNode = this.createNode(
|
|
78
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
79
|
+
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
83
80
|
}
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
82
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
83
|
+
if (key === undefined || key === null) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
newNode = this.createNode(key, value, count);
|
|
88
|
+
}
|
|
90
89
|
}
|
|
91
90
|
else {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
cur.value = newNode.value;
|
|
99
|
-
cur.count += newNode.count;
|
|
100
|
-
this._count += newNode.count;
|
|
101
|
-
traversing = false;
|
|
102
|
-
inserted = cur;
|
|
103
|
-
}
|
|
104
|
-
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
105
|
-
// Traverse left of the node
|
|
106
|
-
if (cur.left === undefined) {
|
|
107
|
-
//Add to the left of the current node
|
|
108
|
-
cur.left = newNode;
|
|
109
|
-
this._size = this.size + 1;
|
|
110
|
-
this._count += newNode.count;
|
|
111
|
-
traversing = false;
|
|
112
|
-
inserted = cur.left;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
//Traverse the left of the current node
|
|
116
|
-
if (cur.left)
|
|
117
|
-
cur = cur.left;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
121
|
-
// Traverse right of the node
|
|
122
|
-
if (cur.right === undefined) {
|
|
123
|
-
//Add to the right of the current node
|
|
124
|
-
cur.right = newNode;
|
|
125
|
-
this._size = this.size + 1;
|
|
126
|
-
this._count += newNode.count;
|
|
127
|
-
traversing = false;
|
|
128
|
-
inserted = cur.right;
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
//Traverse the left of the current node
|
|
132
|
-
if (cur.right)
|
|
133
|
-
cur = cur.right;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
// TODO may need to support undefined inserted
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
traversing = false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const orgNodeCount = newNode?.count || 0;
|
|
94
|
+
const inserted = super.add(newNode);
|
|
95
|
+
if (inserted) {
|
|
96
|
+
this._count += orgNodeCount;
|
|
145
97
|
}
|
|
146
|
-
if (inserted)
|
|
147
|
-
this._balancePath(inserted);
|
|
148
98
|
return inserted;
|
|
149
99
|
}
|
|
150
100
|
/**
|
|
151
|
-
* 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.
|
|
101
|
+
* 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.
|
|
152
102
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
153
103
|
*/
|
|
154
104
|
/**
|
|
155
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
105
|
+
* 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.
|
|
156
106
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
157
107
|
*
|
|
158
|
-
* The function
|
|
159
|
-
*
|
|
160
|
-
* @param
|
|
161
|
-
*
|
|
162
|
-
* @
|
|
163
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
164
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
165
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
108
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
109
|
+
* structure.
|
|
110
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
111
|
+
* either keys, nodes, or entries.
|
|
112
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
166
113
|
*/
|
|
167
|
-
addMany(
|
|
168
|
-
|
|
169
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
170
|
-
const keyOrNode = keysOrNodes[i];
|
|
171
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
172
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
if (keyOrNode === undefined) {
|
|
176
|
-
inserted.push(this.add(NaN, undefined, 0));
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
180
|
-
}
|
|
181
|
-
return inserted;
|
|
114
|
+
addMany(keysOrNodesOrEntries) {
|
|
115
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
182
116
|
}
|
|
183
117
|
/**
|
|
184
118
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
@@ -206,7 +140,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
206
140
|
return;
|
|
207
141
|
const m = l + Math.floor((r - l) / 2);
|
|
208
142
|
const midNode = sorted[m];
|
|
209
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
143
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
210
144
|
buildBalanceBST(l, m - 1);
|
|
211
145
|
buildBalanceBST(m + 1, r);
|
|
212
146
|
};
|
|
@@ -222,7 +156,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
222
156
|
if (l <= r) {
|
|
223
157
|
const m = l + Math.floor((r - l) / 2);
|
|
224
158
|
const midNode = sorted[m];
|
|
225
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
159
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
226
160
|
stack.push([m + 1, r]);
|
|
227
161
|
stack.push([l, m - 1]);
|
|
228
162
|
}
|
|
@@ -288,7 +222,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
288
222
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
289
223
|
if (leftSubTreeRightMost) {
|
|
290
224
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
291
|
-
orgCurrent = this.
|
|
225
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
292
226
|
if (parentOfLeftSubTreeMax) {
|
|
293
227
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
294
228
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -322,23 +256,6 @@ export class TreeMultimap extends AVLTree {
|
|
|
322
256
|
super.clear();
|
|
323
257
|
this._count = 0;
|
|
324
258
|
}
|
|
325
|
-
/**
|
|
326
|
-
* 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.
|
|
327
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
328
|
-
*/
|
|
329
|
-
init(elements) {
|
|
330
|
-
if (elements) {
|
|
331
|
-
for (const entryOrKey of elements) {
|
|
332
|
-
if (Array.isArray(entryOrKey)) {
|
|
333
|
-
const [key, value] = entryOrKey;
|
|
334
|
-
this.add(key, value);
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
this.add(entryOrKey);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
259
|
/**
|
|
343
260
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
344
261
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -355,7 +272,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
355
272
|
* added, or `undefined` if no node was added.
|
|
356
273
|
*/
|
|
357
274
|
_addTo(newNode, parent) {
|
|
358
|
-
parent = this.
|
|
275
|
+
parent = this.ensureNode(parent);
|
|
359
276
|
if (parent) {
|
|
360
277
|
if (parent.left === undefined) {
|
|
361
278
|
parent.left = newNode;
|
|
@@ -382,7 +299,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
382
299
|
}
|
|
383
300
|
}
|
|
384
301
|
/**
|
|
385
|
-
* The `
|
|
302
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
386
303
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
387
304
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
388
305
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -390,9 +307,9 @@ export class TreeMultimap extends AVLTree {
|
|
|
390
307
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
391
308
|
* if either `srcNode` or `destNode` is undefined.
|
|
392
309
|
*/
|
|
393
|
-
|
|
394
|
-
srcNode = this.
|
|
395
|
-
destNode = this.
|
|
310
|
+
_swapProperties(srcNode, destNode) {
|
|
311
|
+
srcNode = this.ensureNode(srcNode);
|
|
312
|
+
destNode = this.ensureNode(destNode);
|
|
396
313
|
if (srcNode && destNode) {
|
|
397
314
|
const { key, value, count, height } = destNode;
|
|
398
315
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -411,4 +328,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
411
328
|
}
|
|
412
329
|
return undefined;
|
|
413
330
|
}
|
|
331
|
+
_replaceNode(oldNode, newNode) {
|
|
332
|
+
newNode.count = oldNode.count + newNode.count;
|
|
333
|
+
return super._replaceNode(oldNode, newNode);
|
|
334
|
+
}
|
|
414
335
|
}
|
|
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
14
14
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
15
|
protected _hashFn: (key: K) => string;
|
|
16
16
|
protected _objHashFn: (key: K) => object;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options?: HashMapOptions<K, V>);
|
|
17
|
+
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
23
18
|
protected _size: number;
|
|
24
19
|
get size(): number;
|
|
25
20
|
/**
|
|
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
172
167
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
173
168
|
*/
|
|
174
169
|
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
170
|
+
print(): void;
|
|
175
171
|
/**
|
|
176
172
|
* Time Complexity: O(1)
|
|
177
173
|
* Space Complexity: O(1)
|
|
@@ -14,19 +14,13 @@ export class HashMap {
|
|
|
14
14
|
_sentinel;
|
|
15
15
|
_hashFn;
|
|
16
16
|
_objHashFn;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options = {
|
|
23
|
-
elements: [],
|
|
17
|
+
constructor(elements, options = {
|
|
24
18
|
hashFn: (key) => String(key),
|
|
25
19
|
objHashFn: (key) => key
|
|
26
20
|
}) {
|
|
27
21
|
this._sentinel = {};
|
|
28
22
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
29
|
-
const {
|
|
23
|
+
const { hashFn, objHashFn } = options;
|
|
30
24
|
this._hashFn = hashFn;
|
|
31
25
|
this._objHashFn = objHashFn;
|
|
32
26
|
if (elements) {
|
|
@@ -349,6 +343,9 @@ export class HashMap {
|
|
|
349
343
|
node = node.next;
|
|
350
344
|
}
|
|
351
345
|
}
|
|
346
|
+
print() {
|
|
347
|
+
console.log([...this]);
|
|
348
|
+
}
|
|
352
349
|
/**
|
|
353
350
|
* Time Complexity: O(1)
|
|
354
351
|
* Space Complexity: O(1)
|
|
@@ -20,6 +20,8 @@ export declare class TrieNode {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare class Trie {
|
|
22
22
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
23
|
+
protected _size: number;
|
|
24
|
+
get size(): number;
|
|
23
25
|
protected _caseSensitive: boolean;
|
|
24
26
|
get caseSensitive(): boolean;
|
|
25
27
|
protected _root: TrieNode;
|
|
@@ -145,6 +147,7 @@ export declare class Trie {
|
|
|
145
147
|
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
148
|
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
149
|
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
150
|
+
print(): void;
|
|
148
151
|
/**
|
|
149
152
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
150
153
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -26,12 +26,17 @@ export class Trie {
|
|
|
26
26
|
constructor(words, caseSensitive = true) {
|
|
27
27
|
this._root = new TrieNode('');
|
|
28
28
|
this._caseSensitive = caseSensitive;
|
|
29
|
+
this._size = 0;
|
|
29
30
|
if (words) {
|
|
30
|
-
for (const
|
|
31
|
-
this.add(
|
|
31
|
+
for (const word of words) {
|
|
32
|
+
this.add(word);
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
}
|
|
36
|
+
_size;
|
|
37
|
+
get size() {
|
|
38
|
+
return this._size;
|
|
39
|
+
}
|
|
35
40
|
_caseSensitive;
|
|
36
41
|
get caseSensitive() {
|
|
37
42
|
return this._caseSensitive;
|
|
@@ -55,6 +60,7 @@ export class Trie {
|
|
|
55
60
|
add(word) {
|
|
56
61
|
word = this._caseProcess(word);
|
|
57
62
|
let cur = this.root;
|
|
63
|
+
let isNewWord = false;
|
|
58
64
|
for (const c of word) {
|
|
59
65
|
let nodeC = cur.children.get(c);
|
|
60
66
|
if (!nodeC) {
|
|
@@ -63,8 +69,12 @@ export class Trie {
|
|
|
63
69
|
}
|
|
64
70
|
cur = nodeC;
|
|
65
71
|
}
|
|
66
|
-
cur.isEnd
|
|
67
|
-
|
|
72
|
+
if (!cur.isEnd) {
|
|
73
|
+
isNewWord = true;
|
|
74
|
+
cur.isEnd = true;
|
|
75
|
+
this._size++;
|
|
76
|
+
}
|
|
77
|
+
return isNewWord;
|
|
68
78
|
}
|
|
69
79
|
/**
|
|
70
80
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -131,6 +141,9 @@ export class Trie {
|
|
|
131
141
|
return false;
|
|
132
142
|
};
|
|
133
143
|
dfs(this.root, 0);
|
|
144
|
+
if (isDeleted) {
|
|
145
|
+
this._size--;
|
|
146
|
+
}
|
|
134
147
|
return isDeleted;
|
|
135
148
|
}
|
|
136
149
|
/**
|
|
@@ -353,6 +366,9 @@ export class Trie {
|
|
|
353
366
|
}
|
|
354
367
|
return accumulator;
|
|
355
368
|
}
|
|
369
|
+
print() {
|
|
370
|
+
console.log([...this]);
|
|
371
|
+
}
|
|
356
372
|
/**
|
|
357
373
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
358
374
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey,
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
|
|
7
|
+
addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
|
@@ -19,4 +19,9 @@ export type BinaryTreePrintOptions = {
|
|
|
19
19
|
isShowNull?: boolean;
|
|
20
20
|
isShowRedBlackNIL?: boolean;
|
|
21
21
|
};
|
|
22
|
-
export type
|
|
22
|
+
export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
|
|
23
|
+
export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
|
|
24
|
+
export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
|
|
25
|
+
export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
|
|
26
|
+
export type BTNodePureKeyOrNode<N> = BTNKey | N;
|
|
27
|
+
export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
|
|
@@ -4,8 +4,7 @@ export type HashMapLinkedNode<K, V> = {
|
|
|
4
4
|
next: HashMapLinkedNode<K, V>;
|
|
5
5
|
prev: HashMapLinkedNode<K, V>;
|
|
6
6
|
};
|
|
7
|
-
export type HashMapOptions<K
|
|
8
|
-
elements: Iterable<[K, V]>;
|
|
7
|
+
export type HashMapOptions<K> = {
|
|
9
8
|
hashFn: (key: K) => string;
|
|
10
9
|
objHashFn: (key: K) => object;
|
|
11
10
|
};
|