directed-graph-typed 1.47.5 → 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 (71) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +36 -18
  2. package/dist/data-structures/binary-tree/avl-tree.js +46 -29
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
  4. package/dist/data-structures/binary-tree/binary-tree.js +182 -184
  5. package/dist/data-structures/binary-tree/bst.d.ts +73 -63
  6. package/dist/data-structures/binary-tree/bst.js +168 -169
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
  8. package/dist/data-structures/binary-tree/rb-tree.js +77 -31
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  10. package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  13. package/dist/data-structures/hash/hash-map.js +5 -8
  14. package/dist/data-structures/heap/heap.d.ts +19 -21
  15. package/dist/data-structures/heap/heap.js +52 -34
  16. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  17. package/dist/data-structures/heap/max-heap.js +2 -2
  18. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/min-heap.js +2 -2
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  23. package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
  24. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  26. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  30. package/dist/data-structures/queue/deque.d.ts +1 -0
  31. package/dist/data-structures/queue/deque.js +3 -0
  32. package/dist/data-structures/queue/queue.d.ts +1 -0
  33. package/dist/data-structures/queue/queue.js +3 -0
  34. package/dist/data-structures/stack/stack.d.ts +2 -1
  35. package/dist/data-structures/stack/stack.js +10 -2
  36. package/dist/data-structures/trie/trie.d.ts +3 -0
  37. package/dist/data-structures/trie/trie.js +19 -4
  38. package/dist/interfaces/binary-tree.d.ts +4 -2
  39. package/dist/types/common.d.ts +7 -0
  40. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  41. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  42. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  43. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  44. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  47. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  48. package/src/data-structures/binary-tree/bst.ts +193 -170
  49. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  50. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  51. package/src/data-structures/graph/abstract-graph.ts +1 -1
  52. package/src/data-structures/hash/hash-map.ts +8 -8
  53. package/src/data-structures/heap/heap.ts +57 -39
  54. package/src/data-structures/heap/max-heap.ts +5 -5
  55. package/src/data-structures/heap/min-heap.ts +5 -5
  56. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  57. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  58. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  59. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  60. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  61. package/src/data-structures/queue/deque.ts +4 -0
  62. package/src/data-structures/queue/queue.ts +4 -0
  63. package/src/data-structures/stack/stack.ts +12 -3
  64. package/src/data-structures/trie/trie.ts +23 -4
  65. package/src/interfaces/binary-tree.ts +14 -2
  66. package/src/types/common.ts +15 -1
  67. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  68. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  69. package/src/types/data-structures/hash/hash-map.ts +1 -2
  70. package/src/types/data-structures/heap/heap.ts +3 -1
  71. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -8,8 +8,10 @@
8
8
 
9
9
  import {
10
10
  BiTreeDeleteResult,
11
+ BSTNodeKeyOrNode,
11
12
  BTNCallback,
12
13
  BTNKey,
14
+ BTNodeExemplar,
13
15
  IterationType,
14
16
  RBTNColor,
15
17
  RBTreeOptions,
@@ -43,21 +45,23 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
43
45
  extends BST<V, N, TREE>
44
46
  implements IBinaryTree<V, N, TREE> {
45
47
  Sentinel: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
46
- override options: RBTreeOptions;
47
48
 
48
49
  /**
49
- * The constructor function initializes a Red-Black Tree with an optional set of options.
50
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
51
- * passed to the constructor. It is used to configure the RBTree object with specific options.
50
+ * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
51
+ * initializes the tree with optional elements and options.
52
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
53
+ * objects. It represents the initial elements that will be added to the RBTree during its
54
+ * construction. If this parameter is provided, the `addMany` method is called to add all the
55
+ * elements to the
56
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
57
+ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
58
+ * only a subset of the properties defined in the `RBTreeOptions` interface.
52
59
  */
53
- constructor(options?: RBTreeOptions) {
54
- super(options);
55
- if (options) {
56
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
57
- } else {
58
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
59
- }
60
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions>) {
61
+ super([], options);
62
+
60
63
  this._root = this.Sentinel;
64
+ if (elements) super.addMany(elements);
61
65
  }
62
66
 
63
67
  protected _root: N;
@@ -72,12 +76,34 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
72
76
  return this._size;
73
77
  }
74
78
 
79
+ /**
80
+ * The function creates a new Red-Black Tree node with the specified key, value, and color.
81
+ * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
82
+ * identify and compare nodes in the Red-Black Tree.
83
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
84
+ * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
85
+ * specific type when using the `createNode` method.
86
+ * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
87
+ * Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
88
+ * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
89
+ * value, and color.
90
+ */
75
91
  override createNode(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK): N {
76
92
  return new RedBlackTreeNode<V, N>(key, value, color) as N;
77
93
  }
78
94
 
95
+ /**
96
+ * The function creates a Red-Black Tree with the specified options and returns it.
97
+ * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
98
+ * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
99
+ * class.
100
+ * @returns a new instance of a RedBlackTree object.
101
+ */
79
102
  override createTree(options?: RBTreeOptions): TREE {
80
- return new RedBlackTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
103
+ return new RedBlackTree<V, N, TREE>([], {
104
+ iterationType: this.iterationType,
105
+ comparator: this.comparator, ...options
106
+ }) as TREE;
81
107
  }
82
108
 
83
109
  /**
@@ -85,27 +111,28 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
85
111
  * Space Complexity: O(1)
86
112
  */
87
113
 
114
+
88
115
  /**
89
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
90
- * Space Complexity: O(1)
91
- *
92
- * The `add` function adds a new node to a Red-Black Tree data structure.
93
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
94
- * following types:
95
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
96
- * key in the node being added to the Red-Black Tree.
97
- * @returns The method returns either a node (`N`) or `undefined`.
116
+ * The function adds a node to a Red-Black Tree data structure.
117
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
118
+ * @returns The method `add` returns either an instance of `N` (the node that was added) or
119
+ * `undefined`.
98
120
  */
99
- override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
121
+ override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
100
122
  let node: N;
101
- if (this.isNodeKey(keyOrNode)) {
102
- node = this.createNode(keyOrNode, value, RBTNColor.RED);
103
- } else if (keyOrNode instanceof RedBlackTreeNode) {
104
- node = keyOrNode;
105
- } else if (keyOrNode === null) {
106
- return;
107
- } else if (keyOrNode === undefined) {
123
+ if (this.isNodeKey(keyOrNodeOrEntry)) {
124
+ node = this.createNode(keyOrNodeOrEntry, undefined, RBTNColor.RED);
125
+ } else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
126
+ node = keyOrNodeOrEntry;
127
+ } else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
108
128
  return;
129
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
130
+ const [key, value] = keyOrNodeOrEntry;
131
+ if (key === undefined || key === null) {
132
+ return;
133
+ } else {
134
+ node = this.createNode(key, value, RBTNColor.RED);
135
+ }
109
136
  } else {
110
137
  return;
111
138
  }
@@ -124,6 +151,9 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
124
151
  } else if (node.key > x.key) {
125
152
  x = x?.right;
126
153
  } else {
154
+ if (node !== x) {
155
+ this._replaceNode(x, node)
156
+ }
127
157
  return;
128
158
  }
129
159
  }
@@ -235,6 +265,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
235
265
  return ans;
236
266
  }
237
267
 
268
+ /**
269
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
270
+ * Space Complexity: O(1)
271
+ */
272
+
238
273
  override isRealNode(node: N | undefined): node is N {
239
274
  return node !== this.Sentinel && node !== undefined;
240
275
  }
@@ -289,11 +324,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
289
324
  getNode<C extends BTNCallback<N>>(
290
325
  identifier: ReturnType<C> | undefined,
291
326
  callback: C = this._defaultOneParamCallback as C,
292
- beginRoot: BTNKey | N | undefined = this.root,
293
- iterationType = this.options.iterationType
327
+ beginRoot: BSTNodeKeyOrNode<N> = this.root,
328
+ iterationType = this.iterationType
294
329
  ): N | null | undefined {
295
330
  if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
296
- beginRoot = this.ensureNotKey(beginRoot);
331
+ beginRoot = this.ensureNode(beginRoot);
297
332
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
298
333
  }
299
334
 
@@ -351,6 +386,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
351
386
  return y!;
352
387
  }
353
388
 
389
+ /**
390
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
391
+ * Space Complexity: O(1)
392
+ */
393
+
354
394
  override clear() {
355
395
  this._root = this.Sentinel;
356
396
  this._size = 0;
@@ -582,4 +622,19 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
582
622
  }
583
623
  this.root.color = RBTNColor.BLACK;
584
624
  }
625
+
626
+ /**
627
+ * The function replaces an old node with a new node while preserving the color of the old node.
628
+ * @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
629
+ * data structure. It is of type `N`, which is the type of the nodes in the data structure.
630
+ * @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
631
+ * data structure.
632
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
633
+ * superclass, passing in the `oldNode` and `newNode` as arguments.
634
+ */
635
+ protected _replaceNode(oldNode: N, newNode: N): N {
636
+ newNode.color = oldNode.color;
637
+
638
+ return super._replaceNode(oldNode, newNode)
639
+ }
585
640
  }
@@ -5,8 +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 { BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
8
+ import type {
9
+ BSTNodeKeyOrNode,
10
+ BTNKey,
11
+ BTNodeExemplar,
12
+ TreeMultimapNodeNested,
13
+ TreeMultimapOptions
14
+ } from '../../types';
15
+ import { BiTreeDeleteResult, BTNCallback, FamilyPosition, IterationType, TreeMultimapNested } from '../../types';
10
16
  import { IBinaryTree } from '../../interfaces';
11
17
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
18
 
@@ -40,27 +46,18 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
40
46
  extends AVLTree<V, N, TREE>
41
47
  implements IBinaryTree<V, N, TREE> {
42
48
 
43
- override options: TreeMultimapOptions;
44
-
45
- /**
46
- * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
47
- * merge duplicated values.
48
- * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
49
- * TreeMultimap.
50
- */
51
- constructor(options: TreeMultimapOptions = { iterationType: IterationType.ITERATIVE }) {
52
- super(options);
53
- if (options) {
54
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
55
- } else {
56
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
57
- }
49
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>) {
50
+ super([], options);
51
+ if (elements) this.addMany(elements);
58
52
  }
59
53
 
60
54
  private _count = 0;
61
55
 
56
+ // TODO the _count is not accurate after nodes count modified
62
57
  get count(): number {
63
- return this._count;
58
+ let sum = 0;
59
+ this.subTreeTraverse(node => sum += node.count);
60
+ return sum;
64
61
  }
65
62
 
66
63
  /**
@@ -77,7 +74,10 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
77
74
  }
78
75
 
79
76
  override createTree(options?: TreeMultimapOptions): TREE {
80
- return new TreeMultimap<V, N, TREE>({ ...this.options, ...options }) as TREE;
77
+ return new TreeMultimap<V, N, TREE>([], {
78
+ iterationType: this.iterationType,
79
+ comparator: this.comparator, ...options
80
+ }) as TREE;
81
81
  }
82
82
 
83
83
  /**
@@ -89,127 +89,62 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
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(1) - constant time, as it performs basic pointer assignments.
173
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
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.
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
  /**
211
- * 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.
212
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
146
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
147
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
213
148
  */
214
149
 
215
150
  /**
@@ -223,7 +158,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
223
158
  * values:
224
159
  * @returns a boolean value.
225
160
  */
226
- override perfectlyBalance(iterationType = this.options.iterationType): boolean {
161
+ override perfectlyBalance(iterationType = this.iterationType): boolean {
227
162
  const sorted = this.dfs(node => node, 'in'),
228
163
  n = sorted.length;
229
164
  if (sorted.length < 1) return false;
@@ -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
  }
@@ -262,8 +197,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
262
197
  }
263
198
 
264
199
  /**
265
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
266
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
200
+ * 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.
201
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
267
202
  */
268
203
 
269
204
  /**
@@ -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;
@@ -346,8 +281,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
346
281
  }
347
282
 
348
283
  /**
349
- * 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.
350
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
284
+ * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
285
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
351
286
  */
352
287
 
353
288
  /**
@@ -373,8 +308,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
373
308
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
374
309
  * added, or `undefined` if no node was added.
375
310
  */
376
- protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
377
- parent = this.ensureNotKey(parent);
311
+ protected override _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined {
312
+ parent = this.ensureNode(parent);
378
313
  if (parent) {
379
314
  if (parent.left === undefined) {
380
315
  parent.left = newNode;
@@ -400,7 +335,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
400
335
  }
401
336
 
402
337
  /**
403
- * 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.
404
339
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
405
340
  * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
406
341
  * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
@@ -408,9 +343,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
408
343
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
409
344
  * if either `srcNode` or `destNode` is undefined.
410
345
  */
411
- protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
412
- srcNode = this.ensureNotKey(srcNode);
413
- 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);
414
349
  if (srcNode && destNode) {
415
350
  const { key, value, count, height } = destNode;
416
351
  const tempNode = this.createNode(key, value, count);
@@ -432,4 +367,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
432
367
  }
433
368
  return undefined;
434
369
  }
370
+
371
+ protected _replaceNode(oldNode: N, newNode: N): N {
372
+ newNode.count = oldNode.count + newNode.count
373
+ return super._replaceNode(oldNode, newNode);
374
+ }
435
375
  }
@@ -691,7 +691,7 @@ export abstract class AbstractGraph<
691
691
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
692
692
  }
693
693
 
694
- const heap = new PriorityQueue<{ key: number; value: VO }>({ comparator: (a, b) => a.key - b.key });
694
+ const heap = new PriorityQueue<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key });
695
695
  heap.add({ key: 0, value: srcVertex });
696
696
 
697
697
  distMap.set(srcVertex, 0);
@@ -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)