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