queue-typed 1.54.2 → 2.0.0

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/base/iterable-element-base.d.ts +14 -40
  2. package/dist/data-structures/base/iterable-element-base.js +14 -11
  3. package/dist/data-structures/base/linear-base.d.ts +277 -0
  4. package/dist/data-structures/base/linear-base.js +552 -0
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
  10. package/dist/data-structures/binary-tree/avl-tree.js +76 -8
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  12. package/dist/data-structures/binary-tree/binary-tree.js +244 -149
  13. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  14. package/dist/data-structures/binary-tree/bst.js +89 -133
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  16. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  18. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
  21. package/dist/data-structures/graph/abstract-graph.js +2 -2
  22. package/dist/data-structures/heap/heap.d.ts +3 -11
  23. package/dist/data-structures/heap/heap.js +0 -10
  24. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  25. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  26. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  27. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  28. package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  29. package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
  30. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  31. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  32. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  33. package/dist/data-structures/queue/deque.d.ts +130 -91
  34. package/dist/data-structures/queue/deque.js +269 -169
  35. package/dist/data-structures/queue/queue.d.ts +84 -40
  36. package/dist/data-structures/queue/queue.js +134 -50
  37. package/dist/data-structures/stack/stack.d.ts +3 -11
  38. package/dist/data-structures/stack/stack.js +0 -10
  39. package/dist/data-structures/trie/trie.d.ts +4 -3
  40. package/dist/data-structures/trie/trie.js +3 -0
  41. package/dist/types/data-structures/base/base.d.ts +9 -4
  42. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  46. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  47. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  48. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  49. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  50. package/dist/utils/utils.d.ts +2 -2
  51. package/package.json +2 -2
  52. package/src/data-structures/base/iterable-element-base.ts +29 -20
  53. package/src/data-structures/base/linear-base.ts +649 -0
  54. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  55. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  56. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  57. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  58. package/src/data-structures/binary-tree/bst.ts +150 -152
  59. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  60. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  61. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  62. package/src/data-structures/graph/abstract-graph.ts +2 -2
  63. package/src/data-structures/heap/heap.ts +3 -14
  64. package/src/data-structures/heap/max-heap.ts +2 -2
  65. package/src/data-structures/heap/min-heap.ts +2 -2
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  67. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  70. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  71. package/src/data-structures/queue/deque.ts +286 -183
  72. package/src/data-structures/queue/queue.ts +149 -63
  73. package/src/data-structures/stack/stack.ts +3 -18
  74. package/src/data-structures/trie/trie.ts +7 -3
  75. package/src/types/data-structures/base/base.ts +17 -8
  76. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  80. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  81. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  82. package/src/types/data-structures/queue/deque.ts +2 -3
  83. package/src/types/data-structures/queue/queue.ts +2 -2
  84. package/src/utils/utils.ts +2 -2
@@ -9,15 +9,15 @@ import type {
9
9
  AVLTreeCounterOptions,
10
10
  BinaryTreeDeleteResult,
11
11
  BSTNOptKeyOrNode,
12
- BTNRep,
13
12
  EntryCallback,
14
- IterationType,
15
- OptNodeOrNull
13
+ IterationType
16
14
  } from '../../types';
17
15
  import { IBinaryTree } from '../../interfaces';
18
16
  import { AVLTree, AVLTreeNode } from './avl-tree';
19
17
 
20
18
  export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
19
+ override parent?: AVLTreeCounterNode<K, V> = undefined;
20
+
21
21
  /**
22
22
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
23
23
  * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@@ -33,28 +33,26 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
33
33
  this.count = count;
34
34
  }
35
35
 
36
- override parent?: AVLTreeCounterNode<K, V> = undefined;
37
-
38
- override _left?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
36
+ override _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
39
37
 
40
- override get left(): OptNodeOrNull<AVLTreeCounterNode<K, V>> {
38
+ override get left(): AVLTreeCounterNode<K, V> | null | undefined {
41
39
  return this._left;
42
40
  }
43
41
 
44
- override set left(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) {
42
+ override set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
45
43
  if (v) {
46
44
  v.parent = this;
47
45
  }
48
46
  this._left = v;
49
47
  }
50
48
 
51
- override _right?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
49
+ override _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
52
50
 
53
- override get right(): OptNodeOrNull<AVLTreeCounterNode<K, V>> {
51
+ override get right(): AVLTreeCounterNode<K, V> | null | undefined {
54
52
  return this._right;
55
53
  }
56
54
 
57
- override set right(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) {
55
+ override set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
58
56
  if (v) {
59
57
  v.parent = this;
60
58
  }
@@ -78,7 +76,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
78
76
  * `compareValues` functions to define custom comparison logic for keys and values, respectively.
79
77
  */
80
78
  constructor(
81
- keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeCounterNode<K, V>> | R> = [],
79
+ keysNodesEntriesOrRaws: Iterable<
80
+ K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
81
+ > = [],
82
82
  options?: AVLTreeCounterOptions<K, V, R>
83
83
  ) {
84
84
  super([], options);
@@ -145,12 +145,14 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
145
145
 
146
146
  /**
147
147
  * The function checks if the input is an instance of AVLTreeCounterNode.
148
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter
149
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
148
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
149
+ * `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
150
150
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
151
151
  * an instance of the `AVLTreeCounterNode` class.
152
152
  */
153
- override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
153
+ override isNode(
154
+ keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
155
+ ): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
154
156
  return keyNodeOrEntry instanceof AVLTreeCounterNode;
155
157
  }
156
158
 
@@ -160,9 +162,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
160
162
  *
161
163
  * The function overrides the add method of a TypeScript class to add a new node to a data structure
162
164
  * and update the count.
163
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
165
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
164
166
  * `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
165
- * can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node,
167
+ * can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
166
168
  * entry, or raw element
167
169
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
168
170
  * data structure. It is an optional parameter, so it can be omitted if not needed.
@@ -171,7 +173,11 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
171
173
  * be added once. However, you can specify a different value for `count` if you want to add
172
174
  * @returns a boolean value.
173
175
  */
174
- override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, value?: V, count = 1): boolean {
176
+ override add(
177
+ keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
178
+ value?: V,
179
+ count = 1
180
+ ): boolean {
175
181
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
176
182
  if (newNode === undefined) return false;
177
183
 
@@ -189,7 +195,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
189
195
  *
190
196
  * The function overrides the delete method in a binary tree data structure, handling deletion of
191
197
  * nodes and maintaining balance in the tree.
192
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate`
198
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
193
199
  * parameter in the `delete` method is used to specify the condition for deleting a node from the
194
200
  * binary tree. It can be a key, node, or entry that determines which
195
201
  * node(s) should be deleted.
@@ -203,7 +209,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
203
209
  * deleted node and whether balancing is needed in the tree.
204
210
  */
205
211
  override delete(
206
- keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>,
212
+ keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
207
213
  ignoreCount = false
208
214
  ): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] {
209
215
  const deletedResult: BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] = [];
@@ -276,6 +282,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
276
282
  /**
277
283
  * Time Complexity: O(n log n)
278
284
  * Space Complexity: O(log n)
285
+ *
279
286
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
280
287
  * tree using either a recursive or iterative approach.
281
288
  * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
@@ -374,8 +381,8 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
374
381
  /**
375
382
  * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
376
383
  * a node object.
377
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
378
- * `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
384
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
385
+ * `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
379
386
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
380
387
  * `override` function. It represents the value associated with the key in the data structure. If no
381
388
  * value is provided, it will default to `undefined`.
@@ -384,7 +391,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
384
391
  * @returns either a AVLTreeCounterNode<K, V> object or undefined.
385
392
  */
386
393
  protected override _keyValueNodeOrEntryToNodeAndValue(
387
- keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>,
394
+ keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
388
395
  value?: V,
389
396
  count = 1
390
397
  ): [AVLTreeCounterNode<K, V> | undefined, V | undefined] {
@@ -5,11 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types';
8
+ import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
9
9
  import { AVLTree, AVLTreeNode } from './avl-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
 
12
12
  export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
13
+ override parent?: AVLTreeMultiMapNode<K, V> = undefined;
14
+
13
15
  /**
14
16
  * This TypeScript constructor initializes an object with a key of type K and an array of values of
15
17
  * type V.
@@ -23,28 +25,26 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
23
25
  super(key, value);
24
26
  }
25
27
 
26
- override parent?: AVLTreeMultiMapNode<K, V> = undefined;
28
+ override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
27
29
 
28
- override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
29
-
30
- override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
30
+ override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
31
31
  return this._left;
32
32
  }
33
33
 
34
- override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
34
+ override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
35
35
  if (v) {
36
36
  v.parent = this;
37
37
  }
38
38
  this._left = v;
39
39
  }
40
40
 
41
- override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
41
+ override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
42
42
 
43
- override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
43
+ override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
44
44
  return this._right;
45
45
  }
46
46
 
47
- override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
47
+ override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
48
48
  if (v) {
49
49
  v.parent = this;
50
50
  }
@@ -71,7 +71,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
71
71
  * additional options for configuring the AVLTreeMultiMap instance.
72
72
  */
73
73
  constructor(
74
- keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [],
74
+ keysNodesEntriesOrRaws: Iterable<
75
+ K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
76
+ > = [],
75
77
  options?: AVLTreeMultiMapOptions<K, V[], R>
76
78
  ) {
77
79
  super([], { ...options, isMapMode: true });
@@ -98,6 +100,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
98
100
  specifyComparable: this._specifyComparable,
99
101
  toEntryFn: this._toEntryFn,
100
102
  isReverse: this._isReverse,
103
+ isMapMode: this._isMapMode,
101
104
  ...options
102
105
  });
103
106
  }
@@ -106,18 +109,24 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
106
109
  * Time Complexity: O(1)
107
110
  * Space Complexity: O(1)
108
111
  *
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.
112
+ * The `createNode` function in TypeScript overrides the default implementation to create a new
113
+ * AVLTreeMultiMapNode with a specified key and value array.
114
+ * @param {K} key - The `key` parameter represents the key of the node being created in the
115
+ * AVLTreeMultiMap.
116
+ * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
117
+ * values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
118
+ * calling the method, an empty array `[]` is used as the default value.
119
+ * @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
120
+ * AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
121
+ * array is used.
115
122
  */
116
- override createNode(key: K): AVLTreeMultiMapNode<K, V> {
117
- return new AVLTreeMultiMapNode<K, V>(key, []);
123
+ override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
124
+ return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
118
125
  }
119
126
 
120
- override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean;
127
+ override add(
128
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
129
+ ): boolean;
121
130
 
122
131
  override add(key: K, value: V): boolean;
123
132
 
@@ -125,45 +134,58 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
125
134
  * Time Complexity: O(log n)
126
135
  * Space Complexity: O(log n)
127
136
  *
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.
137
+ * The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
138
+ * to an AVLTreeMultiMap, handling different input types and scenarios.
139
+ * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
140
+ * be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
141
+ * can be a single key value, a node of the AVLTree
142
+ * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
143
+ * that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
144
+ * specific key.
145
+ * @returns The `add` method is returning a boolean value, which indicates whether the operation was
146
+ * successful or not.
139
147
  */
140
- override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean {
148
+ override add(
149
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
150
+ value?: V
151
+ ): boolean {
141
152
  if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
142
153
 
143
154
  const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
144
155
  if (key === undefined || key === null) return false;
145
156
 
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
- }
151
-
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) {
157
+ const _addToValues = () => {
158
+ const existingValues = this.get(key);
159
+ if (existingValues !== undefined && values !== undefined) {
159
160
  for (const value of values) existingValues.push(value);
160
161
  return true;
162
+ }
163
+ return false;
164
+ };
165
+
166
+ const _addByNode = () => {
167
+ const existingNode = this.getNode(key);
168
+ if (this.isRealNode(existingNode)) {
169
+ const existingValues = this.get(existingNode);
170
+ if (existingValues === undefined) {
171
+ super.add(key, values);
172
+ return true;
173
+ }
174
+ if (values !== undefined) {
175
+ for (const value of values) existingValues.push(value);
176
+ return true;
177
+ } else {
178
+ return false;
179
+ }
161
180
  } else {
162
- return false;
181
+ return super.add(key, values);
163
182
  }
164
- } else {
165
- return super.add(key, values);
183
+ };
184
+
185
+ if (this._isMapMode) {
186
+ return _addByNode() || _addToValues();
166
187
  }
188
+ return _addToValues() || _addByNode();
167
189
  };
168
190
 
169
191
  if (this.isEntry(keyNodeOrEntry)) {
@@ -180,7 +202,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
180
202
  *
181
203
  * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
182
204
  * 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`
205
+ * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
184
206
  * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
185
207
  * pair in the AVLTreeMultiMapNode, or just the key itself.
186
208
  * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
@@ -191,7 +213,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
191
213
  * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
192
214
  * the value was not found in the array, it returns `false`.
193
215
  */
194
- deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean {
216
+ deleteValue(
217
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
218
+ value: V
219
+ ): boolean {
195
220
  const values = this.get(keyNodeOrEntry);
196
221
  if (Array.isArray(values)) {
197
222
  const index = values.indexOf(value);
@@ -6,17 +6,12 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
- import type {
10
- AVLTreeOptions,
11
- BinaryTreeDeleteResult,
12
- BSTNOptKeyOrNode,
13
- BTNRep,
14
- EntryCallback,
15
- OptNodeOrNull
16
- } from '../../types';
9
+ import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
17
10
  import { IBinaryTree } from '../../interfaces';
18
11
 
19
12
  export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
13
+ override parent?: AVLTreeNode<K, V> = undefined;
14
+
20
15
  /**
21
16
  * This TypeScript constructor function initializes an instance with a key and an optional value.
22
17
  * @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
@@ -30,28 +25,26 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
30
25
  super(key, value);
31
26
  }
32
27
 
33
- override parent?: AVLTreeNode<K, V> = undefined;
34
-
35
- override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
28
+ override _left?: AVLTreeNode<K, V> | null | undefined = undefined;
36
29
 
37
- override get left(): OptNodeOrNull<AVLTreeNode<K, V>> {
30
+ override get left(): AVLTreeNode<K, V> | null | undefined {
38
31
  return this._left;
39
32
  }
40
33
 
41
- override set left(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
34
+ override set left(v: AVLTreeNode<K, V> | null | undefined) {
42
35
  if (v) {
43
36
  v.parent = this;
44
37
  }
45
38
  this._left = v;
46
39
  }
47
40
 
48
- override _right?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
41
+ override _right?: AVLTreeNode<K, V> | null | undefined = undefined;
49
42
 
50
- override get right(): OptNodeOrNull<AVLTreeNode<K, V>> {
43
+ override get right(): AVLTreeNode<K, V> | null | undefined {
51
44
  return this._right;
52
45
  }
53
46
 
54
- override set right(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
47
+ override set right(v: AVLTreeNode<K, V> | null | undefined) {
55
48
  if (v) {
56
49
  v.parent = this;
57
50
  }
@@ -67,6 +60,70 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
67
60
  * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
68
61
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
69
62
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
63
+ * @example
64
+ * // Find elements in a range
65
+ * // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
66
+ * type Datum = { timestamp: Date; temperature: number };
67
+ * // Fixed dataset of CPU temperature readings
68
+ * const cpuData: Datum[] = [
69
+ * { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
70
+ * { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
71
+ * { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
72
+ * { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
73
+ * { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
74
+ * { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
75
+ * { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
76
+ * { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
77
+ * { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
78
+ * { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
79
+ * { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
80
+ * { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
81
+ * { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
82
+ * { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
83
+ * { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
84
+ * { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
85
+ * { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
86
+ * { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
87
+ * { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
88
+ * { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
89
+ * { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
90
+ * { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
91
+ * { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
92
+ * { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
93
+ * { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
94
+ * { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
95
+ * { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
96
+ * { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
97
+ * { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
98
+ * { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
99
+ * ];
100
+ *
101
+ * // Create an AVL tree to store CPU temperature data
102
+ * const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
103
+ * toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
104
+ * });
105
+ *
106
+ * // Query a specific time range (e.g., from 00:05 to 00:15)
107
+ * const rangeStart = new Date('2024-12-02T00:05:00');
108
+ * const rangeEnd = new Date('2024-12-02T00:15:00');
109
+ * const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
110
+ * minute: node ? node.key.getMinutes() : 0,
111
+ * temperature: cpuTemperatureTree.get(node ? node.key : undefined)
112
+ * }));
113
+ *
114
+ * console.log(rangeResults); // [
115
+ * // { minute: 5, temperature: 59.4 },
116
+ * // { minute: 6, temperature: 60.1 },
117
+ * // { minute: 7, temperature: 61.3 },
118
+ * // { minute: 8, temperature: 62 },
119
+ * // { minute: 9, temperature: 63.5 },
120
+ * // { minute: 10, temperature: 64 },
121
+ * // { minute: 11, temperature: 62.8 },
122
+ * // { minute: 12, temperature: 61.5 },
123
+ * // { minute: 13, temperature: 60.2 },
124
+ * // { minute: 14, temperature: 59.8 },
125
+ * // { minute: 15, temperature: 58.6 }
126
+ * // ]
70
127
  */
71
128
  export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
72
129
  extends BST<K, V, R, MK, MV, MR>
@@ -76,7 +133,8 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
76
133
  * This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
77
134
  * in an iterable format.
78
135
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
79
- * iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
136
+ * iterable that can contain either `
137
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
80
138
  * used to initialize the AVLTree with key-value pairs or raw data entries. If provided
81
139
  * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
82
140
  * R>`. It is an optional parameter that allows you to specify additional options for configuring the
@@ -84,7 +142,9 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
84
142
  * other configuration settings specific
85
143
  */
86
144
  constructor(
87
- keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [],
145
+ keysNodesEntriesOrRaws: Iterable<
146
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
147
+ > = [],
88
148
  options?: AVLTreeOptions<K, V, R>
89
149
  ) {
90
150
  super([], options);
@@ -133,12 +193,15 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
133
193
  * Space Complexity: O(1)
134
194
  *
135
195
  * The function checks if the input is an instance of AVLTreeNode.
136
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
137
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
196
+ * @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
197
+ * `keyNodeOrEntry` can be of type `R` or `
198
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
138
199
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
139
200
  * an instance of the `AVLTreeNode` class.
140
201
  */
141
- override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> {
202
+ override isNode(
203
+ keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
204
+ ): keyNodeOrEntry is AVLTreeNode<K, V> {
142
205
  return keyNodeOrEntry instanceof AVLTreeNode;
143
206
  }
144
207
 
@@ -148,13 +211,17 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
148
211
  *
149
212
  * The function overrides the add method of a class and inserts a key-value pair into a data
150
213
  * structure, then balances the path.
151
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
152
- * `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
214
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
215
+ * `keyNodeOrEntry` can accept values of type `R`, `
216
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
153
217
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
154
218
  * the key or node being added to the data structure.
155
219
  * @returns The method is returning a boolean value.
156
220
  */
157
- override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean {
221
+ override add(
222
+ keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
223
+ value?: V
224
+ ): boolean {
158
225
  if (keyNodeOrEntry === null) return false;
159
226
  const inserted = super.add(keyNodeOrEntry, value);
160
227
  if (inserted) this._balancePath(keyNodeOrEntry);
@@ -167,14 +234,16 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
167
234
  *
168
235
  * The function overrides the delete method in a TypeScript class, performs deletion, and then
169
236
  * balances the tree if necessary.
170
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
237
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
171
238
  * parameter in the `override delete` method can be one of the following types:
172
239
  * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
173
240
  * method from the superclass (presumably a parent class) with the provided `predicate`, which could
174
241
  * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
175
242
  * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
176
243
  */
177
- override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
244
+ override delete(
245
+ keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
246
+ ): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
178
247
  const deletedResults = super.delete(keyNodeOrEntry);
179
248
  for (const { needBalanced } of deletedResults) {
180
249
  if (needBalanced) {
@@ -487,10 +556,11 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
487
556
  *
488
557
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
489
558
  * to restore balance in an AVL tree after inserting a node.
490
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
491
- * `BTNRep<K, V, AVLTreeNode<K, V>>`.
559
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
560
+ * `
561
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
492
562
  */
493
- protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void {
563
+ protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void {
494
564
  node = this.ensureNode(node);
495
565
  const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
496
566
  for (let i = 0; i < path.length; i++) {