stack-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 (28) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  11. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  12. package/dist/data-structures/hash/hash-map.js +5 -8
  13. package/dist/data-structures/trie/trie.d.ts +3 -0
  14. package/dist/data-structures/trie/trie.js +19 -4
  15. package/dist/interfaces/binary-tree.d.ts +3 -3
  16. package/dist/types/common.d.ts +6 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  18. package/package.json +2 -2
  19. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  20. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  21. package/src/data-structures/binary-tree/bst.ts +157 -154
  22. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  23. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  24. package/src/data-structures/hash/hash-map.ts +8 -8
  25. package/src/data-structures/trie/trie.ts +23 -4
  26. package/src/interfaces/binary-tree.ts +3 -3
  27. package/src/types/common.ts +11 -1
  28. 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 { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
- import {
10
- BiTreeDeleteResult,
11
- BTNCallback,
12
- CP,
13
- FamilyPosition,
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.init(elements);
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
- return this._count;
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 adds a new node to the tree multimap, updating the count if the key already
93
- * exists, and balances the tree if necessary.
94
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
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-value pair should be added to the multimap. If not provided, the default value is 1.
100
- * @returns a node (`N`) or `undefined`.
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(keyOrNode: BTNKey | N | null | undefined, value?: V, count = 1): N | undefined {
103
- if (keyOrNode === null) return undefined;
104
- let inserted: N | undefined = undefined,
105
- newNode: N | undefined;
106
- if (keyOrNode instanceof TreeMultimapNode) {
107
- newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
108
- } else if (keyOrNode === undefined) {
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
- newNode = this.createNode(keyOrNode, value, count);
116
+ return;
112
117
  }
113
- if (!this.root) {
114
- this._setRoot(newNode);
115
- this._size = this.size + 1;
116
- if (newNode) this._count += newNode.count;
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 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.
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 `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
181
- * returning an array of the inserted nodes.
182
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
183
- * of type BTNKey, N, or undefined.
184
- * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
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(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[] {
190
- const inserted: (N | undefined)[] = [];
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._swap(curr, leftSubTreeRightMost);
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: BTNKey | N | undefined): N | undefined {
395
- parent = this.ensureNotKey(parent);
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 `_swap` function swaps the key, value, count, and height properties between two nodes.
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 _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
430
- srcNode = this.ensureNotKey(srcNode);
431
- destNode = this.ensureNotKey(destNode);
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
- * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
24
- * @param options - The `options` parameter is an object that contains the `elements` property. The
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 { elements, hashFn, objHashFn } = options;
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 i of words) {
34
- this.add(i);
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 = true;
76
- return true;
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
- IterableEntriesOrKeys
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
- init(elements: IterableEntriesOrKeys<V>): void;
17
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
18
18
 
19
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
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
  }
@@ -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 IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>
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;
@@ -5,8 +5,7 @@ export type HashMapLinkedNode<K, V> = {
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
7
 
8
- export type HashMapOptions<K, V> = {
9
- elements: Iterable<[K, V]>;
8
+ export type HashMapOptions<K> = {
10
9
  hashFn: (key: K) => string;
11
10
  objHashFn: (key: K) => object
12
11
  }