heap-typed 1.53.9 → 1.54.1

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 (84) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  2. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
  6. package/dist/data-structures/binary-tree/avl-tree.js +126 -79
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
  10. package/dist/data-structures/binary-tree/binary-tree.js +273 -229
  11. package/dist/data-structures/binary-tree/bst.d.ts +141 -122
  12. package/dist/data-structures/binary-tree/bst.js +170 -134
  13. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  14. package/dist/data-structures/binary-tree/index.js +2 -0
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  16. package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  18. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
  21. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  22. package/dist/data-structures/graph/directed-graph.js +3 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/map-graph.js +3 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/undirected-graph.js +3 -0
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  28. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  31. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  32. package/dist/data-structures/matrix/matrix.js +3 -0
  33. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  34. package/dist/data-structures/matrix/navigator.js +3 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  39. package/dist/data-structures/trie/trie.d.ts +0 -4
  40. package/dist/data-structures/trie/trie.js +0 -4
  41. package/dist/interfaces/binary-tree.d.ts +7 -6
  42. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  43. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
  45. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  46. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  47. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  48. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  49. package/dist/types/data-structures/binary-tree/index.js +2 -0
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
  51. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  52. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
  57. package/src/data-structures/binary-tree/avl-tree.ts +152 -112
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  59. package/src/data-structures/binary-tree/binary-tree.ts +446 -379
  60. package/src/data-structures/binary-tree/bst.ts +224 -201
  61. package/src/data-structures/binary-tree/index.ts +2 -0
  62. package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
  63. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
  65. package/src/data-structures/graph/directed-graph.ts +3 -0
  66. package/src/data-structures/graph/map-graph.ts +3 -0
  67. package/src/data-structures/graph/undirected-graph.ts +3 -0
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  69. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/src/data-structures/matrix/matrix.ts +3 -0
  71. package/src/data-structures/matrix/navigator.ts +3 -0
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  74. package/src/data-structures/trie/trie.ts +0 -4
  75. package/src/interfaces/binary-tree.ts +10 -11
  76. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  80. package/src/types/data-structures/binary-tree/bst.ts +5 -3
  81. package/src/types/data-structures/binary-tree/index.ts +2 -0
  82. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
  83. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  84. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
@@ -5,495 +5,223 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {
9
- BinaryTreeDeleteResult,
10
- BSTNOptKeyOrNode,
11
- BTNRep,
12
- EntryCallback,
13
- IterationType,
14
- OptNode,
15
- RBTNColor,
16
- TreeMultiMapNodeNested,
17
- TreeMultiMapOptions
18
- } from '../../types';
19
- import { IBinaryTree } from '../../interfaces';
8
+ import type { BTNOptKeyOrNull, BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types';
20
9
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
10
+ import { IBinaryTree } from '../../interfaces';
21
11
 
22
- export class TreeMultiMapNode<
23
- K = any,
24
- V = any,
25
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
26
- > extends RedBlackTreeNode<K, V, NODE> {
12
+ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
27
13
  /**
28
- * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
29
- * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
30
- * used to identify and locate the node within the tree.
31
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
32
- * associated with the key in the Red-Black Tree node. It is not required and can be omitted when
33
- * creating a new node.
34
- * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
35
- * in the Red-Black Tree. It is an optional parameter with a default value of 1.
36
- * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
37
- * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
14
+ * This TypeScript constructor initializes an object with a key of type K and an array of values of
15
+ * type V.
16
+ * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
17
+ * data being stored in the data structure. It helps in quickly accessing or retrieving the
18
+ * associated value in the data structure.
19
+ * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
20
+ * type `V`.
38
21
  */
39
- constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
40
- super(key, value, color);
41
- this.count = count;
22
+ constructor(key: K, value: V[]) {
23
+ super(key, value);
42
24
  }
43
25
 
44
- protected _count: number = 1;
26
+ override parent?: TreeMultiMapNode<K, V> = undefined;
45
27
 
46
- /**
47
- * The function returns the value of the private variable _count.
48
- * @returns The count property of the object, which is of type number.
49
- */
50
- get count(): number {
51
- return this._count;
52
- }
28
+ override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
53
29
 
54
- /**
55
- * The above function sets the value of the count property.
56
- * @param {number} value - The value parameter is of type number, which means it can accept any
57
- * numeric value.
58
- */
59
- set count(value: number) {
60
- this._count = value;
30
+ override get left(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
31
+ return this._left;
61
32
  }
62
- }
63
33
 
64
- export class TreeMultiMap<
65
- K = any,
66
- V = any,
67
- R = object,
68
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>
69
- >
70
- extends RedBlackTree<K, V, R, NODE>
71
- implements IBinaryTree<K, V, R, NODE>
72
- {
73
- /**
74
- * The constructor function initializes a TreeMultiMap object with optional initial data.
75
- * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
76
- * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
77
- * TreeMultiMap with initial data.
78
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
79
- * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
80
- * `compareValues`, which are functions used to compare keys and values respectively.
81
- */
82
- constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE>> = [], options?: TreeMultiMapOptions<K, V, R>) {
83
- super([], options);
84
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
34
+ override set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
35
+ if (v) {
36
+ v.parent = this;
37
+ }
38
+ this._left = v;
85
39
  }
86
40
 
87
- protected _count = 0;
41
+ override _right?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
88
42
 
89
- // TODO the _count is not accurate after nodes count modified
90
- /**
91
- * The function calculates the sum of the count property of all nodes in a tree structure.
92
- * @returns the sum of the count property of all nodes in the tree.
93
- */
94
- get count(): number {
95
- return this._count;
43
+ override get right(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
44
+ return this._right;
96
45
  }
97
46
 
98
- /**
99
- * Time Complexity: O(n)
100
- * Space Complexity: O(1)
101
- *
102
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
103
- * search.
104
- * @returns the sum of the count property of all nodes in the tree.
105
- */
106
- getComputedCount(): number {
107
- let sum = 0;
108
- this.dfs(node => (sum += node.count));
109
- return sum;
47
+ override set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
48
+ if (v) {
49
+ v.parent = this;
50
+ }
51
+ this._right = v;
110
52
  }
53
+ }
111
54
 
55
+ /**
56
+ *
57
+ * @example
58
+ * // Find elements in a range
59
+ * const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
60
+ * console.log(tmm.search(new Range(5, 10))); // [5, 10, 7]
61
+ * console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7]
62
+ * console.log(tmm.search(new Range(15, 20))); // [15, 18]
63
+ */
64
+ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
65
+ extends RedBlackTree<K, V[], R, MK, MV[], MR>
66
+ implements IBinaryTree<K, V[], R, MK, MV, MR>
67
+ {
112
68
  /**
113
- * The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
114
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
115
- * which is a generic type representing the type of keys in the tree.
116
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
117
- * associated with the key in the node. It is of type `V`, which can be any data type.
118
- * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
119
- * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
120
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
121
- * the tree. It is an optional parameter and is used to keep track of the number of values associated
122
- * with a key in the tree.
123
- * @returns A new instance of the TreeMultiMapNode class, casted as NODE.
124
- */
125
- override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
126
- return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color) as NODE;
69
+ * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
70
+ * and options.
71
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
72
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
73
+ * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
74
+ * the RedBlackTreeMulti
75
+ * @param [options] - The `options` parameter in the constructor is of type
76
+ * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
77
+ * additional options for configuring the TreeMultiMap instance.
78
+ */
79
+ constructor(
80
+ keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R> = [],
81
+ options?: TreeMultiMapOptions<K, V[], R>
82
+ ) {
83
+ super([], { ...options, isMapMode: true });
84
+ if (keysNodesEntriesOrRaws) {
85
+ this.addMany(keysNodesEntriesOrRaws);
86
+ }
127
87
  }
128
88
 
129
89
  /**
130
- * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
131
- * @param [options] - The `options` parameter is an optional object that contains additional
132
- * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
133
- * R>`.
134
- * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
135
- * existing `iterationType` property. The returned value is casted as `TREE`.
136
- */
137
- // @ts-ignore
138
- override createTree(options?: TreeMultiMapOptions<K, V, R>) {
139
- return new TreeMultiMap<K, V, R, NODE>([], {
90
+ * Time Complexity: O(1)
91
+ * Space Complexity: O(1)
92
+ *
93
+ * The `createTree` function in TypeScript overrides the default implementation to create a new
94
+ * TreeMultiMap with specified options.
95
+ * @param [options] - The `options` parameter in the `createTree` method is of type
96
+ * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
97
+ * options when creating a new `TreeMultiMap` instance. It includes properties such as
98
+ * `iterationType`, `specifyComparable
99
+ * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
100
+ * data and the provided options merged with the existing properties of the current object.
101
+ */
102
+ override createTree(options?: TreeMultiMapOptions<K, V[], R>) {
103
+ return new TreeMultiMap<K, V, R, MK, MV, MR>([], {
140
104
  iterationType: this.iterationType,
141
- isMapMode: this._isMapMode,
142
105
  specifyComparable: this._specifyComparable,
143
106
  toEntryFn: this._toEntryFn,
107
+ isReverse: this._isReverse,
144
108
  ...options
145
109
  });
146
110
  }
147
111
 
148
112
  /**
149
- * The function checks if the input is an instance of the TreeMultiMapNode class.
150
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
151
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
152
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
153
- * an instance of the `TreeMultiMapNode` class.
154
- */
155
- override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
156
- return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
157
- }
158
-
159
- /**
160
- * Time Complexity: O(log n)
113
+ * Time Complexity: O(1)
161
114
  * Space Complexity: O(1)
162
115
  *
163
- * The function overrides the add method of a class and adds a new node to a data structure, updating
164
- * the count and returning a boolean indicating success.
165
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
166
- * `keyNodeEntryOrRaw` parameter can accept one of the following types:
167
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
168
- * data structure. It is an optional parameter, so it can be omitted if not needed.
169
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
170
- * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
171
- * for `count`, the key-value pair will be added once.
172
- * @returns The method is returning a boolean value. It returns true if the addition of the new node
173
- * was successful, and false otherwise.
116
+ * The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified
117
+ * key and an empty array of values.
118
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
119
+ * that will be created in the TreeMultiMap data structure.
120
+ * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and
121
+ * an empty array as its value.
174
122
  */
175
- override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
176
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
177
- const orgCount = newNode?.count || 0;
178
- const isSuccessAdded = super.add(newNode, newValue);
179
-
180
- if (isSuccessAdded) {
181
- this._count += orgCount;
182
- return true;
183
- } else {
184
- return false;
185
- }
123
+ override createNode(key: K): TreeMultiMapNode<K, V> {
124
+ return new TreeMultiMapNode<K, V>(key, []);
186
125
  }
187
126
 
127
+ override add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean;
128
+
129
+ override add(key: K, value: V): boolean;
130
+
188
131
  /**
189
132
  * Time Complexity: O(log n)
190
- * Space Complexity: O(1)
133
+ * Space Complexity: O(log n)
191
134
  *
192
- * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
193
- * structure, handling cases where nodes have children and maintaining balance in the tree.
194
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
195
- * parameter in the `delete` method is used to specify the condition or key based on which a node
196
- * should be deleted from the binary tree. It can be a key, a node, or an entry.
197
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
198
- * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
199
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
200
- * `ignoreCount` is `false
201
- * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
202
- */
203
- override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] {
204
- if (keyNodeEntryOrRaw === null) return [];
205
-
206
- const results: BinaryTreeDeleteResult<NODE>[] = [];
207
-
208
- let nodeToDelete: OptNode<NODE>;
209
- if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
210
- else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
211
-
212
- if (!nodeToDelete) {
213
- return results;
214
- }
215
-
216
- let originalColor = nodeToDelete.color;
217
- let replacementNode: NODE | undefined;
218
-
219
- if (!this.isRealNode(nodeToDelete.left)) {
220
- replacementNode = nodeToDelete.right;
221
- if (ignoreCount || nodeToDelete.count <= 1) {
222
- this._transplant(nodeToDelete, nodeToDelete.right);
223
- this._count -= nodeToDelete.count;
224
- } else {
225
- nodeToDelete.count--;
226
- this._count--;
227
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
228
- return results;
135
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
136
+ * TreeMultiMapNode, handling different input types and scenarios.
137
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
138
+ * parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
139
+ * of values, and a `TreeMultiMapNode`, or just a key.
140
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
141
+ * you want to add to the TreeMultiMap. If the key is already present in the map, the new value will
142
+ * be added to the existing list of values associated with that key. If the key is not present,
143
+ * @returns The `add` method is returning a boolean value, which indicates whether the operation was
144
+ * successful or not.
145
+ */
146
+ override add(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value?: V): boolean {
147
+ if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
148
+
149
+ const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
150
+ if (key === undefined || key === null) return false;
151
+
152
+ const existingValues = this.get(key);
153
+ if (existingValues !== undefined && values !== undefined) {
154
+ for (const value of values) existingValues.push(value);
155
+ return true;
229
156
  }
230
- } else if (!this.isRealNode(nodeToDelete.right)) {
231
- replacementNode = nodeToDelete.left;
232
- if (ignoreCount || nodeToDelete.count <= 1) {
233
- this._transplant(nodeToDelete, nodeToDelete.left);
234
- this._count -= nodeToDelete.count;
235
- } else {
236
- nodeToDelete.count--;
237
- this._count--;
238
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
239
- return results;
240
- }
241
- } else {
242
- const successor = this.getLeftMost(node => node, nodeToDelete.right);
243
- if (successor) {
244
- originalColor = successor.color;
245
- replacementNode = successor.right;
246
157
 
247
- if (successor.parent === nodeToDelete) {
248
- if (this.isRealNode(replacementNode)) {
249
- replacementNode.parent = successor;
250
- }
251
- } else {
252
- if (ignoreCount || nodeToDelete.count <= 1) {
253
- this._transplant(successor, successor.right);
254
- this._count -= nodeToDelete.count;
255
- } else {
256
- nodeToDelete.count--;
257
- this._count--;
258
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
259
- return results;
260
- }
261
- successor.right = nodeToDelete.right;
262
- if (this.isRealNode(successor.right)) {
263
- successor.right.parent = successor;
264
- }
158
+ const existingNode = this.getNode(key);
159
+ if (this.isRealNode(existingNode)) {
160
+ if (existingValues === undefined) {
161
+ super.add(key, values);
162
+ return true;
265
163
  }
266
- if (ignoreCount || nodeToDelete.count <= 1) {
267
- this._transplant(nodeToDelete, successor);
268
- this._count -= nodeToDelete.count;
164
+ if (values !== undefined) {
165
+ for (const value of values) existingValues.push(value);
166
+ return true;
269
167
  } else {
270
- nodeToDelete.count--;
271
- this._count--;
272
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
273
- return results;
274
- }
275
- successor.left = nodeToDelete.left;
276
- if (this.isRealNode(successor.left)) {
277
- successor.left.parent = successor;
168
+ return false;
278
169
  }
279
- successor.color = nodeToDelete.color;
170
+ } else {
171
+ return super.add(key, values);
280
172
  }
281
- }
282
- this._size--;
173
+ };
283
174
 
284
- // If the original color was black, fix the tree
285
- if (originalColor === 'BLACK') {
286
- this._deleteFixup(replacementNode);
175
+ if (this.isEntry(keyNodeOrEntry)) {
176
+ const [key, values] = keyNodeOrEntry;
177
+ return _commonAdd(key, value !== undefined ? [value] : values);
287
178
  }
288
179
 
289
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
290
-
291
- return results;
292
- }
293
-
294
- /**
295
- * Time Complexity: O(1)
296
- * Space Complexity: O(1)
297
- *
298
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
299
- * zero.
300
- */
301
- override clear() {
302
- super.clear();
303
- this._count = 0;
180
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
304
181
  }
305
182
 
306
183
  /**
307
- * Time Complexity: O(n log n)
184
+ * Time Complexity: O(log n)
308
185
  * Space Complexity: O(log n)
309
186
  *
310
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
311
- * tree using either a recursive or iterative approach.
312
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
313
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
314
- * default value of `this.iterationType`, which means it will use the iteration type specified by the
315
- * `iterationType` property of the current object.
316
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
317
- * balancing operation is successful, and `false` if there are no nodes to balance.
318
- */
319
- override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
320
- const sorted = this.dfs(node => node, 'IN'),
321
- n = sorted.length;
322
- if (sorted.length < 1) return false;
323
-
324
- this.clear();
325
-
326
- if (iterationType === 'RECURSIVE') {
327
- const buildBalanceBST = (l: number, r: number) => {
328
- if (l > r) return;
329
- const m = l + Math.floor((r - l) / 2);
330
- const midNode = sorted[m];
331
- if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
332
- else this.add(midNode.key, midNode.value, midNode.count);
333
- buildBalanceBST(l, m - 1);
334
- buildBalanceBST(m + 1, r);
335
- };
187
+ * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
188
+ * and deletes the entire node if no values are left for that key.
189
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
190
+ * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
191
+ * array of values, or just a key itself.
192
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
193
+ * value that you want to remove from the multi-map data structure associated with a particular key.
194
+ * The function checks if the value exists in the array of values associated with the key, and if
195
+ * found, removes it from the array.
196
+ * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
197
+ * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
198
+ */
199
+ deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean {
200
+ const values = this.get(keyNodeOrEntry);
201
+ if (Array.isArray(values)) {
202
+ const index = values.indexOf(value);
203
+ if (index === -1) return false;
204
+ values.splice(index, 1);
205
+
206
+ // If no values left, remove the entire node
207
+ if (values.length === 0) this.delete(keyNodeOrEntry);
336
208
 
337
- buildBalanceBST(0, n - 1);
338
- return true;
339
- } else {
340
- const stack: [[number, number]] = [[0, n - 1]];
341
- while (stack.length > 0) {
342
- const popped = stack.pop();
343
- if (popped) {
344
- const [l, r] = popped;
345
- if (l <= r) {
346
- const m = l + Math.floor((r - l) / 2);
347
- const midNode = sorted[m];
348
- if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
349
- else this.add(midNode.key, midNode.value, midNode.count);
350
- stack.push([m + 1, r]);
351
- stack.push([l, m - 1]);
352
- }
353
- }
354
- }
355
209
  return true;
356
210
  }
211
+ return false;
357
212
  }
358
213
 
359
214
  /**
360
- * Time complexity: O(n)
361
- * Space complexity: O(n)
215
+ * Time Complexity: O(n)
216
+ * Space Complexity: O(n)
362
217
  *
363
- * The function overrides the clone method to create a deep copy of a tree object.
364
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
218
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
219
+ * structure.
220
+ * @returns The `cloned` object is being returned.
365
221
  */
366
- // @ts-ignore
367
222
  override clone() {
368
223
  const cloned = this.createTree();
369
- this.bfs(node => cloned.add(node.key, undefined, node.count));
370
- if (this._isMapMode) cloned._store = this._store;
224
+ this._clone(cloned);
371
225
  return cloned;
372
226
  }
373
-
374
- /**
375
- * The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
376
- * modified entries based on a provided callback.
377
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
378
- * map. It takes four arguments:
379
- * @param [options] - The `options` parameter in the `override map` function is of type
380
- * `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
381
- * options when creating a new `TreeMultiMap` instance within the `map` function. These options could
382
- * include things like
383
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
384
- * the value of `this` when executing the `callback` function. It allows you to set the context
385
- * (value of `this`) for the callback function when it is called within the `map` function. This
386
- * @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
387
- * by the provided callback function.
388
- */
389
- // @ts-ignore
390
- override map<MK, MV, MR>(
391
- callback: EntryCallback<K, V | undefined, [MK, MV]>,
392
- options?: TreeMultiMapOptions<MK, MV, MR>,
393
- thisArg?: any
394
- ) {
395
- const newTree = new TreeMultiMap<MK, MV, MR>([], options);
396
- let index = 0;
397
- for (const [key, value] of this) {
398
- newTree.add(callback.call(thisArg, key, value, index++, this));
399
- }
400
- return newTree;
401
- }
402
-
403
- /**
404
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
405
- * node based on the input.
406
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
407
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
408
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
409
- * associated with the key in the node. It is used when creating a new node or updating the value of
410
- * an existing node.
411
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
412
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
413
- * @returns either a NODE object or undefined.
414
- */
415
- protected override _keyValueNodeEntryRawToNodeAndValue(
416
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
417
- value?: V,
418
- count = 1
419
- ): [NODE | undefined, V | undefined] {
420
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
421
-
422
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
423
-
424
- if (this.isEntry(keyNodeEntryOrRaw)) {
425
- const [key, entryValue] = keyNodeEntryOrRaw;
426
- if (key === undefined || key === null) return [undefined, undefined];
427
- const finalValue = value ?? entryValue;
428
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
429
- }
430
-
431
- if (this.isRaw(keyNodeEntryOrRaw)) {
432
- const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
433
- const finalValue = value ?? entryValue;
434
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
435
- }
436
-
437
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
438
-
439
- return [undefined, undefined];
440
- }
441
-
442
- /**
443
- * Time Complexity: O(1)
444
- * Space Complexity: O(1)
445
- *
446
- * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
447
- * in a binary search tree.
448
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
449
- * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
450
- * instance of the `BSTNOptKeyOrNode<K, NODE>` class.
451
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
452
- * node where the properties will be swapped with the source node.
453
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
454
- * If either `srcNode` or `destNode` is undefined, it returns undefined.
455
- */
456
- protected override _swapProperties(
457
- srcNode: R | BSTNOptKeyOrNode<K, NODE>,
458
- destNode: R | BSTNOptKeyOrNode<K, NODE>
459
- ): NODE | undefined {
460
- srcNode = this.ensureNode(srcNode);
461
- destNode = this.ensureNode(destNode);
462
- if (srcNode && destNode) {
463
- const { key, value, count, color } = destNode;
464
- const tempNode = this.createNode(key, value, color, count);
465
- if (tempNode) {
466
- tempNode.color = color;
467
-
468
- destNode.key = srcNode.key;
469
- if (!this._isMapMode) destNode.value = srcNode.value;
470
- destNode.count = srcNode.count;
471
- destNode.color = srcNode.color;
472
-
473
- srcNode.key = tempNode.key;
474
- if (!this._isMapMode) srcNode.value = tempNode.value;
475
- srcNode.count = tempNode.count;
476
- srcNode.color = tempNode.color;
477
- }
478
-
479
- return destNode;
480
- }
481
- return undefined;
482
- }
483
-
484
- /**
485
- * Time Complexity: O(1)
486
- * Space Complexity: O(1)
487
- *
488
- * The function replaces an old node with a new node and updates the count property of the new node.
489
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
490
- * structure.
491
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
492
- * @returns The method is returning the result of calling the `_replaceNode` method from the
493
- * superclass, which is of type `NODE`.
494
- */
495
- protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
496
- newNode.count = oldNode.count + newNode.count;
497
- return super._replaceNode(oldNode, newNode);
498
- }
499
227
  }