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