directed-graph-typed 1.54.0 → 1.54.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/LICENSE +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/index.ts +2 -2
- package/src/interfaces/binary-tree.ts +10 -24
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -5,456 +5,218 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
9
|
-
AVLTreeMultiMapNested,
|
|
10
|
-
AVLTreeMultiMapNodeNested,
|
|
11
|
-
AVLTreeMultiMapOptions,
|
|
12
|
-
BinaryTreeDeleteResult,
|
|
13
|
-
BSTNOptKeyOrNode,
|
|
14
|
-
BTNRep,
|
|
15
|
-
EntryCallback,
|
|
16
|
-
IterationType
|
|
17
|
-
} from '../../types';
|
|
18
|
-
import { IBinaryTree } from '../../interfaces';
|
|
8
|
+
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types';
|
|
19
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
20
11
|
|
|
21
|
-
export class AVLTreeMultiMapNode<
|
|
22
|
-
K = any,
|
|
23
|
-
V = any,
|
|
24
|
-
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
|
|
25
|
-
> extends AVLTreeNode<K, V, NODE> {
|
|
12
|
+
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
26
13
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @param {
|
|
33
|
-
*
|
|
34
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
14
|
+
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
|
+
* type V.
|
|
16
|
+
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
|
|
17
|
+
* data being stored in the data structure. It helps in quickly accessing or retrieving the
|
|
18
|
+
* associated value in the data structure.
|
|
19
|
+
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of
|
|
20
|
+
* type `V`.
|
|
35
21
|
*/
|
|
36
|
-
constructor(key: K, value
|
|
22
|
+
constructor(key: K, value: V[]) {
|
|
37
23
|
super(key, value);
|
|
38
|
-
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
|
|
27
|
+
|
|
28
|
+
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
|
|
29
|
+
|
|
30
|
+
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
|
|
31
|
+
return this._left;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
|
|
35
|
+
if (v) {
|
|
36
|
+
v.parent = this;
|
|
37
|
+
}
|
|
38
|
+
this._left = v;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
|
|
42
|
+
|
|
43
|
+
override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
|
|
44
|
+
return this._right;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
|
|
48
|
+
if (v) {
|
|
49
|
+
v.parent = this;
|
|
50
|
+
}
|
|
51
|
+
this._right = v;
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
54
|
|
|
42
55
|
/**
|
|
43
|
-
*
|
|
56
|
+
*
|
|
44
57
|
*/
|
|
45
|
-
export class AVLTreeMultiMap<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
R = object,
|
|
49
|
-
MK = any,
|
|
50
|
-
MV = any,
|
|
51
|
-
MR = object,
|
|
52
|
-
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
53
|
-
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<
|
|
54
|
-
K,
|
|
55
|
-
V,
|
|
56
|
-
R,
|
|
57
|
-
MK,
|
|
58
|
-
MV,
|
|
59
|
-
MR,
|
|
60
|
-
NODE,
|
|
61
|
-
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
62
|
-
>
|
|
63
|
-
>
|
|
64
|
-
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
65
|
-
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
58
|
+
export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
59
|
+
extends AVLTree<K, V[], R, MK, MV[], MR>
|
|
60
|
+
implements IBinaryTree<K, V[], R, MK, MV, MR>
|
|
66
61
|
{
|
|
67
62
|
/**
|
|
68
|
-
* The constructor initializes
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
63
|
+
* The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
|
|
64
|
+
* and options.
|
|
65
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
66
|
+
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
|
|
67
|
+
* AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
|
|
68
|
+
* the AVLTreeMulti
|
|
69
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
70
|
+
* `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
|
|
71
|
+
* additional options for configuring the AVLTreeMultiMap instance.
|
|
74
72
|
*/
|
|
75
73
|
constructor(
|
|
76
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
77
|
-
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
74
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [],
|
|
75
|
+
options?: AVLTreeMultiMapOptions<K, V[], R>
|
|
78
76
|
) {
|
|
79
|
-
super([], options);
|
|
80
|
-
if (keysNodesEntriesOrRaws)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
protected _count = 0;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
87
|
-
* search.
|
|
88
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
89
|
-
*/
|
|
90
|
-
get count(): number {
|
|
91
|
-
return this._count;
|
|
77
|
+
super([], { ...options, isMapMode: true });
|
|
78
|
+
if (keysNodesEntriesOrRaws) {
|
|
79
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
80
|
+
}
|
|
92
81
|
}
|
|
93
82
|
|
|
94
83
|
/**
|
|
95
|
-
* Time Complexity: O(
|
|
84
|
+
* Time Complexity: O(1)
|
|
96
85
|
* Space Complexity: O(1)
|
|
97
86
|
*
|
|
98
|
-
* The function
|
|
99
|
-
*
|
|
100
|
-
* @
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
|
|
110
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
111
|
-
* which is a generic type that can be replaced with any specific type when using the function.
|
|
112
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
113
|
-
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
114
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
115
|
-
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
|
|
116
|
-
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
117
|
-
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
118
|
-
*/
|
|
119
|
-
override createNode(key: K, value?: V, count?: number): NODE {
|
|
120
|
-
return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count) as NODE;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
125
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
126
|
-
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
|
|
127
|
-
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
128
|
-
* object.
|
|
129
|
-
*/
|
|
130
|
-
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
131
|
-
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
87
|
+
* The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
|
|
88
|
+
* specified options.
|
|
89
|
+
* @param [options] - The `options` parameter in the `createTree` function is of type
|
|
90
|
+
* `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
|
|
91
|
+
* `K`, `V[]`, and `R`. The function creates a new `AVL
|
|
92
|
+
* @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
|
|
93
|
+
* provided options.
|
|
94
|
+
*/
|
|
95
|
+
override createTree(options?: AVLTreeMultiMapOptions<K, V[], R>) {
|
|
96
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR>([], {
|
|
132
97
|
iterationType: this.iterationType,
|
|
133
|
-
isMapMode: this._isMapMode,
|
|
134
98
|
specifyComparable: this._specifyComparable,
|
|
135
99
|
toEntryFn: this._toEntryFn,
|
|
136
100
|
isReverse: this._isReverse,
|
|
137
101
|
...options
|
|
138
|
-
})
|
|
102
|
+
});
|
|
139
103
|
}
|
|
140
104
|
|
|
141
105
|
/**
|
|
142
|
-
*
|
|
143
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
144
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
145
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
146
|
-
* an instance of the `AVLTreeMultiMapNode` class.
|
|
147
|
-
*/
|
|
148
|
-
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
149
|
-
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Time Complexity: O(log n)
|
|
106
|
+
* Time Complexity: O(1)
|
|
154
107
|
* Space Complexity: O(1)
|
|
155
108
|
*
|
|
156
|
-
* The function overrides the
|
|
157
|
-
* and
|
|
158
|
-
* @param {
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
163
|
-
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
164
|
-
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
165
|
-
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
166
|
-
* be added once. However, you can specify a different value for `count` if you want to add
|
|
167
|
-
* @returns a boolean value.
|
|
109
|
+
* The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
|
|
110
|
+
* specified key and an empty array of values.
|
|
111
|
+
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
|
|
112
|
+
* that will be created in the AVLTreeMultiMap.
|
|
113
|
+
* @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
|
|
114
|
+
* empty array.
|
|
168
115
|
*/
|
|
169
|
-
override
|
|
170
|
-
|
|
171
|
-
if (newNode === undefined) return false;
|
|
172
|
-
|
|
173
|
-
const orgNodeCount = newNode?.count || 0;
|
|
174
|
-
const inserted = super.add(newNode, newValue);
|
|
175
|
-
if (inserted) {
|
|
176
|
-
this._count += orgNodeCount;
|
|
177
|
-
}
|
|
178
|
-
return true;
|
|
116
|
+
override createNode(key: K): AVLTreeMultiMapNode<K, V> {
|
|
117
|
+
return new AVLTreeMultiMapNode<K, V>(key, []);
|
|
179
118
|
}
|
|
180
119
|
|
|
120
|
+
override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean;
|
|
121
|
+
|
|
122
|
+
override add(key: K, value: V): boolean;
|
|
123
|
+
|
|
181
124
|
/**
|
|
182
125
|
* Time Complexity: O(log n)
|
|
183
|
-
* Space Complexity: O(
|
|
126
|
+
* Space Complexity: O(log n)
|
|
184
127
|
*
|
|
185
|
-
* The function overrides the
|
|
186
|
-
*
|
|
187
|
-
* @param {BTNRep<K, V,
|
|
188
|
-
* parameter in the `
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @param [
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* `
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
let needBalanced: NODE | undefined = undefined,
|
|
209
|
-
orgCurrent: NODE | undefined = curr;
|
|
128
|
+
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
|
|
129
|
+
* tree multi-map.
|
|
130
|
+
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
131
|
+
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it
|
|
132
|
+
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
|
|
133
|
+
* `values`
|
|
134
|
+
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that
|
|
135
|
+
* you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
|
|
136
|
+
* with a specific key.
|
|
137
|
+
* @returns The `override add` method is returning a boolean value, which indicates whether the
|
|
138
|
+
* addition operation was successful or not.
|
|
139
|
+
*/
|
|
140
|
+
override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean {
|
|
141
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
142
|
+
|
|
143
|
+
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
144
|
+
if (key === undefined || key === null) return false;
|
|
145
|
+
|
|
146
|
+
const existingValues = this.get(key);
|
|
147
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
148
|
+
for (const value of values) existingValues.push(value);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
210
151
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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) {
|
|
159
|
+
for (const value of values) existingValues.push(value);
|
|
160
|
+
return true;
|
|
218
161
|
} else {
|
|
219
|
-
|
|
220
|
-
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
221
|
-
parent.left = curr.right;
|
|
222
|
-
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
223
|
-
parent.right = curr.right;
|
|
224
|
-
}
|
|
225
|
-
needBalanced = parent;
|
|
162
|
+
return false;
|
|
226
163
|
}
|
|
227
164
|
} else {
|
|
228
|
-
|
|
229
|
-
if (leftSubTreeRightMost) {
|
|
230
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
231
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
232
|
-
if (parentOfLeftSubTreeMax) {
|
|
233
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
234
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
235
|
-
} else {
|
|
236
|
-
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
237
|
-
}
|
|
238
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
165
|
+
return super.add(key, values);
|
|
241
166
|
}
|
|
242
|
-
|
|
243
|
-
// TODO How to handle when the count of target node is lesser than current node's count
|
|
244
|
-
if (orgCurrent) this._count -= orgCurrent.count;
|
|
245
|
-
}
|
|
167
|
+
};
|
|
246
168
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
this._balancePath(needBalanced);
|
|
169
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
170
|
+
const [key, values] = keyNodeOrEntry;
|
|
171
|
+
return _commonAdd(key, value !== undefined ? [value] : values);
|
|
251
172
|
}
|
|
252
173
|
|
|
253
|
-
return
|
|
174
|
+
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
|
|
254
175
|
}
|
|
255
176
|
|
|
256
177
|
/**
|
|
257
|
-
* Time Complexity: O(
|
|
258
|
-
* Space Complexity: O(1)
|
|
259
|
-
*
|
|
260
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
261
|
-
* zero.
|
|
262
|
-
*/
|
|
263
|
-
override clear() {
|
|
264
|
-
super.clear();
|
|
265
|
-
this._count = 0;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Time Complexity: O(n log n)
|
|
178
|
+
* Time Complexity: O(log n)
|
|
270
179
|
* Space Complexity: O(log n)
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
* the
|
|
277
|
-
* @
|
|
278
|
-
*
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
if (
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
buildBalanceBST(l, m - 1);
|
|
295
|
-
buildBalanceBST(m + 1, r);
|
|
296
|
-
};
|
|
180
|
+
*
|
|
181
|
+
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
|
|
182
|
+
* structure and deletes the entire node if no values are left for that key.
|
|
183
|
+
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
184
|
+
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
|
|
185
|
+
* pair in the AVLTreeMultiMapNode, or just the key itself.
|
|
186
|
+
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
187
|
+
* value that you want to delete from the multi-map data structure associated with a particular key.
|
|
188
|
+
* The function checks if the value exists in the array of values associated with the key, and if
|
|
189
|
+
* found, removes it from the array.
|
|
190
|
+
* @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
|
|
191
|
+
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
|
|
192
|
+
* the value was not found in the array, it returns `false`.
|
|
193
|
+
*/
|
|
194
|
+
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean {
|
|
195
|
+
const values = this.get(keyNodeOrEntry);
|
|
196
|
+
if (Array.isArray(values)) {
|
|
197
|
+
const index = values.indexOf(value);
|
|
198
|
+
if (index === -1) return false;
|
|
199
|
+
values.splice(index, 1);
|
|
200
|
+
|
|
201
|
+
// If no values left, remove the entire node
|
|
202
|
+
if (values.length === 0) this.delete(keyNodeOrEntry);
|
|
297
203
|
|
|
298
|
-
buildBalanceBST(0, n - 1);
|
|
299
|
-
return true;
|
|
300
|
-
} else {
|
|
301
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
302
|
-
while (stack.length > 0) {
|
|
303
|
-
const popped = stack.pop();
|
|
304
|
-
if (popped) {
|
|
305
|
-
const [l, r] = popped;
|
|
306
|
-
if (l <= r) {
|
|
307
|
-
const m = l + Math.floor((r - l) / 2);
|
|
308
|
-
const midNode = sorted[m];
|
|
309
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
310
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
311
|
-
stack.push([m + 1, r]);
|
|
312
|
-
stack.push([l, m - 1]);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
204
|
return true;
|
|
317
205
|
}
|
|
206
|
+
return false;
|
|
318
207
|
}
|
|
319
208
|
|
|
320
209
|
/**
|
|
321
|
-
* Time
|
|
322
|
-
* Space
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(n)
|
|
323
212
|
*
|
|
324
|
-
* The function overrides the
|
|
325
|
-
*
|
|
213
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
214
|
+
* structure.
|
|
215
|
+
* @returns A cloned tree object is being returned.
|
|
326
216
|
*/
|
|
327
|
-
override clone()
|
|
217
|
+
override clone() {
|
|
328
218
|
const cloned = this.createTree();
|
|
329
|
-
|
|
330
|
-
else this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
331
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
219
|
+
this._clone(cloned);
|
|
332
220
|
return cloned;
|
|
333
221
|
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
337
|
-
* with modified entries based on a provided callback.
|
|
338
|
-
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
339
|
-
* AVLTreeMultiMap. It takes four arguments:
|
|
340
|
-
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
341
|
-
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
342
|
-
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
343
|
-
* These options
|
|
344
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
345
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
346
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
347
|
-
* or
|
|
348
|
-
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
349
|
-
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
350
|
-
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
351
|
-
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
352
|
-
*/
|
|
353
|
-
override map<MK, MV, MR>(
|
|
354
|
-
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
355
|
-
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
|
|
356
|
-
thisArg?: any
|
|
357
|
-
): AVLTreeMultiMap<MK, MV, MR> {
|
|
358
|
-
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
|
|
359
|
-
let index = 0;
|
|
360
|
-
for (const [key, value] of this) {
|
|
361
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
362
|
-
}
|
|
363
|
-
return newTree;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
368
|
-
* a node object.
|
|
369
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
370
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
371
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
372
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
373
|
-
* value is provided, it will default to `undefined`.
|
|
374
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
375
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
376
|
-
* @returns either a NODE object or undefined.
|
|
377
|
-
*/
|
|
378
|
-
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
379
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
380
|
-
value?: V,
|
|
381
|
-
count = 1
|
|
382
|
-
): [NODE | undefined, V | undefined] {
|
|
383
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
384
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
385
|
-
|
|
386
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
387
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
388
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
389
|
-
const finalValue = value ?? entryValue;
|
|
390
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
394
|
-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
395
|
-
const finalValue = value ?? entryValue;
|
|
396
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
400
|
-
|
|
401
|
-
return [undefined, undefined];
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
/**
|
|
405
|
-
* Time Complexity: O(1)
|
|
406
|
-
* Space Complexity: O(1)
|
|
407
|
-
*
|
|
408
|
-
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
409
|
-
* in a binary search tree.
|
|
410
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
411
|
-
* that will be swapped with the `destNode`.
|
|
412
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
413
|
-
* node where the properties will be swapped with the source node.
|
|
414
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
415
|
-
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
416
|
-
*/
|
|
417
|
-
protected override _swapProperties(
|
|
418
|
-
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
419
|
-
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
420
|
-
): NODE | undefined {
|
|
421
|
-
srcNode = this.ensureNode(srcNode);
|
|
422
|
-
destNode = this.ensureNode(destNode);
|
|
423
|
-
if (srcNode && destNode) {
|
|
424
|
-
const { key, value, count, height } = destNode;
|
|
425
|
-
const tempNode = this.createNode(key, value, count);
|
|
426
|
-
if (tempNode) {
|
|
427
|
-
tempNode.height = height;
|
|
428
|
-
|
|
429
|
-
destNode.key = srcNode.key;
|
|
430
|
-
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
431
|
-
destNode.count = srcNode.count;
|
|
432
|
-
destNode.height = srcNode.height;
|
|
433
|
-
|
|
434
|
-
srcNode.key = tempNode.key;
|
|
435
|
-
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
436
|
-
srcNode.count = tempNode.count;
|
|
437
|
-
srcNode.height = tempNode.height;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
return destNode;
|
|
441
|
-
}
|
|
442
|
-
return undefined;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Time Complexity: O(1)
|
|
447
|
-
* Space Complexity: O(1)
|
|
448
|
-
*
|
|
449
|
-
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
450
|
-
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
451
|
-
* data structure. It is of type NODE.
|
|
452
|
-
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
453
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
454
|
-
* superclass, which is of type `NODE`.
|
|
455
|
-
*/
|
|
456
|
-
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
457
|
-
newNode.count = oldNode.count + newNode.count;
|
|
458
|
-
return super._replaceNode(oldNode, newNode);
|
|
459
|
-
}
|
|
460
222
|
}
|