red-black-tree-typed 1.53.7 → 1.54.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/LICENSE +2 -2
- package/README.md +52 -0
- package/dist/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -7
- package/src/index.ts +4 -4
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -11,31 +11,37 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param {V} [value] - The
|
|
19
|
-
*
|
|
14
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
15
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
16
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
17
|
+
* associated value or data.
|
|
18
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
19
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
20
|
+
* default to `undefined`.
|
|
20
21
|
*/
|
|
21
22
|
constructor(key, value) {
|
|
22
23
|
super(key, value);
|
|
23
|
-
this.
|
|
24
|
+
this.parent = undefined;
|
|
25
|
+
this._left = undefined;
|
|
26
|
+
this._right = undefined;
|
|
24
27
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
* @returns The height of the object.
|
|
28
|
-
*/
|
|
29
|
-
get height() {
|
|
30
|
-
return this._height;
|
|
28
|
+
get left() {
|
|
29
|
+
return this._left;
|
|
31
30
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
set left(v) {
|
|
32
|
+
if (v) {
|
|
33
|
+
v.parent = this;
|
|
34
|
+
}
|
|
35
|
+
this._left = v;
|
|
36
|
+
}
|
|
37
|
+
get right() {
|
|
38
|
+
return this._right;
|
|
39
|
+
}
|
|
40
|
+
set right(v) {
|
|
41
|
+
if (v) {
|
|
42
|
+
v.parent = this;
|
|
43
|
+
}
|
|
44
|
+
this._right = v;
|
|
39
45
|
}
|
|
40
46
|
}
|
|
41
47
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -50,15 +56,15 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
50
56
|
*/
|
|
51
57
|
class AVLTree extends bst_1.BST {
|
|
52
58
|
/**
|
|
53
|
-
* This
|
|
54
|
-
*
|
|
55
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
56
|
-
* iterable
|
|
57
|
-
*
|
|
58
|
-
* @param [options] - The `options` parameter
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
59
|
+
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
60
|
+
* in an iterable format.
|
|
61
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
62
|
+
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
|
|
63
|
+
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
64
|
+
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
65
|
+
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
66
|
+
* AVL tree. These options could include things like custom comparators, initial capacity, or any
|
|
67
|
+
* other configuration settings specific
|
|
62
68
|
*/
|
|
63
69
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
64
70
|
super([], options);
|
|
@@ -66,18 +72,24 @@ class AVLTree extends bst_1.BST {
|
|
|
66
72
|
super.addMany(keysNodesEntriesOrRaws);
|
|
67
73
|
}
|
|
68
74
|
/**
|
|
75
|
+
* Time Complexity: O(1)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
69
78
|
* The function creates a new AVL tree node with the given key and value.
|
|
70
79
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
71
80
|
* created.
|
|
72
81
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
73
82
|
* value associated with the key in the node being created.
|
|
74
83
|
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
75
|
-
* type
|
|
84
|
+
* type AVLTreeNode<K, V>.
|
|
76
85
|
*/
|
|
77
86
|
createNode(key, value) {
|
|
78
87
|
return new AVLTreeNode(key, this._isMapMode ? undefined : value);
|
|
79
88
|
}
|
|
80
89
|
/**
|
|
90
|
+
* Time Complexity: O(1)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
81
93
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
82
94
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
83
95
|
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
@@ -85,54 +97,56 @@ class AVLTree extends bst_1.BST {
|
|
|
85
97
|
* @returns a new AVLTree object.
|
|
86
98
|
*/
|
|
87
99
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
100
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
89
101
|
}
|
|
90
102
|
/**
|
|
103
|
+
* Time Complexity: O(1)
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
91
106
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
92
|
-
* @param {BTNRep<K, V,
|
|
93
|
-
* `
|
|
94
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
107
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
108
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
109
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
95
110
|
* an instance of the `AVLTreeNode` class.
|
|
96
111
|
*/
|
|
97
|
-
isNode(
|
|
98
|
-
return
|
|
112
|
+
isNode(keyNodeOrEntry) {
|
|
113
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
99
114
|
}
|
|
100
115
|
/**
|
|
101
116
|
* Time Complexity: O(log n)
|
|
102
|
-
* Space Complexity: O(
|
|
117
|
+
* Space Complexity: O(log n)
|
|
103
118
|
*
|
|
104
119
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
105
120
|
* structure, then balances the path.
|
|
106
|
-
* @param {BTNRep<K, V,
|
|
107
|
-
* `
|
|
108
|
-
* `RawElement`.
|
|
121
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
122
|
+
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
|
|
109
123
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
110
124
|
* the key or node being added to the data structure.
|
|
111
125
|
* @returns The method is returning a boolean value.
|
|
112
126
|
*/
|
|
113
|
-
add(
|
|
114
|
-
if (
|
|
127
|
+
add(keyNodeOrEntry, value) {
|
|
128
|
+
if (keyNodeOrEntry === null)
|
|
115
129
|
return false;
|
|
116
|
-
const inserted = super.add(
|
|
130
|
+
const inserted = super.add(keyNodeOrEntry, value);
|
|
117
131
|
if (inserted)
|
|
118
|
-
this._balancePath(
|
|
132
|
+
this._balancePath(keyNodeOrEntry);
|
|
119
133
|
return inserted;
|
|
120
134
|
}
|
|
121
135
|
/**
|
|
122
136
|
* Time Complexity: O(log n)
|
|
123
|
-
* Space Complexity: O(
|
|
137
|
+
* Space Complexity: O(log n)
|
|
124
138
|
*
|
|
125
139
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
126
140
|
* balances the tree if necessary.
|
|
127
|
-
* @param {BTNRep<K, V,
|
|
141
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
128
142
|
* parameter in the `override delete` method can be one of the following types:
|
|
129
143
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
130
144
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
131
145
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
132
146
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
133
147
|
*/
|
|
134
|
-
delete(
|
|
135
|
-
const deletedResults = super.delete(
|
|
148
|
+
delete(keyNodeOrEntry) {
|
|
149
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
136
150
|
for (const { needBalanced } of deletedResults) {
|
|
137
151
|
if (needBalanced) {
|
|
138
152
|
this._balancePath(needBalanced);
|
|
@@ -140,16 +154,57 @@ class AVLTree extends bst_1.BST {
|
|
|
140
154
|
}
|
|
141
155
|
return deletedResults;
|
|
142
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(n)
|
|
159
|
+
* Space Complexity: O(n)
|
|
160
|
+
*
|
|
161
|
+
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure
|
|
162
|
+
* by applying a callback function to each entry and creating a new AVLTree with the results.
|
|
163
|
+
* @param callback - A function that will be called for each entry in the AVLTree. It takes four
|
|
164
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
165
|
+
* the AVLTree itself.
|
|
166
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
167
|
+
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
|
|
168
|
+
* options for the AVL tree being created during the mapping process. These options could include
|
|
169
|
+
* custom comparators, initial
|
|
170
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
171
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
172
|
+
* (value of `this`) within the callback function. This can be useful when you want to access
|
|
173
|
+
* properties or
|
|
174
|
+
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
|
|
175
|
+
* modified by the provided callback function.
|
|
176
|
+
*/
|
|
177
|
+
map(callback, options, thisArg) {
|
|
178
|
+
const newTree = new AVLTree([], options);
|
|
179
|
+
let index = 0;
|
|
180
|
+
for (const [key, value] of this) {
|
|
181
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
182
|
+
}
|
|
183
|
+
return newTree;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Time Complexity: O(n)
|
|
187
|
+
* Space Complexity: O(n)
|
|
188
|
+
*
|
|
189
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
190
|
+
* structure.
|
|
191
|
+
* @returns A cloned tree object is being returned.
|
|
192
|
+
*/
|
|
193
|
+
clone() {
|
|
194
|
+
const cloned = this.createTree();
|
|
195
|
+
this._clone(cloned);
|
|
196
|
+
return cloned;
|
|
197
|
+
}
|
|
143
198
|
/**
|
|
144
199
|
* Time Complexity: O(1)
|
|
145
200
|
* Space Complexity: O(1)
|
|
146
201
|
*
|
|
147
202
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
148
203
|
* binary search tree.
|
|
149
|
-
* @param {
|
|
150
|
-
* object (`
|
|
151
|
-
* @param {
|
|
152
|
-
* `R` or an instance of `BSTNOptKeyOrNode<K,
|
|
204
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node
|
|
205
|
+
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
|
|
206
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
|
|
207
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
|
|
153
208
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
154
209
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
155
210
|
*/
|
|
@@ -179,7 +234,7 @@ class AVLTree extends bst_1.BST {
|
|
|
179
234
|
* Space Complexity: O(1)
|
|
180
235
|
*
|
|
181
236
|
* The function calculates the balance factor of a node in a binary tree.
|
|
182
|
-
* @param {
|
|
237
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
183
238
|
* binary tree data structure.
|
|
184
239
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
185
240
|
* height of the left subtree from the height of the right subtree.
|
|
@@ -200,7 +255,7 @@ class AVLTree extends bst_1.BST {
|
|
|
200
255
|
*
|
|
201
256
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
202
257
|
* right children.
|
|
203
|
-
* @param {
|
|
258
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
204
259
|
*/
|
|
205
260
|
_updateHeight(node) {
|
|
206
261
|
if (!node.left && !node.right)
|
|
@@ -219,12 +274,13 @@ class AVLTree extends bst_1.BST {
|
|
|
219
274
|
* Space Complexity: O(1)
|
|
220
275
|
*
|
|
221
276
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
222
|
-
* @param {
|
|
277
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
223
278
|
*/
|
|
224
279
|
_balanceLL(A) {
|
|
225
280
|
const parentOfA = A.parent;
|
|
226
281
|
const B = A.left;
|
|
227
|
-
|
|
282
|
+
if (B !== null)
|
|
283
|
+
A.parent = B;
|
|
228
284
|
if (B && B.right) {
|
|
229
285
|
B.right.parent = A;
|
|
230
286
|
}
|
|
@@ -256,7 +312,7 @@ class AVLTree extends bst_1.BST {
|
|
|
256
312
|
* Space Complexity: O(1)
|
|
257
313
|
*
|
|
258
314
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
259
|
-
* @param {
|
|
315
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
260
316
|
*/
|
|
261
317
|
_balanceLR(A) {
|
|
262
318
|
const parentOfA = A.parent;
|
|
@@ -265,13 +321,14 @@ class AVLTree extends bst_1.BST {
|
|
|
265
321
|
if (B) {
|
|
266
322
|
C = B.right;
|
|
267
323
|
}
|
|
268
|
-
if (A)
|
|
324
|
+
if (A && C !== null)
|
|
269
325
|
A.parent = C;
|
|
270
|
-
if (B)
|
|
326
|
+
if (B && C !== null)
|
|
271
327
|
B.parent = C;
|
|
272
328
|
if (C) {
|
|
273
329
|
if (C.left) {
|
|
274
|
-
|
|
330
|
+
if (B !== null)
|
|
331
|
+
C.left.parent = B;
|
|
275
332
|
}
|
|
276
333
|
if (C.right) {
|
|
277
334
|
C.right.parent = A;
|
|
@@ -310,12 +367,13 @@ class AVLTree extends bst_1.BST {
|
|
|
310
367
|
* Space Complexity: O(1)
|
|
311
368
|
*
|
|
312
369
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
313
|
-
* @param {
|
|
370
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
314
371
|
*/
|
|
315
372
|
_balanceRR(A) {
|
|
316
373
|
const parentOfA = A.parent;
|
|
317
374
|
const B = A.right;
|
|
318
|
-
|
|
375
|
+
if (B !== null)
|
|
376
|
+
A.parent = B;
|
|
319
377
|
if (B) {
|
|
320
378
|
if (B.left) {
|
|
321
379
|
B.left.parent = A;
|
|
@@ -349,7 +407,7 @@ class AVLTree extends bst_1.BST {
|
|
|
349
407
|
* Space Complexity: O(1)
|
|
350
408
|
*
|
|
351
409
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
352
|
-
* @param {
|
|
410
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
353
411
|
*/
|
|
354
412
|
_balanceRL(A) {
|
|
355
413
|
const parentOfA = A.parent;
|
|
@@ -358,15 +416,17 @@ class AVLTree extends bst_1.BST {
|
|
|
358
416
|
if (B) {
|
|
359
417
|
C = B.left;
|
|
360
418
|
}
|
|
361
|
-
|
|
362
|
-
|
|
419
|
+
if (C !== null)
|
|
420
|
+
A.parent = C;
|
|
421
|
+
if (B && C !== null)
|
|
363
422
|
B.parent = C;
|
|
364
423
|
if (C) {
|
|
365
424
|
if (C.left) {
|
|
366
425
|
C.left.parent = A;
|
|
367
426
|
}
|
|
368
427
|
if (C.right) {
|
|
369
|
-
|
|
428
|
+
if (B !== null)
|
|
429
|
+
C.right.parent = B;
|
|
370
430
|
}
|
|
371
431
|
C.parent = parentOfA;
|
|
372
432
|
}
|
|
@@ -404,8 +464,8 @@ class AVLTree extends bst_1.BST {
|
|
|
404
464
|
*
|
|
405
465
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
406
466
|
* to restore balance in an AVL tree after inserting a node.
|
|
407
|
-
* @param {BTNRep<K, V,
|
|
408
|
-
* `BTNRep<K, V,
|
|
467
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
|
|
468
|
+
* `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
409
469
|
*/
|
|
410
470
|
_balancePath(node) {
|
|
411
471
|
node = this.ensureNode(node);
|
|
@@ -455,9 +515,9 @@ class AVLTree extends bst_1.BST {
|
|
|
455
515
|
*
|
|
456
516
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
457
517
|
* same as the old node.
|
|
458
|
-
* @param {
|
|
518
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
459
519
|
* the data structure.
|
|
460
|
-
* @param {
|
|
520
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
461
521
|
* the data structure.
|
|
462
522
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
463
523
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
@@ -9,6 +9,9 @@ exports.BinaryIndexedTree = void 0;
|
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
12
15
|
class BinaryIndexedTree {
|
|
13
16
|
/**
|
|
14
17
|
* The constructor initializes the properties of an object, including default frequency, maximum
|