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