doubly-linked-list-typed 1.54.0 → 1.54.2

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 (92) hide show
  1. package/LICENSE +1 -1
  2. package/coverage/lcov-report/index.ts.html +2 -2
  3. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  4. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
  6. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
  8. package/dist/data-structures/binary-tree/avl-tree.js +110 -47
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
  12. package/dist/data-structures/binary-tree/binary-tree.js +269 -240
  13. package/dist/data-structures/binary-tree/bst.d.ts +145 -112
  14. package/dist/data-structures/binary-tree/bst.js +180 -129
  15. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  16. package/dist/data-structures/binary-tree/index.js +2 -0
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
  18. package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  20. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
  23. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/directed-graph.js +3 -0
  25. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/map-graph.js +3 -0
  27. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  28. package/dist/data-structures/graph/undirected-graph.js +3 -0
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  31. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  32. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  33. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  34. package/dist/data-structures/matrix/matrix.js +3 -0
  35. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  36. package/dist/data-structures/matrix/navigator.js +3 -0
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  41. package/dist/data-structures/trie/trie.d.ts +0 -4
  42. package/dist/data-structures/trie/trie.js +0 -4
  43. package/dist/index.d.ts +2 -2
  44. package/dist/index.js +2 -3
  45. package/dist/interfaces/binary-tree.d.ts +8 -8
  46. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  48. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  49. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  50. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  51. package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
  52. package/dist/types/data-structures/binary-tree/index.js +3 -1
  53. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
  55. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  57. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  58. package/package.json +3 -3
  59. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  60. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
  61. package/src/data-structures/binary-tree/avl-tree.ts +144 -93
  62. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  63. package/src/data-structures/binary-tree/binary-tree.ts +433 -405
  64. package/src/data-structures/binary-tree/bst.ts +261 -239
  65. package/src/data-structures/binary-tree/index.ts +2 -0
  66. package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
  67. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  68. package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
  69. package/src/data-structures/graph/directed-graph.ts +3 -0
  70. package/src/data-structures/graph/map-graph.ts +3 -0
  71. package/src/data-structures/graph/undirected-graph.ts +3 -0
  72. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  73. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  74. package/src/data-structures/matrix/matrix.ts +3 -0
  75. package/src/data-structures/matrix/navigator.ts +3 -0
  76. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  77. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  78. package/src/data-structures/trie/trie.ts +0 -4
  79. package/src/index.ts +2 -3
  80. package/src/interfaces/binary-tree.ts +10 -24
  81. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  82. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  83. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  84. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  85. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  86. package/src/types/data-structures/binary-tree/index.ts +3 -1
  87. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  88. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  89. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  90. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
  91. package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
  92. /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
@@ -4,391 +4,186 @@ exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
4
4
  const avl_tree_1 = require("./avl-tree");
5
5
  class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
6
6
  /**
7
- * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
8
- * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
9
- * of the binary tree node.
10
- * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
11
- * tree node. If no value is provided, it will be `undefined`.
12
- * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
13
- * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
14
- * parameter when creating a new instance of the `BinaryTreeNode` class.
15
- */
16
- constructor(key, value, count = 1) {
7
+ * This TypeScript constructor initializes an object with a key of type K and an array of values of
8
+ * type V.
9
+ * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
10
+ * data being stored in the data structure. It helps in quickly accessing or retrieving the
11
+ * associated value in the data structure.
12
+ * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
13
+ * type `V`.
14
+ */
15
+ constructor(key, value) {
17
16
  super(key, value);
18
- this.count = count;
17
+ this.parent = undefined;
18
+ this._left = undefined;
19
+ this._right = undefined;
20
+ }
21
+ get left() {
22
+ return this._left;
23
+ }
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
29
+ }
30
+ get right() {
31
+ return this._right;
32
+ }
33
+ set right(v) {
34
+ if (v) {
35
+ v.parent = this;
36
+ }
37
+ this._right = v;
19
38
  }
20
39
  }
21
40
  exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
22
41
  /**
23
- * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
42
+ *
24
43
  */
25
44
  class AVLTreeMultiMap extends avl_tree_1.AVLTree {
26
45
  /**
27
- * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
28
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
29
- * iterable object that can contain either keys, nodes, entries, or raw elements.
30
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
31
- * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
32
- * `compareValues` functions to define custom comparison logic for keys and values, respectively.
46
+ * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
47
+ * and options.
48
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
49
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
50
+ * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
51
+ * the AVLTreeMulti
52
+ * @param [options] - The `options` parameter in the constructor is of type
53
+ * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
54
+ * additional options for configuring the AVLTreeMultiMap instance.
33
55
  */
34
56
  constructor(keysNodesEntriesOrRaws = [], options) {
35
- super([], options);
36
- this._count = 0;
37
- if (keysNodesEntriesOrRaws)
57
+ super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
58
+ if (keysNodesEntriesOrRaws) {
38
59
  this.addMany(keysNodesEntriesOrRaws);
60
+ }
39
61
  }
40
62
  /**
41
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
42
- * search.
43
- * @returns the sum of the count property of all nodes in the tree.
44
- */
45
- get count() {
46
- return this._count;
47
- }
48
- /**
49
- * Time Complexity: O(n)
63
+ * Time Complexity: O(1)
50
64
  * Space Complexity: O(1)
51
65
  *
52
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
53
- * search.
54
- * @returns the sum of the count property of all nodes in the tree.
55
- */
56
- getComputedCount() {
57
- let sum = 0;
58
- this.dfs(node => (sum += node.count));
59
- return sum;
60
- }
61
- /**
62
- * The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
63
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
64
- * which is a generic type that can be replaced with any specific type when using the function.
65
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
66
- * associated with the key in the node. It is of type `V`, which can be any data type.
67
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a
68
- * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
69
- * calling the `createNode` method. If provided, it specifies the initial count for the node.
70
- * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
71
- */
72
- createNode(key, value, count) {
73
- return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count);
74
- }
75
- /**
76
- * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
77
- * @param [options] - The `options` parameter is an optional object that contains additional
78
- * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
79
- * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
80
- * object.
66
+ * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
67
+ * specified options.
68
+ * @param [options] - The `options` parameter in the `createTree` function is of type
69
+ * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
70
+ * `K`, `V[]`, and `R`. The function creates a new `AVL
71
+ * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
72
+ * provided options.
81
73
  */
82
74
  createTree(options) {
83
- return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
75
+ return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
84
76
  }
85
77
  /**
86
- * The function checks if the input is an instance of AVLTreeMultiMapNode.
87
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
88
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
89
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
90
- * an instance of the `AVLTreeMultiMapNode` class.
91
- */
92
- isNode(keyNodeEntryOrRaw) {
93
- return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
94
- }
95
- /**
96
- * Time Complexity: O(log n)
78
+ * Time Complexity: O(1)
97
79
  * Space Complexity: O(1)
98
80
  *
99
- * The function overrides the add method of a TypeScript class to add a new node to a data structure
100
- * and update the count.
101
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
102
- * `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
103
- * can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
104
- * entry, or raw element
105
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
106
- * data structure. It is an optional parameter, so it can be omitted if not needed.
107
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
108
- * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
109
- * be added once. However, you can specify a different value for `count` if you want to add
110
- * @returns a boolean value.
81
+ * The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
82
+ * specified key and an empty array of values.
83
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
84
+ * that will be created in the AVLTreeMultiMap.
85
+ * @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
86
+ * empty array.
111
87
  */
112
- add(keyNodeEntryOrRaw, value, count = 1) {
113
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
114
- if (newNode === undefined)
115
- return false;
116
- const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
117
- const inserted = super.add(newNode, newValue);
118
- if (inserted) {
119
- this._count += orgNodeCount;
120
- }
121
- return true;
88
+ createNode(key) {
89
+ return new AVLTreeMultiMapNode(key, []);
122
90
  }
123
91
  /**
124
92
  * Time Complexity: O(log n)
125
- * Space Complexity: O(1)
93
+ * Space Complexity: O(log n)
126
94
  *
127
- * The function overrides the delete method in a binary tree data structure, handling deletion of
128
- * nodes and maintaining balance in the tree.
129
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
130
- * parameter in the `delete` method is used to specify the condition for deleting a node from the
131
- * binary tree. It can be a key, node, or entry that determines which
132
- * node(s) should be deleted.
133
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
134
- * boolean flag that determines whether to ignore the count of the node being deleted. If
135
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
136
- * `ignoreCount` is set to
137
- * @returns The `delete` method overrides the default delete behavior in a binary tree data
138
- * structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
139
- * method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
140
- * deleted node and whether balancing is needed in the tree.
141
- */
142
- delete(keyNodeEntryOrRaw, ignoreCount = false) {
143
- var _a;
144
- const deletedResult = [];
145
- if (!this.root)
146
- return deletedResult;
147
- const curr = (_a = this.getNode(keyNodeEntryOrRaw)) !== null && _a !== void 0 ? _a : undefined;
148
- if (!curr)
149
- return deletedResult;
150
- const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
151
- let needBalanced = undefined, orgCurrent = curr;
152
- if (curr.count > 1 && !ignoreCount) {
153
- curr.count--;
154
- this._count--;
155
- }
156
- else {
157
- if (!curr.left) {
158
- if (!parent) {
159
- if (curr.right !== undefined && curr.right !== null)
160
- this._setRoot(curr.right);
95
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
96
+ * tree multi-map.
97
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
98
+ * parameter in the `override add` method can be either a key-value pair entry or just a key. If it
99
+ * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
100
+ * `values`
101
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
102
+ * you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
103
+ * with a specific key.
104
+ * @returns The `override add` method is returning a boolean value, which indicates whether the
105
+ * addition operation was successful or not.
106
+ */
107
+ add(keyNodeOrEntry, value) {
108
+ if (this.isRealNode(keyNodeOrEntry))
109
+ return super.add(keyNodeOrEntry);
110
+ const _commonAdd = (key, values) => {
111
+ if (key === undefined || key === null)
112
+ return false;
113
+ const existingValues = this.get(key);
114
+ if (existingValues !== undefined && values !== undefined) {
115
+ for (const value of values)
116
+ existingValues.push(value);
117
+ return true;
118
+ }
119
+ const existingNode = this.getNode(key);
120
+ if (this.isRealNode(existingNode)) {
121
+ if (existingValues === undefined) {
122
+ super.add(key, values);
123
+ return true;
124
+ }
125
+ if (values !== undefined) {
126
+ for (const value of values)
127
+ existingValues.push(value);
128
+ return true;
161
129
  }
162
130
  else {
163
- const { familyPosition: fp } = curr;
164
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
165
- parent.left = curr.right;
166
- }
167
- else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
168
- parent.right = curr.right;
169
- }
170
- needBalanced = parent;
131
+ return false;
171
132
  }
172
133
  }
173
134
  else {
174
- const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined;
175
- if (leftSubTreeRightMost) {
176
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
177
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
178
- if (parentOfLeftSubTreeMax) {
179
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
180
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
181
- }
182
- else {
183
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
184
- }
185
- needBalanced = parentOfLeftSubTreeMax;
186
- }
187
- }
135
+ return super.add(key, values);
188
136
  }
189
- this._size = this._size - 1;
190
- // TODO How to handle when the count of target node is lesser than current node's count
191
- if (orgCurrent)
192
- this._count -= orgCurrent.count;
193
- }
194
- deletedResult.push({ deleted: orgCurrent, needBalanced });
195
- if (needBalanced) {
196
- this._balancePath(needBalanced);
137
+ };
138
+ if (this.isEntry(keyNodeOrEntry)) {
139
+ const [key, values] = keyNodeOrEntry;
140
+ return _commonAdd(key, value !== undefined ? [value] : values);
197
141
  }
198
- return deletedResult;
199
- }
200
- /**
201
- * Time Complexity: O(1)
202
- * Space Complexity: O(1)
203
- *
204
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
205
- * zero.
206
- */
207
- clear() {
208
- super.clear();
209
- this._count = 0;
142
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
210
143
  }
211
144
  /**
212
- * Time Complexity: O(n log n)
145
+ * Time Complexity: O(log n)
213
146
  * Space Complexity: O(log n)
214
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
215
- * tree using either a recursive or iterative approach.
216
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
217
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
218
- * default value of `this.iterationType`, which means it will use the iteration type currently set in
219
- * the object.
220
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
221
- * balancing operation is successful, and `false` if there are no nodes to balance.
222
- */
223
- perfectlyBalance(iterationType = this.iterationType) {
224
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
225
- if (sorted.length < 1)
226
- return false;
227
- this.clear();
228
- if (iterationType === 'RECURSIVE') {
229
- const buildBalanceBST = (l, r) => {
230
- if (l > r)
231
- return;
232
- const m = l + Math.floor((r - l) / 2);
233
- const midNode = sorted[m];
234
- if (this._isMapMode)
235
- this.add(midNode.key, undefined, midNode.count);
236
- else
237
- this.add(midNode.key, midNode.value, midNode.count);
238
- buildBalanceBST(l, m - 1);
239
- buildBalanceBST(m + 1, r);
240
- };
241
- buildBalanceBST(0, n - 1);
242
- return true;
243
- }
244
- else {
245
- const stack = [[0, n - 1]];
246
- while (stack.length > 0) {
247
- const popped = stack.pop();
248
- if (popped) {
249
- const [l, r] = popped;
250
- if (l <= r) {
251
- const m = l + Math.floor((r - l) / 2);
252
- const midNode = sorted[m];
253
- if (this._isMapMode)
254
- this.add(midNode.key, undefined, midNode.count);
255
- else
256
- this.add(midNode.key, midNode.value, midNode.count);
257
- stack.push([m + 1, r]);
258
- stack.push([l, m - 1]);
259
- }
260
- }
261
- }
147
+ *
148
+ * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
149
+ * structure and deletes the entire node if no values are left for that key.
150
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
151
+ * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
152
+ * pair in the AVLTreeMultiMapNode, or just the key itself.
153
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
154
+ * value that you want to delete from the multi-map data structure associated with a particular key.
155
+ * The function checks if the value exists in the array of values associated with the key, and if
156
+ * found, removes it from the array.
157
+ * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
158
+ * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
159
+ * the value was not found in the array, it returns `false`.
160
+ */
161
+ deleteValue(keyNodeOrEntry, value) {
162
+ const values = this.get(keyNodeOrEntry);
163
+ if (Array.isArray(values)) {
164
+ const index = values.indexOf(value);
165
+ if (index === -1)
166
+ return false;
167
+ values.splice(index, 1);
168
+ // If no values left, remove the entire node
169
+ if (values.length === 0)
170
+ this.delete(keyNodeOrEntry);
262
171
  return true;
263
172
  }
173
+ return false;
264
174
  }
265
175
  /**
266
- * Time complexity: O(n)
267
- * Space complexity: O(n)
176
+ * Time Complexity: O(n)
177
+ * Space Complexity: O(n)
268
178
  *
269
- * The function overrides the clone method to create a deep copy of a tree object.
270
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
179
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
180
+ * structure.
181
+ * @returns A cloned tree object is being returned.
271
182
  */
272
183
  clone() {
273
184
  const cloned = this.createTree();
274
- if (this._isMapMode)
275
- this.bfs(node => cloned.add(node.key, undefined, node.count));
276
- else
277
- this.bfs(node => cloned.add(node.key, node.value, node.count));
278
- if (this._isMapMode)
279
- cloned._store = this._store;
185
+ this._clone(cloned);
280
186
  return cloned;
281
187
  }
282
- /**
283
- * The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
284
- * with modified entries based on a provided callback.
285
- * @param callback - The `callback` parameter is a function that will be called for each entry in the
286
- * AVLTreeMultiMap. It takes four arguments:
287
- * @param [options] - The `options` parameter in the `override map` function is of type
288
- * `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
289
- * configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
290
- * These options
291
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
292
- * the value of `this` when executing the `callback` function. It allows you to set the context
293
- * (value of `this`) for the callback function. This can be useful when you want to access properties
294
- * or
295
- * @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
296
- * transformed by the provided `callback` function. Each entry in the original tree is passed to the
297
- * `callback` function along with the index and the original tree itself. The transformed entries are
298
- * then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
299
- */
300
- map(callback, options, thisArg) {
301
- const newTree = new AVLTreeMultiMap([], options);
302
- let index = 0;
303
- for (const [key, value] of this) {
304
- newTree.add(callback.call(thisArg, key, value, index++, this));
305
- }
306
- return newTree;
307
- }
308
- /**
309
- * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
310
- * a node object.
311
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
312
- * `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
313
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
314
- * `override` function. It represents the value associated with the key in the data structure. If no
315
- * value is provided, it will default to `undefined`.
316
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
317
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
318
- * @returns either a NODE object or undefined.
319
- */
320
- _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
321
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
322
- return [undefined, undefined];
323
- if (this.isNode(keyNodeEntryOrRaw))
324
- return [keyNodeEntryOrRaw, value];
325
- if (this.isEntry(keyNodeEntryOrRaw)) {
326
- const [key, entryValue] = keyNodeEntryOrRaw;
327
- if (key === undefined || key === null)
328
- return [undefined, undefined];
329
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
330
- return [this.createNode(key, finalValue, count), finalValue];
331
- }
332
- if (this.isRaw(keyNodeEntryOrRaw)) {
333
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
334
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
335
- if (this.isKey(key))
336
- return [this.createNode(key, finalValue, count), finalValue];
337
- }
338
- if (this.isKey(keyNodeEntryOrRaw))
339
- return [this.createNode(keyNodeEntryOrRaw, value, count), value];
340
- return [undefined, undefined];
341
- }
342
- /**
343
- * Time Complexity: O(1)
344
- * Space Complexity: O(1)
345
- *
346
- * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
347
- * in a binary search tree.
348
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
349
- * that will be swapped with the `destNode`.
350
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
351
- * node where the properties will be swapped with the source node.
352
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
353
- * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
354
- */
355
- _swapProperties(srcNode, destNode) {
356
- srcNode = this.ensureNode(srcNode);
357
- destNode = this.ensureNode(destNode);
358
- if (srcNode && destNode) {
359
- const { key, value, count, height } = destNode;
360
- const tempNode = this.createNode(key, value, count);
361
- if (tempNode) {
362
- tempNode.height = height;
363
- destNode.key = srcNode.key;
364
- if (!this._isMapMode)
365
- destNode.value = srcNode.value;
366
- destNode.count = srcNode.count;
367
- destNode.height = srcNode.height;
368
- srcNode.key = tempNode.key;
369
- if (!this._isMapMode)
370
- srcNode.value = tempNode.value;
371
- srcNode.count = tempNode.count;
372
- srcNode.height = tempNode.height;
373
- }
374
- return destNode;
375
- }
376
- return undefined;
377
- }
378
- /**
379
- * Time Complexity: O(1)
380
- * Space Complexity: O(1)
381
- *
382
- * The function replaces an old node with a new node and updates the count property of the new node.
383
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
384
- * data structure. It is of type NODE.
385
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
386
- * @returns The method is returning the result of calling the `_replaceNode` method from the
387
- * superclass, which is of type `NODE`.
388
- */
389
- _replaceNode(oldNode, newNode) {
390
- newNode.count = oldNode.count + newNode.count;
391
- return super._replaceNode(oldNode, newNode);
392
- }
393
188
  }
394
189
  exports.AVLTreeMultiMap = AVLTreeMultiMap;