red-black-tree-typed 1.53.6 → 1.54.1
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/README.md +52 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -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 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- 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 +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- 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} +181 -108
- 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 +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- 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 +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- 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-counter.js +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 +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- 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/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- 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 +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- 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 +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- 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 +123 -17
- package/src/index.ts +4 -3
- 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 +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- 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/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -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,108 +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,
|
|
126
|
+
isReverse: this._isReverse,
|
|
118
127
|
...options
|
|
119
|
-
})
|
|
128
|
+
});
|
|
120
129
|
}
|
|
121
130
|
|
|
122
131
|
/**
|
|
132
|
+
* Time Complexity: O(1)
|
|
133
|
+
* Space Complexity: O(1)
|
|
134
|
+
*
|
|
123
135
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
124
|
-
* @param {BTNRep<K, V,
|
|
125
|
-
* `
|
|
126
|
-
* @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
|
|
127
139
|
* an instance of the `AVLTreeNode` class.
|
|
128
140
|
*/
|
|
129
|
-
override isNode(
|
|
130
|
-
return
|
|
141
|
+
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
131
143
|
}
|
|
132
144
|
|
|
133
145
|
/**
|
|
134
146
|
* Time Complexity: O(log n)
|
|
135
|
-
* Space Complexity: O(
|
|
147
|
+
* Space Complexity: O(log n)
|
|
136
148
|
*
|
|
137
149
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
138
150
|
* structure, then balances the path.
|
|
139
|
-
* @param {BTNRep<K, V,
|
|
140
|
-
* `
|
|
141
|
-
* `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>>`
|
|
142
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
143
154
|
* the key or node being added to the data structure.
|
|
144
155
|
* @returns The method is returning a boolean value.
|
|
145
156
|
*/
|
|
146
|
-
override add(
|
|
147
|
-
if (
|
|
148
|
-
const inserted = super.add(
|
|
149
|
-
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);
|
|
150
161
|
return inserted;
|
|
151
162
|
}
|
|
152
163
|
|
|
153
164
|
/**
|
|
154
165
|
* Time Complexity: O(log n)
|
|
155
|
-
* Space Complexity: O(
|
|
166
|
+
* Space Complexity: O(log n)
|
|
156
167
|
*
|
|
157
168
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
158
169
|
* balances the tree if necessary.
|
|
159
|
-
* @param {BTNRep<K, V,
|
|
170
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
160
171
|
* parameter in the `override delete` method can be one of the following types:
|
|
161
172
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
162
173
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
163
174
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
164
175
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
165
176
|
*/
|
|
166
|
-
override delete(
|
|
167
|
-
const deletedResults = super.delete(
|
|
177
|
+
override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
168
179
|
for (const { needBalanced } of deletedResults) {
|
|
169
180
|
if (needBalanced) {
|
|
170
181
|
this._balancePath(needBalanced);
|
|
@@ -173,23 +184,70 @@ export class AVLTree<
|
|
|
173
184
|
return deletedResults;
|
|
174
185
|
}
|
|
175
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
|
+
|
|
176
234
|
/**
|
|
177
235
|
* Time Complexity: O(1)
|
|
178
236
|
* Space Complexity: O(1)
|
|
179
237
|
*
|
|
180
238
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
181
239
|
* binary search tree.
|
|
182
|
-
* @param {
|
|
183
|
-
* object (`
|
|
184
|
-
* @param {
|
|
185
|
-
* `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>>`.
|
|
186
244
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
187
245
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
188
246
|
*/
|
|
189
247
|
protected override _swapProperties(
|
|
190
|
-
srcNode:
|
|
191
|
-
destNode:
|
|
192
|
-
):
|
|
248
|
+
srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>,
|
|
249
|
+
destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>
|
|
250
|
+
): AVLTreeNode<K, V> | undefined {
|
|
193
251
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
194
252
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
195
253
|
|
|
@@ -219,12 +277,12 @@ export class AVLTree<
|
|
|
219
277
|
* Space Complexity: O(1)
|
|
220
278
|
*
|
|
221
279
|
* The function calculates the balance factor of a node in a binary tree.
|
|
222
|
-
* @param {
|
|
280
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
223
281
|
* binary tree data structure.
|
|
224
282
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
225
283
|
* height of the left subtree from the height of the right subtree.
|
|
226
284
|
*/
|
|
227
|
-
protected _balanceFactor(node:
|
|
285
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number {
|
|
228
286
|
if (!node.right)
|
|
229
287
|
// node has no right subtree
|
|
230
288
|
return -node.height;
|
|
@@ -240,9 +298,9 @@ export class AVLTree<
|
|
|
240
298
|
*
|
|
241
299
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
242
300
|
* right children.
|
|
243
|
-
* @param {
|
|
301
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
244
302
|
*/
|
|
245
|
-
protected _updateHeight(node:
|
|
303
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void {
|
|
246
304
|
if (!node.left && !node.right) node.height = 0;
|
|
247
305
|
else if (!node.left) {
|
|
248
306
|
const rightHeight = node.right ? node.right.height : 0;
|
|
@@ -256,12 +314,12 @@ export class AVLTree<
|
|
|
256
314
|
* Space Complexity: O(1)
|
|
257
315
|
*
|
|
258
316
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
259
|
-
* @param {
|
|
317
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
260
318
|
*/
|
|
261
|
-
protected _balanceLL(A:
|
|
319
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void {
|
|
262
320
|
const parentOfA = A.parent;
|
|
263
321
|
const B = A.left;
|
|
264
|
-
A.parent = B;
|
|
322
|
+
if (B !== null) A.parent = B;
|
|
265
323
|
if (B && B.right) {
|
|
266
324
|
B.right.parent = A;
|
|
267
325
|
}
|
|
@@ -289,21 +347,21 @@ export class AVLTree<
|
|
|
289
347
|
* Space Complexity: O(1)
|
|
290
348
|
*
|
|
291
349
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
292
|
-
* @param {
|
|
350
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
293
351
|
*/
|
|
294
|
-
protected _balanceLR(A:
|
|
352
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void {
|
|
295
353
|
const parentOfA = A.parent;
|
|
296
354
|
const B = A.left;
|
|
297
355
|
let C = undefined;
|
|
298
356
|
if (B) {
|
|
299
357
|
C = B.right;
|
|
300
358
|
}
|
|
301
|
-
if (A) A.parent = C;
|
|
302
|
-
if (B) B.parent = C;
|
|
359
|
+
if (A && C !== null) A.parent = C;
|
|
360
|
+
if (B && C !== null) B.parent = C;
|
|
303
361
|
|
|
304
362
|
if (C) {
|
|
305
363
|
if (C.left) {
|
|
306
|
-
C.left.parent = B;
|
|
364
|
+
if (B !== null) C.left.parent = B;
|
|
307
365
|
}
|
|
308
366
|
if (C.right) {
|
|
309
367
|
C.right.parent = A;
|
|
@@ -340,12 +398,12 @@ export class AVLTree<
|
|
|
340
398
|
* Space Complexity: O(1)
|
|
341
399
|
*
|
|
342
400
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
343
|
-
* @param {
|
|
401
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
344
402
|
*/
|
|
345
|
-
protected _balanceRR(A:
|
|
403
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void {
|
|
346
404
|
const parentOfA = A.parent;
|
|
347
405
|
const B = A.right;
|
|
348
|
-
A.parent = B;
|
|
406
|
+
if (B !== null) A.parent = B;
|
|
349
407
|
if (B) {
|
|
350
408
|
if (B.left) {
|
|
351
409
|
B.left.parent = A;
|
|
@@ -378,9 +436,9 @@ export class AVLTree<
|
|
|
378
436
|
* Space Complexity: O(1)
|
|
379
437
|
*
|
|
380
438
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
381
|
-
* @param {
|
|
439
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
382
440
|
*/
|
|
383
|
-
protected _balanceRL(A:
|
|
441
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void {
|
|
384
442
|
const parentOfA = A.parent;
|
|
385
443
|
const B = A.right;
|
|
386
444
|
let C = undefined;
|
|
@@ -388,15 +446,15 @@ export class AVLTree<
|
|
|
388
446
|
C = B.left;
|
|
389
447
|
}
|
|
390
448
|
|
|
391
|
-
A.parent = C;
|
|
392
|
-
if (B) B.parent = C;
|
|
449
|
+
if (C !== null) A.parent = C;
|
|
450
|
+
if (B && C !== null) B.parent = C;
|
|
393
451
|
|
|
394
452
|
if (C) {
|
|
395
453
|
if (C.left) {
|
|
396
454
|
C.left.parent = A;
|
|
397
455
|
}
|
|
398
456
|
if (C.right) {
|
|
399
|
-
C.right.parent = B;
|
|
457
|
+
if (B !== null) C.right.parent = B;
|
|
400
458
|
}
|
|
401
459
|
C.parent = parentOfA;
|
|
402
460
|
}
|
|
@@ -429,12 +487,12 @@ export class AVLTree<
|
|
|
429
487
|
*
|
|
430
488
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
431
489
|
* to restore balance in an AVL tree after inserting a node.
|
|
432
|
-
* @param {BTNRep<K, V,
|
|
433
|
-
* `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>>`.
|
|
434
492
|
*/
|
|
435
|
-
protected _balancePath(node: BTNRep<K, V,
|
|
493
|
+
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void {
|
|
436
494
|
node = this.ensureNode(node);
|
|
437
|
-
const path = this.getPathToRoot(node => node,
|
|
495
|
+
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
438
496
|
for (let i = 0; i < path.length; i++) {
|
|
439
497
|
// second O(log n)
|
|
440
498
|
const A = path[i];
|
|
@@ -480,14 +538,14 @@ export class AVLTree<
|
|
|
480
538
|
*
|
|
481
539
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
482
540
|
* same as the old node.
|
|
483
|
-
* @param {
|
|
541
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
484
542
|
* the data structure.
|
|
485
|
-
* @param {
|
|
543
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
486
544
|
* the data structure.
|
|
487
545
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
488
546
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
489
547
|
*/
|
|
490
|
-
protected override _replaceNode(oldNode:
|
|
548
|
+
protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> {
|
|
491
549
|
newNode.height = oldNode.height;
|
|
492
550
|
|
|
493
551
|
return super._replaceNode(oldNode, newNode);
|