min-heap-typed 1.39.6 → 1.40.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/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -21,8 +21,7 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
26
25
|
/**
|
|
27
26
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
28
27
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -56,7 +55,6 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
56
55
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
57
56
|
*/
|
|
58
57
|
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
59
|
-
// TODO support node as a param
|
|
60
58
|
const inserted = super.add(keyOrNode, value);
|
|
61
59
|
if (inserted) this._balancePath(inserted);
|
|
62
60
|
return inserted;
|
|
@@ -162,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
162
160
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
163
161
|
switch (
|
|
164
162
|
this._balanceFactor(A) // second O(1)
|
|
165
|
-
|
|
163
|
+
) {
|
|
166
164
|
case -2:
|
|
167
165
|
if (A && A.left) {
|
|
168
166
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
20
|
+
constructor({frequency = 0, max}: { frequency?: number; max: number }) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|
|
@@ -31,30 +31,18 @@ export class BinaryIndexedTree {
|
|
|
31
31
|
return this._freqMap;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
set freqMap(value: Record<number, number>) {
|
|
35
|
-
this._freqMap = value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
34
|
protected _msb: number;
|
|
39
35
|
|
|
40
36
|
get msb(): number {
|
|
41
37
|
return this._msb;
|
|
42
38
|
}
|
|
43
39
|
|
|
44
|
-
set msb(value: number) {
|
|
45
|
-
this._msb = value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
40
|
protected _negativeCount: number;
|
|
49
41
|
|
|
50
42
|
get negativeCount(): number {
|
|
51
43
|
return this._negativeCount;
|
|
52
44
|
}
|
|
53
45
|
|
|
54
|
-
set negativeCount(value: number) {
|
|
55
|
-
this._negativeCount = value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
46
|
get freq(): number {
|
|
59
47
|
return this._freq;
|
|
60
48
|
}
|
|
@@ -232,9 +220,9 @@ export class BinaryIndexedTree {
|
|
|
232
220
|
*/
|
|
233
221
|
protected _updateNegativeCount(freqCur: number, freqNew: number): void {
|
|
234
222
|
if (freqCur < 0 && freqNew >= 0) {
|
|
235
|
-
this.
|
|
223
|
+
this._negativeCount--;
|
|
236
224
|
} else if (freqCur >= 0 && freqNew < 0) {
|
|
237
|
-
this.
|
|
225
|
+
this._negativeCount++;
|
|
238
226
|
}
|
|
239
227
|
}
|
|
240
228
|
|
|
@@ -43,7 +43,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
43
43
|
this.value = value;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
protected _left: N | null | undefined;
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* Get the left child node.
|
|
@@ -63,7 +63,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
63
63
|
this._left = v;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
protected _right: N | null | undefined;
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Get the right child node.
|
|
@@ -108,8 +108,9 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
112
|
-
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
112
|
+
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
|
+
|
|
113
114
|
/**
|
|
114
115
|
* Creates a new instance of BinaryTree.
|
|
115
116
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -117,28 +118,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
117
118
|
constructor(options?: BinaryTreeOptions) {
|
|
118
119
|
if (options !== undefined) {
|
|
119
120
|
const {iterationType = IterationType.ITERATIVE} = options;
|
|
120
|
-
this.
|
|
121
|
+
this.iterationType = iterationType;
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Get the iteration type used in the binary tree.
|
|
128
|
-
*/
|
|
129
|
-
get iterationType(): IterationType {
|
|
130
|
-
return this._iterationType;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Set the iteration type for the binary tree.
|
|
135
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
136
|
-
*/
|
|
137
|
-
set iterationType(v: IterationType) {
|
|
138
|
-
this._iterationType = v;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private _root: N | null = null;
|
|
125
|
+
protected _root: N | null = null;
|
|
142
126
|
|
|
143
127
|
/**
|
|
144
128
|
* Get the root node of the binary tree.
|
|
@@ -147,7 +131,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
147
131
|
return this._root;
|
|
148
132
|
}
|
|
149
133
|
|
|
150
|
-
|
|
134
|
+
protected _size = 0;
|
|
151
135
|
|
|
152
136
|
/**
|
|
153
137
|
* Get the number of nodes in the binary tree.
|
|
@@ -170,7 +154,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
170
154
|
* Clear the binary tree, removing all nodes.
|
|
171
155
|
*/
|
|
172
156
|
clear() {
|
|
173
|
-
this.
|
|
157
|
+
this._setRoot(null);
|
|
174
158
|
this._size = 0;
|
|
175
159
|
}
|
|
176
160
|
|
|
@@ -229,9 +213,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
229
213
|
} else {
|
|
230
214
|
this._setRoot(needInsert);
|
|
231
215
|
if (needInsert !== null) {
|
|
232
|
-
this.
|
|
216
|
+
this._size = 1;
|
|
233
217
|
} else {
|
|
234
|
-
this.
|
|
218
|
+
this._size = 0;
|
|
235
219
|
}
|
|
236
220
|
inserted = this.root;
|
|
237
221
|
}
|
|
@@ -339,7 +323,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
339
323
|
}
|
|
340
324
|
}
|
|
341
325
|
}
|
|
342
|
-
this.
|
|
326
|
+
this._size = this.size - 1;
|
|
343
327
|
|
|
344
328
|
bstDeletedResult.push({deleted: orgCurrent, needBalanced});
|
|
345
329
|
return bstDeletedResult;
|
|
@@ -401,7 +385,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
401
385
|
return -1;
|
|
402
386
|
}
|
|
403
387
|
|
|
404
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
388
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
405
389
|
let maxHeight = 0;
|
|
406
390
|
|
|
407
391
|
while (stack.length > 0) {
|
|
@@ -904,7 +888,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
904
888
|
_traverse(beginRoot);
|
|
905
889
|
} else {
|
|
906
890
|
// 0: visit, 1: print
|
|
907
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
891
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
908
892
|
|
|
909
893
|
while (stack.length > 0) {
|
|
910
894
|
const cur = stack.pop();
|
|
@@ -1174,7 +1158,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1174
1158
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1175
1159
|
* binary tree nodes in a specific order.
|
|
1176
1160
|
*/
|
|
1177
|
-
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1161
|
+
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1178
1162
|
if (!node) {
|
|
1179
1163
|
return;
|
|
1180
1164
|
}
|
|
@@ -1244,13 +1228,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1244
1228
|
if (parent.left === undefined) {
|
|
1245
1229
|
parent.left = newNode;
|
|
1246
1230
|
if (newNode) {
|
|
1247
|
-
this.
|
|
1231
|
+
this._size = this.size + 1;
|
|
1248
1232
|
}
|
|
1249
1233
|
return parent.left;
|
|
1250
1234
|
} else if (parent.right === undefined) {
|
|
1251
1235
|
parent.right = newNode;
|
|
1252
1236
|
if (newNode) {
|
|
1253
|
-
this.
|
|
1237
|
+
this._size = this.size + 1;
|
|
1254
1238
|
}
|
|
1255
1239
|
return parent.right;
|
|
1256
1240
|
} else {
|
|
@@ -1274,14 +1258,5 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1274
1258
|
this._root = v;
|
|
1275
1259
|
}
|
|
1276
1260
|
|
|
1277
|
-
/**
|
|
1278
|
-
* The function sets the value of the protected property "_size" to the given number.
|
|
1279
|
-
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
1280
|
-
* set.
|
|
1281
|
-
*/
|
|
1282
|
-
protected _setSize(v: number) {
|
|
1283
|
-
this._size = v;
|
|
1284
|
-
}
|
|
1285
|
-
|
|
1286
1261
|
// --- end additional methods ---
|
|
1287
1262
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey} from '../../types';
|
|
9
9
|
import {CP, IterationType} from '../../types';
|
|
10
10
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
@@ -19,8 +19,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
19
19
|
|
|
20
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
21
21
|
extends BinaryTree<V, N>
|
|
22
|
-
implements IBinaryTree<V, N>
|
|
23
|
-
{
|
|
22
|
+
implements IBinaryTree<V, N> {
|
|
24
23
|
/**
|
|
25
24
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
26
25
|
* function.
|
|
@@ -72,7 +71,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
72
71
|
}
|
|
73
72
|
if (this.root === null) {
|
|
74
73
|
this._setRoot(newNode);
|
|
75
|
-
this.
|
|
74
|
+
this._size = this.size + 1;
|
|
76
75
|
inserted = this.root;
|
|
77
76
|
} else {
|
|
78
77
|
let cur = this.root;
|
|
@@ -94,7 +93,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
94
93
|
}
|
|
95
94
|
//Add to the left of the current node
|
|
96
95
|
cur.left = newNode;
|
|
97
|
-
this.
|
|
96
|
+
this._size = this.size + 1;
|
|
98
97
|
traversing = false;
|
|
99
98
|
inserted = cur.left;
|
|
100
99
|
} else {
|
|
@@ -109,7 +108,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
109
108
|
}
|
|
110
109
|
//Add to the right of the current node
|
|
111
110
|
cur.right = newNode;
|
|
112
|
-
this.
|
|
111
|
+
this._size = this.size + 1;
|
|
113
112
|
traversing = false;
|
|
114
113
|
inserted = cur.right;
|
|
115
114
|
} else {
|
|
@@ -5,24 +5,16 @@ import {BST, BSTNode} from './bst';
|
|
|
5
5
|
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
6
|
constructor(key: BTNKey, value?: V) {
|
|
7
7
|
super(key, value);
|
|
8
|
-
this.
|
|
8
|
+
this.color = RBColor.RED;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
color: RBColor;
|
|
12
12
|
|
|
13
|
-
get color(): RBColor {
|
|
14
|
-
return this._color;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
set color(value: RBColor) {
|
|
18
|
-
this._color = value;
|
|
19
|
-
}
|
|
20
13
|
}
|
|
21
14
|
|
|
22
15
|
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
23
16
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
17
|
+
implements IBinaryTree<V, N> {
|
|
26
18
|
constructor(options?: RBTreeOptions) {
|
|
27
19
|
super(options);
|
|
28
20
|
}
|
|
@@ -38,7 +30,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
38
30
|
// }
|
|
39
31
|
//
|
|
40
32
|
// // Method for fixing insert violations in a red-black tree
|
|
41
|
-
//
|
|
33
|
+
// protected _fixInsertViolation(node: N) {
|
|
42
34
|
// while (node !== this.root! && node.color === RBColor.RED && node.parent!.color === RBColor.RED) {
|
|
43
35
|
// const parent = node.parent!;
|
|
44
36
|
// const grandparent = parent.parent!;
|
|
@@ -101,7 +93,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
101
93
|
// }
|
|
102
94
|
//
|
|
103
95
|
// // Left rotation operation
|
|
104
|
-
//
|
|
96
|
+
// protected _rotateLeft(node: N) {
|
|
105
97
|
// const rightChild = node.right;
|
|
106
98
|
// node.right = rightChild!.left;
|
|
107
99
|
//
|
|
@@ -125,7 +117,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
125
117
|
// }
|
|
126
118
|
//
|
|
127
119
|
// // Right rotation operation
|
|
128
|
-
//
|
|
120
|
+
// protected _rotateRight(node: N) {
|
|
129
121
|
// const leftChild = node.left;
|
|
130
122
|
// node.left = leftChild!.right;
|
|
131
123
|
//
|
|
@@ -148,12 +140,12 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
148
140
|
// node.parent = leftChild;
|
|
149
141
|
// }
|
|
150
142
|
//
|
|
151
|
-
//
|
|
143
|
+
// protected _isNodeRed(node: N | null | undefined): boolean {
|
|
152
144
|
// return node ? node.color === RBColor.RED : false;
|
|
153
145
|
// }
|
|
154
146
|
//
|
|
155
147
|
// // Find the sibling node
|
|
156
|
-
//
|
|
148
|
+
// protected _findSibling(node: N): N | null | undefined {
|
|
157
149
|
// if (!node.parent) {
|
|
158
150
|
// return undefined;
|
|
159
151
|
// }
|
|
@@ -162,7 +154,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
162
154
|
// }
|
|
163
155
|
//
|
|
164
156
|
// // Remove a node
|
|
165
|
-
//
|
|
157
|
+
// protected _removeNode(node: N, replacement: N | null | undefined): void {
|
|
166
158
|
// if (node === this.root && !replacement) {
|
|
167
159
|
// // If there's only the root node and no replacement, simply delete the root node
|
|
168
160
|
// this._setRoot(null);
|
|
@@ -230,7 +222,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
230
222
|
// }
|
|
231
223
|
//
|
|
232
224
|
// // Repair operation after node deletion
|
|
233
|
-
//
|
|
225
|
+
// protected _fixDeleteViolation(node: N | null | undefined) {
|
|
234
226
|
// let sibling;
|
|
235
227
|
//
|
|
236
228
|
// while (node && node !== this.root && !this._isNodeRed(node)) {
|
|
@@ -327,7 +319,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
327
319
|
// }
|
|
328
320
|
// }
|
|
329
321
|
//
|
|
330
|
-
//
|
|
322
|
+
// protected _findMin(node: N): N {
|
|
331
323
|
// while (node.left) {
|
|
332
324
|
// node = node.left;
|
|
333
325
|
// }
|
|
@@ -335,7 +327,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
335
327
|
// }
|
|
336
328
|
//
|
|
337
329
|
// // Get the replacement node
|
|
338
|
-
//
|
|
330
|
+
// protected _getReplacementNode(node: N): N | null | undefined {
|
|
339
331
|
// if (node.left && node.right) {
|
|
340
332
|
// return this._findSuccessor(node);
|
|
341
333
|
// }
|
|
@@ -348,7 +340,7 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
348
340
|
// }
|
|
349
341
|
//
|
|
350
342
|
// // Find the successor node
|
|
351
|
-
//
|
|
343
|
+
// protected _findSuccessor(node: N): N | null | undefined {
|
|
352
344
|
// if (node.right) {
|
|
353
345
|
// // If the node has a right child, find the minimum node in the right subtree as the successor
|
|
354
346
|
// return this._findMin(node.right);
|
|
@@ -9,70 +9,18 @@
|
|
|
9
9
|
import type {SegmentTreeNodeVal} from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
private _start = 0;
|
|
20
|
-
get start(): number {
|
|
21
|
-
return this._start;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
set start(v: number) {
|
|
25
|
-
this._start = v;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private _end = 0;
|
|
29
|
-
|
|
30
|
-
get end(): number {
|
|
31
|
-
return this._end;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set end(v: number) {
|
|
35
|
-
this._end = v;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private _value: SegmentTreeNodeVal | null = null;
|
|
39
|
-
|
|
40
|
-
get value(): SegmentTreeNodeVal | null {
|
|
41
|
-
return this._value;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
set value(v: SegmentTreeNodeVal | null) {
|
|
45
|
-
this._value = v;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private _sum = 0;
|
|
12
|
+
start = 0;
|
|
13
|
+
end = 0;
|
|
14
|
+
value: SegmentTreeNodeVal | null = null;
|
|
15
|
+
sum = 0;
|
|
16
|
+
left: SegmentTreeNode | null = null;
|
|
17
|
+
right: SegmentTreeNode | null = null;
|
|
49
18
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this._sum = v;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private _left: SegmentTreeNode | null = null;
|
|
59
|
-
|
|
60
|
-
get left(): SegmentTreeNode | null {
|
|
61
|
-
return this._left;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
set left(v: SegmentTreeNode | null) {
|
|
65
|
-
this._left = v;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private _right: SegmentTreeNode | null = null;
|
|
69
|
-
|
|
70
|
-
get right(): SegmentTreeNode | null {
|
|
71
|
-
return this._right;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
set right(v: SegmentTreeNode | null) {
|
|
75
|
-
this._right = v;
|
|
19
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
|
|
20
|
+
this.start = start;
|
|
21
|
+
this.end = end;
|
|
22
|
+
this.sum = sum;
|
|
23
|
+
this.value = value || null;
|
|
76
24
|
}
|
|
77
25
|
}
|
|
78
26
|
|
|
@@ -101,24 +49,25 @@ export class SegmentTree {
|
|
|
101
49
|
}
|
|
102
50
|
}
|
|
103
51
|
|
|
104
|
-
|
|
52
|
+
protected _values: number[] = [];
|
|
105
53
|
|
|
106
54
|
get values(): number[] {
|
|
107
55
|
return this._values;
|
|
108
56
|
}
|
|
109
57
|
|
|
110
|
-
|
|
58
|
+
protected _start = 0;
|
|
59
|
+
|
|
111
60
|
get start(): number {
|
|
112
61
|
return this._start;
|
|
113
62
|
}
|
|
114
63
|
|
|
115
|
-
|
|
64
|
+
protected _end: number;
|
|
116
65
|
|
|
117
66
|
get end(): number {
|
|
118
67
|
return this._end;
|
|
119
68
|
}
|
|
120
69
|
|
|
121
|
-
|
|
70
|
+
protected _root: SegmentTreeNode | null;
|
|
122
71
|
|
|
123
72
|
get root(): SegmentTreeNode | null {
|
|
124
73
|
return this._root;
|
|
@@ -238,20 +187,4 @@ export class SegmentTree {
|
|
|
238
187
|
};
|
|
239
188
|
return dfs(root, indexA, indexB);
|
|
240
189
|
}
|
|
241
|
-
|
|
242
|
-
protected _setValues(value: number[]) {
|
|
243
|
-
this._values = value;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
protected _setStart(value: number) {
|
|
247
|
-
this._start = value;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
protected _setEnd(value: number) {
|
|
251
|
-
this._end = value;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
protected _setRoot(v: SegmentTreeNode | null) {
|
|
255
|
-
this._root = v;
|
|
256
|
-
}
|
|
257
190
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {BTNKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
|
-
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType
|
|
9
|
+
import {BinaryTreeDeletedResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -37,8 +37,7 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
41
|
-
{
|
|
40
|
+
implements IBinaryTree<V, N> {
|
|
42
41
|
/**
|
|
43
42
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
44
43
|
* merge duplicated values.
|
|
@@ -93,7 +92,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
93
92
|
}
|
|
94
93
|
if (!this.root) {
|
|
95
94
|
this._setRoot(newNode);
|
|
96
|
-
this.
|
|
95
|
+
this._size = this.size + 1;
|
|
97
96
|
newNode && this._setCount(this.count + newNode.count);
|
|
98
97
|
inserted = this.root;
|
|
99
98
|
} else {
|
|
@@ -113,7 +112,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
113
112
|
if (cur.left === undefined) {
|
|
114
113
|
//Add to the left of the current node
|
|
115
114
|
cur.left = newNode;
|
|
116
|
-
this.
|
|
115
|
+
this._size = this.size + 1;
|
|
117
116
|
this._setCount(this.count + newNode.count);
|
|
118
117
|
|
|
119
118
|
traversing = false;
|
|
@@ -127,7 +126,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
127
126
|
if (cur.right === undefined) {
|
|
128
127
|
//Add to the right of the current node
|
|
129
128
|
cur.right = newNode;
|
|
130
|
-
this.
|
|
129
|
+
this._size = this.size + 1;
|
|
131
130
|
this._setCount(this.count + newNode.count);
|
|
132
131
|
|
|
133
132
|
traversing = false;
|
|
@@ -162,7 +161,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
162
161
|
if (parent.left === undefined) {
|
|
163
162
|
parent.left = newNode;
|
|
164
163
|
if (newNode !== null) {
|
|
165
|
-
this.
|
|
164
|
+
this._size = this.size + 1;
|
|
166
165
|
this._setCount(this.count + newNode.count);
|
|
167
166
|
}
|
|
168
167
|
|
|
@@ -170,7 +169,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
170
169
|
} else if (parent.right === undefined) {
|
|
171
170
|
parent.right = newNode;
|
|
172
171
|
if (newNode !== null) {
|
|
173
|
-
this.
|
|
172
|
+
this._size = (this.size + 1);
|
|
174
173
|
this._setCount(this.count + newNode.count);
|
|
175
174
|
}
|
|
176
175
|
return parent.right;
|
|
@@ -321,7 +320,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
321
320
|
}
|
|
322
321
|
}
|
|
323
322
|
}
|
|
324
|
-
this.
|
|
323
|
+
this._size = this.size - 1;
|
|
325
324
|
// TODO How to handle when the count of target node is lesser than current node's count
|
|
326
325
|
this._setCount(this.count - orgCurrent.count);
|
|
327
326
|
}
|