deque-typed 2.0.5 → 2.1.0
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/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +598 -869
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +664 -893
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.AVLTreeCounter = exports.AVLTreeCounterNode = void 0;
|
|
4
11
|
const avl_tree_1 = require("./avl-tree");
|
|
12
|
+
/**
|
|
13
|
+
* AVL node with an extra 'count' field; keeps parent/child links.
|
|
14
|
+
* @remarks Time O(1), Space O(1)
|
|
15
|
+
* @template K
|
|
16
|
+
* @template V
|
|
17
|
+
*/
|
|
5
18
|
class AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
|
|
6
19
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
* of the
|
|
10
|
-
* @param
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
14
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
20
|
+
* Create an AVL counter node.
|
|
21
|
+
* @remarks Time O(1), Space O(1)
|
|
22
|
+
* @param key - Key of the node.
|
|
23
|
+
* @param [value] - Associated value (ignored in map mode).
|
|
24
|
+
* @param [count] - Initial count for this node (default 1).
|
|
25
|
+
* @returns New AVLTreeCounterNode instance.
|
|
15
26
|
*/
|
|
16
27
|
constructor(key, value, count = 1) {
|
|
17
28
|
super(key, value);
|
|
@@ -20,18 +31,40 @@ class AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
|
|
|
20
31
|
this._right = undefined;
|
|
21
32
|
this.count = count;
|
|
22
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the left child pointer.
|
|
36
|
+
* @remarks Time O(1), Space O(1)
|
|
37
|
+
* @returns Left child node, or null/undefined.
|
|
38
|
+
*/
|
|
23
39
|
get left() {
|
|
24
40
|
return this._left;
|
|
25
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Set the left child and update its parent pointer.
|
|
44
|
+
* @remarks Time O(1), Space O(1)
|
|
45
|
+
* @param v - New left child node, or null/undefined.
|
|
46
|
+
* @returns void
|
|
47
|
+
*/
|
|
26
48
|
set left(v) {
|
|
27
49
|
if (v) {
|
|
28
50
|
v.parent = this;
|
|
29
51
|
}
|
|
30
52
|
this._left = v;
|
|
31
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the right child pointer.
|
|
56
|
+
* @remarks Time O(1), Space O(1)
|
|
57
|
+
* @returns Right child node, or null/undefined.
|
|
58
|
+
*/
|
|
32
59
|
get right() {
|
|
33
60
|
return this._right;
|
|
34
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Set the right child and update its parent pointer.
|
|
64
|
+
* @remarks Time O(1), Space O(1)
|
|
65
|
+
* @param v - New right child node, or null/undefined.
|
|
66
|
+
* @returns void
|
|
67
|
+
*/
|
|
35
68
|
set right(v) {
|
|
36
69
|
if (v) {
|
|
37
70
|
v.parent = this;
|
|
@@ -41,16 +74,19 @@ class AVLTreeCounterNode extends avl_tree_1.AVLTreeNode {
|
|
|
41
74
|
}
|
|
42
75
|
exports.AVLTreeCounterNode = AVLTreeCounterNode;
|
|
43
76
|
/**
|
|
44
|
-
*
|
|
77
|
+
* AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
|
|
78
|
+
* @remarks Time O(1), Space O(1)
|
|
79
|
+
* @template K
|
|
80
|
+
* @template V
|
|
81
|
+
* @template R
|
|
45
82
|
*/
|
|
46
83
|
class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
47
84
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @
|
|
50
|
-
*
|
|
51
|
-
* @param
|
|
52
|
-
*
|
|
53
|
-
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
85
|
+
* Create a AVLTreeCounter instance
|
|
86
|
+
* @remarks Time O(n), Space O(n)
|
|
87
|
+
* @param keysNodesEntriesOrRaws
|
|
88
|
+
* @param options
|
|
89
|
+
* @returns New AVLTreeCounterNode instance.
|
|
54
90
|
*/
|
|
55
91
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
56
92
|
super([], options);
|
|
@@ -58,77 +94,37 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
58
94
|
if (keysNodesEntriesOrRaws)
|
|
59
95
|
this.addMany(keysNodesEntriesOrRaws);
|
|
60
96
|
}
|
|
61
|
-
/**
|
|
62
|
-
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
63
|
-
* search.
|
|
64
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
65
|
-
*/
|
|
66
97
|
get count() {
|
|
67
98
|
return this._count;
|
|
68
99
|
}
|
|
69
100
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
74
|
-
* search.
|
|
75
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
101
|
+
* Compute the total count by traversing the tree (sums node.count).
|
|
102
|
+
* @remarks Time O(N), Space O(H)
|
|
103
|
+
* @returns Total count recomputed from nodes.
|
|
76
104
|
*/
|
|
77
105
|
getComputedCount() {
|
|
78
106
|
let sum = 0;
|
|
79
107
|
this.dfs(node => (sum += node.count));
|
|
80
108
|
return sum;
|
|
81
109
|
}
|
|
82
|
-
|
|
83
|
-
* The function creates a new AVLTreeCounterNode with the specified key, value, and count.
|
|
84
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
85
|
-
* which is a generic type that can be replaced with any specific type when using the function.
|
|
86
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
87
|
-
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
88
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
89
|
-
* key-value pair in the AVLTreeCounterNode. It is an optional parameter, so it can be omitted when
|
|
90
|
-
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
91
|
-
* @returns a new instance of the AVLTreeCounterNode class, casted as AVLTreeCounterNode<K, V>.
|
|
92
|
-
*/
|
|
93
|
-
createNode(key, value, count) {
|
|
110
|
+
_createNode(key, value, count) {
|
|
94
111
|
return new AVLTreeCounterNode(key, this._isMapMode ? undefined : value, count);
|
|
95
112
|
}
|
|
96
113
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @
|
|
99
|
-
*
|
|
100
|
-
* @returns a new instance of the AVLTreeCounter class, with the specified options, as a TREE
|
|
101
|
-
* object.
|
|
102
|
-
*/
|
|
103
|
-
createTree(options) {
|
|
104
|
-
return new AVLTreeCounter([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* The function checks if the input is an instance of AVLTreeCounterNode.
|
|
108
|
-
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
109
|
-
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
110
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
111
|
-
* an instance of the `AVLTreeCounterNode` class.
|
|
114
|
+
* Type guard: check whether the input is an AVLTreeCounterNode.
|
|
115
|
+
* @remarks Time O(1), Space O(1)
|
|
116
|
+
* @returns True if the value is an AVLTreeCounterNode.
|
|
112
117
|
*/
|
|
113
118
|
isNode(keyNodeOrEntry) {
|
|
114
119
|
return keyNodeOrEntry instanceof AVLTreeCounterNode;
|
|
115
120
|
}
|
|
116
121
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @
|
|
123
|
-
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
|
|
124
|
-
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
|
|
125
|
-
* entry, or raw element
|
|
126
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
127
|
-
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
128
|
-
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
129
|
-
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
130
|
-
* be added once. However, you can specify a different value for `count` if you want to add
|
|
131
|
-
* @returns a boolean value.
|
|
122
|
+
* Insert or increment a node and update aggregate count.
|
|
123
|
+
* @remarks Time O(log N), Space O(1)
|
|
124
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
125
|
+
* @param [value] - Value when a bare key is provided (ignored in map mode).
|
|
126
|
+
* @param [count] - How much to increase the node's count (default 1).
|
|
127
|
+
* @returns True if inserted/updated; false if ignored.
|
|
132
128
|
*/
|
|
133
129
|
add(keyNodeOrEntry, value, count = 1) {
|
|
134
130
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
@@ -142,23 +138,11 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
142
138
|
return true;
|
|
143
139
|
}
|
|
144
140
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
|
|
151
|
-
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
152
|
-
* binary tree. It can be a key, node, or entry that determines which
|
|
153
|
-
* node(s) should be deleted.
|
|
154
|
-
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
155
|
-
* boolean flag that determines whether to ignore the count of the node being deleted. If
|
|
156
|
-
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
157
|
-
* `ignoreCount` is set to
|
|
158
|
-
* @returns The `delete` method overrides the default delete behavior in a binary tree data
|
|
159
|
-
* structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
|
|
160
|
-
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
161
|
-
* deleted node and whether balancing is needed in the tree.
|
|
141
|
+
* Delete a node (or decrement its count) and rebalance if needed.
|
|
142
|
+
* @remarks Time O(log N), Space O(1)
|
|
143
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
|
|
144
|
+
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
145
|
+
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
162
146
|
*/
|
|
163
147
|
delete(keyNodeOrEntry, ignoreCount = false) {
|
|
164
148
|
var _a;
|
|
@@ -208,7 +192,6 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
208
192
|
}
|
|
209
193
|
}
|
|
210
194
|
this._size = this._size - 1;
|
|
211
|
-
// TODO How to handle when the count of target node is lesser than current node's count
|
|
212
195
|
if (orgCurrent)
|
|
213
196
|
this._count -= orgCurrent.count;
|
|
214
197
|
}
|
|
@@ -219,125 +202,118 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
219
202
|
return deletedResult;
|
|
220
203
|
}
|
|
221
204
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
226
|
-
* zero.
|
|
205
|
+
* Remove all nodes and reset aggregate counters.
|
|
206
|
+
* @remarks Time O(N), Space O(1)
|
|
207
|
+
* @returns void
|
|
227
208
|
*/
|
|
228
209
|
clear() {
|
|
229
210
|
super.clear();
|
|
230
211
|
this._count = 0;
|
|
231
212
|
}
|
|
232
213
|
/**
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* tree using either a recursive or iterative approach.
|
|
238
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
239
|
-
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
240
|
-
* default value of `this.iterationType`, which means it will use the iteration type currently set in
|
|
241
|
-
* the object.
|
|
242
|
-
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
243
|
-
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
214
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
215
|
+
* @remarks Time O(N), Space O(N)
|
|
216
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
217
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
244
218
|
*/
|
|
245
219
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
246
|
-
const
|
|
247
|
-
|
|
220
|
+
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
221
|
+
const n = nodes.length;
|
|
222
|
+
if (n === 0)
|
|
248
223
|
return false;
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const [l, r] = popped;
|
|
272
|
-
if (l <= r) {
|
|
273
|
-
const m = l + Math.floor((r - l) / 2);
|
|
274
|
-
const midNode = sorted[m];
|
|
275
|
-
if (this._isMapMode)
|
|
276
|
-
this.add(midNode.key, undefined, midNode.count);
|
|
277
|
-
else
|
|
278
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
279
|
-
stack.push([m + 1, r]);
|
|
280
|
-
stack.push([l, m - 1]);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
224
|
+
let total = 0;
|
|
225
|
+
for (const nd of nodes)
|
|
226
|
+
total += nd ? nd.count : 0;
|
|
227
|
+
this._clearNodes();
|
|
228
|
+
const build = (l, r, parent) => {
|
|
229
|
+
if (l > r)
|
|
230
|
+
return undefined;
|
|
231
|
+
const m = l + ((r - l) >> 1);
|
|
232
|
+
const root = nodes[m];
|
|
233
|
+
root.left = build(l, m - 1, root);
|
|
234
|
+
root.right = build(m + 1, r, root);
|
|
235
|
+
root.parent = parent;
|
|
236
|
+
const lh = root.left ? root.left.height : -1;
|
|
237
|
+
const rh = root.right ? root.right.height : -1;
|
|
238
|
+
root.height = Math.max(lh, rh) + 1;
|
|
239
|
+
return root;
|
|
240
|
+
};
|
|
241
|
+
const newRoot = build(0, n - 1, undefined);
|
|
242
|
+
this._setRoot(newRoot);
|
|
243
|
+
this._size = n;
|
|
244
|
+
this._count = total;
|
|
245
|
+
return true;
|
|
286
246
|
}
|
|
287
247
|
/**
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
* The function overrides the clone method to create a deep copy of a tree object.
|
|
292
|
-
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
248
|
+
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
249
|
+
* @remarks Time O(N), Space O(N)
|
|
250
|
+
* @returns A deep copy of this tree.
|
|
293
251
|
*/
|
|
294
252
|
clone() {
|
|
295
|
-
const
|
|
296
|
-
if (this._isMapMode)
|
|
297
|
-
this.bfs(node =>
|
|
298
|
-
|
|
299
|
-
|
|
253
|
+
const out = this._createInstance();
|
|
254
|
+
if (this._isMapMode) {
|
|
255
|
+
this.bfs(node => out.add(node.key, undefined, node.count));
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
this.bfs(node => out.add(node.key, node.value, node.count));
|
|
259
|
+
}
|
|
300
260
|
if (this._isMapMode)
|
|
301
|
-
|
|
302
|
-
return
|
|
261
|
+
out._store = this._store;
|
|
262
|
+
return out;
|
|
303
263
|
}
|
|
304
264
|
/**
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* @
|
|
308
|
-
*
|
|
309
|
-
* @
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
* @
|
|
314
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
315
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
316
|
-
* or
|
|
317
|
-
* @returns The `map` method is returning a new `AVLTreeCounter` instance with the entries
|
|
318
|
-
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
319
|
-
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
320
|
-
* then added to the new `AVLTreeCounter` instance, which is returned at the end.
|
|
265
|
+
* Create a new AVLTreeCounter by mapping each [key, value] entry.
|
|
266
|
+
* @remarks Time O(N log N), Space O(N)
|
|
267
|
+
* @template MK
|
|
268
|
+
* @template MV
|
|
269
|
+
* @template MR
|
|
270
|
+
* @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
|
|
271
|
+
* @param [options] - Options for the output tree.
|
|
272
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
273
|
+
* @returns A new AVLTreeCounter with mapped entries.
|
|
321
274
|
*/
|
|
322
275
|
map(callback, options, thisArg) {
|
|
323
|
-
const
|
|
276
|
+
const out = this._createLike([], options);
|
|
324
277
|
let index = 0;
|
|
325
278
|
for (const [key, value] of this) {
|
|
326
|
-
|
|
279
|
+
out.add(callback.call(thisArg, key, value, index++, this));
|
|
327
280
|
}
|
|
328
|
-
return
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
285
|
+
* @remarks Time O(1), Space O(1)
|
|
286
|
+
* @template TK
|
|
287
|
+
* @template TV
|
|
288
|
+
* @template TR
|
|
289
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
290
|
+
* @returns An empty like-kind instance.
|
|
291
|
+
*/
|
|
292
|
+
_createInstance(options) {
|
|
293
|
+
const Ctor = this.constructor;
|
|
294
|
+
return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
298
|
+
* @remarks Time O(N log N), Space O(N)
|
|
299
|
+
* @template TK
|
|
300
|
+
* @template TV
|
|
301
|
+
* @template TR
|
|
302
|
+
* @param iter - Iterable used to seed the new tree.
|
|
303
|
+
* @param [options] - Options merged with the current snapshot.
|
|
304
|
+
* @returns A like-kind AVLTreeCounter built from the iterable.
|
|
305
|
+
*/
|
|
306
|
+
_createLike(iter = [], options) {
|
|
307
|
+
const Ctor = this.constructor;
|
|
308
|
+
return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
329
309
|
}
|
|
330
310
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* @param
|
|
334
|
-
*
|
|
335
|
-
* @param
|
|
336
|
-
*
|
|
337
|
-
* value is provided, it will default to `undefined`.
|
|
338
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
339
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
340
|
-
* @returns either a AVLTreeCounterNode<K, V> object or undefined.
|
|
311
|
+
* (Protected) Normalize input into a node plus its effective value and count.
|
|
312
|
+
* @remarks Time O(1), Space O(1)
|
|
313
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry.
|
|
314
|
+
* @param [value] - Value used when a bare key is provided.
|
|
315
|
+
* @param [count] - Count increment to apply (default 1).
|
|
316
|
+
* @returns Tuple [node, value] where node may be undefined.
|
|
341
317
|
*/
|
|
342
318
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count = 1) {
|
|
343
319
|
if (keyNodeOrEntry === undefined || keyNodeOrEntry === null)
|
|
@@ -349,29 +325,23 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
349
325
|
if (key === undefined || key === null)
|
|
350
326
|
return [undefined, undefined];
|
|
351
327
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
352
|
-
return [this.
|
|
328
|
+
return [this._createNode(key, finalValue, count), finalValue];
|
|
353
329
|
}
|
|
354
|
-
return [this.
|
|
330
|
+
return [this._createNode(keyNodeOrEntry, value, count), value];
|
|
355
331
|
}
|
|
356
332
|
/**
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
* @param {BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>} srcNode - The `srcNode` parameter represents the source node
|
|
363
|
-
* that will be swapped with the `destNode`.
|
|
364
|
-
* @param {BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>} destNode - The `destNode` parameter represents the destination
|
|
365
|
-
* node where the properties will be swapped with the source node.
|
|
366
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
367
|
-
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
333
|
+
* (Protected) Swap keys/values/counters between the source and destination nodes.
|
|
334
|
+
* @remarks Time O(1), Space O(1)
|
|
335
|
+
* @param srcNode - Source node (or key) whose properties will be moved.
|
|
336
|
+
* @param destNode - Destination node (or key) to receive properties.
|
|
337
|
+
* @returns Destination node after swap, or undefined.
|
|
368
338
|
*/
|
|
369
339
|
_swapProperties(srcNode, destNode) {
|
|
370
340
|
srcNode = this.ensureNode(srcNode);
|
|
371
341
|
destNode = this.ensureNode(destNode);
|
|
372
342
|
if (srcNode && destNode) {
|
|
373
343
|
const { key, value, count, height } = destNode;
|
|
374
|
-
const tempNode = this.
|
|
344
|
+
const tempNode = this._createNode(key, value, count);
|
|
375
345
|
if (tempNode) {
|
|
376
346
|
tempNode.height = height;
|
|
377
347
|
destNode.key = srcNode.key;
|
|
@@ -390,15 +360,11 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
|
|
|
390
360
|
return undefined;
|
|
391
361
|
}
|
|
392
362
|
/**
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
* @
|
|
398
|
-
* data structure. It is of type AVLTreeCounterNode<K, V>.
|
|
399
|
-
* @param {AVLTreeCounterNode<K, V>} newNode - The `newNode` parameter is an instance of the `AVLTreeCounterNode<K, V>` class.
|
|
400
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
401
|
-
* superclass, which is of type `AVLTreeCounterNode<K, V>`.
|
|
363
|
+
* (Protected) Replace one node by another and adjust counters accordingly.
|
|
364
|
+
* @remarks Time O(1), Space O(1)
|
|
365
|
+
* @param oldNode - Node being replaced.
|
|
366
|
+
* @param newNode - Replacement node.
|
|
367
|
+
* @returns The new node after replacement.
|
|
402
368
|
*/
|
|
403
369
|
_replaceNode(oldNode, newNode) {
|
|
404
370
|
newNode.count = oldNode.count + newNode.count;
|