data-structure-typed 1.50.0 → 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.
- package/CHANGELOG.md +2 -1
- package/README.md +204 -212
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +370 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/cjs/data-structures/hash/hash-map.js +160 -25
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/cjs/data-structures/heap/heap.js +66 -6
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/cjs/data-structures/queue/deque.js +101 -47
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/cjs/data-structures/queue/queue.js +47 -5
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/cjs/data-structures/stack/stack.js +22 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/cjs/data-structures/trie/trie.js +41 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/mjs/data-structures/hash/hash-map.js +160 -25
- package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/mjs/data-structures/heap/heap.js +66 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/mjs/data-structures/queue/deque.js +101 -47
- package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/mjs/data-structures/queue/queue.js +47 -5
- package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/mjs/data-structures/stack/stack.js +22 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/mjs/data-structures/trie/trie.js +41 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +1730 -1042
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +16 -40
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +71 -10
- package/test/unit/data-structures/queue/queue.test.ts +23 -6
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
- package/test/unit/unrestricted-interconversion.test.ts +143 -10
|
@@ -17,11 +17,11 @@ import type {
|
|
|
17
17
|
} from '../../types';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
|
|
20
|
-
export class AVLTreeNode<
|
|
21
|
-
K,
|
|
22
|
-
V,
|
|
23
|
-
|
|
24
|
-
> {
|
|
20
|
+
export class AVLTreeNode<
|
|
21
|
+
K = any,
|
|
22
|
+
V = any,
|
|
23
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
|
|
24
|
+
> extends BSTNode<K, V, NODE> {
|
|
25
25
|
height: number;
|
|
26
26
|
|
|
27
27
|
constructor(key: K, value?: V) {
|
|
@@ -42,21 +42,21 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
|
|
|
42
42
|
export class AVLTree<
|
|
43
43
|
K = any,
|
|
44
44
|
V = any,
|
|
45
|
-
|
|
46
|
-
TREE extends AVLTree<K, V,
|
|
45
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
46
|
+
TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>
|
|
47
47
|
>
|
|
48
|
-
extends BST<K, V,
|
|
49
|
-
implements IBinaryTree<K, V,
|
|
48
|
+
extends BST<K, V, NODE, TREE>
|
|
49
|
+
implements IBinaryTree<K, V, NODE, TREE> {
|
|
50
50
|
/**
|
|
51
51
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
52
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
52
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
53
53
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
54
54
|
* initialization.
|
|
55
55
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
56
56
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
57
57
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
58
58
|
*/
|
|
59
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
59
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeOptions<K>) {
|
|
60
60
|
super([], options);
|
|
61
61
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
62
62
|
}
|
|
@@ -67,11 +67,11 @@ export class AVLTree<
|
|
|
67
67
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
68
68
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
69
69
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
70
|
-
* node type `
|
|
70
|
+
* node type `NODE`.
|
|
71
71
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
72
72
|
*/
|
|
73
|
-
override createNode(key: K, value?: V):
|
|
74
|
-
return new AVLTreeNode<K, V,
|
|
73
|
+
override createNode(key: K, value?: V): NODE {
|
|
74
|
+
return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
@@ -82,7 +82,7 @@ export class AVLTree<
|
|
|
82
82
|
* @returns a new AVLTree object.
|
|
83
83
|
*/
|
|
84
84
|
override createTree(options?: AVLTreeOptions<K>): TREE {
|
|
85
|
-
return new AVLTree<K, V,
|
|
85
|
+
return new AVLTree<K, V, NODE, TREE>([], {
|
|
86
86
|
iterationType: this.iterationType,
|
|
87
87
|
variant: this.variant,
|
|
88
88
|
...options
|
|
@@ -91,10 +91,10 @@ export class AVLTree<
|
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
94
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
94
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
95
95
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
96
96
|
*/
|
|
97
|
-
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
97
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
98
98
|
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
99
99
|
}
|
|
100
100
|
|
|
@@ -116,7 +116,7 @@ export class AVLTree<
|
|
|
116
116
|
* being added to the binary tree.
|
|
117
117
|
* @returns The method is returning either the inserted node or undefined.
|
|
118
118
|
*/
|
|
119
|
-
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
119
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
120
120
|
if (keyOrNodeOrEntry === null) return false;
|
|
121
121
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
122
122
|
if (inserted) this._balancePath(keyOrNodeOrEntry);
|
|
@@ -140,13 +140,13 @@ export class AVLTree<
|
|
|
140
140
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
141
141
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
142
142
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
143
|
-
* parameter of type `
|
|
144
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
143
|
+
* parameter of type `NODE
|
|
144
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
145
145
|
*/
|
|
146
|
-
override delete<C extends BTNCallback<
|
|
146
|
+
override delete<C extends BTNCallback<NODE>>(
|
|
147
147
|
identifier: ReturnType<C>,
|
|
148
148
|
callback: C = this._defaultOneParamCallback as C
|
|
149
|
-
): BinaryTreeDeleteResult<
|
|
149
|
+
): BinaryTreeDeleteResult<NODE>[] {
|
|
150
150
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
151
151
|
const deletedResults = super.delete(identifier, callback);
|
|
152
152
|
for (const { needBalanced } of deletedResults) {
|
|
@@ -160,14 +160,17 @@ export class AVLTree<
|
|
|
160
160
|
/**
|
|
161
161
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
162
162
|
* tree.
|
|
163
|
-
* @param {K |
|
|
164
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
165
|
-
* @param {K |
|
|
163
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
164
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
165
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
166
166
|
* node where the values from the source node will be swapped to.
|
|
167
167
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
168
168
|
* if either `srcNode` or `destNode` is undefined.
|
|
169
169
|
*/
|
|
170
|
-
protected override _swapProperties(
|
|
170
|
+
protected override _swapProperties(
|
|
171
|
+
srcNode: BSTNKeyOrNode<K, NODE>,
|
|
172
|
+
destNode: BSTNKeyOrNode<K, NODE>
|
|
173
|
+
): NODE | undefined {
|
|
171
174
|
srcNode = this.ensureNode(srcNode);
|
|
172
175
|
destNode = this.ensureNode(destNode);
|
|
173
176
|
|
|
@@ -195,7 +198,6 @@ export class AVLTree<
|
|
|
195
198
|
/**
|
|
196
199
|
* Time Complexity: O(1)
|
|
197
200
|
* Space Complexity: O(1)
|
|
198
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
199
201
|
*/
|
|
200
202
|
|
|
201
203
|
/**
|
|
@@ -203,11 +205,11 @@ export class AVLTree<
|
|
|
203
205
|
* Space Complexity: O(1)
|
|
204
206
|
*
|
|
205
207
|
* The function calculates the balance factor of a node in a binary tree.
|
|
206
|
-
* @param {
|
|
208
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
207
209
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
208
210
|
* height of the left subtree from the height of the right subtree.
|
|
209
211
|
*/
|
|
210
|
-
protected _balanceFactor(node:
|
|
212
|
+
protected _balanceFactor(node: NODE): number {
|
|
211
213
|
if (!node.right)
|
|
212
214
|
// node has no right subtree
|
|
213
215
|
return -node.height;
|
|
@@ -220,7 +222,6 @@ export class AVLTree<
|
|
|
220
222
|
/**
|
|
221
223
|
* Time Complexity: O(1)
|
|
222
224
|
* Space Complexity: O(1)
|
|
223
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
224
225
|
*/
|
|
225
226
|
|
|
226
227
|
/**
|
|
@@ -229,9 +230,9 @@ export class AVLTree<
|
|
|
229
230
|
*
|
|
230
231
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
231
232
|
* right children.
|
|
232
|
-
* @param {
|
|
233
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
233
234
|
*/
|
|
234
|
-
protected _updateHeight(node:
|
|
235
|
+
protected _updateHeight(node: NODE): void {
|
|
235
236
|
if (!node.left && !node.right) node.height = 0;
|
|
236
237
|
else if (!node.left) {
|
|
237
238
|
const rightHeight = node.right ? node.right.height : 0;
|
|
@@ -240,65 +241,9 @@ export class AVLTree<
|
|
|
240
241
|
else node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
241
242
|
}
|
|
242
243
|
|
|
243
|
-
/**
|
|
244
|
-
* Time Complexity: O(log n)
|
|
245
|
-
* Space Complexity: O(1)
|
|
246
|
-
* 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.
|
|
247
|
-
*/
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(log n)
|
|
251
|
-
* Space Complexity: O(1)
|
|
252
|
-
*
|
|
253
|
-
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
254
|
-
* to restore balance in an AVL tree after inserting a node.
|
|
255
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
256
|
-
* AVL tree that needs to be balanced.
|
|
257
|
-
*/
|
|
258
|
-
protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void {
|
|
259
|
-
node = this.ensureNode(node);
|
|
260
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
261
|
-
for (let i = 0; i < path.length; i++) {
|
|
262
|
-
// second O(log n)
|
|
263
|
-
const A = path[i];
|
|
264
|
-
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
265
|
-
this._updateHeight(A); // first O(1)
|
|
266
|
-
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
267
|
-
// 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:
|
|
268
|
-
switch (
|
|
269
|
-
this._balanceFactor(A) // second O(1)
|
|
270
|
-
) {
|
|
271
|
-
case -2:
|
|
272
|
-
if (A && A.left) {
|
|
273
|
-
if (this._balanceFactor(A.left) <= 0) {
|
|
274
|
-
// second O(1)
|
|
275
|
-
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
276
|
-
this._balanceLL(A);
|
|
277
|
-
} else {
|
|
278
|
-
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
279
|
-
this._balanceLR(A);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
break;
|
|
283
|
-
case +2:
|
|
284
|
-
if (A && A.right) {
|
|
285
|
-
if (this._balanceFactor(A.right) >= 0) {
|
|
286
|
-
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
287
|
-
this._balanceRR(A);
|
|
288
|
-
} else {
|
|
289
|
-
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
290
|
-
this._balanceRL(A);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
// 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.
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
244
|
/**
|
|
299
245
|
* Time Complexity: O(1)
|
|
300
246
|
* Space Complexity: O(1)
|
|
301
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
302
247
|
*/
|
|
303
248
|
|
|
304
249
|
/**
|
|
@@ -306,9 +251,9 @@ export class AVLTree<
|
|
|
306
251
|
* Space Complexity: O(1)
|
|
307
252
|
*
|
|
308
253
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
309
|
-
* @param {
|
|
254
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
310
255
|
*/
|
|
311
|
-
protected _balanceLL(A:
|
|
256
|
+
protected _balanceLL(A: NODE): void {
|
|
312
257
|
const parentOfA = A.parent;
|
|
313
258
|
const B = A.left;
|
|
314
259
|
A.parent = B;
|
|
@@ -344,9 +289,9 @@ export class AVLTree<
|
|
|
344
289
|
* Space Complexity: O(1)
|
|
345
290
|
*
|
|
346
291
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
347
|
-
* @param {
|
|
292
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
348
293
|
*/
|
|
349
|
-
protected _balanceLR(A:
|
|
294
|
+
protected _balanceLR(A: NODE): void {
|
|
350
295
|
const parentOfA = A.parent;
|
|
351
296
|
const B = A.left;
|
|
352
297
|
let C = undefined;
|
|
@@ -400,9 +345,9 @@ export class AVLTree<
|
|
|
400
345
|
* Space Complexity: O(1)
|
|
401
346
|
*
|
|
402
347
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
403
|
-
* @param {
|
|
348
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
404
349
|
*/
|
|
405
|
-
protected _balanceRR(A:
|
|
350
|
+
protected _balanceRR(A: NODE): void {
|
|
406
351
|
const parentOfA = A.parent;
|
|
407
352
|
const B = A.right;
|
|
408
353
|
A.parent = B;
|
|
@@ -443,9 +388,9 @@ export class AVLTree<
|
|
|
443
388
|
* Space Complexity: O(1)
|
|
444
389
|
*
|
|
445
390
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
446
|
-
* @param {
|
|
391
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
447
392
|
*/
|
|
448
|
-
protected _balanceRL(A:
|
|
393
|
+
protected _balanceRL(A: NODE): void {
|
|
449
394
|
const parentOfA = A.parent;
|
|
450
395
|
const B = A.right;
|
|
451
396
|
let C = undefined;
|
|
@@ -488,7 +433,62 @@ export class AVLTree<
|
|
|
488
433
|
C && this._updateHeight(C);
|
|
489
434
|
}
|
|
490
435
|
|
|
491
|
-
|
|
436
|
+
/**
|
|
437
|
+
* Time Complexity: O(log n)
|
|
438
|
+
* Space Complexity: O(1)
|
|
439
|
+
* 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.
|
|
440
|
+
*/
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Time Complexity: O(log n)
|
|
444
|
+
* Space Complexity: O(1)
|
|
445
|
+
*
|
|
446
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
447
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
448
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
449
|
+
* AVL tree that needs to be balanced.
|
|
450
|
+
*/
|
|
451
|
+
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void {
|
|
452
|
+
node = this.ensureNode(node);
|
|
453
|
+
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
454
|
+
for (let i = 0; i < path.length; i++) {
|
|
455
|
+
// second O(log n)
|
|
456
|
+
const A = path[i];
|
|
457
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
458
|
+
this._updateHeight(A); // first O(1)
|
|
459
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
460
|
+
// 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:
|
|
461
|
+
switch (
|
|
462
|
+
this._balanceFactor(A) // second O(1)
|
|
463
|
+
) {
|
|
464
|
+
case -2:
|
|
465
|
+
if (A && A.left) {
|
|
466
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
467
|
+
// second O(1)
|
|
468
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
469
|
+
this._balanceLL(A);
|
|
470
|
+
} else {
|
|
471
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
472
|
+
this._balanceLR(A);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
break;
|
|
476
|
+
case +2:
|
|
477
|
+
if (A && A.right) {
|
|
478
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
479
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
480
|
+
this._balanceRR(A);
|
|
481
|
+
} else {
|
|
482
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
483
|
+
this._balanceRL(A);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
// 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.
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
492
492
|
newNode.height = oldNode.height;
|
|
493
493
|
|
|
494
494
|
return super._replaceNode(oldNode, newNode);
|