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