doubly-linked-list-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/README.md +14 -14
- 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
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
|
|
8
9
|
import type {
|
|
9
10
|
BinaryTreeDeleteResult,
|
|
11
|
+
BinaryTreeOptions,
|
|
10
12
|
BSTNOptKeyOrNode,
|
|
11
13
|
EntryCallback,
|
|
12
14
|
IterationType,
|
|
@@ -14,23 +16,28 @@ import type {
|
|
|
14
16
|
RBTNColor,
|
|
15
17
|
TreeCounterOptions
|
|
16
18
|
} from '../../types';
|
|
19
|
+
import { BSTOptions } from '../../types';
|
|
20
|
+
import { BSTNode } from './bst';
|
|
17
21
|
import { IBinaryTree } from '../../interfaces';
|
|
18
22
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
19
23
|
|
|
24
|
+
/**
|
|
25
|
+
* RB-tree node with an extra 'count' field; keeps parent/child links.
|
|
26
|
+
* @remarks Time O(1), Space O(1)
|
|
27
|
+
* @template K
|
|
28
|
+
* @template V
|
|
29
|
+
*/
|
|
20
30
|
export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
21
31
|
override parent?: TreeCounterNode<K, V> = undefined;
|
|
22
32
|
|
|
23
33
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
*
|
|
27
|
-
* @param
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
31
|
-
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
32
|
-
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
33
|
-
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
34
|
+
* Create a tree counter node.
|
|
35
|
+
* @remarks Time O(1), Space O(1)
|
|
36
|
+
* @param key - Key of the node.
|
|
37
|
+
* @param [value] - Value associated with the key (ignored in map mode).
|
|
38
|
+
* @param [count] - Initial count for this node (default 1).
|
|
39
|
+
* @param color - Initial color ('RED' or 'BLACK').
|
|
40
|
+
* @returns New TreeCounterNode instance.
|
|
34
41
|
*/
|
|
35
42
|
constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
|
|
36
43
|
super(key, value, color);
|
|
@@ -39,10 +46,21 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
39
46
|
|
|
40
47
|
override _left?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
41
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Get the left child pointer.
|
|
51
|
+
* @remarks Time O(1), Space O(1)
|
|
52
|
+
* @returns Left child node, or null/undefined.
|
|
53
|
+
*/
|
|
42
54
|
override get left(): TreeCounterNode<K, V> | null | undefined {
|
|
43
55
|
return this._left;
|
|
44
56
|
}
|
|
45
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Set the left child and update its parent pointer.
|
|
60
|
+
* @remarks Time O(1), Space O(1)
|
|
61
|
+
* @param v - New left child node, or null/undefined.
|
|
62
|
+
* @returns void
|
|
63
|
+
*/
|
|
46
64
|
override set left(v: TreeCounterNode<K, V> | null | undefined) {
|
|
47
65
|
if (v) {
|
|
48
66
|
v.parent = this;
|
|
@@ -52,10 +70,21 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
52
70
|
|
|
53
71
|
override _right?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
54
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Get the right child pointer.
|
|
75
|
+
* @remarks Time O(1), Space O(1)
|
|
76
|
+
* @returns Right child node, or null/undefined.
|
|
77
|
+
*/
|
|
55
78
|
override get right(): TreeCounterNode<K, V> | null | undefined {
|
|
56
79
|
return this._right;
|
|
57
80
|
}
|
|
58
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Set the right child and update its parent pointer.
|
|
84
|
+
* @remarks Time O(1), Space O(1)
|
|
85
|
+
* @param v - New right child node, or null/undefined.
|
|
86
|
+
* @returns void
|
|
87
|
+
*/
|
|
59
88
|
override set right(v: TreeCounterNode<K, V> | null | undefined) {
|
|
60
89
|
if (v) {
|
|
61
90
|
v.parent = this;
|
|
@@ -65,20 +94,19 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
65
94
|
}
|
|
66
95
|
|
|
67
96
|
/**
|
|
68
|
-
*
|
|
97
|
+
* Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
|
|
98
|
+
* @remarks Time O(1), Space O(1)
|
|
99
|
+
* @template K
|
|
100
|
+
* @template V
|
|
101
|
+
* @template R
|
|
69
102
|
*/
|
|
70
|
-
export class TreeCounter<K = any, V = any, R =
|
|
71
|
-
extends RedBlackTree<K, V, R, MK, MV, MR>
|
|
72
|
-
implements IBinaryTree<K, V, R, MK, MV, MR>
|
|
73
|
-
{
|
|
103
|
+
export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
74
104
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @
|
|
77
|
-
*
|
|
78
|
-
* TreeCounter
|
|
79
|
-
* @
|
|
80
|
-
* behavior of the `TreeCounter` constructor. It can include properties such as `compareKeys` and
|
|
81
|
-
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
105
|
+
* Create a TreeCounter and optionally bulk-insert items.
|
|
106
|
+
* @remarks Time O(N log N), Space O(N)
|
|
107
|
+
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
|
|
108
|
+
* @param [options] - Options for TreeCounter (comparator, reverse, map mode).
|
|
109
|
+
* @returns New TreeCounter instance.
|
|
82
110
|
*/
|
|
83
111
|
constructor(
|
|
84
112
|
keysNodesEntriesOrRaws: Iterable<
|
|
@@ -92,71 +120,38 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
92
120
|
|
|
93
121
|
protected _count = 0;
|
|
94
122
|
|
|
95
|
-
// TODO the _count is not accurate after nodes count modified
|
|
96
123
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @
|
|
124
|
+
* Get the total aggregate count across all nodes.
|
|
125
|
+
* @remarks Time O(1), Space O(1)
|
|
126
|
+
* @returns Total count.
|
|
99
127
|
*/
|
|
128
|
+
|
|
100
129
|
get count(): number {
|
|
101
130
|
return this._count;
|
|
102
131
|
}
|
|
103
132
|
|
|
104
133
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
109
|
-
* search.
|
|
110
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
134
|
+
* Compute the total count by traversing the tree (sums node.count).
|
|
135
|
+
* @remarks Time O(N), Space O(H)
|
|
136
|
+
* @returns Total count recomputed from nodes.
|
|
111
137
|
*/
|
|
138
|
+
|
|
112
139
|
getComputedCount(): number {
|
|
113
140
|
let sum = 0;
|
|
114
141
|
this.dfs(node => (sum += node ? node.count : 0));
|
|
115
142
|
return sum;
|
|
116
143
|
}
|
|
117
144
|
|
|
118
|
-
|
|
119
|
-
* The function creates a new TreeCounterNode with the specified key, value, color, and count.
|
|
120
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
121
|
-
* which is a generic type representing the type of keys in the tree.
|
|
122
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
123
|
-
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
124
|
-
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
125
|
-
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
126
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
127
|
-
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
128
|
-
* with a key in the tree.
|
|
129
|
-
* @returns A new instance of the TreeCounterNode class, casted as TreeCounterNode<K, V>.
|
|
130
|
-
*/
|
|
131
|
-
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
|
|
145
|
+
override _createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
|
|
132
146
|
return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color) as TreeCounterNode<K, V>;
|
|
133
147
|
}
|
|
134
148
|
|
|
135
149
|
/**
|
|
136
|
-
*
|
|
137
|
-
* @
|
|
138
|
-
*
|
|
139
|
-
* R>`.
|
|
140
|
-
* @returns a new instance of the `TreeCounter` class, with the provided options merged with the
|
|
141
|
-
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
150
|
+
* Type guard: check whether the input is a TreeCounterNode.
|
|
151
|
+
* @remarks Time O(1), Space O(1)
|
|
152
|
+
* @returns True if the value is a TreeCounterNode.
|
|
142
153
|
*/
|
|
143
|
-
override createTree(options?: TreeCounterOptions<K, V, R>) {
|
|
144
|
-
return new TreeCounter<K, V, R, MK, MV, MR>([], {
|
|
145
|
-
iterationType: this.iterationType,
|
|
146
|
-
specifyComparable: this._specifyComparable,
|
|
147
|
-
isMapMode: this._isMapMode,
|
|
148
|
-
toEntryFn: this._toEntryFn,
|
|
149
|
-
...options
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
154
|
|
|
153
|
-
/**
|
|
154
|
-
* The function checks if the input is an instance of the TreeCounterNode class.
|
|
155
|
-
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
156
|
-
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
157
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
158
|
-
* an instance of the `TreeCounterNode` class.
|
|
159
|
-
*/
|
|
160
155
|
override isNode(
|
|
161
156
|
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
162
157
|
): keyNodeOrEntry is TreeCounterNode<K, V> {
|
|
@@ -164,20 +159,12 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
164
159
|
}
|
|
165
160
|
|
|
166
161
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* @
|
|
173
|
-
* `keyNodeOrEntry` parameter can accept one of the following types:
|
|
174
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
175
|
-
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
176
|
-
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
177
|
-
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
178
|
-
* for `count`, the key-value pair will be added once.
|
|
179
|
-
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
180
|
-
* was successful, and false otherwise.
|
|
162
|
+
* Insert or increment a node and update aggregate count.
|
|
163
|
+
* @remarks Time O(log N), Space O(1)
|
|
164
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
165
|
+
* @param [value] - Value when a bare key is provided (ignored in map mode).
|
|
166
|
+
* @param [count] - How much to increase the node's count (default 1).
|
|
167
|
+
* @returns True if inserted/updated; false if ignored.
|
|
181
168
|
*/
|
|
182
169
|
override add(
|
|
183
170
|
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -187,7 +174,6 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
187
174
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
188
175
|
const orgCount = newNode?.count || 0;
|
|
189
176
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
190
|
-
|
|
191
177
|
if (isSuccessAdded) {
|
|
192
178
|
this._count += orgCount;
|
|
193
179
|
return true;
|
|
@@ -197,19 +183,11 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
197
183
|
}
|
|
198
184
|
|
|
199
185
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
|
|
206
|
-
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
207
|
-
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
208
|
-
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
209
|
-
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
210
|
-
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
211
|
-
* `ignoreCount` is `false
|
|
212
|
-
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<TreeCounterNode<K, V>>` objects.
|
|
186
|
+
* Delete a node (or decrement its count) and rebalance if needed.
|
|
187
|
+
* @remarks Time O(log N), Space O(1)
|
|
188
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
|
|
189
|
+
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
190
|
+
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
213
191
|
*/
|
|
214
192
|
override delete(
|
|
215
193
|
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -222,7 +200,6 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
222
200
|
let nodeToDelete: OptNode<TreeCounterNode<K, V>>;
|
|
223
201
|
if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry);
|
|
224
202
|
else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
|
|
225
|
-
|
|
226
203
|
if (!nodeToDelete) {
|
|
227
204
|
return results;
|
|
228
205
|
}
|
|
@@ -259,7 +236,6 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
259
236
|
if (successor) {
|
|
260
237
|
originalColor = successor.color;
|
|
261
238
|
if (successor.right !== null) replacementNode = successor.right;
|
|
262
|
-
|
|
263
239
|
if (successor.parent === nodeToDelete) {
|
|
264
240
|
if (this.isRealNode(replacementNode)) {
|
|
265
241
|
replacementNode.parent = successor;
|
|
@@ -298,8 +274,6 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
298
274
|
}
|
|
299
275
|
}
|
|
300
276
|
this._size--;
|
|
301
|
-
|
|
302
|
-
// If the original color was black, fix the tree
|
|
303
277
|
if (originalColor === 'BLACK') {
|
|
304
278
|
this._deleteFixup(replacementNode);
|
|
305
279
|
}
|
|
@@ -310,11 +284,9 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
310
284
|
}
|
|
311
285
|
|
|
312
286
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
317
|
-
* zero.
|
|
287
|
+
* Remove all nodes and reset aggregate counters.
|
|
288
|
+
* @remarks Time O(N), Space O(1)
|
|
289
|
+
* @returns void
|
|
318
290
|
*/
|
|
319
291
|
override clear() {
|
|
320
292
|
super.clear();
|
|
@@ -322,111 +294,125 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
322
294
|
}
|
|
323
295
|
|
|
324
296
|
/**
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* tree using either a recursive or iterative approach.
|
|
330
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
331
|
-
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
332
|
-
* default value of `this.iterationType`, which means it will use the iteration type specified by the
|
|
333
|
-
* `iterationType` property of the current object.
|
|
334
|
-
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
335
|
-
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
297
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
298
|
+
* @remarks Time O(N), Space O(N)
|
|
299
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
300
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
336
301
|
*/
|
|
337
302
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
if (
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
const m = l + Math.floor((r - l) / 2);
|
|
365
|
-
const midNode = sorted[m];
|
|
366
|
-
if (this._isMapMode && midNode !== null) this.add(midNode.key, undefined, midNode.count);
|
|
367
|
-
else if (midNode !== null) this.add(midNode.key, midNode.value, midNode.count);
|
|
368
|
-
stack.push([m + 1, r]);
|
|
369
|
-
stack.push([l, m - 1]);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
return true;
|
|
374
|
-
}
|
|
303
|
+
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
304
|
+
const n = nodes.length;
|
|
305
|
+
if (n < 1) return false;
|
|
306
|
+
|
|
307
|
+
let total = 0;
|
|
308
|
+
for (const nd of nodes) total += nd ? nd.count : 0;
|
|
309
|
+
|
|
310
|
+
this._clearNodes();
|
|
311
|
+
|
|
312
|
+
const build = (l: number, r: number, parent?: TreeCounterNode<K, V>): TreeCounterNode<K, V> | undefined => {
|
|
313
|
+
if (l > r) return undefined;
|
|
314
|
+
const m = l + ((r - l) >> 1);
|
|
315
|
+
const root = nodes[m]! as TreeCounterNode<K, V>;
|
|
316
|
+
const leftChild = build(l, m - 1, root);
|
|
317
|
+
const rightChild = build(m + 1, r, root);
|
|
318
|
+
root.left = leftChild;
|
|
319
|
+
root.right = rightChild;
|
|
320
|
+
root.parent = parent;
|
|
321
|
+
return root;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const newRoot = build(0, n - 1, undefined);
|
|
325
|
+
this._setRoot(newRoot);
|
|
326
|
+
this._size = n;
|
|
327
|
+
this._count = total;
|
|
328
|
+
return true;
|
|
375
329
|
}
|
|
376
330
|
|
|
377
331
|
/**
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
* @
|
|
332
|
+
* Create a new TreeCounter by mapping each [key, value] entry.
|
|
333
|
+
* @remarks Time O(N log N), Space O(N)
|
|
334
|
+
* @template MK
|
|
335
|
+
* @template MV
|
|
336
|
+
* @template MR
|
|
337
|
+
* @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
|
|
338
|
+
* @param [options] - Options for the output tree.
|
|
339
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
340
|
+
* @returns A new TreeCounter with mapped entries.
|
|
383
341
|
*/
|
|
384
|
-
override
|
|
385
|
-
const cloned = this.createTree();
|
|
386
|
-
this.bfs(node => cloned.add(node === null ? null : node.key, undefined, node === null ? 0 : node.count));
|
|
387
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
388
|
-
return cloned;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* The `map` function in TypeScript overrides the default behavior to create a new TreeCounter with
|
|
393
|
-
* modified entries based on a provided callback.
|
|
394
|
-
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
395
|
-
* map. It takes four arguments:
|
|
396
|
-
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
397
|
-
* `TreeCounterOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
398
|
-
* options when creating a new `TreeCounter` instance within the `map` function. These options could
|
|
399
|
-
* include things like
|
|
400
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
401
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
402
|
-
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
403
|
-
* @returns A new TreeCounter instance is being returned, which is populated with entries generated
|
|
404
|
-
* by the provided callback function.
|
|
405
|
-
*/
|
|
406
|
-
override map(
|
|
342
|
+
override map<MK = K, MV = V, MR = any>(
|
|
407
343
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
408
|
-
options?:
|
|
409
|
-
thisArg?:
|
|
344
|
+
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
345
|
+
thisArg?: unknown
|
|
410
346
|
): TreeCounter<MK, MV, MR> {
|
|
411
|
-
const
|
|
347
|
+
const out = this._createLike<MK, MV, MR>([], options);
|
|
348
|
+
|
|
412
349
|
let index = 0;
|
|
413
350
|
for (const [key, value] of this) {
|
|
414
|
-
|
|
351
|
+
out.add(callback.call(thisArg, key, value, index++, this));
|
|
415
352
|
}
|
|
416
|
-
return
|
|
353
|
+
return out;
|
|
417
354
|
}
|
|
418
355
|
|
|
419
356
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* @
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
357
|
+
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
358
|
+
* @remarks Time O(N), Space O(N)
|
|
359
|
+
* @returns A deep copy of this tree.
|
|
360
|
+
*/
|
|
361
|
+
override clone(): this {
|
|
362
|
+
const out = this._createInstance<K, V, R>();
|
|
363
|
+
this._clone(out as unknown as any);
|
|
364
|
+
(out as any)._count = (this as any)._count;
|
|
365
|
+
return out as unknown as this;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
370
|
+
* @remarks Time O(1), Space O(1)
|
|
371
|
+
* @template TK
|
|
372
|
+
* @template TV
|
|
373
|
+
* @template TR
|
|
374
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
375
|
+
* @returns An empty like-kind instance.
|
|
376
|
+
*/
|
|
377
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
378
|
+
const Ctor = this.constructor as unknown as new (
|
|
379
|
+
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
380
|
+
opts?: BSTOptions<TK, TV, TR>
|
|
381
|
+
) => this;
|
|
382
|
+
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
387
|
+
* @remarks Time O(N log N), Space O(N)
|
|
388
|
+
* @template TK
|
|
389
|
+
* @template TV
|
|
390
|
+
* @template TR
|
|
391
|
+
* @param iter - Iterable used to seed the new tree.
|
|
392
|
+
* @param [options] - Options merged with the current snapshot.
|
|
393
|
+
* @returns A like-kind TreeCounter built from the iterable.
|
|
394
|
+
*/
|
|
395
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
396
|
+
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
397
|
+
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
398
|
+
): TreeCounter<TK, TV, TR> {
|
|
399
|
+
const Ctor = this.constructor as unknown as new (
|
|
400
|
+
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
401
|
+
opts?: BSTOptions<TK, TV, TR>
|
|
402
|
+
) => TreeCounter<TK, TV, TR>;
|
|
403
|
+
return new Ctor(iter as unknown as Iterable<TK | any>, {
|
|
404
|
+
...this._snapshotOptions<TK, TV, TR>(),
|
|
405
|
+
...(options ?? {})
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* (Protected) Normalize input into a node plus its effective value and count.
|
|
411
|
+
* @remarks Time O(1), Space O(1)
|
|
412
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry.
|
|
413
|
+
* @param [value] - Value used when a bare key is provided.
|
|
414
|
+
* @param [count] - Count increment to apply (default 1).
|
|
415
|
+
* @returns Tuple [node, value] where node may be undefined.
|
|
430
416
|
*/
|
|
431
417
|
protected override _keyValueNodeOrEntryToNodeAndValue(
|
|
432
418
|
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -441,25 +427,18 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
441
427
|
const [key, entryValue] = keyNodeOrEntry;
|
|
442
428
|
if (key === undefined || key === null) return [undefined, undefined];
|
|
443
429
|
const finalValue = value ?? entryValue;
|
|
444
|
-
return [this.
|
|
430
|
+
return [this._createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
445
431
|
}
|
|
446
432
|
|
|
447
|
-
return [this.
|
|
433
|
+
return [this._createNode(keyNodeOrEntry, value, 'BLACK', count), value];
|
|
448
434
|
}
|
|
449
435
|
|
|
450
436
|
/**
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
* @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} srcNode - The `srcNode` parameter represents the source node
|
|
457
|
-
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
458
|
-
* instance of the `BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>` class.
|
|
459
|
-
* @param {R | BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>} destNode - The `destNode` parameter represents the destination
|
|
460
|
-
* node where the properties will be swapped with the source node.
|
|
461
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
462
|
-
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
437
|
+
* (Protected) Swap keys/values/counters between the source and destination nodes.
|
|
438
|
+
* @remarks Time O(1), Space O(1)
|
|
439
|
+
* @param srcNode - Source node (or key) whose properties will be moved.
|
|
440
|
+
* @param destNode - Destination node (or key) to receive properties.
|
|
441
|
+
* @returns Destination node after swap, or undefined.
|
|
463
442
|
*/
|
|
464
443
|
protected override _swapProperties(
|
|
465
444
|
srcNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>,
|
|
@@ -469,7 +448,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
469
448
|
destNode = this.ensureNode(destNode);
|
|
470
449
|
if (srcNode && destNode) {
|
|
471
450
|
const { key, value, count, color } = destNode;
|
|
472
|
-
const tempNode = this.
|
|
451
|
+
const tempNode = this._createNode(key, value, color, count);
|
|
473
452
|
if (tempNode) {
|
|
474
453
|
tempNode.color = color;
|
|
475
454
|
|
|
@@ -490,16 +469,13 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
490
469
|
}
|
|
491
470
|
|
|
492
471
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* @
|
|
498
|
-
* structure.
|
|
499
|
-
* @param {TreeCounterNode<K, V>} newNode - The `newNode` parameter is an instance of the `TreeCounterNode<K, V>` class.
|
|
500
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
501
|
-
* superclass, which is of type `TreeCounterNode<K, V>`.
|
|
472
|
+
* (Protected) Replace one node by another and adjust counters accordingly.
|
|
473
|
+
* @remarks Time O(1), Space O(1)
|
|
474
|
+
* @param oldNode - Node being replaced.
|
|
475
|
+
* @param newNode - Replacement node.
|
|
476
|
+
* @returns The new node after replacement.
|
|
502
477
|
*/
|
|
478
|
+
|
|
503
479
|
protected override _replaceNode(
|
|
504
480
|
oldNode: TreeCounterNode<K, V>,
|
|
505
481
|
newNode: TreeCounterNode<K, V>
|