avl-tree-typed 1.52.0 → 1.52.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +97 -97
- package/dist/data-structures/binary-tree/binary-tree.js +52 -52
- package/dist/data-structures/binary-tree/bst.d.ts +35 -35
- package/dist/data-structures/binary-tree/bst.js +17 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/queue/deque.d.ts +7 -0
- package/dist/data-structures/queue/deque.js +16 -1
- package/dist/data-structures/queue/queue.d.ts +18 -1
- package/dist/data-structures/queue/queue.js +32 -7
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -1
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -22
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -24
- package/src/data-structures/binary-tree/avl-tree.ts +20 -19
- package/src/data-structures/binary-tree/binary-tree.ts +162 -157
- package/src/data-structures/binary-tree/bst.ts +54 -50
- package/src/data-structures/binary-tree/rb-tree.ts +18 -17
- package/src/data-structures/binary-tree/tree-multi-map.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +18 -4
- package/src/data-structures/queue/queue.ts +38 -8
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
- package/src/types/data-structures/binary-tree/bst.ts +7 -0
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +4 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "avl-tree-typed",
|
|
3
|
-
"version": "1.52.
|
|
3
|
+
"version": "1.52.2",
|
|
4
4
|
"description": "AVLTree(Adelson-Velsky and Landis Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -163,6 +163,6 @@
|
|
|
163
163
|
"typescript": "^4.9.5"
|
|
164
164
|
},
|
|
165
165
|
"dependencies": {
|
|
166
|
-
"data-structure-typed": "^1.52.
|
|
166
|
+
"data-structure-typed": "^1.52.2"
|
|
167
167
|
}
|
|
168
168
|
}
|
|
@@ -42,7 +42,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
42
42
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
43
43
|
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
44
44
|
*/
|
|
45
|
-
*
|
|
45
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<E> {
|
|
46
46
|
yield* this._getIterator(...args);
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -56,7 +56,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
56
56
|
*
|
|
57
57
|
* The function returns an iterator that yields all the values in the object.
|
|
58
58
|
*/
|
|
59
|
-
*
|
|
59
|
+
*values(): IterableIterator<E> {
|
|
60
60
|
for (const item of this) {
|
|
61
61
|
yield item;
|
|
62
62
|
}
|
|
@@ -35,7 +35,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
35
35
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
36
36
|
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
37
37
|
*/
|
|
38
|
-
*
|
|
38
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
39
39
|
yield* this._getIterator(...args);
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -50,7 +50,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
50
50
|
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
51
51
|
* be undefined.
|
|
52
52
|
*/
|
|
53
|
-
*
|
|
53
|
+
*entries(): IterableIterator<[K, V | undefined]> {
|
|
54
54
|
for (const item of this) {
|
|
55
55
|
yield item;
|
|
56
56
|
}
|
|
@@ -66,7 +66,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
66
66
|
*
|
|
67
67
|
* The function returns an iterator that yields the keys of a data structure.
|
|
68
68
|
*/
|
|
69
|
-
*
|
|
69
|
+
*keys(): IterableIterator<K> {
|
|
70
70
|
for (const item of this) {
|
|
71
71
|
yield item[0];
|
|
72
72
|
}
|
|
@@ -82,7 +82,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
82
82
|
*
|
|
83
83
|
* The function returns an iterator that yields the values of a collection.
|
|
84
84
|
*/
|
|
85
|
-
*
|
|
85
|
+
*values(): IterableIterator<V> {
|
|
86
86
|
for (const item of this) {
|
|
87
87
|
yield item[1];
|
|
88
88
|
}
|
|
@@ -12,8 +12,8 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
14
|
BTNCallback,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
BTNKeyOrNodeOrEntry,
|
|
16
|
+
IterationType
|
|
17
17
|
} from '../../types';
|
|
18
18
|
import { BTNEntry } from '../../types';
|
|
19
19
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -63,20 +63,21 @@ export class AVLTreeMultiMapNode<
|
|
|
63
63
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
64
64
|
*/
|
|
65
65
|
export class AVLTreeMultiMap<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
K = any,
|
|
67
|
+
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
69
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
70
|
+
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
71
|
+
K,
|
|
72
|
+
V,
|
|
73
|
+
R,
|
|
74
|
+
NODE,
|
|
75
|
+
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
76
|
+
>
|
|
76
77
|
>
|
|
77
|
-
>
|
|
78
78
|
extends AVLTree<K, V, R, NODE, TREE>
|
|
79
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
79
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
80
|
+
{
|
|
80
81
|
/**
|
|
81
82
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
82
83
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -86,7 +87,7 @@ export class AVLTreeMultiMap<
|
|
|
86
87
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
87
88
|
*/
|
|
88
89
|
constructor(
|
|
89
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
90
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
90
91
|
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
91
92
|
) {
|
|
92
93
|
super([], options);
|
|
@@ -155,13 +156,13 @@ export class AVLTreeMultiMap<
|
|
|
155
156
|
|
|
156
157
|
/**
|
|
157
158
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
158
|
-
* @param {R |
|
|
159
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
159
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
160
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
160
161
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
161
162
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
162
163
|
*/
|
|
163
164
|
override isNode(
|
|
164
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
165
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
165
166
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
166
167
|
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
|
|
167
168
|
}
|
|
@@ -169,8 +170,8 @@ export class AVLTreeMultiMap<
|
|
|
169
170
|
/**
|
|
170
171
|
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
171
172
|
* a node object.
|
|
172
|
-
* @param {R |
|
|
173
|
-
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `
|
|
173
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
174
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
174
175
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
175
176
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
176
177
|
* value is provided, it will default to `undefined`.
|
|
@@ -179,7 +180,7 @@ export class AVLTreeMultiMap<
|
|
|
179
180
|
* @returns either a NODE object or undefined.
|
|
180
181
|
*/
|
|
181
182
|
override keyValueOrEntryOrRawElementToNode(
|
|
182
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
183
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
183
184
|
value?: V,
|
|
184
185
|
count = 1
|
|
185
186
|
): NODE | undefined {
|
|
@@ -213,9 +214,9 @@ export class AVLTreeMultiMap<
|
|
|
213
214
|
*
|
|
214
215
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
215
216
|
* and update the count.
|
|
216
|
-
* @param {R |
|
|
217
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
217
218
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
|
|
218
|
-
* can also accept a value of type `
|
|
219
|
+
* can also accept a value of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
219
220
|
* entry, or raw element
|
|
220
221
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
221
222
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -224,7 +225,7 @@ export class AVLTreeMultiMap<
|
|
|
224
225
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
225
226
|
* @returns a boolean value.
|
|
226
227
|
*/
|
|
227
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
228
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
228
229
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
229
230
|
if (newNode === undefined) return false;
|
|
230
231
|
|
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
15
|
BTNCallback,
|
|
16
|
-
|
|
16
|
+
BTNKeyOrNodeOrEntry
|
|
17
17
|
} from '../../types';
|
|
18
18
|
import { BTNEntry } from '../../types';
|
|
19
19
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -66,14 +66,15 @@ 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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
>
|
|
69
|
+
K = any,
|
|
70
|
+
V = any,
|
|
71
|
+
R = BTNEntry<K, V>,
|
|
72
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
73
|
+
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
74
|
+
>
|
|
75
75
|
extends BST<K, V, R, NODE, TREE>
|
|
76
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
76
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
77
|
+
{
|
|
77
78
|
/**
|
|
78
79
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
79
80
|
* entries, or raw elements.
|
|
@@ -86,7 +87,7 @@ export class AVLTree<
|
|
|
86
87
|
* `nodeBuilder` (
|
|
87
88
|
*/
|
|
88
89
|
constructor(
|
|
89
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
90
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
90
91
|
options?: AVLTreeOptions<K, V, R>
|
|
91
92
|
) {
|
|
92
93
|
super([], options);
|
|
@@ -123,13 +124,13 @@ export class AVLTree<
|
|
|
123
124
|
|
|
124
125
|
/**
|
|
125
126
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
126
|
-
* @param {R |
|
|
127
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
127
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
128
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
128
129
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
129
130
|
* an instance of the `AVLTreeNode` class.
|
|
130
131
|
*/
|
|
131
132
|
override isNode(
|
|
132
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
133
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
133
134
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
134
135
|
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
135
136
|
}
|
|
@@ -146,14 +147,14 @@ export class AVLTree<
|
|
|
146
147
|
*
|
|
147
148
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
148
149
|
* structure, then balances the path.
|
|
149
|
-
* @param {R |
|
|
150
|
-
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `
|
|
150
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
151
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
151
152
|
* `RawElement`.
|
|
152
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
153
154
|
* the key or node being added to the data structure.
|
|
154
155
|
* @returns The method is returning a boolean value.
|
|
155
156
|
*/
|
|
156
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
157
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
157
158
|
if (keyOrNodeOrEntryOrRawElement === null) return false;
|
|
158
159
|
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
159
160
|
if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
@@ -488,10 +489,10 @@ export class AVLTree<
|
|
|
488
489
|
*
|
|
489
490
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
490
491
|
* to restore balance in an AVL tree after inserting a node.
|
|
491
|
-
* @param {R |
|
|
492
|
-
* `
|
|
492
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
493
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
493
494
|
*/
|
|
494
|
-
protected _balancePath(node: R |
|
|
495
|
+
protected _balancePath(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): void {
|
|
495
496
|
node = this.ensureNode(node);
|
|
496
497
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
497
498
|
for (let i = 0; i < path.length; i++) {
|
|
@@ -503,7 +504,7 @@ export class AVLTree<
|
|
|
503
504
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
504
505
|
switch (
|
|
505
506
|
this._balanceFactor(A) // second O(1)
|
|
506
|
-
|
|
507
|
+
) {
|
|
507
508
|
case -2:
|
|
508
509
|
if (A && A.left) {
|
|
509
510
|
if (this._balanceFactor(A.left) <= 0) {
|