directed-graph-typed 2.0.5 → 2.1.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/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 +602 -873
- 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 +196 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
- package/src/data-structures/binary-tree/avl-tree.ts +237 -206
- package/src/data-structures/binary-tree/binary-tree.ts +665 -896
- package/src/data-structures/binary-tree/bst.ts +565 -572
- package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
- package/src/data-structures/binary-tree/tree-counter.ts +195 -219
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
- 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,19 +1,29 @@
|
|
|
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.TreeCounter = exports.TreeCounterNode = void 0;
|
|
4
11
|
const red_black_tree_1 = require("./red-black-tree");
|
|
12
|
+
/**
|
|
13
|
+
* RB-tree 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 TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
|
|
6
19
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
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'`.
|
|
20
|
+
* Create a tree counter node.
|
|
21
|
+
* @remarks Time O(1), Space O(1)
|
|
22
|
+
* @param key - Key of the node.
|
|
23
|
+
* @param [value] - Value associated with the key (ignored in map mode).
|
|
24
|
+
* @param [count] - Initial count for this node (default 1).
|
|
25
|
+
* @param color - Initial color ('RED' or 'BLACK').
|
|
26
|
+
* @returns New TreeCounterNode instance.
|
|
17
27
|
*/
|
|
18
28
|
constructor(key, value, count = 1, color = 'BLACK') {
|
|
19
29
|
super(key, value, color);
|
|
@@ -22,18 +32,40 @@ class TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
|
|
|
22
32
|
this._right = undefined;
|
|
23
33
|
this.count = count;
|
|
24
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Get the left child pointer.
|
|
37
|
+
* @remarks Time O(1), Space O(1)
|
|
38
|
+
* @returns Left child node, or null/undefined.
|
|
39
|
+
*/
|
|
25
40
|
get left() {
|
|
26
41
|
return this._left;
|
|
27
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Set the left child and update its parent pointer.
|
|
45
|
+
* @remarks Time O(1), Space O(1)
|
|
46
|
+
* @param v - New left child node, or null/undefined.
|
|
47
|
+
* @returns void
|
|
48
|
+
*/
|
|
28
49
|
set left(v) {
|
|
29
50
|
if (v) {
|
|
30
51
|
v.parent = this;
|
|
31
52
|
}
|
|
32
53
|
this._left = v;
|
|
33
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Get the right child pointer.
|
|
57
|
+
* @remarks Time O(1), Space O(1)
|
|
58
|
+
* @returns Right child node, or null/undefined.
|
|
59
|
+
*/
|
|
34
60
|
get right() {
|
|
35
61
|
return this._right;
|
|
36
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Set the right child and update its parent pointer.
|
|
65
|
+
* @remarks Time O(1), Space O(1)
|
|
66
|
+
* @param v - New right child node, or null/undefined.
|
|
67
|
+
* @returns void
|
|
68
|
+
*/
|
|
37
69
|
set right(v) {
|
|
38
70
|
if (v) {
|
|
39
71
|
v.parent = this;
|
|
@@ -43,17 +75,19 @@ class TreeCounterNode extends red_black_tree_1.RedBlackTreeNode {
|
|
|
43
75
|
}
|
|
44
76
|
exports.TreeCounterNode = TreeCounterNode;
|
|
45
77
|
/**
|
|
46
|
-
*
|
|
78
|
+
* Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
|
|
79
|
+
* @remarks Time O(1), Space O(1)
|
|
80
|
+
* @template K
|
|
81
|
+
* @template V
|
|
82
|
+
* @template R
|
|
47
83
|
*/
|
|
48
84
|
class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
49
85
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @
|
|
52
|
-
*
|
|
53
|
-
* TreeCounter
|
|
54
|
-
* @
|
|
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.
|
|
86
|
+
* Create a TreeCounter and optionally bulk-insert items.
|
|
87
|
+
* @remarks Time O(N log N), Space O(N)
|
|
88
|
+
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
|
|
89
|
+
* @param [options] - Options for TreeCounter (comparator, reverse, map mode).
|
|
90
|
+
* @returns New TreeCounter instance.
|
|
57
91
|
*/
|
|
58
92
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
59
93
|
super([], options);
|
|
@@ -61,79 +95,42 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
61
95
|
if (keysNodesEntriesOrRaws)
|
|
62
96
|
this.addMany(keysNodesEntriesOrRaws);
|
|
63
97
|
}
|
|
64
|
-
// TODO the _count is not accurate after nodes count modified
|
|
65
98
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @
|
|
99
|
+
* Get the total aggregate count across all nodes.
|
|
100
|
+
* @remarks Time O(1), Space O(1)
|
|
101
|
+
* @returns Total count.
|
|
68
102
|
*/
|
|
69
103
|
get count() {
|
|
70
104
|
return this._count;
|
|
71
105
|
}
|
|
72
106
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
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.
|
|
107
|
+
* Compute the total count by traversing the tree (sums node.count).
|
|
108
|
+
* @remarks Time O(N), Space O(H)
|
|
109
|
+
* @returns Total count recomputed from nodes.
|
|
79
110
|
*/
|
|
80
111
|
getComputedCount() {
|
|
81
112
|
let sum = 0;
|
|
82
113
|
this.dfs(node => (sum += node ? node.count : 0));
|
|
83
114
|
return sum;
|
|
84
115
|
}
|
|
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) {
|
|
116
|
+
_createNode(key, value, color = 'BLACK', count) {
|
|
99
117
|
return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color);
|
|
100
118
|
}
|
|
101
119
|
/**
|
|
102
|
-
*
|
|
103
|
-
* @
|
|
104
|
-
*
|
|
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 {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
115
|
-
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
116
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
117
|
-
* an instance of the `TreeCounterNode` class.
|
|
120
|
+
* Type guard: check whether the input is a TreeCounterNode.
|
|
121
|
+
* @remarks Time O(1), Space O(1)
|
|
122
|
+
* @returns True if the value is a TreeCounterNode.
|
|
118
123
|
*/
|
|
119
124
|
isNode(keyNodeOrEntry) {
|
|
120
125
|
return keyNodeOrEntry instanceof TreeCounterNode;
|
|
121
126
|
}
|
|
122
127
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* @
|
|
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.
|
|
128
|
+
* Insert or increment a node and update aggregate count.
|
|
129
|
+
* @remarks Time O(log N), Space O(1)
|
|
130
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
131
|
+
* @param [value] - Value when a bare key is provided (ignored in map mode).
|
|
132
|
+
* @param [count] - How much to increase the node's count (default 1).
|
|
133
|
+
* @returns True if inserted/updated; false if ignored.
|
|
137
134
|
*/
|
|
138
135
|
add(keyNodeOrEntry, value, count = 1) {
|
|
139
136
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
@@ -148,19 +145,11 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
148
145
|
}
|
|
149
146
|
}
|
|
150
147
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} 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.
|
|
148
|
+
* Delete a node (or decrement its count) and rebalance if needed.
|
|
149
|
+
* @remarks Time O(log N), Space O(1)
|
|
150
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
|
|
151
|
+
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
152
|
+
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
164
153
|
*/
|
|
165
154
|
delete(keyNodeOrEntry, ignoreCount = false) {
|
|
166
155
|
if (keyNodeOrEntry === null)
|
|
@@ -252,7 +241,6 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
252
241
|
}
|
|
253
242
|
}
|
|
254
243
|
this._size--;
|
|
255
|
-
// If the original color was black, fix the tree
|
|
256
244
|
if (originalColor === 'BLACK') {
|
|
257
245
|
this._deleteFixup(replacementNode);
|
|
258
246
|
}
|
|
@@ -260,119 +248,111 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
260
248
|
return results;
|
|
261
249
|
}
|
|
262
250
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
267
|
-
* zero.
|
|
251
|
+
* Remove all nodes and reset aggregate counters.
|
|
252
|
+
* @remarks Time O(N), Space O(1)
|
|
253
|
+
* @returns void
|
|
268
254
|
*/
|
|
269
255
|
clear() {
|
|
270
256
|
super.clear();
|
|
271
257
|
this._count = 0;
|
|
272
258
|
}
|
|
273
259
|
/**
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
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.
|
|
260
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
261
|
+
* @remarks Time O(N), Space O(N)
|
|
262
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
263
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
285
264
|
*/
|
|
286
265
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
287
|
-
const
|
|
288
|
-
|
|
266
|
+
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
267
|
+
const n = nodes.length;
|
|
268
|
+
if (n < 1)
|
|
289
269
|
return false;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
270
|
+
let total = 0;
|
|
271
|
+
for (const nd of nodes)
|
|
272
|
+
total += nd ? nd.count : 0;
|
|
273
|
+
this._clearNodes();
|
|
274
|
+
const build = (l, r, parent) => {
|
|
275
|
+
if (l > r)
|
|
276
|
+
return undefined;
|
|
277
|
+
const m = l + ((r - l) >> 1);
|
|
278
|
+
const root = nodes[m];
|
|
279
|
+
const leftChild = build(l, m - 1, root);
|
|
280
|
+
const rightChild = build(m + 1, r, root);
|
|
281
|
+
root.left = leftChild;
|
|
282
|
+
root.right = rightChild;
|
|
283
|
+
root.parent = parent;
|
|
284
|
+
return root;
|
|
285
|
+
};
|
|
286
|
+
const newRoot = build(0, n - 1, undefined);
|
|
287
|
+
this._setRoot(newRoot);
|
|
288
|
+
this._size = n;
|
|
289
|
+
this._count = total;
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Create a new TreeCounter by mapping each [key, value] entry.
|
|
294
|
+
* @remarks Time O(N log N), Space O(N)
|
|
295
|
+
* @template MK
|
|
296
|
+
* @template MV
|
|
297
|
+
* @template MR
|
|
298
|
+
* @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
|
|
299
|
+
* @param [options] - Options for the output tree.
|
|
300
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
301
|
+
* @returns A new TreeCounter with mapped entries.
|
|
302
|
+
*/
|
|
303
|
+
map(callback, options, thisArg) {
|
|
304
|
+
const out = this._createLike([], options);
|
|
305
|
+
let index = 0;
|
|
306
|
+
for (const [key, value] of this) {
|
|
307
|
+
out.add(callback.call(thisArg, key, value, index++, this));
|
|
326
308
|
}
|
|
309
|
+
return out;
|
|
327
310
|
}
|
|
328
311
|
/**
|
|
329
|
-
*
|
|
330
|
-
*
|
|
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.
|
|
312
|
+
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
313
|
+
* @remarks Time O(N), Space O(N)
|
|
314
|
+
* @returns A deep copy of this tree.
|
|
334
315
|
*/
|
|
335
316
|
clone() {
|
|
336
|
-
const
|
|
337
|
-
this.
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
return cloned;
|
|
317
|
+
const out = this._createInstance();
|
|
318
|
+
this._clone(out);
|
|
319
|
+
out._count = this._count;
|
|
320
|
+
return out;
|
|
341
321
|
}
|
|
342
322
|
/**
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
* @
|
|
346
|
-
*
|
|
347
|
-
* @
|
|
348
|
-
*
|
|
349
|
-
*
|
|
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.
|
|
323
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
324
|
+
* @remarks Time O(1), Space O(1)
|
|
325
|
+
* @template TK
|
|
326
|
+
* @template TV
|
|
327
|
+
* @template TR
|
|
328
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
329
|
+
* @returns An empty like-kind instance.
|
|
356
330
|
*/
|
|
357
|
-
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
331
|
+
_createInstance(options) {
|
|
332
|
+
const Ctor = this.constructor;
|
|
333
|
+
return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
337
|
+
* @remarks Time O(N log N), Space O(N)
|
|
338
|
+
* @template TK
|
|
339
|
+
* @template TV
|
|
340
|
+
* @template TR
|
|
341
|
+
* @param iter - Iterable used to seed the new tree.
|
|
342
|
+
* @param [options] - Options merged with the current snapshot.
|
|
343
|
+
* @returns A like-kind TreeCounter built from the iterable.
|
|
344
|
+
*/
|
|
345
|
+
_createLike(iter = [], options) {
|
|
346
|
+
const Ctor = this.constructor;
|
|
347
|
+
return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
364
348
|
}
|
|
365
349
|
/**
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
* @param
|
|
369
|
-
*
|
|
370
|
-
* @param
|
|
371
|
-
*
|
|
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.
|
|
350
|
+
* (Protected) Normalize input into a node plus its effective value and count.
|
|
351
|
+
* @remarks Time O(1), Space O(1)
|
|
352
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry.
|
|
353
|
+
* @param [value] - Value used when a bare key is provided.
|
|
354
|
+
* @param [count] - Count increment to apply (default 1).
|
|
355
|
+
* @returns Tuple [node, value] where node may be undefined.
|
|
376
356
|
*/
|
|
377
357
|
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count = 1) {
|
|
378
358
|
if (keyNodeOrEntry === undefined || keyNodeOrEntry === null)
|
|
@@ -384,30 +364,23 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
384
364
|
if (key === undefined || key === null)
|
|
385
365
|
return [undefined, undefined];
|
|
386
366
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
387
|
-
return [this.
|
|
367
|
+
return [this._createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
388
368
|
}
|
|
389
|
-
return [this.
|
|
369
|
+
return [this._createNode(keyNodeOrEntry, value, 'BLACK', count), value];
|
|
390
370
|
}
|
|
391
371
|
/**
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
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.
|
|
372
|
+
* (Protected) Swap keys/values/counters between the source and destination nodes.
|
|
373
|
+
* @remarks Time O(1), Space O(1)
|
|
374
|
+
* @param srcNode - Source node (or key) whose properties will be moved.
|
|
375
|
+
* @param destNode - Destination node (or key) to receive properties.
|
|
376
|
+
* @returns Destination node after swap, or undefined.
|
|
404
377
|
*/
|
|
405
378
|
_swapProperties(srcNode, destNode) {
|
|
406
379
|
srcNode = this.ensureNode(srcNode);
|
|
407
380
|
destNode = this.ensureNode(destNode);
|
|
408
381
|
if (srcNode && destNode) {
|
|
409
382
|
const { key, value, count, color } = destNode;
|
|
410
|
-
const tempNode = this.
|
|
383
|
+
const tempNode = this._createNode(key, value, color, count);
|
|
411
384
|
if (tempNode) {
|
|
412
385
|
tempNode.color = color;
|
|
413
386
|
destNode.key = srcNode.key;
|
|
@@ -426,15 +399,11 @@ class TreeCounter extends red_black_tree_1.RedBlackTree {
|
|
|
426
399
|
return undefined;
|
|
427
400
|
}
|
|
428
401
|
/**
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
* @
|
|
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>`.
|
|
402
|
+
* (Protected) Replace one node by another and adjust counters accordingly.
|
|
403
|
+
* @remarks Time O(1), Space O(1)
|
|
404
|
+
* @param oldNode - Node being replaced.
|
|
405
|
+
* @param newNode - Replacement node.
|
|
406
|
+
* @returns The new node after replacement.
|
|
438
407
|
*/
|
|
439
408
|
_replaceNode(oldNode, newNode) {
|
|
440
409
|
newNode.count = oldNode.count + newNode.count;
|