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