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