stack-typed 2.0.5 → 2.1.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/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +602 -873
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
- package/src/data-structures/binary-tree/avl-tree.ts +237 -206
- package/src/data-structures/binary-tree/binary-tree.ts +665 -896
- package/src/data-structures/binary-tree/bst.ts +565 -572
- package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
- package/src/data-structures/binary-tree/tree-counter.ts +195 -219
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
|
@@ -6,36 +6,72 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
|
|
10
|
+
import { BSTOptions } from '../../types';
|
|
10
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
/**
|
|
13
|
+
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
14
|
+
* It extends a BSTNode and ensures the 'height' property is maintained.
|
|
15
|
+
*
|
|
16
|
+
* @template K - The type of the key.
|
|
17
|
+
* @template V - The type of the value.
|
|
18
|
+
*/
|
|
11
19
|
export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
12
20
|
parent?: AVLTreeNode<K, V>;
|
|
13
21
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param
|
|
19
|
-
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
20
|
-
* default to `undefined`.
|
|
22
|
+
* Creates an instance of AVLTreeNode.
|
|
23
|
+
* @remarks Time O(1), Space O(1)
|
|
24
|
+
*
|
|
25
|
+
* @param key - The key of the node.
|
|
26
|
+
* @param [value] - The value associated with the key.
|
|
21
27
|
*/
|
|
22
28
|
constructor(key: K, value?: V);
|
|
23
29
|
_left?: AVLTreeNode<K, V> | null | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the left child of the node.
|
|
32
|
+
* @remarks Time O(1), Space O(1)
|
|
33
|
+
*
|
|
34
|
+
* @returns The left child.
|
|
35
|
+
*/
|
|
24
36
|
get left(): AVLTreeNode<K, V> | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Sets the left child of the node and updates its parent reference.
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
40
|
+
*
|
|
41
|
+
* @param v - The node to set as the left child.
|
|
42
|
+
*/
|
|
25
43
|
set left(v: AVLTreeNode<K, V> | null | undefined);
|
|
26
44
|
_right?: AVLTreeNode<K, V> | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the right child of the node.
|
|
47
|
+
* @remarks Time O(1), Space O(1)
|
|
48
|
+
*
|
|
49
|
+
* @returns The right child.
|
|
50
|
+
*/
|
|
27
51
|
get right(): AVLTreeNode<K, V> | null | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the right child of the node and updates its parent reference.
|
|
54
|
+
* @remarks Time O(1), Space O(1)
|
|
55
|
+
*
|
|
56
|
+
* @param v - The node to set as the right child.
|
|
57
|
+
*/
|
|
28
58
|
set right(v: AVLTreeNode<K, V> | null | undefined);
|
|
29
59
|
}
|
|
30
60
|
/**
|
|
61
|
+
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
62
|
+
* This tree extends BST and performs rotations on add/delete to maintain balance.
|
|
63
|
+
*
|
|
64
|
+
* @template K - The type of the key.
|
|
65
|
+
* @template V - The type of the value.
|
|
66
|
+
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
67
|
+
*
|
|
31
68
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
32
69
|
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
33
70
|
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
34
71
|
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
35
72
|
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
36
73
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
37
|
-
* 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
|
|
38
|
-
* @example
|
|
74
|
+
* 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.@example
|
|
39
75
|
* // Find elements in a range
|
|
40
76
|
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
41
77
|
* type Datum = { timestamp: Date; temperature: number };
|
|
@@ -100,203 +136,156 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
100
136
|
* // { minute: 15, temperature: 58.6 }
|
|
101
137
|
* // ]
|
|
102
138
|
*/
|
|
103
|
-
export declare class AVLTree<K = any, V = any, R =
|
|
104
|
-
/**
|
|
105
|
-
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
106
|
-
* in an iterable format.
|
|
107
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
108
|
-
* iterable that can contain either `
|
|
109
|
-
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
|
|
110
|
-
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
111
|
-
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
112
|
-
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
113
|
-
* AVL tree. These options could include things like custom comparators, initial capacity, or any
|
|
114
|
-
* other configuration settings specific
|
|
115
|
-
*/
|
|
116
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
|
|
139
|
+
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
117
140
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
141
|
+
* Creates an instance of AVLTree.
|
|
142
|
+
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
120
143
|
*
|
|
121
|
-
*
|
|
122
|
-
* @param
|
|
123
|
-
* created.
|
|
124
|
-
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
125
|
-
* value associated with the key in the node being created.
|
|
126
|
-
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
127
|
-
* type AVLTreeNode<K, V>.
|
|
144
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
145
|
+
* @param [options] - Configuration options for the AVL tree.
|
|
128
146
|
*/
|
|
129
|
-
|
|
147
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
|
|
130
148
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
149
|
+
* (Protected) Creates a new AVL tree node.
|
|
150
|
+
* @remarks Time O(1), Space O(1)
|
|
133
151
|
*
|
|
134
|
-
*
|
|
135
|
-
* @param
|
|
136
|
-
*
|
|
137
|
-
* being created.
|
|
138
|
-
* @returns a new AVLTree object.
|
|
152
|
+
* @param key - The key for the new node.
|
|
153
|
+
* @param [value] - The value for the new node.
|
|
154
|
+
* @returns The newly created AVLTreeNode.
|
|
139
155
|
*/
|
|
140
|
-
|
|
156
|
+
_createNode(key: K, value?: V): AVLTreeNode<K, V>;
|
|
141
157
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
158
|
+
* Checks if the given item is an `AVLTreeNode` instance.
|
|
159
|
+
* @remarks Time O(1), Space O(1)
|
|
144
160
|
*
|
|
145
|
-
*
|
|
146
|
-
* @
|
|
147
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
148
|
-
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
149
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
150
|
-
* an instance of the `AVLTreeNode` class.
|
|
161
|
+
* @param keyNodeOrEntry - The item to check.
|
|
162
|
+
* @returns True if it's an AVLTreeNode, false otherwise.
|
|
151
163
|
*/
|
|
152
164
|
isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
|
|
153
165
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
166
|
+
* Adds a new node to the AVL tree and balances the tree path.
|
|
167
|
+
* @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
156
168
|
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* @
|
|
160
|
-
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
161
|
-
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
|
|
162
|
-
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
163
|
-
* the key or node being added to the data structure.
|
|
164
|
-
* @returns The method is returning a boolean value.
|
|
169
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
170
|
+
* @param [value] - The value, if providing just a key.
|
|
171
|
+
* @returns True if the addition was successful, false otherwise.
|
|
165
172
|
*/
|
|
166
173
|
add(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
167
174
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
175
|
+
* Deletes a node from the AVL tree and re-balances the tree.
|
|
176
|
+
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
170
177
|
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
174
|
-
* parameter in the `override delete` method can be one of the following types:
|
|
175
|
-
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
176
|
-
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
177
|
-
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
178
|
-
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
178
|
+
* @param keyNodeOrEntry - The node to delete.
|
|
179
|
+
* @returns An array containing deletion results.
|
|
179
180
|
*/
|
|
180
181
|
delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
|
|
181
182
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
183
|
+
* Rebuilds the tree to be perfectly balanced.
|
|
184
|
+
* @remarks AVL trees are already height-balanced, but this makes them *perfectly* balanced (minimal height and all leaves at N or N-1).
|
|
185
|
+
* Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
186
|
+
*
|
|
187
|
+
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
188
|
+
* @returns True if successful, false if the tree was empty.
|
|
189
|
+
*/
|
|
190
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
193
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `add` for each item into the new tree). Space O(N) for the new tree.
|
|
194
|
+
*
|
|
195
|
+
* @template MK - New key type.
|
|
196
|
+
* @template MV - New value type.
|
|
197
|
+
* @template MR - New raw type.
|
|
198
|
+
* @param callback - A function to map each [key, value] pair.
|
|
199
|
+
* @param [options] - Options for the new AVLTree.
|
|
200
|
+
* @param [thisArg] - `this` context for the callback.
|
|
201
|
+
* @returns A new, mapped AVLTree.
|
|
202
|
+
*/
|
|
203
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
204
|
+
/**
|
|
205
|
+
* (Protected) Creates a new, empty instance of the same AVLTree constructor.
|
|
206
|
+
* @remarks Time O(1)
|
|
184
207
|
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* @
|
|
188
|
-
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
189
|
-
* the AVLTree itself.
|
|
190
|
-
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
191
|
-
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
|
|
192
|
-
* options for the AVL tree being created during the mapping process. These options could include
|
|
193
|
-
* custom comparators, initial
|
|
194
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
195
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
196
|
-
* (value of `this`) within the callback function. This can be useful when you want to access
|
|
197
|
-
* properties or
|
|
198
|
-
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
|
|
199
|
-
* modified by the provided callback function.
|
|
208
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
209
|
+
* @param [options] - Options for the new tree.
|
|
210
|
+
* @returns A new, empty tree.
|
|
200
211
|
*/
|
|
201
|
-
|
|
212
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
202
213
|
/**
|
|
203
|
-
*
|
|
204
|
-
*
|
|
214
|
+
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
215
|
+
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
205
216
|
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* @
|
|
217
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
218
|
+
* @param [iter=[]] - An iterable to populate the new tree.
|
|
219
|
+
* @param [options] - Options for the new tree.
|
|
220
|
+
* @returns A new AVLTree.
|
|
209
221
|
*/
|
|
210
|
-
|
|
222
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
211
223
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
224
|
+
* (Protected) Swaps properties of two nodes, including height.
|
|
225
|
+
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
214
226
|
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* @
|
|
218
|
-
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
|
|
219
|
-
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
|
|
220
|
-
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
|
|
221
|
-
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
222
|
-
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
227
|
+
* @param srcNode - The source node.
|
|
228
|
+
* @param destNode - The destination node.
|
|
229
|
+
* @returns The `destNode` (now holding `srcNode`'s properties).
|
|
223
230
|
*/
|
|
224
231
|
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>): AVLTreeNode<K, V> | undefined;
|
|
225
232
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
233
|
+
* (Protected) Calculates the balance factor (height(right) - height(left)).
|
|
234
|
+
* @remarks Time O(1) (assumes heights are stored).
|
|
228
235
|
*
|
|
229
|
-
*
|
|
230
|
-
* @
|
|
231
|
-
* binary tree data structure.
|
|
232
|
-
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
233
|
-
* height of the left subtree from the height of the right subtree.
|
|
236
|
+
* @param node - The node to check.
|
|
237
|
+
* @returns The balance factor (positive if right-heavy, negative if left-heavy).
|
|
234
238
|
*/
|
|
235
239
|
protected _balanceFactor(node: AVLTreeNode<K, V>): number;
|
|
236
240
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
241
|
+
* (Protected) Recalculates and updates the height of a node based on its children's heights.
|
|
242
|
+
* @remarks Time O(1) (assumes children's heights are correct).
|
|
239
243
|
*
|
|
240
|
-
*
|
|
241
|
-
* right children.
|
|
242
|
-
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
244
|
+
* @param node - The node to update.
|
|
243
245
|
*/
|
|
244
246
|
protected _updateHeight(node: AVLTreeNode<K, V>): void;
|
|
245
247
|
/**
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
+
* (Protected) Performs a Left-Left (LL) rotation (a single right rotation).
|
|
249
|
+
* @remarks Time O(1), Space O(1)
|
|
248
250
|
*
|
|
249
|
-
*
|
|
250
|
-
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
251
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
251
252
|
*/
|
|
252
253
|
protected _balanceLL(A: AVLTreeNode<K, V>): void;
|
|
253
254
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
255
|
+
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
256
|
+
* @remarks Time O(1), Space O(1)
|
|
256
257
|
*
|
|
257
|
-
*
|
|
258
|
-
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
258
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
259
259
|
*/
|
|
260
260
|
protected _balanceLR(A: AVLTreeNode<K, V>): void;
|
|
261
261
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
262
|
+
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
263
|
+
* @remarks Time O(1), Space O(1)
|
|
264
264
|
*
|
|
265
|
-
*
|
|
266
|
-
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
265
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
267
266
|
*/
|
|
268
267
|
protected _balanceRR(A: AVLTreeNode<K, V>): void;
|
|
269
268
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
269
|
+
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
270
|
+
* @remarks Time O(1), Space O(1)
|
|
272
271
|
*
|
|
273
|
-
*
|
|
274
|
-
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
272
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
275
273
|
*/
|
|
276
274
|
protected _balanceRL(A: AVLTreeNode<K, V>): void;
|
|
277
275
|
/**
|
|
278
|
-
*
|
|
279
|
-
*
|
|
276
|
+
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
277
|
+
* @remarks Time O(log N) (O(H)), as it traverses the path to root. Space O(H) for the path array.
|
|
280
278
|
*
|
|
281
|
-
*
|
|
282
|
-
* to restore balance in an AVL tree after inserting a node.
|
|
283
|
-
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
|
|
284
|
-
* `
|
|
285
|
-
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
279
|
+
* @param node - The node to start balancing from (e.g., the newly inserted node or parent of the deleted node).
|
|
286
280
|
*/
|
|
287
281
|
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
|
|
288
282
|
/**
|
|
289
|
-
*
|
|
290
|
-
*
|
|
283
|
+
* (Protected) Replaces a node, ensuring height is copied.
|
|
284
|
+
* @remarks Time O(1)
|
|
291
285
|
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* @
|
|
295
|
-
* the data structure.
|
|
296
|
-
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
297
|
-
* the data structure.
|
|
298
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
299
|
-
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
286
|
+
* @param oldNode - The node to be replaced.
|
|
287
|
+
* @param newNode - The node to insert.
|
|
288
|
+
* @returns The `newNode`.
|
|
300
289
|
*/
|
|
301
290
|
protected _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V>;
|
|
302
291
|
}
|