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
|
@@ -7,50 +7,55 @@
|
|
|
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
|
-
BTNRep
|
|
13
|
+
BTNRep,
|
|
14
|
+
EntryCallback,
|
|
15
|
+
OptNodeOrNull
|
|
16
16
|
} from '../../types';
|
|
17
17
|
import { IBinaryTree } from '../../interfaces';
|
|
18
18
|
|
|
19
|
-
export class AVLTreeNode<
|
|
20
|
-
K = any,
|
|
21
|
-
V = any,
|
|
22
|
-
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
|
|
23
|
-
> extends BSTNode<K, V, NODE> {
|
|
19
|
+
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
24
20
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @param {V} [value] - The
|
|
30
|
-
*
|
|
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`.
|
|
31
28
|
*/
|
|
32
29
|
constructor(key: K, value?: V) {
|
|
33
30
|
super(key, value);
|
|
34
|
-
this._height = 0;
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
override parent?: AVLTreeNode<K, V> = undefined;
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
get height(): number {
|
|
44
|
-
return this._height;
|
|
35
|
+
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
36
|
+
|
|
37
|
+
override get left(): OptNodeOrNull<AVLTreeNode<K, V>> {
|
|
38
|
+
return this._left;
|
|
45
39
|
}
|
|
46
40
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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;
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
|
|
@@ -63,109 +68,114 @@ export class AVLTreeNode<
|
|
|
63
68
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
64
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.
|
|
65
70
|
*/
|
|
66
|
-
export class AVLTree<
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
R = object,
|
|
70
|
-
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
71
|
-
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
72
|
-
>
|
|
73
|
-
extends BST<K, V, R, NODE, TREE>
|
|
74
|
-
implements IBinaryTree<K, V, R, 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>
|
|
75
74
|
{
|
|
76
75
|
/**
|
|
77
|
-
* This
|
|
78
|
-
*
|
|
79
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
80
|
-
* iterable
|
|
81
|
-
*
|
|
82
|
-
* @param [options] - The `options` parameter
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
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
|
|
86
85
|
*/
|
|
87
|
-
constructor(
|
|
86
|
+
constructor(
|
|
87
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [],
|
|
88
|
+
options?: AVLTreeOptions<K, V, R>
|
|
89
|
+
) {
|
|
88
90
|
super([], options);
|
|
89
91
|
if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
/**
|
|
95
|
+
* Time Complexity: O(1)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
|
+
*
|
|
93
98
|
* The function creates a new AVL tree node with the given key and value.
|
|
94
99
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
95
100
|
* created.
|
|
96
101
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
97
102
|
* value associated with the key in the node being created.
|
|
98
103
|
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
99
|
-
* type
|
|
104
|
+
* type AVLTreeNode<K, V>.
|
|
100
105
|
*/
|
|
101
|
-
override createNode(key: K, value?: V):
|
|
102
|
-
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>;
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
/**
|
|
111
|
+
* Time Complexity: O(1)
|
|
112
|
+
* Space Complexity: O(1)
|
|
113
|
+
*
|
|
106
114
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
107
115
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
108
116
|
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
109
117
|
* being created.
|
|
110
118
|
* @returns a new AVLTree object.
|
|
111
119
|
*/
|
|
112
|
-
override createTree(options?: AVLTreeOptions<K, V, R>)
|
|
113
|
-
return new AVLTree<K, V, R,
|
|
120
|
+
override createTree(options?: AVLTreeOptions<K, V, R>) {
|
|
121
|
+
return new AVLTree<K, V, R, MK, MV, MR>([], {
|
|
114
122
|
iterationType: this.iterationType,
|
|
115
123
|
isMapMode: this._isMapMode,
|
|
116
|
-
|
|
124
|
+
specifyComparable: this._specifyComparable,
|
|
117
125
|
toEntryFn: this._toEntryFn,
|
|
118
126
|
isReverse: this._isReverse,
|
|
119
127
|
...options
|
|
120
|
-
})
|
|
128
|
+
});
|
|
121
129
|
}
|
|
122
130
|
|
|
123
131
|
/**
|
|
132
|
+
* Time Complexity: O(1)
|
|
133
|
+
* Space Complexity: O(1)
|
|
134
|
+
*
|
|
124
135
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
125
|
-
* @param {BTNRep<K, V,
|
|
126
|
-
* `
|
|
127
|
-
* @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
|
|
128
139
|
* an instance of the `AVLTreeNode` class.
|
|
129
140
|
*/
|
|
130
|
-
override isNode(
|
|
131
|
-
return
|
|
141
|
+
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
132
143
|
}
|
|
133
144
|
|
|
134
145
|
/**
|
|
135
146
|
* Time Complexity: O(log n)
|
|
136
|
-
* Space Complexity: O(
|
|
147
|
+
* Space Complexity: O(log n)
|
|
137
148
|
*
|
|
138
149
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
139
150
|
* structure, then balances the path.
|
|
140
|
-
* @param {BTNRep<K, V,
|
|
141
|
-
* `
|
|
142
|
-
* `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>>`
|
|
143
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
144
154
|
* the key or node being added to the data structure.
|
|
145
155
|
* @returns The method is returning a boolean value.
|
|
146
156
|
*/
|
|
147
|
-
override add(
|
|
148
|
-
if (
|
|
149
|
-
const inserted = super.add(
|
|
150
|
-
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);
|
|
151
161
|
return inserted;
|
|
152
162
|
}
|
|
153
163
|
|
|
154
164
|
/**
|
|
155
165
|
* Time Complexity: O(log n)
|
|
156
|
-
* Space Complexity: O(
|
|
166
|
+
* Space Complexity: O(log n)
|
|
157
167
|
*
|
|
158
168
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
159
169
|
* balances the tree if necessary.
|
|
160
|
-
* @param {BTNRep<K, V,
|
|
170
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
161
171
|
* parameter in the `override delete` method can be one of the following types:
|
|
162
172
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
163
173
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
164
174
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
165
175
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
166
176
|
*/
|
|
167
|
-
override delete(
|
|
168
|
-
const deletedResults = super.delete(
|
|
177
|
+
override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
169
179
|
for (const { needBalanced } of deletedResults) {
|
|
170
180
|
if (needBalanced) {
|
|
171
181
|
this._balancePath(needBalanced);
|
|
@@ -174,23 +184,70 @@ export class AVLTree<
|
|
|
174
184
|
return deletedResults;
|
|
175
185
|
}
|
|
176
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
|
+
*/
|
|
207
|
+
override map(
|
|
208
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
209
|
+
options?: AVLTreeOptions<MK, MV, MR>,
|
|
210
|
+
thisArg?: any
|
|
211
|
+
): AVLTree<MK, MV, MR> {
|
|
212
|
+
const newTree = new AVLTree<MK, MV, MR>([], options);
|
|
213
|
+
let index = 0;
|
|
214
|
+
for (const [key, value] of this) {
|
|
215
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
216
|
+
}
|
|
217
|
+
return newTree;
|
|
218
|
+
}
|
|
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
|
+
|
|
177
234
|
/**
|
|
178
235
|
* Time Complexity: O(1)
|
|
179
236
|
* Space Complexity: O(1)
|
|
180
237
|
*
|
|
181
238
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
182
239
|
* binary search tree.
|
|
183
|
-
* @param {
|
|
184
|
-
* object (`
|
|
185
|
-
* @param {
|
|
186
|
-
* `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>>`.
|
|
187
244
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
188
245
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
189
246
|
*/
|
|
190
247
|
protected override _swapProperties(
|
|
191
|
-
srcNode:
|
|
192
|
-
destNode:
|
|
193
|
-
):
|
|
248
|
+
srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>,
|
|
249
|
+
destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>
|
|
250
|
+
): AVLTreeNode<K, V> | undefined {
|
|
194
251
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
195
252
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
196
253
|
|
|
@@ -220,12 +277,12 @@ export class AVLTree<
|
|
|
220
277
|
* Space Complexity: O(1)
|
|
221
278
|
*
|
|
222
279
|
* The function calculates the balance factor of a node in a binary tree.
|
|
223
|
-
* @param {
|
|
280
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
224
281
|
* binary tree data structure.
|
|
225
282
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
226
283
|
* height of the left subtree from the height of the right subtree.
|
|
227
284
|
*/
|
|
228
|
-
protected _balanceFactor(node:
|
|
285
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number {
|
|
229
286
|
if (!node.right)
|
|
230
287
|
// node has no right subtree
|
|
231
288
|
return -node.height;
|
|
@@ -241,9 +298,9 @@ export class AVLTree<
|
|
|
241
298
|
*
|
|
242
299
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
243
300
|
* right children.
|
|
244
|
-
* @param {
|
|
301
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
245
302
|
*/
|
|
246
|
-
protected _updateHeight(node:
|
|
303
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void {
|
|
247
304
|
if (!node.left && !node.right) node.height = 0;
|
|
248
305
|
else if (!node.left) {
|
|
249
306
|
const rightHeight = node.right ? node.right.height : 0;
|
|
@@ -257,12 +314,12 @@ export class AVLTree<
|
|
|
257
314
|
* Space Complexity: O(1)
|
|
258
315
|
*
|
|
259
316
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
260
|
-
* @param {
|
|
317
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
261
318
|
*/
|
|
262
|
-
protected _balanceLL(A:
|
|
319
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void {
|
|
263
320
|
const parentOfA = A.parent;
|
|
264
321
|
const B = A.left;
|
|
265
|
-
A.parent = B;
|
|
322
|
+
if (B !== null) A.parent = B;
|
|
266
323
|
if (B && B.right) {
|
|
267
324
|
B.right.parent = A;
|
|
268
325
|
}
|
|
@@ -290,21 +347,21 @@ export class AVLTree<
|
|
|
290
347
|
* Space Complexity: O(1)
|
|
291
348
|
*
|
|
292
349
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
293
|
-
* @param {
|
|
350
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
294
351
|
*/
|
|
295
|
-
protected _balanceLR(A:
|
|
352
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void {
|
|
296
353
|
const parentOfA = A.parent;
|
|
297
354
|
const B = A.left;
|
|
298
355
|
let C = undefined;
|
|
299
356
|
if (B) {
|
|
300
357
|
C = B.right;
|
|
301
358
|
}
|
|
302
|
-
if (A) A.parent = C;
|
|
303
|
-
if (B) B.parent = C;
|
|
359
|
+
if (A && C !== null) A.parent = C;
|
|
360
|
+
if (B && C !== null) B.parent = C;
|
|
304
361
|
|
|
305
362
|
if (C) {
|
|
306
363
|
if (C.left) {
|
|
307
|
-
C.left.parent = B;
|
|
364
|
+
if (B !== null) C.left.parent = B;
|
|
308
365
|
}
|
|
309
366
|
if (C.right) {
|
|
310
367
|
C.right.parent = A;
|
|
@@ -341,12 +398,12 @@ export class AVLTree<
|
|
|
341
398
|
* Space Complexity: O(1)
|
|
342
399
|
*
|
|
343
400
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
344
|
-
* @param {
|
|
401
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
345
402
|
*/
|
|
346
|
-
protected _balanceRR(A:
|
|
403
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void {
|
|
347
404
|
const parentOfA = A.parent;
|
|
348
405
|
const B = A.right;
|
|
349
|
-
A.parent = B;
|
|
406
|
+
if (B !== null) A.parent = B;
|
|
350
407
|
if (B) {
|
|
351
408
|
if (B.left) {
|
|
352
409
|
B.left.parent = A;
|
|
@@ -379,9 +436,9 @@ export class AVLTree<
|
|
|
379
436
|
* Space Complexity: O(1)
|
|
380
437
|
*
|
|
381
438
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
382
|
-
* @param {
|
|
439
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
383
440
|
*/
|
|
384
|
-
protected _balanceRL(A:
|
|
441
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void {
|
|
385
442
|
const parentOfA = A.parent;
|
|
386
443
|
const B = A.right;
|
|
387
444
|
let C = undefined;
|
|
@@ -389,15 +446,15 @@ export class AVLTree<
|
|
|
389
446
|
C = B.left;
|
|
390
447
|
}
|
|
391
448
|
|
|
392
|
-
A.parent = C;
|
|
393
|
-
if (B) B.parent = C;
|
|
449
|
+
if (C !== null) A.parent = C;
|
|
450
|
+
if (B && C !== null) B.parent = C;
|
|
394
451
|
|
|
395
452
|
if (C) {
|
|
396
453
|
if (C.left) {
|
|
397
454
|
C.left.parent = A;
|
|
398
455
|
}
|
|
399
456
|
if (C.right) {
|
|
400
|
-
C.right.parent = B;
|
|
457
|
+
if (B !== null) C.right.parent = B;
|
|
401
458
|
}
|
|
402
459
|
C.parent = parentOfA;
|
|
403
460
|
}
|
|
@@ -430,10 +487,10 @@ export class AVLTree<
|
|
|
430
487
|
*
|
|
431
488
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
432
489
|
* to restore balance in an AVL tree after inserting a node.
|
|
433
|
-
* @param {BTNRep<K, V,
|
|
434
|
-
* `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>>`.
|
|
435
492
|
*/
|
|
436
|
-
protected _balancePath(node: BTNRep<K, V,
|
|
493
|
+
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void {
|
|
437
494
|
node = this.ensureNode(node);
|
|
438
495
|
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
439
496
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -481,14 +538,14 @@ export class AVLTree<
|
|
|
481
538
|
*
|
|
482
539
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
483
540
|
* same as the old node.
|
|
484
|
-
* @param {
|
|
541
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
485
542
|
* the data structure.
|
|
486
|
-
* @param {
|
|
543
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
487
544
|
* the data structure.
|
|
488
545
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
489
546
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
490
547
|
*/
|
|
491
|
-
protected override _replaceNode(oldNode:
|
|
548
|
+
protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> {
|
|
492
549
|
newNode.height = oldNode.height;
|
|
493
550
|
|
|
494
551
|
return super._replaceNode(oldNode, newNode);
|