undirected-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
@@ -27,21 +27,23 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
27
27
  */
28
28
  class RedBlackTree extends bst_1.BST {
29
29
  /**
30
- * The constructor function initializes a Red-Black Tree with an optional set of options.
31
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
32
- * passed to the constructor. It is used to configure the RBTree object with specific options.
30
+ * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
31
+ * initializes the tree with optional elements and options.
32
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
33
+ * objects. It represents the initial elements that will be added to the RBTree during its
34
+ * construction. If this parameter is provided, the `addMany` method is called to add all the
35
+ * elements to the
36
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
37
+ * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
38
+ * only a subset of the properties defined in the `RBTreeOptions` interface.
33
39
  */
34
- constructor(options) {
35
- super(options);
40
+ constructor(elements, options) {
41
+ super([], options);
36
42
  this.Sentinel = new RedBlackTreeNode(NaN);
37
43
  this._size = 0;
38
- if (options) {
39
- this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
40
- }
41
- else {
42
- this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
43
- }
44
44
  this._root = this.Sentinel;
45
+ if (elements)
46
+ super.addMany(elements);
45
47
  }
46
48
  get root() {
47
49
  return this._root;
@@ -49,40 +51,60 @@ class RedBlackTree extends bst_1.BST {
49
51
  get size() {
50
52
  return this._size;
51
53
  }
54
+ /**
55
+ * The function creates a new Red-Black Tree node with the specified key, value, and color.
56
+ * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
57
+ * identify and compare nodes in the Red-Black Tree.
58
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
59
+ * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
60
+ * specific type when using the `createNode` method.
61
+ * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
62
+ * Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
63
+ * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
64
+ * value, and color.
65
+ */
52
66
  createNode(key, value, color = types_1.RBTNColor.BLACK) {
53
67
  return new RedBlackTreeNode(key, value, color);
54
68
  }
69
+ /**
70
+ * The function creates a Red-Black Tree with the specified options and returns it.
71
+ * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
72
+ * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
73
+ * class.
74
+ * @returns a new instance of a RedBlackTree object.
75
+ */
55
76
  createTree(options) {
56
- return new RedBlackTree(Object.assign(Object.assign({}, this.options), options));
77
+ return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
57
78
  }
58
79
  /**
59
80
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
60
81
  * Space Complexity: O(1)
61
82
  */
62
83
  /**
63
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
64
- * Space Complexity: O(1)
65
- *
66
- * The `add` function adds a new node to a Red-Black Tree data structure.
67
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
68
- * following types:
69
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
70
- * key in the node being added to the Red-Black Tree.
71
- * @returns The method returns either a node (`N`) or `undefined`.
84
+ * The function adds a node to a Red-Black Tree data structure.
85
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
86
+ * @returns The method `add` returns either an instance of `N` (the node that was added) or
87
+ * `undefined`.
72
88
  */
73
- add(keyOrNode, value) {
89
+ add(keyOrNodeOrEntry) {
74
90
  let node;
75
- if (this.isNodeKey(keyOrNode)) {
76
- node = this.createNode(keyOrNode, value, types_1.RBTNColor.RED);
91
+ if (this.isNodeKey(keyOrNodeOrEntry)) {
92
+ node = this.createNode(keyOrNodeOrEntry, undefined, types_1.RBTNColor.RED);
77
93
  }
78
- else if (keyOrNode instanceof RedBlackTreeNode) {
79
- node = keyOrNode;
94
+ else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
95
+ node = keyOrNodeOrEntry;
80
96
  }
81
- else if (keyOrNode === null) {
97
+ else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
82
98
  return;
83
99
  }
84
- else if (keyOrNode === undefined) {
85
- return;
100
+ else if (this.isEntry(keyOrNodeOrEntry)) {
101
+ const [key, value] = keyOrNodeOrEntry;
102
+ if (key === undefined || key === null) {
103
+ return;
104
+ }
105
+ else {
106
+ node = this.createNode(key, value, types_1.RBTNColor.RED);
107
+ }
86
108
  }
87
109
  else {
88
110
  return;
@@ -101,6 +123,9 @@ class RedBlackTree extends bst_1.BST {
101
123
  x = x === null || x === void 0 ? void 0 : x.right;
102
124
  }
103
125
  else {
126
+ if (node !== x) {
127
+ this._replaceNode(x, node);
128
+ }
104
129
  return;
105
130
  }
106
131
  }
@@ -204,6 +229,10 @@ class RedBlackTree extends bst_1.BST {
204
229
  // TODO
205
230
  return ans;
206
231
  }
232
+ /**
233
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
234
+ * Space Complexity: O(1)
235
+ */
207
236
  isRealNode(node) {
208
237
  return node !== this.Sentinel && node !== undefined;
209
238
  }
@@ -232,11 +261,11 @@ class RedBlackTree extends bst_1.BST {
232
261
  * `getNodes` method, which is called within the `getNode` method.
233
262
  * @returns a value of type `N`, `null`, or `undefined`.
234
263
  */
235
- getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
264
+ getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
236
265
  var _a;
237
266
  if (identifier instanceof binary_tree_1.BinaryTreeNode)
238
267
  callback = (node => node);
239
- beginRoot = this.ensureNotKey(beginRoot);
268
+ beginRoot = this.ensureNode(beginRoot);
240
269
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
241
270
  }
242
271
  /**
@@ -287,6 +316,10 @@ class RedBlackTree extends bst_1.BST {
287
316
  }
288
317
  return y;
289
318
  }
319
+ /**
320
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
321
+ * Space Complexity: O(1)
322
+ */
290
323
  clear() {
291
324
  this._root = this.Sentinel;
292
325
  this._size = 0;
@@ -520,5 +553,18 @@ class RedBlackTree extends bst_1.BST {
520
553
  }
521
554
  this.root.color = types_1.RBTNColor.BLACK;
522
555
  }
556
+ /**
557
+ * The function replaces an old node with a new node while preserving the color of the old node.
558
+ * @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
559
+ * data structure. It is of type `N`, which is the type of the nodes in the data structure.
560
+ * @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
561
+ * data structure.
562
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
563
+ * superclass, passing in the `oldNode` and `newNode` as arguments.
564
+ */
565
+ _replaceNode(oldNode, newNode) {
566
+ newNode.color = oldNode.color;
567
+ return super._replaceNode(oldNode, newNode);
568
+ }
523
569
  }
524
570
  exports.RedBlackTree = RedBlackTree;
@@ -5,7 +5,7 @@
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';
8
+ import type { BSTNodeKeyOrNode, BTNKey, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
9
  import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
@@ -27,14 +27,7 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
27
27
  * 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.
28
28
  */
29
29
  export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
30
- options: TreeMultimapOptions;
31
- /**
32
- * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
33
- * merge duplicated values.
34
- * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
35
- * TreeMultimap.
36
- */
37
- constructor(options?: TreeMultimapOptions);
30
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>);
38
31
  private _count;
39
32
  get count(): number;
40
33
  /**
@@ -56,38 +49,33 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
56
49
  * 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.
57
50
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
58
51
  *
59
- * The `add` function adds a new node to the tree multimap, updating the count if the key already
60
- * exists, and balances the tree if necessary.
61
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
62
- * following types:
63
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
64
- * being added to the tree. It is an optional parameter, so it can be omitted if not needed.
52
+ * The `add` function overrides the base class `add` function to add a new node to the tree multimap
53
+ * and update the count.
54
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
65
55
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
66
- * times the key-value pair should be added to the multimap. If not provided, the default value is 1.
67
- * @returns a node (`N`) or `undefined`.
56
+ * times the key or node or entry should be added to the multimap. If not provided, the default value
57
+ * is 1.
58
+ * @returns either a node (`N`) or `undefined`.
68
59
  */
69
- add(keyOrNode: BTNKey | N | null | undefined, value?: V, count?: number): N | undefined;
60
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
70
61
  /**
71
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
72
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
62
+ * 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.
63
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
73
64
  */
74
65
  /**
75
- * 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.
66
+ * 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.
76
67
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
77
68
  *
78
- * The function `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
79
- * returning an array of the inserted nodes.
80
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
81
- * of type BTNKey, N, or undefined.
82
- * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
83
- * keys or nodes being added. It is used to associate data with each key or node being added to the
84
- * TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
85
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
69
+ * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
70
+ * structure.
71
+ * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
72
+ * either keys, nodes, or entries.
73
+ * @returns The method is returning an array of type `N | undefined`.
86
74
  */
87
- addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[];
75
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
88
76
  /**
89
- * 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.
90
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
77
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
78
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
91
79
  */
92
80
  /**
93
81
  * 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.
@@ -100,10 +88,10 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
100
88
  * values:
101
89
  * @returns a boolean value.
102
90
  */
103
- perfectlyBalance(iterationType?: IterationType | undefined): boolean;
91
+ perfectlyBalance(iterationType?: IterationType): boolean;
104
92
  /**
105
- * 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.
106
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
93
+ * 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.
94
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
107
95
  */
108
96
  /**
109
97
  * 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.
@@ -126,8 +114,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
126
114
  */
127
115
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
128
116
  /**
129
- * 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.
130
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
117
+ * 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.
118
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
131
119
  */
132
120
  /**
133
121
  * The clear() function clears the contents of a data structure and sets the count to zero.
@@ -148,9 +136,9 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
148
136
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
149
137
  * added, or `undefined` if no node was added.
150
138
  */
151
- protected _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined;
139
+ protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
152
140
  /**
153
- * The `_swap` function swaps the key, value, count, and height properties between two nodes.
141
+ * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
154
142
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
155
143
  * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
156
144
  * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
@@ -158,5 +146,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
158
146
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
159
147
  * if either `srcNode` or `destNode` is undefined.
160
148
  */
161
- protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined;
149
+ protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
150
+ protected _replaceNode(oldNode: N, newNode: N): N;
162
151
  }
@@ -24,24 +24,17 @@ exports.TreeMultimapNode = TreeMultimapNode;
24
24
  * 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.
25
25
  */
26
26
  class TreeMultimap extends avl_tree_1.AVLTree {
27
- /**
28
- * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
29
- * merge duplicated values.
30
- * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
31
- * TreeMultimap.
32
- */
33
- constructor(options = { iterationType: types_1.IterationType.ITERATIVE }) {
34
- super(options);
27
+ constructor(elements, options) {
28
+ super([], options);
35
29
  this._count = 0;
36
- if (options) {
37
- this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
38
- }
39
- else {
40
- this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
41
- }
30
+ if (elements)
31
+ this.addMany(elements);
42
32
  }
33
+ // TODO the _count is not accurate after nodes count modified
43
34
  get count() {
44
- return this._count;
35
+ let sum = 0;
36
+ this.subTreeTraverse(node => sum += node.count);
37
+ return sum;
45
38
  }
46
39
  /**
47
40
  * The function creates a new BSTNode with the given key, value, and count.
@@ -56,7 +49,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
56
49
  return new TreeMultimapNode(key, value, count);
57
50
  }
58
51
  createTree(options) {
59
- return new TreeMultimap(Object.assign(Object.assign({}, this.options), options));
52
+ return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
60
53
  }
61
54
  /**
62
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.
@@ -66,131 +59,64 @@ class TreeMultimap extends avl_tree_1.AVLTree {
66
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.
67
60
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
68
61
  *
69
- * The `add` function adds a new node to the tree multimap, updating the count if the key already
70
- * exists, and balances the tree if necessary.
71
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
72
- * following types:
73
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
74
- * 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:
75
65
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
76
- * times the key-value pair should be added to the multimap. If not provided, the default value is 1.
77
- * @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`.
78
69
  */
79
- add(keyOrNode, value, count = 1) {
80
- if (keyOrNode === null)
81
- return undefined;
82
- let inserted = undefined, newNode;
83
- if (keyOrNode instanceof TreeMultimapNode) {
84
- 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;
85
74
  }
86
- else if (keyOrNode === undefined) {
87
- newNode = undefined;
75
+ else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
76
+ newNode = keyOrNodeOrEntry;
88
77
  }
89
- else {
90
- newNode = this.createNode(keyOrNode, value, count);
78
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
79
+ newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
91
80
  }
92
- if (!this.root) {
93
- this._setRoot(newNode);
94
- this._size = this.size + 1;
95
- if (newNode)
96
- this._count += newNode.count;
97
- 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
+ }
98
89
  }
99
90
  else {
100
- let cur = this.root;
101
- let traversing = true;
102
- while (traversing) {
103
- if (cur) {
104
- if (newNode) {
105
- if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
106
- cur.value = newNode.value;
107
- cur.count += newNode.count;
108
- this._count += newNode.count;
109
- traversing = false;
110
- inserted = cur;
111
- }
112
- else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
113
- // Traverse left of the node
114
- if (cur.left === undefined) {
115
- //Add to the left of the current node
116
- cur.left = newNode;
117
- this._size = this.size + 1;
118
- this._count += newNode.count;
119
- traversing = false;
120
- inserted = cur.left;
121
- }
122
- else {
123
- //Traverse the left of the current node
124
- if (cur.left)
125
- cur = cur.left;
126
- }
127
- }
128
- else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
129
- // Traverse right of the node
130
- if (cur.right === undefined) {
131
- //Add to the right of the current node
132
- cur.right = newNode;
133
- this._size = this.size + 1;
134
- this._count += newNode.count;
135
- traversing = false;
136
- inserted = cur.right;
137
- }
138
- else {
139
- //Traverse the left of the current node
140
- if (cur.right)
141
- cur = cur.right;
142
- }
143
- }
144
- }
145
- else {
146
- // TODO may need to support undefined inserted
147
- }
148
- }
149
- else {
150
- traversing = false;
151
- }
152
- }
91
+ return;
92
+ }
93
+ const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
94
+ const inserted = super.add(newNode);
95
+ if (inserted) {
96
+ this._count += orgNodeCount;
153
97
  }
154
- if (inserted)
155
- this._balancePath(inserted);
156
98
  return inserted;
157
99
  }
158
100
  /**
159
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
160
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
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.
102
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
161
103
  */
162
104
  /**
163
- * 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.
164
106
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
165
107
  *
166
- * The function `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
167
- * returning an array of the inserted nodes.
168
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
169
- * of type BTNKey, N, or undefined.
170
- * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
171
- * keys or nodes being added. It is used to associate data with each key or node being added to the
172
- * TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
173
- * @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`.
174
113
  */
175
- addMany(keysOrNodes, data) {
176
- const inserted = [];
177
- for (let i = 0; i < keysOrNodes.length; i++) {
178
- const keyOrNode = keysOrNodes[i];
179
- if (keyOrNode instanceof TreeMultimapNode) {
180
- inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
181
- continue;
182
- }
183
- if (keyOrNode === undefined) {
184
- inserted.push(this.add(NaN, undefined, 0));
185
- continue;
186
- }
187
- inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
188
- }
189
- return inserted;
114
+ addMany(keysOrNodesOrEntries) {
115
+ return super.addMany(keysOrNodesOrEntries);
190
116
  }
191
117
  /**
192
- * 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.
193
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
118
+ * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
119
+ * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
194
120
  */
195
121
  /**
196
122
  * 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.
@@ -203,7 +129,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
203
129
  * values:
204
130
  * @returns a boolean value.
205
131
  */
206
- perfectlyBalance(iterationType = this.options.iterationType) {
132
+ perfectlyBalance(iterationType = this.iterationType) {
207
133
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
208
134
  if (sorted.length < 1)
209
135
  return false;
@@ -214,7 +140,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
214
140
  return;
215
141
  const m = l + Math.floor((r - l) / 2);
216
142
  const midNode = sorted[m];
217
- this.add(midNode.key, midNode.value, midNode.count);
143
+ this.add([midNode.key, midNode.value], midNode.count);
218
144
  buildBalanceBST(l, m - 1);
219
145
  buildBalanceBST(m + 1, r);
220
146
  };
@@ -230,7 +156,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
230
156
  if (l <= r) {
231
157
  const m = l + Math.floor((r - l) / 2);
232
158
  const midNode = sorted[m];
233
- this.add(midNode.key, midNode.value, midNode.count);
159
+ this.add([midNode.key, midNode.value], midNode.count);
234
160
  stack.push([m + 1, r]);
235
161
  stack.push([l, m - 1]);
236
162
  }
@@ -240,8 +166,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
240
166
  }
241
167
  }
242
168
  /**
243
- * 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.
244
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
169
+ * 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.
170
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
245
171
  */
246
172
  /**
247
173
  * 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.
@@ -297,7 +223,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
297
223
  const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
298
224
  if (leftSubTreeRightMost) {
299
225
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
300
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
226
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
301
227
  if (parentOfLeftSubTreeMax) {
302
228
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
303
229
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -321,8 +247,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
321
247
  return deletedResult;
322
248
  }
323
249
  /**
324
- * 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.
325
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
250
+ * 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.
251
+ * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
326
252
  */
327
253
  /**
328
254
  * The clear() function clears the contents of a data structure and sets the count to zero.
@@ -347,7 +273,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
347
273
  * added, or `undefined` if no node was added.
348
274
  */
349
275
  _addTo(newNode, parent) {
350
- parent = this.ensureNotKey(parent);
276
+ parent = this.ensureNode(parent);
351
277
  if (parent) {
352
278
  if (parent.left === undefined) {
353
279
  parent.left = newNode;
@@ -374,7 +300,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
374
300
  }
375
301
  }
376
302
  /**
377
- * The `_swap` function swaps the key, value, count, and height properties between two nodes.
303
+ * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
378
304
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
379
305
  * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
380
306
  * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
@@ -382,9 +308,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
382
308
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
383
309
  * if either `srcNode` or `destNode` is undefined.
384
310
  */
385
- _swap(srcNode, destNode) {
386
- srcNode = this.ensureNotKey(srcNode);
387
- destNode = this.ensureNotKey(destNode);
311
+ _swapProperties(srcNode, destNode) {
312
+ srcNode = this.ensureNode(srcNode);
313
+ destNode = this.ensureNode(destNode);
388
314
  if (srcNode && destNode) {
389
315
  const { key, value, count, height } = destNode;
390
316
  const tempNode = this.createNode(key, value, count);
@@ -403,5 +329,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
403
329
  }
404
330
  return undefined;
405
331
  }
332
+ _replaceNode(oldNode, newNode) {
333
+ newNode.count = oldNode.count + newNode.count;
334
+ return super._replaceNode(oldNode, newNode);
335
+ }
406
336
  }
407
337
  exports.TreeMultimap = TreeMultimap;
@@ -594,7 +594,7 @@ class AbstractGraph {
594
594
  if (vertexOrKey instanceof AbstractVertex)
595
595
  distMap.set(vertexOrKey, Infinity);
596
596
  }
597
- const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.key - b.key });
597
+ const heap = new priority_queue_1.PriorityQueue([], { comparator: (a, b) => a.key - b.key });
598
598
  heap.add({ key: 0, value: srcVertex });
599
599
  distMap.set(srcVertex, 0);
600
600
  preMap.set(srcVertex, null);