data-structure-typed 1.52.6 → 1.52.8

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 (178) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +33 -30
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +156 -156
  5. package/dist/cjs/constants/index.d.ts +4 -0
  6. package/dist/cjs/constants/index.js +9 -0
  7. package/dist/cjs/constants/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
  9. package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
  10. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  11. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
  12. package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
  13. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
  18. package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  21. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.js +668 -597
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
  25. package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
  26. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
  28. package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
  29. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
  31. package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
  32. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
  33. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
  34. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  35. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
  36. package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
  37. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  38. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  39. package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
  40. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  41. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
  42. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
  43. package/dist/cjs/data-structures/hash/hash-map.js +1 -1
  44. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  45. package/dist/cjs/data-structures/heap/heap.js +3 -3
  46. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  53. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  54. package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
  55. package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
  56. package/dist/cjs/data-structures/matrix/navigator.js +4 -2
  57. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  58. package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
  59. package/dist/cjs/data-structures/queue/deque.js +29 -29
  60. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  61. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  62. package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
  63. package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
  64. package/dist/cjs/data-structures/trie/trie.js +1 -1
  65. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  66. package/dist/cjs/index.d.ts +1 -0
  67. package/dist/cjs/index.js +1 -0
  68. package/dist/cjs/index.js.map +1 -1
  69. package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
  70. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  71. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
  72. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  73. package/dist/mjs/constants/index.d.ts +4 -0
  74. package/dist/mjs/constants/index.js +5 -0
  75. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
  76. package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
  77. package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
  78. package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
  79. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  80. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
  81. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
  82. package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
  83. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  84. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
  85. package/dist/mjs/data-structures/binary-tree/binary-tree.js +667 -591
  86. package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
  87. package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
  88. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
  89. package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
  90. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
  91. package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
  92. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
  93. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
  94. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
  95. package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
  96. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  97. package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
  98. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
  99. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
  100. package/dist/mjs/data-structures/hash/hash-map.js +1 -1
  101. package/dist/mjs/data-structures/heap/heap.js +3 -3
  102. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  103. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
  104. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  105. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
  106. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  107. package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
  108. package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
  109. package/dist/mjs/data-structures/matrix/navigator.js +4 -2
  110. package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
  111. package/dist/mjs/data-structures/queue/deque.js +29 -29
  112. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  113. package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
  114. package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
  115. package/dist/mjs/data-structures/trie/trie.js +1 -1
  116. package/dist/mjs/index.d.ts +1 -0
  117. package/dist/mjs/index.js +1 -0
  118. package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
  119. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  120. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
  121. package/dist/umd/data-structure-typed.js +1343 -1615
  122. package/dist/umd/data-structure-typed.min.js +10 -3
  123. package/dist/umd/data-structure-typed.min.js.map +1 -1
  124. package/eslint.config.mjs +69 -0
  125. package/package.json +30 -28
  126. package/src/constants/index.ts +4 -0
  127. package/src/data-structures/base/iterable-element-base.ts +11 -1
  128. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  129. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
  130. package/src/data-structures/binary-tree/avl-tree.ts +69 -71
  131. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  132. package/src/data-structures/binary-tree/binary-tree.ts +697 -725
  133. package/src/data-structures/binary-tree/bst.ts +123 -129
  134. package/src/data-structures/binary-tree/rb-tree.ts +44 -46
  135. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  136. package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
  137. package/src/data-structures/graph/abstract-graph.ts +6 -6
  138. package/src/data-structures/graph/directed-graph.ts +4 -4
  139. package/src/data-structures/graph/undirected-graph.ts +2 -2
  140. package/src/data-structures/hash/hash-map.ts +3 -3
  141. package/src/data-structures/heap/heap.ts +3 -3
  142. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  143. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  144. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  145. package/src/data-structures/matrix/matrix.ts +2 -2
  146. package/src/data-structures/matrix/navigator.ts +4 -4
  147. package/src/data-structures/queue/deque.ts +31 -31
  148. package/src/data-structures/queue/queue.ts +1 -1
  149. package/src/data-structures/stack/stack.ts +2 -2
  150. package/src/data-structures/trie/trie.ts +3 -3
  151. package/src/index.ts +1 -0
  152. package/src/interfaces/binary-tree.ts +3 -3
  153. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
  154. package/test/config.ts +1 -7
  155. package/test/integration/all-in-one.test.ts +2 -2
  156. package/test/integration/avl-tree.test.ts +2 -2
  157. package/test/integration/bst.test.ts +17 -16
  158. package/test/integration/heap.test.js +6 -1
  159. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
  160. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  161. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +51 -26
  162. package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
  163. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
  164. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
  165. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
  166. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
  167. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
  168. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  169. package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
  170. package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
  171. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
  172. package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
  173. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
  174. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
  175. package/test/unit/data-structures/stack/stack.test.ts +6 -0
  176. package/test/unit/unrestricted-interconversion.test.ts +24 -24
  177. package/test/utils/big-o.ts +5 -4
  178. package/.eslintrc.js +0 -64
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
@@ -12,10 +12,10 @@ import type {
12
12
  AVLTreeOptions,
13
13
  BinaryTreeDeleteResult,
14
14
  BSTNKeyOrNode,
15
- BTNCallback,
16
- BTNKeyOrNodeOrEntry
15
+ BTNKeyOrNodeOrEntry,
16
+ BTNPredicate,
17
+ BTNEntry
17
18
  } from '../../types';
18
- import { BTNEntry } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
 
21
21
  export class AVLTreeNode<
@@ -78,7 +78,7 @@ export class AVLTree<
78
78
  /**
79
79
  * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
80
80
  * entries, or raw elements.
81
- * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
81
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
82
82
  * iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
83
83
  * be used to initialize the AVLTree.
84
84
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
@@ -87,11 +87,11 @@ export class AVLTree<
87
87
  * `nodeBuilder` (
88
88
  */
89
89
  constructor(
90
- keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
90
+ keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
91
91
  options?: AVLTreeOptions<K, V, R>
92
92
  ) {
93
93
  super([], options);
94
- if (keysOrNodesOrEntriesOrRawElements) super.addMany(keysOrNodesOrEntriesOrRawElements);
94
+ if (keysOrNodesOrEntriesOrRaws) super.addMany(keysOrNodesOrEntriesOrRaws);
95
95
  }
96
96
 
97
97
  /**
@@ -117,22 +117,21 @@ export class AVLTree<
117
117
  override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
118
118
  return new AVLTree<K, V, R, NODE, TREE>([], {
119
119
  iterationType: this.iterationType,
120
- comparator: this.comparator,
120
+ comparator: this._comparator,
121
+ toEntryFn: this._toEntryFn,
121
122
  ...options
122
123
  }) as TREE;
123
124
  }
124
125
 
125
126
  /**
126
127
  * The function checks if the input is an instance of AVLTreeNode.
127
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
128
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
129
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
128
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
129
+ * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
130
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
130
131
  * an instance of the `AVLTreeNode` class.
131
132
  */
132
- override isNode(
133
- keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
134
- ): keyOrNodeOrEntryOrRawElement is NODE {
135
- return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
133
+ override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
134
+ return keyOrNodeOrEntryOrRaw instanceof AVLTreeNode;
136
135
  }
137
136
 
138
137
  /**
@@ -141,17 +140,17 @@ export class AVLTree<
141
140
  *
142
141
  * The function overrides the add method of a class and inserts a key-value pair into a data
143
142
  * structure, then balances the path.
144
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
145
- * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
143
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
144
+ * `keyOrNodeOrEntryOrRaw` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
146
145
  * `RawElement`.
147
146
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
148
147
  * the key or node being added to the data structure.
149
148
  * @returns The method is returning a boolean value.
150
149
  */
151
- override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
152
- if (keyOrNodeOrEntryOrRawElement === null) return false;
153
- const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
154
- if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
150
+ override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
151
+ if (keyOrNodeOrEntryOrRaw === null) return false;
152
+ const inserted = super.add(keyOrNodeOrEntryOrRaw, value);
153
+ if (inserted) this._balancePath(keyOrNodeOrEntryOrRaw);
155
154
  return inserted;
156
155
  }
157
156
 
@@ -159,20 +158,17 @@ export class AVLTree<
159
158
  * Time Complexity: O(log n)
160
159
  * Space Complexity: O(1)
161
160
  *
162
- * The function overrides the delete method of a binary tree class and performs additional operations
163
- * to balance the tree after deletion.
164
- * @param identifier - The `identifier` parameter is the value or condition used to identify the
165
- * node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
166
- * binary tree's node type.
167
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
168
- * node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
169
- * @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
161
+ * The function overrides the delete method in a TypeScript class, performs deletion, and then
162
+ * balances the tree if necessary.
163
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
164
+ * parameter in the `override delete` method can be one of the following types:
165
+ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
166
+ * method from the superclass (presumably a parent class) with the provided `predicate`, which could
167
+ * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
168
+ * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
170
169
  */
171
- override delete<C extends BTNCallback<NODE>>(
172
- identifier: ReturnType<C>,
173
- callback: C = this._DEFAULT_CALLBACK as C
174
- ): BinaryTreeDeleteResult<NODE>[] {
175
- const deletedResults = super.delete(identifier, callback);
170
+ override delete(predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[] {
171
+ const deletedResults = super.delete(predicate);
176
172
  for (const { needBalanced } of deletedResults) {
177
173
  if (needBalanced) {
178
174
  this._balancePath(needBalanced);
@@ -339,8 +335,8 @@ export class AVLTree<
339
335
  }
340
336
 
341
337
  this._updateHeight(A);
342
- B && this._updateHeight(B);
343
- C && this._updateHeight(C);
338
+ if (B) this._updateHeight(B);
339
+ if (C) this._updateHeight(C);
344
340
  }
345
341
 
346
342
  /**
@@ -378,7 +374,7 @@ export class AVLTree<
378
374
  B.left = A;
379
375
  }
380
376
  this._updateHeight(A);
381
- B && this._updateHeight(B);
377
+ if (B) this._updateHeight(B);
382
378
  }
383
379
 
384
380
  /**
@@ -427,8 +423,8 @@ export class AVLTree<
427
423
  if (C) C.right = B;
428
424
 
429
425
  this._updateHeight(A);
430
- B && this._updateHeight(B);
431
- C && this._updateHeight(C);
426
+ if (B) this._updateHeight(B);
427
+ if (C) this._updateHeight(C);
432
428
  }
433
429
 
434
430
  /**
@@ -437,46 +433,48 @@ export class AVLTree<
437
433
  *
438
434
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
439
435
  * to restore balance in an AVL tree after inserting a node.
440
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
436
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
441
437
  * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
442
438
  */
443
- protected _balancePath(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): void {
439
+ protected _balancePath(node: BTNKeyOrNodeOrEntry<K, V, NODE> | R): void {
444
440
  node = this.ensureNode(node);
445
- const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
441
+ const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
446
442
  for (let i = 0; i < path.length; i++) {
447
443
  // second O(log n)
448
444
  const A = path[i];
449
- // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
450
- this._updateHeight(A); // first O(1)
451
- // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
452
- // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
453
- switch (
454
- this._balanceFactor(A) // second O(1)
455
- ) {
456
- case -2:
457
- if (A && A.left) {
458
- if (this._balanceFactor(A.left) <= 0) {
459
- // second O(1)
460
- // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
461
- this._balanceLL(A);
462
- } else {
463
- // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
464
- this._balanceLR(A);
445
+ if (A) {
446
+ // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
447
+ this._updateHeight(A); // first O(1)
448
+ // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
449
+ // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
450
+ switch (
451
+ this._balanceFactor(A) // second O(1)
452
+ ) {
453
+ case -2:
454
+ if (A && A.left) {
455
+ if (this._balanceFactor(A.left) <= 0) {
456
+ // second O(1)
457
+ // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
458
+ this._balanceLL(A);
459
+ } else {
460
+ // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
461
+ this._balanceLR(A);
462
+ }
465
463
  }
466
- }
467
- break;
468
- case +2:
469
- if (A && A.right) {
470
- if (this._balanceFactor(A.right) >= 0) {
471
- // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
472
- this._balanceRR(A);
473
- } else {
474
- // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
475
- this._balanceRL(A);
464
+ break;
465
+ case +2:
466
+ if (A && A.right) {
467
+ if (this._balanceFactor(A.right) >= 0) {
468
+ // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
469
+ this._balanceRR(A);
470
+ } else {
471
+ // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
472
+ this._balanceRL(A);
473
+ }
476
474
  }
477
- }
475
+ }
476
+ // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
478
477
  }
479
- // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
480
478
  }
481
479
  }
482
480
 
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
  import { getMSB } from '../../utils';