linked-list-typed 1.51.7 → 1.51.9
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/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -13,12 +13,14 @@ import type {
|
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
15
|
BTNCallback,
|
|
16
|
+
Comparable,
|
|
16
17
|
KeyOrNodeOrEntry
|
|
17
18
|
} from '../../types';
|
|
19
|
+
import { BTNEntry } from '../../types';
|
|
18
20
|
import { IBinaryTree } from '../../interfaces';
|
|
19
21
|
|
|
20
22
|
export class AVLTreeNode<
|
|
21
|
-
K
|
|
23
|
+
K extends Comparable,
|
|
22
24
|
V = any,
|
|
23
25
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
|
|
24
26
|
> extends BSTNode<K, V, NODE> {
|
|
@@ -65,35 +67,41 @@ export class AVLTreeNode<
|
|
|
65
67
|
* 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.
|
|
66
68
|
*/
|
|
67
69
|
export class AVLTree<
|
|
68
|
-
K
|
|
70
|
+
K extends Comparable,
|
|
69
71
|
V = any,
|
|
72
|
+
R = BTNEntry<K, V>,
|
|
70
73
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
71
|
-
TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>
|
|
74
|
+
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
72
75
|
>
|
|
73
|
-
extends BST<K, V, NODE, TREE>
|
|
74
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
76
|
+
extends BST<K, V, R, NODE, TREE>
|
|
77
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
75
78
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
79
|
+
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
80
|
+
* entries, or raw elements.
|
|
81
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
82
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
83
|
+
* be used to initialize the AVLTree.
|
|
84
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
85
|
+
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
|
|
86
|
+
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
87
|
+
* `nodeBuilder` (
|
|
83
88
|
*/
|
|
84
|
-
constructor(
|
|
89
|
+
constructor(
|
|
90
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
91
|
+
options?: AVLTreeOptions<K, V, R>
|
|
92
|
+
) {
|
|
85
93
|
super([], options);
|
|
86
|
-
if (
|
|
94
|
+
if (keysOrNodesOrEntriesOrRawElements) super.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
87
95
|
}
|
|
88
96
|
|
|
89
97
|
/**
|
|
90
|
-
* The function creates a new AVL tree node with the
|
|
91
|
-
* @param {K} key - The key parameter is the key
|
|
92
|
-
*
|
|
93
|
-
* @param [value] - The parameter
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
98
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
99
|
+
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
100
|
+
* created.
|
|
101
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
102
|
+
* value associated with the key in the node being created.
|
|
103
|
+
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
104
|
+
* type NODE.
|
|
97
105
|
*/
|
|
98
106
|
override createNode(key: K, value?: V): NODE {
|
|
99
107
|
return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
|
|
@@ -106,21 +114,25 @@ export class AVLTree<
|
|
|
106
114
|
* being created.
|
|
107
115
|
* @returns a new AVLTree object.
|
|
108
116
|
*/
|
|
109
|
-
override createTree(options?: AVLTreeOptions<K>): TREE {
|
|
110
|
-
return new AVLTree<K, V, NODE, TREE>([], {
|
|
117
|
+
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
118
|
+
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
111
119
|
iterationType: this.iterationType,
|
|
112
|
-
|
|
120
|
+
comparator: this.comparator,
|
|
113
121
|
...options
|
|
114
122
|
}) as TREE;
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
/**
|
|
118
|
-
* The function checks if
|
|
119
|
-
* @param
|
|
120
|
-
*
|
|
126
|
+
* The function checks if the input is an instance of AVLTreeNode.
|
|
127
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
128
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
129
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
130
|
+
* an instance of the `AVLTreeNode` class.
|
|
121
131
|
*/
|
|
122
|
-
override isNode(
|
|
123
|
-
|
|
132
|
+
override isNode(
|
|
133
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
134
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
135
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
124
136
|
}
|
|
125
137
|
|
|
126
138
|
/**
|
|
@@ -133,18 +145,19 @@ export class AVLTree<
|
|
|
133
145
|
* Time Complexity: O(log n)
|
|
134
146
|
* Space Complexity: O(1)
|
|
135
147
|
*
|
|
136
|
-
* The function overrides the add method of a
|
|
137
|
-
*
|
|
138
|
-
* @param
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
148
|
+
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
149
|
+
* structure, then balances the path.
|
|
150
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
151
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
152
|
+
* `RawElement`.
|
|
153
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
154
|
+
* the key or node being added to the data structure.
|
|
155
|
+
* @returns The method is returning a boolean value.
|
|
143
156
|
*/
|
|
144
|
-
override add(
|
|
145
|
-
if (
|
|
146
|
-
const inserted = super.add(
|
|
147
|
-
if (inserted) this._balancePath(
|
|
157
|
+
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
158
|
+
if (keyOrNodeOrEntryOrRawElement === null) return false;
|
|
159
|
+
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
160
|
+
if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
148
161
|
return inserted;
|
|
149
162
|
}
|
|
150
163
|
|
|
@@ -157,16 +170,14 @@ export class AVLTree<
|
|
|
157
170
|
* Time Complexity: O(log n)
|
|
158
171
|
* Space Complexity: O(1)
|
|
159
172
|
*
|
|
160
|
-
* The function overrides the delete method of a binary tree
|
|
161
|
-
*
|
|
173
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
174
|
+
* to balance the tree after deletion.
|
|
162
175
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
163
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
164
|
-
*
|
|
165
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* parameter of type `NODE
|
|
169
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
176
|
+
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
|
|
177
|
+
* binary tree's node type.
|
|
178
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
179
|
+
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
|
|
180
|
+
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
|
|
170
181
|
*/
|
|
171
182
|
override delete<C extends BTNCallback<NODE>>(
|
|
172
183
|
identifier: ReturnType<C>,
|
|
@@ -182,39 +193,47 @@ export class AVLTree<
|
|
|
182
193
|
}
|
|
183
194
|
|
|
184
195
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
196
|
+
* Time Complexity: O(1)
|
|
197
|
+
* Space Complexity: O(1)
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Time Complexity: O(1)
|
|
202
|
+
* Space Complexity: O(1)
|
|
203
|
+
*
|
|
204
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
205
|
+
* binary search tree.
|
|
206
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
207
|
+
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
208
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
209
|
+
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
|
|
210
|
+
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
211
|
+
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
193
212
|
*/
|
|
194
213
|
protected override _swapProperties(
|
|
195
|
-
srcNode: BSTNKeyOrNode<K, NODE>,
|
|
196
|
-
destNode: BSTNKeyOrNode<K, NODE>
|
|
214
|
+
srcNode: R | BSTNKeyOrNode<K, NODE>,
|
|
215
|
+
destNode: R | BSTNKeyOrNode<K, NODE>
|
|
197
216
|
): NODE | undefined {
|
|
198
|
-
|
|
199
|
-
|
|
217
|
+
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
218
|
+
const destNodeEnsured = this.ensureNode(destNode);
|
|
200
219
|
|
|
201
|
-
if (
|
|
202
|
-
const { key, value, height } =
|
|
220
|
+
if (srcNodeEnsured && destNodeEnsured) {
|
|
221
|
+
const { key, value, height } = destNodeEnsured;
|
|
203
222
|
const tempNode = this.createNode(key, value);
|
|
204
223
|
|
|
205
224
|
if (tempNode) {
|
|
206
225
|
tempNode.height = height;
|
|
207
226
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
227
|
+
destNodeEnsured.key = srcNodeEnsured.key;
|
|
228
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
229
|
+
destNodeEnsured.height = srcNodeEnsured.height;
|
|
211
230
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
231
|
+
srcNodeEnsured.key = tempNode.key;
|
|
232
|
+
srcNodeEnsured.value = tempNode.value;
|
|
233
|
+
srcNodeEnsured.height = tempNode.height;
|
|
215
234
|
}
|
|
216
235
|
|
|
217
|
-
return
|
|
236
|
+
return destNodeEnsured;
|
|
218
237
|
}
|
|
219
238
|
return undefined;
|
|
220
239
|
}
|
|
@@ -229,7 +248,8 @@ export class AVLTree<
|
|
|
229
248
|
* Space Complexity: O(1)
|
|
230
249
|
*
|
|
231
250
|
* The function calculates the balance factor of a node in a binary tree.
|
|
232
|
-
* @param {NODE} node - The parameter "node" represents a node in a
|
|
251
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
|
|
252
|
+
* binary tree data structure.
|
|
233
253
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
234
254
|
* height of the left subtree from the height of the right subtree.
|
|
235
255
|
*/
|
|
@@ -274,7 +294,7 @@ export class AVLTree<
|
|
|
274
294
|
* Time Complexity: O(1)
|
|
275
295
|
* Space Complexity: O(1)
|
|
276
296
|
*
|
|
277
|
-
* The
|
|
297
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
278
298
|
* @param {NODE} A - A is a node in a binary tree.
|
|
279
299
|
*/
|
|
280
300
|
protected _balanceLL(A: NODE): void {
|
|
@@ -469,10 +489,10 @@ export class AVLTree<
|
|
|
469
489
|
*
|
|
470
490
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
471
491
|
* to restore balance in an AVL tree after inserting a node.
|
|
472
|
-
* @param {NODE} node - The `node` parameter
|
|
473
|
-
*
|
|
492
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
493
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
474
494
|
*/
|
|
475
|
-
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void {
|
|
495
|
+
protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void {
|
|
476
496
|
node = this.ensureNode(node);
|
|
477
497
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
478
498
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -513,13 +533,22 @@ export class AVLTree<
|
|
|
513
533
|
}
|
|
514
534
|
|
|
515
535
|
/**
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
|
|
536
|
+
* Time Complexity: O(1)
|
|
537
|
+
* Space Complexity: O(1)
|
|
538
|
+
*/
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Time Complexity: O(1)
|
|
542
|
+
* Space Complexity: O(1)
|
|
543
|
+
*
|
|
544
|
+
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
545
|
+
* same as the old node.
|
|
546
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
547
|
+
* the data structure.
|
|
519
548
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
520
549
|
* the data structure.
|
|
521
|
-
* @returns the result of calling the `_replaceNode` method
|
|
522
|
-
* `oldNode` and `newNode` as arguments.
|
|
550
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
551
|
+
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
523
552
|
*/
|
|
524
553
|
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
525
554
|
newNode.height = oldNode.height;
|