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.
Files changed (77) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.github/workflows/release-package.yml +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE_OF_CONDUCT.md +32 -10
  6. package/COMMANDS.md +3 -1
  7. package/CONTRIBUTING.md +4 -3
  8. package/README.md +103 -28
  9. package/SECURITY.md +1 -1
  10. package/benchmark/report.html +46 -1
  11. package/benchmark/report.json +563 -8
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
  19. package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
  20. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  25. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
  26. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  27. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
  28. package/dist/cjs/data-structures/hash/hash-map.js +5 -8
  29. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  30. package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
  31. package/dist/cjs/data-structures/trie/trie.js +19 -4
  32. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  33. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  34. package/dist/cjs/types/common.d.ts +6 -1
  35. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
  36. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
  37. package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
  38. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
  39. package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
  40. package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
  41. package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
  42. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
  43. package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
  44. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  45. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
  46. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
  47. package/dist/mjs/data-structures/hash/hash-map.js +5 -8
  48. package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
  49. package/dist/mjs/data-structures/trie/trie.js +20 -4
  50. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  51. package/dist/mjs/types/common.d.ts +6 -1
  52. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
  53. package/dist/umd/data-structure-typed.js +422 -466
  54. package/dist/umd/data-structure-typed.min.js +2 -2
  55. package/dist/umd/data-structure-typed.min.js.map +1 -1
  56. package/package.json +1 -1
  57. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  58. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  59. package/src/data-structures/binary-tree/bst.ts +157 -154
  60. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  61. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  62. package/src/data-structures/hash/hash-map.ts +8 -8
  63. package/src/data-structures/trie/trie.ts +23 -4
  64. package/src/interfaces/binary-tree.ts +3 -3
  65. package/src/types/common.ts +11 -1
  66. package/src/types/data-structures/hash/hash-map.ts +1 -2
  67. package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
  68. package/test/integration/index.html +87 -0
  69. package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
  70. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
  71. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
  72. package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
  73. package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
  74. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
  75. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
  76. package/test/unit/unrestricted-interconversion.test.ts +61 -5
  77. package/tsconfig-cjs.json +1 -1
@@ -1,4 +1,4 @@
1
- import { CP, FamilyPosition, IterationType } from '../../types';
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.init(elements);
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
- return this._count;
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 adds a new node to the tree multimap, updating the count if the key already
62
- * exists, and balances the tree if necessary.
63
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
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-value pair should be added to the multimap. If not provided, the default value is 1.
69
- * @returns a node (`N`) or `undefined`.
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(keyOrNode, value, count = 1) {
72
- if (keyOrNode === null)
73
- return undefined;
74
- let inserted = undefined, newNode;
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 (keyOrNode === undefined) {
79
- newNode = undefined;
75
+ else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
76
+ newNode = keyOrNodeOrEntry;
80
77
  }
81
- else {
82
- newNode = this.createNode(keyOrNode, value, count);
78
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
79
+ newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
83
80
  }
84
- if (!this.root) {
85
- this._setRoot(newNode);
86
- this._size = this.size + 1;
87
- if (newNode)
88
- this._count += newNode.count;
89
- inserted = this.root;
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
- let cur = this.root;
93
- let traversing = true;
94
- while (traversing) {
95
- if (cur) {
96
- if (newNode) {
97
- if (this._compare(cur.key, newNode.key) === CP.eq) {
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 for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
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 `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
159
- * returning an array of the inserted nodes.
160
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
161
- * of type BTNKey, N, or undefined.
162
- * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
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(keysOrNodes, data) {
168
- const inserted = [];
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._swap(curr, leftSubTreeRightMost);
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.ensureNotKey(parent);
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 `_swap` function swaps the key, value, count, and height properties between two nodes.
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
- _swap(srcNode, destNode) {
394
- srcNode = this.ensureNotKey(srcNode);
395
- destNode = this.ensureNotKey(destNode);
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 { elements, hashFn, objHashFn } = options;
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 i of words) {
31
- this.add(i);
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 = true;
67
- return true;
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, IterableEntriesOrKeys } from '../types';
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
- init(elements: IterableEntriesOrKeys<V>): void;
7
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
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 IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
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, V> = {
8
- elements: Iterable<[K, V]>;
7
+ export type HashMapOptions<K> = {
9
8
  hashFn: (key: K) => string;
10
9
  objHashFn: (key: K) => object;
11
10
  };