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