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