doubly-linked-list-typed 1.54.0 → 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 +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- 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 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- 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 +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
- 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 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- 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/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- 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/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -3
- package/dist/interfaces/binary-tree.d.ts +8 -8
- 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 +3 -3
- 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/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- 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/linked-list/singly-linked-list.ts +3 -0
- 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/trie/trie.ts +0 -4
- package/src/index.ts +2 -3
- package/src/interfaces/binary-tree.ts +10 -24
- 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 +5 -5
- 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/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
|
@@ -7,32 +7,56 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type {
|
|
10
|
-
AVLTreeNested,
|
|
11
|
-
AVLTreeNodeNested,
|
|
12
10
|
AVLTreeOptions,
|
|
13
11
|
BinaryTreeDeleteResult,
|
|
14
12
|
BSTNOptKeyOrNode,
|
|
15
13
|
BTNRep,
|
|
16
|
-
EntryCallback
|
|
14
|
+
EntryCallback,
|
|
15
|
+
OptNodeOrNull
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { IBinaryTree } from '../../interfaces';
|
|
19
18
|
|
|
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> {
|
|
19
|
+
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
25
20
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @param {V} [value] - The
|
|
31
|
-
*
|
|
21
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
22
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
23
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
24
|
+
* associated value or data.
|
|
25
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
26
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
27
|
+
* default to `undefined`.
|
|
32
28
|
*/
|
|
33
29
|
constructor(key: K, value?: V) {
|
|
34
30
|
super(key, value);
|
|
35
31
|
}
|
|
32
|
+
|
|
33
|
+
override parent?: AVLTreeNode<K, V> = undefined;
|
|
34
|
+
|
|
35
|
+
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
36
|
+
|
|
37
|
+
override get left(): OptNodeOrNull<AVLTreeNode<K, V>> {
|
|
38
|
+
return this._left;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override set left(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
|
|
42
|
+
if (v) {
|
|
43
|
+
v.parent = this;
|
|
44
|
+
}
|
|
45
|
+
this._left = v;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
override _right?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
49
|
+
|
|
50
|
+
override get right(): OptNodeOrNull<AVLTreeNode<K, V>> {
|
|
51
|
+
return this._right;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
override set right(v: OptNodeOrNull<AVLTreeNode<K, V>>) {
|
|
55
|
+
if (v) {
|
|
56
|
+
v.parent = this;
|
|
57
|
+
}
|
|
58
|
+
this._right = v;
|
|
59
|
+
}
|
|
36
60
|
}
|
|
37
61
|
|
|
38
62
|
/**
|
|
@@ -44,121 +68,114 @@ export class AVLTreeNode<
|
|
|
44
68
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
45
69
|
* 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.
|
|
46
70
|
*/
|
|
47
|
-
export class AVLTree<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
R = object,
|
|
51
|
-
MK = any,
|
|
52
|
-
MV = any,
|
|
53
|
-
MR = object,
|
|
54
|
-
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
55
|
-
TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
|
|
56
|
-
K,
|
|
57
|
-
V,
|
|
58
|
-
R,
|
|
59
|
-
MK,
|
|
60
|
-
MV,
|
|
61
|
-
MR,
|
|
62
|
-
NODE,
|
|
63
|
-
AVLTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
64
|
-
>
|
|
65
|
-
>
|
|
66
|
-
extends BST<K, V, R, MK, MV, MR, NODE, TREE>
|
|
67
|
-
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
71
|
+
export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
72
|
+
extends BST<K, V, R, MK, MV, MR>
|
|
73
|
+
implements IBinaryTree<K, V, R, MK, MV, MR>
|
|
68
74
|
{
|
|
69
75
|
/**
|
|
70
|
-
* This
|
|
71
|
-
*
|
|
72
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
73
|
-
* iterable
|
|
74
|
-
*
|
|
75
|
-
* @param [options] - The `options` parameter
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
76
|
+
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
77
|
+
* in an iterable format.
|
|
78
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
79
|
+
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
|
|
80
|
+
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
81
|
+
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
82
|
+
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
83
|
+
* AVL tree. These options could include things like custom comparators, initial capacity, or any
|
|
84
|
+
* other configuration settings specific
|
|
79
85
|
*/
|
|
80
|
-
constructor(
|
|
86
|
+
constructor(
|
|
87
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [],
|
|
88
|
+
options?: AVLTreeOptions<K, V, R>
|
|
89
|
+
) {
|
|
81
90
|
super([], options);
|
|
82
91
|
if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
|
|
83
92
|
}
|
|
84
93
|
|
|
85
94
|
/**
|
|
95
|
+
* Time Complexity: O(1)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
|
+
*
|
|
86
98
|
* The function creates a new AVL tree node with the given key and value.
|
|
87
99
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
88
100
|
* created.
|
|
89
101
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
90
102
|
* value associated with the key in the node being created.
|
|
91
103
|
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
92
|
-
* type
|
|
104
|
+
* type AVLTreeNode<K, V>.
|
|
93
105
|
*/
|
|
94
|
-
override createNode(key: K, value?: V):
|
|
95
|
-
return new AVLTreeNode<K, V
|
|
106
|
+
override createNode(key: K, value?: V): AVLTreeNode<K, V> {
|
|
107
|
+
return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>;
|
|
96
108
|
}
|
|
97
109
|
|
|
98
110
|
/**
|
|
111
|
+
* Time Complexity: O(1)
|
|
112
|
+
* Space Complexity: O(1)
|
|
113
|
+
*
|
|
99
114
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
100
115
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
101
116
|
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
102
117
|
* being created.
|
|
103
118
|
* @returns a new AVLTree object.
|
|
104
119
|
*/
|
|
105
|
-
override createTree(options?: AVLTreeOptions<K, V, R>)
|
|
106
|
-
return new AVLTree<K, V, R, MK, MV, MR
|
|
120
|
+
override createTree(options?: AVLTreeOptions<K, V, R>) {
|
|
121
|
+
return new AVLTree<K, V, R, MK, MV, MR>([], {
|
|
107
122
|
iterationType: this.iterationType,
|
|
108
123
|
isMapMode: this._isMapMode,
|
|
109
124
|
specifyComparable: this._specifyComparable,
|
|
110
125
|
toEntryFn: this._toEntryFn,
|
|
111
126
|
isReverse: this._isReverse,
|
|
112
127
|
...options
|
|
113
|
-
})
|
|
128
|
+
});
|
|
114
129
|
}
|
|
115
130
|
|
|
116
131
|
/**
|
|
132
|
+
* Time Complexity: O(1)
|
|
133
|
+
* Space Complexity: O(1)
|
|
134
|
+
*
|
|
117
135
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
118
|
-
* @param {BTNRep<K, V,
|
|
119
|
-
* `
|
|
120
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
136
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
137
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
138
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
121
139
|
* an instance of the `AVLTreeNode` class.
|
|
122
140
|
*/
|
|
123
|
-
override isNode(
|
|
124
|
-
return
|
|
141
|
+
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
125
143
|
}
|
|
126
144
|
|
|
127
145
|
/**
|
|
128
146
|
* Time Complexity: O(log n)
|
|
129
|
-
* Space Complexity: O(
|
|
147
|
+
* Space Complexity: O(log n)
|
|
130
148
|
*
|
|
131
149
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
132
150
|
* structure, then balances the path.
|
|
133
|
-
* @param {BTNRep<K, V,
|
|
134
|
-
* `
|
|
135
|
-
* `RawElement`.
|
|
151
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
152
|
+
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
|
|
136
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
137
154
|
* the key or node being added to the data structure.
|
|
138
155
|
* @returns The method is returning a boolean value.
|
|
139
156
|
*/
|
|
140
|
-
override add(
|
|
141
|
-
if (
|
|
142
|
-
const inserted = super.add(
|
|
143
|
-
if (inserted) this._balancePath(
|
|
157
|
+
override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean {
|
|
158
|
+
if (keyNodeOrEntry === null) return false;
|
|
159
|
+
const inserted = super.add(keyNodeOrEntry, value);
|
|
160
|
+
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
144
161
|
return inserted;
|
|
145
162
|
}
|
|
146
163
|
|
|
147
164
|
/**
|
|
148
165
|
* Time Complexity: O(log n)
|
|
149
|
-
* Space Complexity: O(
|
|
166
|
+
* Space Complexity: O(log n)
|
|
150
167
|
*
|
|
151
168
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
152
169
|
* balances the tree if necessary.
|
|
153
|
-
* @param {BTNRep<K, V,
|
|
170
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
154
171
|
* parameter in the `override delete` method can be one of the following types:
|
|
155
172
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
156
173
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
157
174
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
158
175
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
159
176
|
*/
|
|
160
|
-
override delete(
|
|
161
|
-
const deletedResults = super.delete(
|
|
177
|
+
override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
162
179
|
for (const { needBalanced } of deletedResults) {
|
|
163
180
|
if (needBalanced) {
|
|
164
181
|
this._balancePath(needBalanced);
|
|
@@ -167,6 +184,26 @@ export class AVLTree<
|
|
|
167
184
|
return deletedResults;
|
|
168
185
|
}
|
|
169
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(n)
|
|
189
|
+
* Space Complexity: O(n)
|
|
190
|
+
*
|
|
191
|
+
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure
|
|
192
|
+
* by applying a callback function to each entry and creating a new AVLTree with the results.
|
|
193
|
+
* @param callback - A function that will be called for each entry in the AVLTree. It takes four
|
|
194
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
195
|
+
* the AVLTree itself.
|
|
196
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
197
|
+
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
|
|
198
|
+
* options for the AVL tree being created during the mapping process. These options could include
|
|
199
|
+
* custom comparators, initial
|
|
200
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
201
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
202
|
+
* (value of `this`) within the callback function. This can be useful when you want to access
|
|
203
|
+
* properties or
|
|
204
|
+
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
|
|
205
|
+
* modified by the provided callback function.
|
|
206
|
+
*/
|
|
170
207
|
override map(
|
|
171
208
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
172
209
|
options?: AVLTreeOptions<MK, MV, MR>,
|
|
@@ -180,23 +217,37 @@ export class AVLTree<
|
|
|
180
217
|
return newTree;
|
|
181
218
|
}
|
|
182
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Time Complexity: O(n)
|
|
222
|
+
* Space Complexity: O(n)
|
|
223
|
+
*
|
|
224
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
225
|
+
* structure.
|
|
226
|
+
* @returns A cloned tree object is being returned.
|
|
227
|
+
*/
|
|
228
|
+
override clone() {
|
|
229
|
+
const cloned = this.createTree();
|
|
230
|
+
this._clone(cloned);
|
|
231
|
+
return cloned;
|
|
232
|
+
}
|
|
233
|
+
|
|
183
234
|
/**
|
|
184
235
|
* Time Complexity: O(1)
|
|
185
236
|
* Space Complexity: O(1)
|
|
186
237
|
*
|
|
187
238
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
188
239
|
* binary search tree.
|
|
189
|
-
* @param {
|
|
190
|
-
* object (`
|
|
191
|
-
* @param {
|
|
192
|
-
* `R` or an instance of `BSTNOptKeyOrNode<K,
|
|
240
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node
|
|
241
|
+
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
|
|
242
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
|
|
243
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
|
|
193
244
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
194
245
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
195
246
|
*/
|
|
196
247
|
protected override _swapProperties(
|
|
197
|
-
srcNode:
|
|
198
|
-
destNode:
|
|
199
|
-
):
|
|
248
|
+
srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>,
|
|
249
|
+
destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>
|
|
250
|
+
): AVLTreeNode<K, V> | undefined {
|
|
200
251
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
201
252
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
202
253
|
|
|
@@ -226,12 +277,12 @@ export class AVLTree<
|
|
|
226
277
|
* Space Complexity: O(1)
|
|
227
278
|
*
|
|
228
279
|
* The function calculates the balance factor of a node in a binary tree.
|
|
229
|
-
* @param {
|
|
280
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
230
281
|
* binary tree data structure.
|
|
231
282
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
232
283
|
* height of the left subtree from the height of the right subtree.
|
|
233
284
|
*/
|
|
234
|
-
protected _balanceFactor(node:
|
|
285
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number {
|
|
235
286
|
if (!node.right)
|
|
236
287
|
// node has no right subtree
|
|
237
288
|
return -node.height;
|
|
@@ -247,9 +298,9 @@ export class AVLTree<
|
|
|
247
298
|
*
|
|
248
299
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
249
300
|
* right children.
|
|
250
|
-
* @param {
|
|
301
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
251
302
|
*/
|
|
252
|
-
protected _updateHeight(node:
|
|
303
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void {
|
|
253
304
|
if (!node.left && !node.right) node.height = 0;
|
|
254
305
|
else if (!node.left) {
|
|
255
306
|
const rightHeight = node.right ? node.right.height : 0;
|
|
@@ -263,9 +314,9 @@ export class AVLTree<
|
|
|
263
314
|
* Space Complexity: O(1)
|
|
264
315
|
*
|
|
265
316
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
266
|
-
* @param {
|
|
317
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
267
318
|
*/
|
|
268
|
-
protected _balanceLL(A:
|
|
319
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void {
|
|
269
320
|
const parentOfA = A.parent;
|
|
270
321
|
const B = A.left;
|
|
271
322
|
if (B !== null) A.parent = B;
|
|
@@ -296,9 +347,9 @@ export class AVLTree<
|
|
|
296
347
|
* Space Complexity: O(1)
|
|
297
348
|
*
|
|
298
349
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
299
|
-
* @param {
|
|
350
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
300
351
|
*/
|
|
301
|
-
protected _balanceLR(A:
|
|
352
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void {
|
|
302
353
|
const parentOfA = A.parent;
|
|
303
354
|
const B = A.left;
|
|
304
355
|
let C = undefined;
|
|
@@ -347,9 +398,9 @@ export class AVLTree<
|
|
|
347
398
|
* Space Complexity: O(1)
|
|
348
399
|
*
|
|
349
400
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
350
|
-
* @param {
|
|
401
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
351
402
|
*/
|
|
352
|
-
protected _balanceRR(A:
|
|
403
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void {
|
|
353
404
|
const parentOfA = A.parent;
|
|
354
405
|
const B = A.right;
|
|
355
406
|
if (B !== null) A.parent = B;
|
|
@@ -385,9 +436,9 @@ export class AVLTree<
|
|
|
385
436
|
* Space Complexity: O(1)
|
|
386
437
|
*
|
|
387
438
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
388
|
-
* @param {
|
|
439
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
389
440
|
*/
|
|
390
|
-
protected _balanceRL(A:
|
|
441
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void {
|
|
391
442
|
const parentOfA = A.parent;
|
|
392
443
|
const B = A.right;
|
|
393
444
|
let C = undefined;
|
|
@@ -436,10 +487,10 @@ export class AVLTree<
|
|
|
436
487
|
*
|
|
437
488
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
438
489
|
* to restore balance in an AVL tree after inserting a node.
|
|
439
|
-
* @param {BTNRep<K, V,
|
|
440
|
-
* `BTNRep<K, V,
|
|
490
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
|
|
491
|
+
* `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
441
492
|
*/
|
|
442
|
-
protected _balancePath(node: BTNRep<K, V,
|
|
493
|
+
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void {
|
|
443
494
|
node = this.ensureNode(node);
|
|
444
495
|
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
445
496
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -487,14 +538,14 @@ export class AVLTree<
|
|
|
487
538
|
*
|
|
488
539
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
489
540
|
* same as the old node.
|
|
490
|
-
* @param {
|
|
541
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
491
542
|
* the data structure.
|
|
492
|
-
* @param {
|
|
543
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
493
544
|
* the data structure.
|
|
494
545
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
495
546
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
496
547
|
*/
|
|
497
|
-
protected override _replaceNode(oldNode:
|
|
548
|
+
protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> {
|
|
498
549
|
newNode.height = oldNode.height;
|
|
499
550
|
|
|
500
551
|
return super._replaceNode(oldNode, newNode);
|