data-structure-typed 1.50.1 → 1.50.3
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 +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -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 +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-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 +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -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 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -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 +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- 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 +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- 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/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- 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 +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- 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 +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -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 +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- 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/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- 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 +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- 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/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- 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 +4 -4
- 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 +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- 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 +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -7,10 +7,33 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
export class AVLTreeNode extends BSTNode {
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The constructor function initializes a new instance of a class with a key and an optional value,
|
|
12
|
+
* and sets the height property to 0.
|
|
13
|
+
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
14
|
+
* constructor. It is used to initialize the key property of the object being created.
|
|
15
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
16
|
+
* value associated with the key in the constructor.
|
|
17
|
+
*/
|
|
11
18
|
constructor(key, value) {
|
|
12
19
|
super(key, value);
|
|
13
|
-
this.
|
|
20
|
+
this._height = 0;
|
|
21
|
+
}
|
|
22
|
+
_height;
|
|
23
|
+
/**
|
|
24
|
+
* The function returns the value of the height property.
|
|
25
|
+
* @returns The height of the object.
|
|
26
|
+
*/
|
|
27
|
+
get height() {
|
|
28
|
+
return this._height;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The above function sets the value of the height property.
|
|
32
|
+
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
33
|
+
* set.
|
|
34
|
+
*/
|
|
35
|
+
set height(value) {
|
|
36
|
+
this._height = value;
|
|
14
37
|
}
|
|
15
38
|
}
|
|
16
39
|
/**
|
|
@@ -25,7 +48,7 @@ export class AVLTreeNode extends BSTNode {
|
|
|
25
48
|
export class AVLTree extends BST {
|
|
26
49
|
/**
|
|
27
50
|
* 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,
|
|
51
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
29
52
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
30
53
|
* initialization.
|
|
31
54
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -43,7 +66,7 @@ export class AVLTree extends BST {
|
|
|
43
66
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
44
67
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
45
68
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
46
|
-
* node type `
|
|
69
|
+
* node type `NODE`.
|
|
47
70
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
48
71
|
*/
|
|
49
72
|
createNode(key, value) {
|
|
@@ -65,7 +88,7 @@ export class AVLTree extends BST {
|
|
|
65
88
|
}
|
|
66
89
|
/**
|
|
67
90
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
68
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
91
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
69
92
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
70
93
|
*/
|
|
71
94
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -112,8 +135,8 @@ export class AVLTree extends BST {
|
|
|
112
135
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
113
136
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
114
137
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
115
|
-
* parameter of type `
|
|
116
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
138
|
+
* parameter of type `NODE
|
|
139
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
117
140
|
*/
|
|
118
141
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
119
142
|
if (identifier instanceof AVLTreeNode)
|
|
@@ -129,9 +152,9 @@ export class AVLTree extends BST {
|
|
|
129
152
|
/**
|
|
130
153
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
131
154
|
* tree.
|
|
132
|
-
* @param {K |
|
|
133
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
134
|
-
* @param {K |
|
|
155
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
156
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
157
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
135
158
|
* node where the values from the source node will be swapped to.
|
|
136
159
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
137
160
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -158,14 +181,13 @@ export class AVLTree extends BST {
|
|
|
158
181
|
/**
|
|
159
182
|
* Time Complexity: O(1)
|
|
160
183
|
* 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
184
|
*/
|
|
163
185
|
/**
|
|
164
186
|
* Time Complexity: O(1)
|
|
165
187
|
* Space Complexity: O(1)
|
|
166
188
|
*
|
|
167
189
|
* The function calculates the balance factor of a node in a binary tree.
|
|
168
|
-
* @param {
|
|
190
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
169
191
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
170
192
|
* height of the left subtree from the height of the right subtree.
|
|
171
193
|
*/
|
|
@@ -182,7 +204,6 @@ export class AVLTree extends BST {
|
|
|
182
204
|
/**
|
|
183
205
|
* Time Complexity: O(1)
|
|
184
206
|
* 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
207
|
*/
|
|
187
208
|
/**
|
|
188
209
|
* Time Complexity: O(1)
|
|
@@ -190,7 +211,7 @@ export class AVLTree extends BST {
|
|
|
190
211
|
*
|
|
191
212
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
192
213
|
* right children.
|
|
193
|
-
* @param {
|
|
214
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
194
215
|
*/
|
|
195
216
|
_updateHeight(node) {
|
|
196
217
|
if (!node.left && !node.right)
|
|
@@ -204,71 +225,16 @@ export class AVLTree extends BST {
|
|
|
204
225
|
else
|
|
205
226
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
206
227
|
}
|
|
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
228
|
/**
|
|
262
229
|
* Time Complexity: O(1)
|
|
263
230
|
* 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
231
|
*/
|
|
266
232
|
/**
|
|
267
233
|
* Time Complexity: O(1)
|
|
268
234
|
* Space Complexity: O(1)
|
|
269
235
|
*
|
|
270
236
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
271
|
-
* @param {
|
|
237
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
272
238
|
*/
|
|
273
239
|
_balanceLL(A) {
|
|
274
240
|
const parentOfA = A.parent;
|
|
@@ -309,7 +275,7 @@ export class AVLTree extends BST {
|
|
|
309
275
|
* Space Complexity: O(1)
|
|
310
276
|
*
|
|
311
277
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
312
|
-
* @param {
|
|
278
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
313
279
|
*/
|
|
314
280
|
_balanceLR(A) {
|
|
315
281
|
const parentOfA = A.parent;
|
|
@@ -365,7 +331,7 @@ export class AVLTree extends BST {
|
|
|
365
331
|
* Space Complexity: O(1)
|
|
366
332
|
*
|
|
367
333
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
368
|
-
* @param {
|
|
334
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
369
335
|
*/
|
|
370
336
|
_balanceRR(A) {
|
|
371
337
|
const parentOfA = A.parent;
|
|
@@ -407,7 +373,7 @@ export class AVLTree extends BST {
|
|
|
407
373
|
* Space Complexity: O(1)
|
|
408
374
|
*
|
|
409
375
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
410
|
-
* @param {
|
|
376
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
411
377
|
*/
|
|
412
378
|
_balanceRL(A) {
|
|
413
379
|
const parentOfA = A.parent;
|
|
@@ -454,6 +420,69 @@ export class AVLTree extends BST {
|
|
|
454
420
|
B && this._updateHeight(B);
|
|
455
421
|
C && this._updateHeight(C);
|
|
456
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Time Complexity: O(log n)
|
|
425
|
+
* Space Complexity: O(1)
|
|
426
|
+
* 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.
|
|
427
|
+
*/
|
|
428
|
+
/**
|
|
429
|
+
* Time Complexity: O(log n)
|
|
430
|
+
* Space Complexity: O(1)
|
|
431
|
+
*
|
|
432
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
433
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
434
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
435
|
+
* AVL tree that needs to be balanced.
|
|
436
|
+
*/
|
|
437
|
+
_balancePath(node) {
|
|
438
|
+
node = this.ensureNode(node);
|
|
439
|
+
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
440
|
+
for (let i = 0; i < path.length; i++) {
|
|
441
|
+
// second O(log n)
|
|
442
|
+
const A = path[i];
|
|
443
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
444
|
+
this._updateHeight(A); // first O(1)
|
|
445
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
446
|
+
// 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:
|
|
447
|
+
switch (this._balanceFactor(A) // second O(1)
|
|
448
|
+
) {
|
|
449
|
+
case -2:
|
|
450
|
+
if (A && A.left) {
|
|
451
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
452
|
+
// second O(1)
|
|
453
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
454
|
+
this._balanceLL(A);
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
458
|
+
this._balanceLR(A);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
case +2:
|
|
463
|
+
if (A && A.right) {
|
|
464
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
465
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
466
|
+
this._balanceRR(A);
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
470
|
+
this._balanceRL(A);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
// 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.
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* The function replaces an old node with a new node while preserving the height of the old node.
|
|
479
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
|
|
480
|
+
* `newNode`.
|
|
481
|
+
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
482
|
+
* the data structure.
|
|
483
|
+
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the
|
|
484
|
+
* `oldNode` and `newNode` as arguments.
|
|
485
|
+
*/
|
|
457
486
|
_replaceNode(oldNode, newNode) {
|
|
458
487
|
newNode.height = oldNode.height;
|
|
459
488
|
return super._replaceNode(oldNode, newNode);
|
|
@@ -12,12 +12,34 @@ export declare class BinaryIndexedTree {
|
|
|
12
12
|
max: number;
|
|
13
13
|
});
|
|
14
14
|
protected _freqMap: Record<number, number>;
|
|
15
|
+
/**
|
|
16
|
+
* The function returns the frequency map of numbers.
|
|
17
|
+
* @returns The `_freqMap` property, which is a record with number keys and number values, is being
|
|
18
|
+
* returned.
|
|
19
|
+
*/
|
|
15
20
|
get freqMap(): Record<number, number>;
|
|
16
21
|
protected _msb: number;
|
|
22
|
+
/**
|
|
23
|
+
* The function returns the value of the _msb property.
|
|
24
|
+
* @returns The `_msb` property of the object.
|
|
25
|
+
*/
|
|
17
26
|
get msb(): number;
|
|
18
27
|
protected _negativeCount: number;
|
|
28
|
+
/**
|
|
29
|
+
* The function returns the value of the _negativeCount property.
|
|
30
|
+
* @returns The method is returning the value of the variable `_negativeCount`, which is of type
|
|
31
|
+
* `number`.
|
|
32
|
+
*/
|
|
19
33
|
get negativeCount(): number;
|
|
34
|
+
/**
|
|
35
|
+
* The above function returns the value of the protected variable `_freq`.
|
|
36
|
+
* @returns The frequency value stored in the protected variable `_freq`.
|
|
37
|
+
*/
|
|
20
38
|
get freq(): number;
|
|
39
|
+
/**
|
|
40
|
+
* The above function returns the maximum value.
|
|
41
|
+
* @returns The maximum value stored in the variable `_max`.
|
|
42
|
+
*/
|
|
21
43
|
get max(): number;
|
|
22
44
|
/**
|
|
23
45
|
* The function "readSingle" reads a single number from a specified index.
|
|
@@ -23,20 +23,42 @@ export class BinaryIndexedTree {
|
|
|
23
23
|
this._negativeCount = frequency < 0 ? max : 0;
|
|
24
24
|
}
|
|
25
25
|
_freqMap;
|
|
26
|
+
/**
|
|
27
|
+
* The function returns the frequency map of numbers.
|
|
28
|
+
* @returns The `_freqMap` property, which is a record with number keys and number values, is being
|
|
29
|
+
* returned.
|
|
30
|
+
*/
|
|
26
31
|
get freqMap() {
|
|
27
32
|
return this._freqMap;
|
|
28
33
|
}
|
|
29
34
|
_msb;
|
|
35
|
+
/**
|
|
36
|
+
* The function returns the value of the _msb property.
|
|
37
|
+
* @returns The `_msb` property of the object.
|
|
38
|
+
*/
|
|
30
39
|
get msb() {
|
|
31
40
|
return this._msb;
|
|
32
41
|
}
|
|
33
42
|
_negativeCount;
|
|
43
|
+
/**
|
|
44
|
+
* The function returns the value of the _negativeCount property.
|
|
45
|
+
* @returns The method is returning the value of the variable `_negativeCount`, which is of type
|
|
46
|
+
* `number`.
|
|
47
|
+
*/
|
|
34
48
|
get negativeCount() {
|
|
35
49
|
return this._negativeCount;
|
|
36
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* The above function returns the value of the protected variable `_freq`.
|
|
53
|
+
* @returns The frequency value stored in the protected variable `_freq`.
|
|
54
|
+
*/
|
|
37
55
|
get freq() {
|
|
38
56
|
return this._freq;
|
|
39
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* The above function returns the maximum value.
|
|
60
|
+
* @returns The maximum value stored in the variable `_max`.
|
|
61
|
+
*/
|
|
40
62
|
get max() {
|
|
41
63
|
return this._max;
|
|
42
64
|
}
|