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