data-structure-typed 1.50.1 → 1.50.2

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 (155) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +26 -26
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +153 -159
  5. package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
  6. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
  15. package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
  22. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
  27. package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
  28. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  30. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  31. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  32. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
  33. package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
  34. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  35. package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
  36. package/dist/cjs/data-structures/hash/hash-map.js +160 -25
  37. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
  39. package/dist/cjs/data-structures/heap/heap.js +66 -6
  40. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  48. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  49. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  51. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  52. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  53. package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
  54. package/dist/cjs/data-structures/queue/deque.js +101 -47
  55. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  56. package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
  57. package/dist/cjs/data-structures/queue/queue.js +47 -5
  58. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  59. package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
  60. package/dist/cjs/data-structures/stack/stack.js +22 -0
  61. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  62. package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
  63. package/dist/cjs/data-structures/trie/trie.js +41 -0
  64. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  65. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  66. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  67. package/dist/cjs/types/utils/utils.d.ts +1 -0
  68. package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
  69. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
  75. package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
  80. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
  81. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
  82. package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
  83. package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
  84. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  85. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  86. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
  87. package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
  88. package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
  89. package/dist/mjs/data-structures/hash/hash-map.js +160 -25
  90. package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
  91. package/dist/mjs/data-structures/heap/heap.js +66 -6
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  93. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  95. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
  96. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  97. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  98. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  99. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  100. package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
  101. package/dist/mjs/data-structures/queue/deque.js +101 -47
  102. package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
  103. package/dist/mjs/data-structures/queue/queue.js +47 -5
  104. package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
  105. package/dist/mjs/data-structures/stack/stack.js +22 -0
  106. package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
  107. package/dist/mjs/data-structures/trie/trie.js +41 -0
  108. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  109. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  110. package/dist/mjs/types/utils/utils.d.ts +1 -0
  111. package/dist/umd/data-structure-typed.js +1730 -1042
  112. package/dist/umd/data-structure-typed.min.js +3 -3
  113. package/dist/umd/data-structure-typed.min.js.map +1 -1
  114. package/package.json +8 -3
  115. package/src/data-structures/base/iterable-base.ts +172 -19
  116. package/src/data-structures/binary-tree/avl-tree.ts +97 -97
  117. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  118. package/src/data-structures/binary-tree/bst.ts +89 -131
  119. package/src/data-structures/binary-tree/rb-tree.ts +127 -155
  120. package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
  121. package/src/data-structures/graph/abstract-graph.ts +4 -0
  122. package/src/data-structures/graph/directed-graph.ts +30 -0
  123. package/src/data-structures/graph/map-graph.ts +15 -0
  124. package/src/data-structures/graph/undirected-graph.ts +28 -0
  125. package/src/data-structures/hash/hash-map.ts +175 -34
  126. package/src/data-structures/heap/heap.ts +66 -6
  127. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
  128. package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
  129. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  130. package/src/data-structures/matrix/matrix.ts +52 -12
  131. package/src/data-structures/queue/deque.ts +108 -49
  132. package/src/data-structures/queue/queue.ts +51 -5
  133. package/src/data-structures/stack/stack.ts +24 -0
  134. package/src/data-structures/trie/trie.ts +45 -1
  135. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  136. package/src/types/data-structures/hash/hash-map.ts +4 -3
  137. package/src/types/utils/utils.ts +2 -0
  138. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
  139. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  140. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  141. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  142. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  143. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  144. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  145. package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
  146. package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
  147. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  148. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  149. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  150. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  151. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  152. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  153. package/test/unit/data-structures/queue/queue.test.ts +22 -5
  154. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  155. package/test/unit/data-structures/trie/trie.test.ts +17 -0
@@ -8,7 +8,7 @@
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
- export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
11
+ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
12
12
  height: number;
13
13
  constructor(key: K, value?: V);
14
14
  }
@@ -21,27 +21,27 @@ export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N
21
21
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
22
22
  * 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.
23
23
  */
24
- export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
24
+ export declare class AVLTree<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
25
25
  /**
26
26
  * The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
27
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
27
+ * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
28
28
  * objects. It represents a collection of nodes that will be added to the AVL tree during
29
29
  * initialization.
30
30
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
31
31
  * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
32
32
  * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
33
33
  */
34
- constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: AVLTreeOptions<K>);
34
+ constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeOptions<K>);
35
35
  /**
36
36
  * The function creates a new AVL tree node with the specified key and value.
37
37
  * @param {K} key - The key parameter is the key value that will be associated with
38
38
  * the new node. It is used to determine the position of the node in the binary search tree.
39
39
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
40
40
  * type `V`, which means it can be any value that is assignable to the `value` property of the
41
- * node type `N`.
41
+ * node type `NODE`.
42
42
  * @returns a new AVLTreeNode object with the specified key and value.
43
43
  */
44
- createNode(key: K, value?: V): N;
44
+ createNode(key: K, value?: V): NODE;
45
45
  /**
46
46
  * The function creates a new AVL tree with the specified options and returns it.
47
47
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
@@ -52,10 +52,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
52
52
  createTree(options?: AVLTreeOptions<K>): TREE;
53
53
  /**
54
54
  * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
55
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
55
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
56
56
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
57
57
  */
58
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
58
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
59
59
  /**
60
60
  * Time Complexity: O(log n)
61
61
  * Space Complexity: O(1)
@@ -73,7 +73,7 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
73
73
  * being added to the binary tree.
74
74
  * @returns The method is returning either the inserted node or undefined.
75
75
  */
76
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
76
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
77
77
  /**
78
78
  * Time Complexity: O(log n)
79
79
  * Space Complexity: O(1)
@@ -90,40 +90,38 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
90
90
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
91
91
  * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
92
92
  * default to the `_defaultOneParamCallback` function. The `callback` function should have a single
93
- * parameter of type `N
94
- * @returns The method is returning an array of `BinaryTreeDeleteResult<N>`.
93
+ * parameter of type `NODE
94
+ * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
95
95
  */
96
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<N>[];
96
+ delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<NODE>[];
97
97
  /**
98
98
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
99
99
  * tree.
100
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
101
- * needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
102
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
100
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
101
+ * needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
102
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
103
103
  * node where the values from the source node will be swapped to.
104
104
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
105
105
  * if either `srcNode` or `destNode` is undefined.
106
106
  */
107
- protected _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined;
107
+ protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
108
108
  /**
109
109
  * Time Complexity: O(1)
110
110
  * Space Complexity: O(1)
111
- * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
112
111
  */
113
112
  /**
114
113
  * Time Complexity: O(1)
115
114
  * Space Complexity: O(1)
116
115
  *
117
116
  * The function calculates the balance factor of a node in a binary tree.
118
- * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
117
+ * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
119
118
  * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
120
119
  * height of the left subtree from the height of the right subtree.
121
120
  */
122
- protected _balanceFactor(node: N): number;
121
+ protected _balanceFactor(node: NODE): number;
123
122
  /**
124
123
  * Time Complexity: O(1)
125
124
  * Space Complexity: O(1)
126
- * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
127
125
  */
128
126
  /**
129
127
  * Time Complexity: O(1)
@@ -131,37 +129,33 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
131
129
  *
132
130
  * The function updates the height of a node in a binary tree based on the heights of its left and
133
131
  * right children.
134
- * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
132
+ * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
135
133
  */
136
- protected _updateHeight(node: N): void;
134
+ protected _updateHeight(node: NODE): void;
137
135
  /**
138
- * Time Complexity: O(log n)
136
+ * Time Complexity: O(1)
139
137
  * Space Complexity: O(1)
140
- * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
141
138
  */
142
139
  /**
143
- * Time Complexity: O(log n)
140
+ * Time Complexity: O(1)
144
141
  * Space Complexity: O(1)
145
142
  *
146
- * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
147
- * to restore balance in an AVL tree after inserting a node.
148
- * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
149
- * AVL tree that needs to be balanced.
143
+ * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
144
+ * @param {NODE} A - A is a node in a binary tree.
150
145
  */
151
- protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void;
146
+ protected _balanceLL(A: NODE): void;
152
147
  /**
153
148
  * Time Complexity: O(1)
154
149
  * Space Complexity: O(1)
155
- * constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
156
150
  */
157
151
  /**
158
152
  * Time Complexity: O(1)
159
153
  * Space Complexity: O(1)
160
154
  *
161
- * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
162
- * @param {N} A - A is a node in a binary tree.
155
+ * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
156
+ * @param {NODE} A - A is a node in a binary tree.
163
157
  */
164
- protected _balanceLL(A: N): void;
158
+ protected _balanceLR(A: NODE): void;
165
159
  /**
166
160
  * Time Complexity: O(1)
167
161
  * Space Complexity: O(1)
@@ -170,10 +164,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
170
164
  * Time Complexity: O(1)
171
165
  * Space Complexity: O(1)
172
166
  *
173
- * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
174
- * @param {N} A - A is a node in a binary tree.
167
+ * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
168
+ * @param {NODE} A - A is a node in a binary tree.
175
169
  */
176
- protected _balanceLR(A: N): void;
170
+ protected _balanceRR(A: NODE): void;
177
171
  /**
178
172
  * Time Complexity: O(1)
179
173
  * Space Complexity: O(1)
@@ -182,21 +176,24 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
182
176
  * Time Complexity: O(1)
183
177
  * Space Complexity: O(1)
184
178
  *
185
- * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
186
- * @param {N} A - A is a node in a binary tree.
179
+ * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
180
+ * @param {NODE} A - A is a node in a binary tree.
187
181
  */
188
- protected _balanceRR(A: N): void;
182
+ protected _balanceRL(A: NODE): void;
189
183
  /**
190
- * Time Complexity: O(1)
184
+ * Time Complexity: O(log n)
191
185
  * Space Complexity: O(1)
186
+ * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
192
187
  */
193
188
  /**
194
- * Time Complexity: O(1)
189
+ * Time Complexity: O(log n)
195
190
  * Space Complexity: O(1)
196
191
  *
197
- * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
198
- * @param {N} A - A is a node in a binary tree.
192
+ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
193
+ * to restore balance in an AVL tree after inserting a node.
194
+ * @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
195
+ * AVL tree that needs to be balanced.
199
196
  */
200
- protected _balanceRL(A: N): void;
201
- protected _replaceNode(oldNode: N, newNode: N): N;
197
+ protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void;
198
+ protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
202
199
  }
@@ -25,7 +25,7 @@ export class AVLTreeNode extends BSTNode {
25
25
  export class AVLTree extends BST {
26
26
  /**
27
27
  * The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
28
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
28
+ * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
29
29
  * objects. It represents a collection of nodes that will be added to the AVL tree during
30
30
  * initialization.
31
31
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
@@ -43,7 +43,7 @@ export class AVLTree extends BST {
43
43
  * the new node. It is used to determine the position of the node in the binary search tree.
44
44
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
45
45
  * type `V`, which means it can be any value that is assignable to the `value` property of the
46
- * node type `N`.
46
+ * node type `NODE`.
47
47
  * @returns a new AVLTreeNode object with the specified key and value.
48
48
  */
49
49
  createNode(key, value) {
@@ -65,7 +65,7 @@ export class AVLTree extends BST {
65
65
  }
66
66
  /**
67
67
  * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
68
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
68
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
69
69
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
70
70
  */
71
71
  isNode(keyOrNodeOrEntry) {
@@ -112,8 +112,8 @@ export class AVLTree extends BST {
112
112
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
113
113
  * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
114
114
  * default to the `_defaultOneParamCallback` function. The `callback` function should have a single
115
- * parameter of type `N
116
- * @returns The method is returning an array of `BinaryTreeDeleteResult<N>`.
115
+ * parameter of type `NODE
116
+ * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
117
117
  */
118
118
  delete(identifier, callback = this._defaultOneParamCallback) {
119
119
  if (identifier instanceof AVLTreeNode)
@@ -129,9 +129,9 @@ export class AVLTree extends BST {
129
129
  /**
130
130
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
131
131
  * tree.
132
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
133
- * needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
134
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
132
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
133
+ * needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
134
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
135
135
  * node where the values from the source node will be swapped to.
136
136
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
137
137
  * if either `srcNode` or `destNode` is undefined.
@@ -158,14 +158,13 @@ export class AVLTree extends BST {
158
158
  /**
159
159
  * Time Complexity: O(1)
160
160
  * Space Complexity: O(1)
161
- * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
162
161
  */
163
162
  /**
164
163
  * Time Complexity: O(1)
165
164
  * Space Complexity: O(1)
166
165
  *
167
166
  * The function calculates the balance factor of a node in a binary tree.
168
- * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
167
+ * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
169
168
  * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
170
169
  * height of the left subtree from the height of the right subtree.
171
170
  */
@@ -182,7 +181,6 @@ export class AVLTree extends BST {
182
181
  /**
183
182
  * Time Complexity: O(1)
184
183
  * Space Complexity: O(1)
185
- * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
186
184
  */
187
185
  /**
188
186
  * Time Complexity: O(1)
@@ -190,7 +188,7 @@ export class AVLTree extends BST {
190
188
  *
191
189
  * The function updates the height of a node in a binary tree based on the heights of its left and
192
190
  * right children.
193
- * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
191
+ * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
194
192
  */
195
193
  _updateHeight(node) {
196
194
  if (!node.left && !node.right)
@@ -204,71 +202,16 @@ export class AVLTree extends BST {
204
202
  else
205
203
  node.height = 1 + Math.max(node.right.height, node.left.height);
206
204
  }
207
- /**
208
- * Time Complexity: O(log n)
209
- * Space Complexity: O(1)
210
- * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
211
- */
212
- /**
213
- * Time Complexity: O(log n)
214
- * Space Complexity: O(1)
215
- *
216
- * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
217
- * to restore balance in an AVL tree after inserting a node.
218
- * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
219
- * AVL tree that needs to be balanced.
220
- */
221
- _balancePath(node) {
222
- node = this.ensureNode(node);
223
- const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
224
- for (let i = 0; i < path.length; i++) {
225
- // second O(log n)
226
- const A = path[i];
227
- // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
228
- this._updateHeight(A); // first O(1)
229
- // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
230
- // 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:
231
- switch (this._balanceFactor(A) // second O(1)
232
- ) {
233
- case -2:
234
- if (A && A.left) {
235
- if (this._balanceFactor(A.left) <= 0) {
236
- // second O(1)
237
- // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
238
- this._balanceLL(A);
239
- }
240
- else {
241
- // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
242
- this._balanceLR(A);
243
- }
244
- }
245
- break;
246
- case +2:
247
- if (A && A.right) {
248
- if (this._balanceFactor(A.right) >= 0) {
249
- // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
250
- this._balanceRR(A);
251
- }
252
- else {
253
- // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
254
- this._balanceRL(A);
255
- }
256
- }
257
- }
258
- // 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.
259
- }
260
- }
261
205
  /**
262
206
  * Time Complexity: O(1)
263
207
  * Space Complexity: O(1)
264
- * constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
265
208
  */
266
209
  /**
267
210
  * Time Complexity: O(1)
268
211
  * Space Complexity: O(1)
269
212
  *
270
213
  * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
271
- * @param {N} A - A is a node in a binary tree.
214
+ * @param {NODE} A - A is a node in a binary tree.
272
215
  */
273
216
  _balanceLL(A) {
274
217
  const parentOfA = A.parent;
@@ -309,7 +252,7 @@ export class AVLTree extends BST {
309
252
  * Space Complexity: O(1)
310
253
  *
311
254
  * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
312
- * @param {N} A - A is a node in a binary tree.
255
+ * @param {NODE} A - A is a node in a binary tree.
313
256
  */
314
257
  _balanceLR(A) {
315
258
  const parentOfA = A.parent;
@@ -365,7 +308,7 @@ export class AVLTree extends BST {
365
308
  * Space Complexity: O(1)
366
309
  *
367
310
  * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
368
- * @param {N} A - A is a node in a binary tree.
311
+ * @param {NODE} A - A is a node in a binary tree.
369
312
  */
370
313
  _balanceRR(A) {
371
314
  const parentOfA = A.parent;
@@ -407,7 +350,7 @@ export class AVLTree extends BST {
407
350
  * Space Complexity: O(1)
408
351
  *
409
352
  * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
410
- * @param {N} A - A is a node in a binary tree.
353
+ * @param {NODE} A - A is a node in a binary tree.
411
354
  */
412
355
  _balanceRL(A) {
413
356
  const parentOfA = A.parent;
@@ -454,6 +397,60 @@ export class AVLTree extends BST {
454
397
  B && this._updateHeight(B);
455
398
  C && this._updateHeight(C);
456
399
  }
400
+ /**
401
+ * Time Complexity: O(log n)
402
+ * Space Complexity: O(1)
403
+ * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
404
+ */
405
+ /**
406
+ * Time Complexity: O(log n)
407
+ * Space Complexity: O(1)
408
+ *
409
+ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
410
+ * to restore balance in an AVL tree after inserting a node.
411
+ * @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
412
+ * AVL tree that needs to be balanced.
413
+ */
414
+ _balancePath(node) {
415
+ node = this.ensureNode(node);
416
+ const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
417
+ for (let i = 0; i < path.length; i++) {
418
+ // second O(log n)
419
+ const A = path[i];
420
+ // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
421
+ this._updateHeight(A); // first O(1)
422
+ // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
423
+ // 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:
424
+ switch (this._balanceFactor(A) // second O(1)
425
+ ) {
426
+ case -2:
427
+ if (A && A.left) {
428
+ if (this._balanceFactor(A.left) <= 0) {
429
+ // second O(1)
430
+ // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
431
+ this._balanceLL(A);
432
+ }
433
+ else {
434
+ // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
435
+ this._balanceLR(A);
436
+ }
437
+ }
438
+ break;
439
+ case +2:
440
+ if (A && A.right) {
441
+ if (this._balanceFactor(A.right) >= 0) {
442
+ // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
443
+ this._balanceRR(A);
444
+ }
445
+ else {
446
+ // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
447
+ this._balanceRL(A);
448
+ }
449
+ }
450
+ }
451
+ // 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.
452
+ }
453
+ }
457
454
  _replaceNode(oldNode, newNode) {
458
455
  newNode.height = oldNode.height;
459
456
  return super._replaceNode(oldNode, newNode);