directed-graph-typed 1.54.2 → 2.0.0
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 +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
- package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
- package/dist/data-structures/binary-tree/avl-tree.js +76 -8
- package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/data-structures/binary-tree/binary-tree.js +244 -149
- package/dist/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/data-structures/binary-tree/bst.js +89 -133
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
- package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
- package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +84 -40
- package/dist/data-structures/queue/queue.js +134 -50
- package/dist/data-structures/stack/stack.d.ts +3 -11
- package/dist/data-structures/stack/stack.js +0 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/utils/utils.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
- package/src/data-structures/binary-tree/avl-tree.ts +99 -29
- package/src/data-structures/binary-tree/binary-tree.ts +474 -257
- package/src/data-structures/binary-tree/bst.ts +150 -152
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/src/utils/utils.ts +2 -2
|
@@ -9,15 +9,15 @@ import type {
|
|
|
9
9
|
AVLTreeCounterOptions,
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNOptKeyOrNode,
|
|
12
|
-
BTNRep,
|
|
13
12
|
EntryCallback,
|
|
14
|
-
IterationType
|
|
15
|
-
OptNodeOrNull
|
|
13
|
+
IterationType
|
|
16
14
|
} from '../../types';
|
|
17
15
|
import { IBinaryTree } from '../../interfaces';
|
|
18
16
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
19
17
|
|
|
20
18
|
export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
19
|
+
override parent?: AVLTreeCounterNode<K, V> = undefined;
|
|
20
|
+
|
|
21
21
|
/**
|
|
22
22
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
23
23
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -33,28 +33,26 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
33
33
|
this.count = count;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
override
|
|
37
|
-
|
|
38
|
-
override _left?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
|
|
36
|
+
override _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
39
37
|
|
|
40
|
-
override get left():
|
|
38
|
+
override get left(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
41
39
|
return this._left;
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
override set left(v:
|
|
42
|
+
override set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
45
43
|
if (v) {
|
|
46
44
|
v.parent = this;
|
|
47
45
|
}
|
|
48
46
|
this._left = v;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
override _right?:
|
|
49
|
+
override _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
52
50
|
|
|
53
|
-
override get right():
|
|
51
|
+
override get right(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
54
52
|
return this._right;
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
override set right(v:
|
|
55
|
+
override set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
58
56
|
if (v) {
|
|
59
57
|
v.parent = this;
|
|
60
58
|
}
|
|
@@ -78,7 +76,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
78
76
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
79
77
|
*/
|
|
80
78
|
constructor(
|
|
81
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
79
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
80
|
+
K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
81
|
+
> = [],
|
|
82
82
|
options?: AVLTreeCounterOptions<K, V, R>
|
|
83
83
|
) {
|
|
84
84
|
super([], options);
|
|
@@ -145,12 +145,14 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
147
|
* The function checks if the input is an instance of AVLTreeCounterNode.
|
|
148
|
-
* @param {
|
|
149
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
148
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
149
|
+
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
150
150
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
151
151
|
* an instance of the `AVLTreeCounterNode` class.
|
|
152
152
|
*/
|
|
153
|
-
override isNode(
|
|
153
|
+
override isNode(
|
|
154
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
155
|
+
): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
|
|
154
156
|
return keyNodeOrEntry instanceof AVLTreeCounterNode;
|
|
155
157
|
}
|
|
156
158
|
|
|
@@ -160,9 +162,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
160
162
|
*
|
|
161
163
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
162
164
|
* and update the count.
|
|
163
|
-
* @param {
|
|
165
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
|
|
164
166
|
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
|
|
165
|
-
* can also accept a value of type `
|
|
167
|
+
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
|
|
166
168
|
* entry, or raw element
|
|
167
169
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
168
170
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -171,7 +173,11 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
171
173
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
172
174
|
* @returns a boolean value.
|
|
173
175
|
*/
|
|
174
|
-
override add(
|
|
176
|
+
override add(
|
|
177
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
178
|
+
value?: V,
|
|
179
|
+
count = 1
|
|
180
|
+
): boolean {
|
|
175
181
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
176
182
|
if (newNode === undefined) return false;
|
|
177
183
|
|
|
@@ -189,7 +195,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
189
195
|
*
|
|
190
196
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
191
197
|
* nodes and maintaining balance in the tree.
|
|
192
|
-
* @param {
|
|
198
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
|
|
193
199
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
194
200
|
* binary tree. It can be a key, node, or entry that determines which
|
|
195
201
|
* node(s) should be deleted.
|
|
@@ -203,7 +209,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
203
209
|
* deleted node and whether balancing is needed in the tree.
|
|
204
210
|
*/
|
|
205
211
|
override delete(
|
|
206
|
-
keyNodeOrEntry:
|
|
212
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
207
213
|
ignoreCount = false
|
|
208
214
|
): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] {
|
|
209
215
|
const deletedResult: BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] = [];
|
|
@@ -276,6 +282,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
276
282
|
/**
|
|
277
283
|
* Time Complexity: O(n log n)
|
|
278
284
|
* Space Complexity: O(log n)
|
|
285
|
+
*
|
|
279
286
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
280
287
|
* tree using either a recursive or iterative approach.
|
|
281
288
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
@@ -374,8 +381,8 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
374
381
|
/**
|
|
375
382
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
376
383
|
* a node object.
|
|
377
|
-
* @param {
|
|
378
|
-
* `keyNodeOrEntry` parameter can be of type `R` or `
|
|
384
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
|
|
385
|
+
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
379
386
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
380
387
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
381
388
|
* value is provided, it will default to `undefined`.
|
|
@@ -384,7 +391,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
384
391
|
* @returns either a AVLTreeCounterNode<K, V> object or undefined.
|
|
385
392
|
*/
|
|
386
393
|
protected override _keyValueNodeOrEntryToNodeAndValue(
|
|
387
|
-
keyNodeOrEntry:
|
|
394
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
388
395
|
value?: V,
|
|
389
396
|
count = 1
|
|
390
397
|
): [AVLTreeCounterNode<K, V> | undefined, V | undefined] {
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull
|
|
8
|
+
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
13
|
+
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
17
|
* type V.
|
|
@@ -23,28 +25,26 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
|
23
25
|
super(key, value);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
override
|
|
28
|
+
override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
27
29
|
|
|
28
|
-
override
|
|
29
|
-
|
|
30
|
-
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
|
|
30
|
+
override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
31
31
|
return this._left;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override set left(v:
|
|
34
|
+
override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
35
35
|
if (v) {
|
|
36
36
|
v.parent = this;
|
|
37
37
|
}
|
|
38
38
|
this._left = v;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
override _right?:
|
|
41
|
+
override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
42
42
|
|
|
43
|
-
override get right():
|
|
43
|
+
override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
44
44
|
return this._right;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
override set right(v:
|
|
47
|
+
override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
48
48
|
if (v) {
|
|
49
49
|
v.parent = this;
|
|
50
50
|
}
|
|
@@ -71,7 +71,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
71
71
|
* additional options for configuring the AVLTreeMultiMap instance.
|
|
72
72
|
*/
|
|
73
73
|
constructor(
|
|
74
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
74
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
75
|
+
K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
|
|
76
|
+
> = [],
|
|
75
77
|
options?: AVLTreeMultiMapOptions<K, V[], R>
|
|
76
78
|
) {
|
|
77
79
|
super([], { ...options, isMapMode: true });
|
|
@@ -98,6 +100,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
98
100
|
specifyComparable: this._specifyComparable,
|
|
99
101
|
toEntryFn: this._toEntryFn,
|
|
100
102
|
isReverse: this._isReverse,
|
|
103
|
+
isMapMode: this._isMapMode,
|
|
101
104
|
...options
|
|
102
105
|
});
|
|
103
106
|
}
|
|
@@ -106,18 +109,24 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
106
109
|
* Time Complexity: O(1)
|
|
107
110
|
* Space Complexity: O(1)
|
|
108
111
|
*
|
|
109
|
-
* The
|
|
110
|
-
* specified key and
|
|
111
|
-
* @param {K} key - The `key` parameter
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
114
|
-
*
|
|
112
|
+
* The `createNode` function in TypeScript overrides the default implementation to create a new
|
|
113
|
+
* AVLTreeMultiMapNode with a specified key and value array.
|
|
114
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
115
|
+
* AVLTreeMultiMap.
|
|
116
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
117
|
+
* values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
|
|
118
|
+
* calling the method, an empty array `[]` is used as the default value.
|
|
119
|
+
* @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
|
|
120
|
+
* AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
|
|
121
|
+
* array is used.
|
|
115
122
|
*/
|
|
116
|
-
override createNode(key: K): AVLTreeMultiMapNode<K, V> {
|
|
117
|
-
return new AVLTreeMultiMapNode<K, V>(key, []);
|
|
123
|
+
override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
|
|
124
|
+
return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
|
|
118
125
|
}
|
|
119
126
|
|
|
120
|
-
override add(
|
|
127
|
+
override add(
|
|
128
|
+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
129
|
+
): boolean;
|
|
121
130
|
|
|
122
131
|
override add(key: K, value: V): boolean;
|
|
123
132
|
|
|
@@ -125,45 +134,58 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
125
134
|
* Time Complexity: O(log n)
|
|
126
135
|
* Space Complexity: O(log n)
|
|
127
136
|
*
|
|
128
|
-
* The function `add` in TypeScript overrides the superclass method to add key-value pairs
|
|
129
|
-
*
|
|
130
|
-
* @param
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* `values`
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* addition operation was successful or not.
|
|
137
|
+
* The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
|
|
138
|
+
* to an AVLTreeMultiMap, handling different input types and scenarios.
|
|
139
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
140
|
+
* be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
|
|
141
|
+
* can be a single key value, a node of the AVLTree
|
|
142
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
143
|
+
* that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
|
|
144
|
+
* specific key.
|
|
145
|
+
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
146
|
+
* successful or not.
|
|
139
147
|
*/
|
|
140
|
-
override add(
|
|
148
|
+
override add(
|
|
149
|
+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
|
|
150
|
+
value?: V
|
|
151
|
+
): boolean {
|
|
141
152
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
142
153
|
|
|
143
154
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
144
155
|
if (key === undefined || key === null) return false;
|
|
145
156
|
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const existingNode = this.getNode(key);
|
|
153
|
-
if (this.isRealNode(existingNode)) {
|
|
154
|
-
if (existingValues === undefined) {
|
|
155
|
-
super.add(key, values);
|
|
156
|
-
return true;
|
|
157
|
-
}
|
|
158
|
-
if (values !== undefined) {
|
|
157
|
+
const _addToValues = () => {
|
|
158
|
+
const existingValues = this.get(key);
|
|
159
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
159
160
|
for (const value of values) existingValues.push(value);
|
|
160
161
|
return true;
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const _addByNode = () => {
|
|
167
|
+
const existingNode = this.getNode(key);
|
|
168
|
+
if (this.isRealNode(existingNode)) {
|
|
169
|
+
const existingValues = this.get(existingNode);
|
|
170
|
+
if (existingValues === undefined) {
|
|
171
|
+
super.add(key, values);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
if (values !== undefined) {
|
|
175
|
+
for (const value of values) existingValues.push(value);
|
|
176
|
+
return true;
|
|
177
|
+
} else {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
161
180
|
} else {
|
|
162
|
-
return
|
|
181
|
+
return super.add(key, values);
|
|
163
182
|
}
|
|
164
|
-
}
|
|
165
|
-
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
if (this._isMapMode) {
|
|
186
|
+
return _addByNode() || _addToValues();
|
|
166
187
|
}
|
|
188
|
+
return _addToValues() || _addByNode();
|
|
167
189
|
};
|
|
168
190
|
|
|
169
191
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
@@ -180,7 +202,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
180
202
|
*
|
|
181
203
|
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
|
|
182
204
|
* structure and deletes the entire node if no values are left for that key.
|
|
183
|
-
* @param {
|
|
205
|
+
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
184
206
|
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
|
|
185
207
|
* pair in the AVLTreeMultiMapNode, or just the key itself.
|
|
186
208
|
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
@@ -191,7 +213,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
191
213
|
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
|
|
192
214
|
* the value was not found in the array, it returns `false`.
|
|
193
215
|
*/
|
|
194
|
-
deleteValue(
|
|
216
|
+
deleteValue(
|
|
217
|
+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
|
|
218
|
+
value: V
|
|
219
|
+
): boolean {
|
|
195
220
|
const values = this.get(keyNodeOrEntry);
|
|
196
221
|
if (Array.isArray(values)) {
|
|
197
222
|
const index = values.indexOf(value);
|
|
@@ -6,17 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type {
|
|
10
|
-
AVLTreeOptions,
|
|
11
|
-
BinaryTreeDeleteResult,
|
|
12
|
-
BSTNOptKeyOrNode,
|
|
13
|
-
BTNRep,
|
|
14
|
-
EntryCallback,
|
|
15
|
-
OptNodeOrNull
|
|
16
|
-
} from '../../types';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
|
|
17
10
|
import { IBinaryTree } from '../../interfaces';
|
|
18
11
|
|
|
19
12
|
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
13
|
+
override parent?: AVLTreeNode<K, V> = undefined;
|
|
14
|
+
|
|
20
15
|
/**
|
|
21
16
|
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
22
17
|
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
@@ -30,28 +25,26 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
30
25
|
super(key, value);
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
override
|
|
34
|
-
|
|
35
|
-
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
28
|
+
override _left?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
36
29
|
|
|
37
|
-
override get left():
|
|
30
|
+
override get left(): AVLTreeNode<K, V> | null | undefined {
|
|
38
31
|
return this._left;
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
override set left(v:
|
|
34
|
+
override set left(v: AVLTreeNode<K, V> | null | undefined) {
|
|
42
35
|
if (v) {
|
|
43
36
|
v.parent = this;
|
|
44
37
|
}
|
|
45
38
|
this._left = v;
|
|
46
39
|
}
|
|
47
40
|
|
|
48
|
-
override _right?:
|
|
41
|
+
override _right?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
49
42
|
|
|
50
|
-
override get right():
|
|
43
|
+
override get right(): AVLTreeNode<K, V> | null | undefined {
|
|
51
44
|
return this._right;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
override set right(v:
|
|
47
|
+
override set right(v: AVLTreeNode<K, V> | null | undefined) {
|
|
55
48
|
if (v) {
|
|
56
49
|
v.parent = this;
|
|
57
50
|
}
|
|
@@ -67,6 +60,70 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
67
60
|
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
68
61
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
69
62
|
* 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.
|
|
63
|
+
* @example
|
|
64
|
+
* // Find elements in a range
|
|
65
|
+
* // 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.
|
|
66
|
+
* type Datum = { timestamp: Date; temperature: number };
|
|
67
|
+
* // Fixed dataset of CPU temperature readings
|
|
68
|
+
* const cpuData: Datum[] = [
|
|
69
|
+
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
70
|
+
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
71
|
+
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
72
|
+
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
73
|
+
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
74
|
+
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
75
|
+
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
76
|
+
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
77
|
+
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
78
|
+
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
79
|
+
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
80
|
+
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
81
|
+
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
82
|
+
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
83
|
+
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
84
|
+
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
85
|
+
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
86
|
+
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
87
|
+
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
88
|
+
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
89
|
+
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
90
|
+
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
91
|
+
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
92
|
+
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
93
|
+
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
94
|
+
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
95
|
+
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
96
|
+
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
97
|
+
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
98
|
+
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
99
|
+
* ];
|
|
100
|
+
*
|
|
101
|
+
* // Create an AVL tree to store CPU temperature data
|
|
102
|
+
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
103
|
+
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
104
|
+
* });
|
|
105
|
+
*
|
|
106
|
+
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
107
|
+
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
108
|
+
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
109
|
+
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
110
|
+
* minute: node ? node.key.getMinutes() : 0,
|
|
111
|
+
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
112
|
+
* }));
|
|
113
|
+
*
|
|
114
|
+
* console.log(rangeResults); // [
|
|
115
|
+
* // { minute: 5, temperature: 59.4 },
|
|
116
|
+
* // { minute: 6, temperature: 60.1 },
|
|
117
|
+
* // { minute: 7, temperature: 61.3 },
|
|
118
|
+
* // { minute: 8, temperature: 62 },
|
|
119
|
+
* // { minute: 9, temperature: 63.5 },
|
|
120
|
+
* // { minute: 10, temperature: 64 },
|
|
121
|
+
* // { minute: 11, temperature: 62.8 },
|
|
122
|
+
* // { minute: 12, temperature: 61.5 },
|
|
123
|
+
* // { minute: 13, temperature: 60.2 },
|
|
124
|
+
* // { minute: 14, temperature: 59.8 },
|
|
125
|
+
* // { minute: 15, temperature: 58.6 }
|
|
126
|
+
* // ]
|
|
70
127
|
*/
|
|
71
128
|
export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
72
129
|
extends BST<K, V, R, MK, MV, MR>
|
|
@@ -76,7 +133,8 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
76
133
|
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
77
134
|
* in an iterable format.
|
|
78
135
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
79
|
-
* iterable that can contain either `
|
|
136
|
+
* iterable that can contain either `
|
|
137
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
|
|
80
138
|
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
81
139
|
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
82
140
|
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
@@ -84,7 +142,9 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
84
142
|
* other configuration settings specific
|
|
85
143
|
*/
|
|
86
144
|
constructor(
|
|
87
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
145
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
146
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
147
|
+
> = [],
|
|
88
148
|
options?: AVLTreeOptions<K, V, R>
|
|
89
149
|
) {
|
|
90
150
|
super([], options);
|
|
@@ -133,12 +193,15 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
133
193
|
* Space Complexity: O(1)
|
|
134
194
|
*
|
|
135
195
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
136
|
-
* @param {
|
|
137
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
196
|
+
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
197
|
+
* `keyNodeOrEntry` can be of type `R` or `
|
|
198
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
138
199
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
139
200
|
* an instance of the `AVLTreeNode` class.
|
|
140
201
|
*/
|
|
141
|
-
override isNode(
|
|
202
|
+
override isNode(
|
|
203
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
204
|
+
): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
205
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
143
206
|
}
|
|
144
207
|
|
|
@@ -148,13 +211,17 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
148
211
|
*
|
|
149
212
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
150
213
|
* structure, then balances the path.
|
|
151
|
-
* @param {
|
|
152
|
-
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
214
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
215
|
+
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
216
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
|
|
153
217
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
154
218
|
* the key or node being added to the data structure.
|
|
155
219
|
* @returns The method is returning a boolean value.
|
|
156
220
|
*/
|
|
157
|
-
override add(
|
|
221
|
+
override add(
|
|
222
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
223
|
+
value?: V
|
|
224
|
+
): boolean {
|
|
158
225
|
if (keyNodeOrEntry === null) return false;
|
|
159
226
|
const inserted = super.add(keyNodeOrEntry, value);
|
|
160
227
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
@@ -167,14 +234,16 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
167
234
|
*
|
|
168
235
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
169
236
|
* balances the tree if necessary.
|
|
170
|
-
* @param {
|
|
237
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
171
238
|
* parameter in the `override delete` method can be one of the following types:
|
|
172
239
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
173
240
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
174
241
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
175
242
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
176
243
|
*/
|
|
177
|
-
override delete(
|
|
244
|
+
override delete(
|
|
245
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
246
|
+
): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
247
|
const deletedResults = super.delete(keyNodeOrEntry);
|
|
179
248
|
for (const { needBalanced } of deletedResults) {
|
|
180
249
|
if (needBalanced) {
|
|
@@ -487,10 +556,11 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
487
556
|
*
|
|
488
557
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
489
558
|
* to restore balance in an AVL tree after inserting a node.
|
|
490
|
-
* @param {
|
|
491
|
-
* `
|
|
559
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
|
|
560
|
+
* `
|
|
561
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
492
562
|
*/
|
|
493
|
-
protected _balancePath(node:
|
|
563
|
+
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void {
|
|
494
564
|
node = this.ensureNode(node);
|
|
495
565
|
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
496
566
|
for (let i = 0; i < path.length; i++) {
|