priority-queue-typed 1.53.9 → 1.54.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  2. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
  6. package/dist/data-structures/binary-tree/avl-tree.js +126 -79
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
  10. package/dist/data-structures/binary-tree/binary-tree.js +273 -229
  11. package/dist/data-structures/binary-tree/bst.d.ts +141 -122
  12. package/dist/data-structures/binary-tree/bst.js +170 -134
  13. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  14. package/dist/data-structures/binary-tree/index.js +2 -0
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  16. package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  18. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
  21. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  22. package/dist/data-structures/graph/directed-graph.js +3 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/map-graph.js +3 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/undirected-graph.js +3 -0
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  28. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  31. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  32. package/dist/data-structures/matrix/matrix.js +3 -0
  33. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  34. package/dist/data-structures/matrix/navigator.js +3 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  39. package/dist/data-structures/trie/trie.d.ts +0 -4
  40. package/dist/data-structures/trie/trie.js +0 -4
  41. package/dist/interfaces/binary-tree.d.ts +7 -6
  42. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  43. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
  45. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  46. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  47. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  48. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  49. package/dist/types/data-structures/binary-tree/index.js +2 -0
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
  51. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  52. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
  57. package/src/data-structures/binary-tree/avl-tree.ts +152 -112
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  59. package/src/data-structures/binary-tree/binary-tree.ts +446 -379
  60. package/src/data-structures/binary-tree/bst.ts +224 -201
  61. package/src/data-structures/binary-tree/index.ts +2 -0
  62. package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
  63. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
  65. package/src/data-structures/graph/directed-graph.ts +3 -0
  66. package/src/data-structures/graph/map-graph.ts +3 -0
  67. package/src/data-structures/graph/undirected-graph.ts +3 -0
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  69. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/src/data-structures/matrix/matrix.ts +3 -0
  71. package/src/data-structures/matrix/navigator.ts +3 -0
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  74. package/src/data-structures/trie/trie.ts +0 -4
  75. package/src/interfaces/binary-tree.ts +10 -11
  76. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  80. package/src/types/data-structures/binary-tree/bst.ts +5 -3
  81. package/src/types/data-structures/binary-tree/index.ts +2 -0
  82. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
  83. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  84. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
@@ -7,50 +7,55 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type {
10
- AVLTreeNodeNested,
11
10
  AVLTreeOptions,
12
11
  BinaryTreeDeleteResult,
13
12
  BSTNOptKeyOrNode,
14
13
  BTNRep,
15
- EntryCallback
14
+ EntryCallback,
15
+ OptNodeOrNull
16
16
  } from '../../types';
17
17
  import { IBinaryTree } from '../../interfaces';
18
18
 
19
- export class AVLTreeNode<
20
- K = any,
21
- V = any,
22
- NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
23
- > extends BSTNode<K, V, NODE> {
19
+ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
24
20
  /**
25
- * The constructor function initializes a new instance of a class with a key and an optional value,
26
- * and sets the height property to 0.
27
- * @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
28
- * constructor. It is used to initialize the key property of the object being created.
29
- * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
30
- * value associated with the key in the constructor.
21
+ * This TypeScript constructor function initializes an instance with a key and an optional value.
22
+ * @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
23
+ * within a data structure. It serves as a reference or identifier for accessing or manipulating the
24
+ * associated value or data.
25
+ * @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
26
+ * have to be provided when creating an instance of the class. If a value is not provided, it will
27
+ * default to `undefined`.
31
28
  */
32
29
  constructor(key: K, value?: V) {
33
30
  super(key, value);
34
- this._height = 0;
35
31
  }
36
32
 
37
- protected _height: number;
33
+ override parent?: AVLTreeNode<K, V> = undefined;
38
34
 
39
- /**
40
- * The function returns the value of the height property.
41
- * @returns The height of the object.
42
- */
43
- get height(): number {
44
- return this._height;
35
+ override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
36
+
37
+ override get left(): OptNodeOrNull<AVLTreeNode<K, V>> {
38
+ return this._left;
45
39
  }
46
40
 
47
- /**
48
- * The above function sets the value of the height property.
49
- * @param {number} value - The value parameter is a number that represents the new height value to be
50
- * set.
51
- */
52
- set height(value: number) {
53
- this._height = value;
41
+ override set left(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
42
+ if (v) {
43
+ v.parent = this;
44
+ }
45
+ this._left = v;
46
+ }
47
+
48
+ override _right?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
49
+
50
+ override get right(): OptNodeOrNull<AVLTreeNode<K, V>> {
51
+ return this._right;
52
+ }
53
+
54
+ override set right(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
55
+ if (v) {
56
+ v.parent = this;
57
+ }
58
+ this._right = v;
54
59
  }
55
60
  }
56
61
 
@@ -63,57 +68,57 @@ export class AVLTreeNode<
63
68
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
64
69
  * 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.
65
70
  */
66
- export class AVLTree<
67
- K = any,
68
- V = any,
69
- R = object,
70
- NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>
71
- >
72
- extends BST<K, V, R, NODE>
73
- implements IBinaryTree<K, V, R, NODE>
71
+ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
72
+ extends BST<K, V, R, MK, MV, MR>
73
+ implements IBinaryTree<K, V, R, MK, MV, MR>
74
74
  {
75
75
  /**
76
- * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
77
- * entries, or raw elements.
78
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
79
- * iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
80
- * be used to initialize the AVLTree.
81
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
82
- * behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
83
- * keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
84
- * `nodeBuilder` (
76
+ * This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
77
+ * in an iterable format.
78
+ * @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
80
+ * used to initialize the AVLTree with key-value pairs or raw data entries. If provided
81
+ * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
82
+ * R>`. It is an optional parameter that allows you to specify additional options for configuring the
83
+ * AVL tree. These options could include things like custom comparators, initial capacity, or any
84
+ * other configuration settings specific
85
85
  */
86
- constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: AVLTreeOptions<K, V, R>) {
86
+ constructor(
87
+ keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [],
88
+ options?: AVLTreeOptions<K, V, R>
89
+ ) {
87
90
  super([], options);
88
91
  if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
89
92
  }
90
93
 
91
94
  /**
95
+ * Time Complexity: O(1)
96
+ * Space Complexity: O(1)
97
+ *
92
98
  * The function creates a new AVL tree node with the given key and value.
93
99
  * @param {K} key - The key parameter is of type K, which represents the key of the node being
94
100
  * created.
95
101
  * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
96
102
  * value associated with the key in the node being created.
97
103
  * @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
98
- * type NODE.
104
+ * type AVLTreeNode<K, V>.
99
105
  */
100
- override createNode(key: K, value?: V): NODE {
101
- return new AVLTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
106
+ override createNode(key: K, value?: V): AVLTreeNode<K, V> {
107
+ return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>;
102
108
  }
103
109
 
104
110
  /**
105
- * The function `createTree` in TypeScript overrides the default AVLTree creation with the provided
106
- * options.
107
- * @param [options] - The `options` parameter in the `createTree` function is an object that contains
108
- * configuration options for creating an AVL tree. These options can include properties such as
109
- * `iterationType`, `isMapMode`, `specifyComparable`, `toEntryFn`, and `isReverse`. The function
110
- * creates a
111
- * @returns An AVLTree object is being returned with the specified options and properties inherited
112
- * from the current object.
111
+ * Time Complexity: O(1)
112
+ * Space Complexity: O(1)
113
+ *
114
+ * The function creates a new AVL tree with the specified options and returns it.
115
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
116
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
117
+ * being created.
118
+ * @returns a new AVLTree object.
113
119
  */
114
- // @ts-ignore
115
120
  override createTree(options?: AVLTreeOptions<K, V, R>) {
116
- return new AVLTree<K, V, R, NODE>([], {
121
+ return new AVLTree<K, V, R, MK, MV, MR>([], {
117
122
  iterationType: this.iterationType,
118
123
  isMapMode: this._isMapMode,
119
124
  specifyComparable: this._specifyComparable,
@@ -124,51 +129,53 @@ export class AVLTree<
124
129
  }
125
130
 
126
131
  /**
132
+ * Time Complexity: O(1)
133
+ * Space Complexity: O(1)
134
+ *
127
135
  * The function checks if the input is an instance of AVLTreeNode.
128
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
129
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
130
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
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>>`.
138
+ * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
131
139
  * an instance of the `AVLTreeNode` class.
132
140
  */
133
- override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
134
- return keyNodeEntryOrRaw instanceof AVLTreeNode;
141
+ override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> {
142
+ return keyNodeOrEntry instanceof AVLTreeNode;
135
143
  }
136
144
 
137
145
  /**
138
146
  * Time Complexity: O(log n)
139
- * Space Complexity: O(1)
147
+ * Space Complexity: O(log n)
140
148
  *
141
149
  * The function overrides the add method of a class and inserts a key-value pair into a data
142
150
  * structure, then balances the path.
143
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
144
- * `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
145
- * `RawElement`.
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>>`
146
153
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
147
154
  * the key or node being added to the data structure.
148
155
  * @returns The method is returning a boolean value.
149
156
  */
150
- override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
151
- if (keyNodeEntryOrRaw === null) return false;
152
- const inserted = super.add(keyNodeEntryOrRaw, value);
153
- if (inserted) this._balancePath(keyNodeEntryOrRaw);
157
+ override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean {
158
+ if (keyNodeOrEntry === null) return false;
159
+ const inserted = super.add(keyNodeOrEntry, value);
160
+ if (inserted) this._balancePath(keyNodeOrEntry);
154
161
  return inserted;
155
162
  }
156
163
 
157
164
  /**
158
165
  * Time Complexity: O(log n)
159
- * Space Complexity: O(1)
166
+ * Space Complexity: O(log n)
160
167
  *
161
168
  * The function overrides the delete method in a TypeScript class, performs deletion, and then
162
169
  * balances the tree if necessary.
163
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
170
+ * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
164
171
  * parameter in the `override delete` method can be one of the following types:
165
172
  * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
166
173
  * method from the superclass (presumably a parent class) with the provided `predicate`, which could
167
174
  * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
168
175
  * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
169
176
  */
170
- override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
171
- const deletedResults = super.delete(keyNodeEntryOrRaw);
177
+ override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
178
+ const deletedResults = super.delete(keyNodeOrEntry);
172
179
  for (const { needBalanced } of deletedResults) {
173
180
  if (needBalanced) {
174
181
  this._balancePath(needBalanced);
@@ -177,12 +184,31 @@ export class AVLTree<
177
184
  return deletedResults;
178
185
  }
179
186
 
180
- // @ts-ignore
181
- override map<MK, MV, MR>(
187
+ /**
188
+ * Time Complexity: O(n)
189
+ * Space Complexity: O(n)
190
+ *
191
+ * The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure
192
+ * by applying a callback function to each entry and creating a new AVLTree with the results.
193
+ * @param callback - A function that will be called for each entry in the AVLTree. It takes four
194
+ * arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
195
+ * the AVLTree itself.
196
+ * @param [options] - The `options` parameter in the `override map` function is of type
197
+ * `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
198
+ * options for the AVL tree being created during the mapping process. These options could include
199
+ * custom comparators, initial
200
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
201
+ * the value of `this` when executing the `callback` function. It allows you to set the context
202
+ * (value of `this`) within the callback function. This can be useful when you want to access
203
+ * properties or
204
+ * @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
205
+ * modified by the provided callback function.
206
+ */
207
+ override map(
182
208
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
183
209
  options?: AVLTreeOptions<MK, MV, MR>,
184
210
  thisArg?: any
185
- ) {
211
+ ): AVLTree<MK, MV, MR> {
186
212
  const newTree = new AVLTree<MK, MV, MR>([], options);
187
213
  let index = 0;
188
214
  for (const [key, value] of this) {
@@ -191,23 +217,37 @@ export class AVLTree<
191
217
  return newTree;
192
218
  }
193
219
 
220
+ /**
221
+ * Time Complexity: O(n)
222
+ * Space Complexity: O(n)
223
+ *
224
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
225
+ * structure.
226
+ * @returns A cloned tree object is being returned.
227
+ */
228
+ override clone() {
229
+ const cloned = this.createTree();
230
+ this._clone(cloned);
231
+ return cloned;
232
+ }
233
+
194
234
  /**
195
235
  * Time Complexity: O(1)
196
236
  * Space Complexity: O(1)
197
237
  *
198
238
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
199
239
  * binary search tree.
200
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
201
- * object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
202
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
203
- * `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
240
+ * @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node
241
+ * object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
242
+ * @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
243
+ * `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
204
244
  * @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
205
245
  * `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
206
246
  */
207
247
  protected override _swapProperties(
208
- srcNode: R | BSTNOptKeyOrNode<K, NODE>,
209
- destNode: R | BSTNOptKeyOrNode<K, NODE>
210
- ): NODE | undefined {
248
+ srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>,
249
+ destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>
250
+ ): AVLTreeNode<K, V> | undefined {
211
251
  const srcNodeEnsured = this.ensureNode(srcNode);
212
252
  const destNodeEnsured = this.ensureNode(destNode);
213
253
 
@@ -237,12 +277,12 @@ export class AVLTree<
237
277
  * Space Complexity: O(1)
238
278
  *
239
279
  * The function calculates the balance factor of a node in a binary tree.
240
- * @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
280
+ * @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
241
281
  * binary tree data structure.
242
282
  * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
243
283
  * height of the left subtree from the height of the right subtree.
244
284
  */
245
- protected _balanceFactor(node: NODE): number {
285
+ protected _balanceFactor(node: AVLTreeNode<K, V>): number {
246
286
  if (!node.right)
247
287
  // node has no right subtree
248
288
  return -node.height;
@@ -258,9 +298,9 @@ export class AVLTree<
258
298
  *
259
299
  * The function updates the height of a node in a binary tree based on the heights of its left and
260
300
  * right children.
261
- * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
301
+ * @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
262
302
  */
263
- protected _updateHeight(node: NODE): void {
303
+ protected _updateHeight(node: AVLTreeNode<K, V>): void {
264
304
  if (!node.left && !node.right) node.height = 0;
265
305
  else if (!node.left) {
266
306
  const rightHeight = node.right ? node.right.height : 0;
@@ -274,12 +314,12 @@ export class AVLTree<
274
314
  * Space Complexity: O(1)
275
315
  *
276
316
  * The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
277
- * @param {NODE} A - A is a node in a binary tree.
317
+ * @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
278
318
  */
279
- protected _balanceLL(A: NODE): void {
319
+ protected _balanceLL(A: AVLTreeNode<K, V>): void {
280
320
  const parentOfA = A.parent;
281
321
  const B = A.left;
282
- A.parent = B;
322
+ if (B !== null) A.parent = B;
283
323
  if (B && B.right) {
284
324
  B.right.parent = A;
285
325
  }
@@ -307,21 +347,21 @@ export class AVLTree<
307
347
  * Space Complexity: O(1)
308
348
  *
309
349
  * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
310
- * @param {NODE} A - A is a node in a binary tree.
350
+ * @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
311
351
  */
312
- protected _balanceLR(A: NODE): void {
352
+ protected _balanceLR(A: AVLTreeNode<K, V>): void {
313
353
  const parentOfA = A.parent;
314
354
  const B = A.left;
315
355
  let C = undefined;
316
356
  if (B) {
317
357
  C = B.right;
318
358
  }
319
- if (A) A.parent = C;
320
- if (B) B.parent = C;
359
+ if (A && C !== null) A.parent = C;
360
+ if (B && C !== null) B.parent = C;
321
361
 
322
362
  if (C) {
323
363
  if (C.left) {
324
- C.left.parent = B;
364
+ if (B !== null) C.left.parent = B;
325
365
  }
326
366
  if (C.right) {
327
367
  C.right.parent = A;
@@ -358,12 +398,12 @@ export class AVLTree<
358
398
  * Space Complexity: O(1)
359
399
  *
360
400
  * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
361
- * @param {NODE} A - A is a node in a binary tree.
401
+ * @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
362
402
  */
363
- protected _balanceRR(A: NODE): void {
403
+ protected _balanceRR(A: AVLTreeNode<K, V>): void {
364
404
  const parentOfA = A.parent;
365
405
  const B = A.right;
366
- A.parent = B;
406
+ if (B !== null) A.parent = B;
367
407
  if (B) {
368
408
  if (B.left) {
369
409
  B.left.parent = A;
@@ -396,9 +436,9 @@ export class AVLTree<
396
436
  * Space Complexity: O(1)
397
437
  *
398
438
  * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
399
- * @param {NODE} A - A is a node in a binary tree.
439
+ * @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
400
440
  */
401
- protected _balanceRL(A: NODE): void {
441
+ protected _balanceRL(A: AVLTreeNode<K, V>): void {
402
442
  const parentOfA = A.parent;
403
443
  const B = A.right;
404
444
  let C = undefined;
@@ -406,15 +446,15 @@ export class AVLTree<
406
446
  C = B.left;
407
447
  }
408
448
 
409
- A.parent = C;
410
- if (B) B.parent = C;
449
+ if (C !== null) A.parent = C;
450
+ if (B && C !== null) B.parent = C;
411
451
 
412
452
  if (C) {
413
453
  if (C.left) {
414
454
  C.left.parent = A;
415
455
  }
416
456
  if (C.right) {
417
- C.right.parent = B;
457
+ if (B !== null) C.right.parent = B;
418
458
  }
419
459
  C.parent = parentOfA;
420
460
  }
@@ -447,10 +487,10 @@ export class AVLTree<
447
487
  *
448
488
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
449
489
  * to restore balance in an AVL tree after inserting a node.
450
- * @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
451
- * `BTNRep<K, V, 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>>`.
452
492
  */
453
- protected _balancePath(node: BTNRep<K, V, NODE> | R): void {
493
+ protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void {
454
494
  node = this.ensureNode(node);
455
495
  const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
456
496
  for (let i = 0; i < path.length; i++) {
@@ -498,14 +538,14 @@ export class AVLTree<
498
538
  *
499
539
  * The function replaces an old node with a new node and sets the height of the new node to be the
500
540
  * same as the old node.
501
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
541
+ * @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
502
542
  * the data structure.
503
- * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
543
+ * @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
504
544
  * the data structure.
505
545
  * @returns The method is returning the result of calling the `_replaceNode` method from the
506
546
  * superclass, with the `oldNode` and `newNode` as arguments.
507
547
  */
508
- protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
548
+ protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> {
509
549
  newNode.height = oldNode.height;
510
550
 
511
551
  return super._replaceNode(oldNode, newNode);
@@ -7,6 +7,9 @@
7
7
  */
8
8
  import { getMSB } from '../../utils';
9
9
 
10
+ /**
11
+ *
12
+ */
10
13
  export class BinaryIndexedTree {
11
14
  protected readonly _freq: number;
12
15
  protected readonly _max: number;