data-structure-typed 1.34.7 → 1.34.9
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/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -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 {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
9
|
import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
|
|
10
10
|
import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
@@ -15,29 +15,21 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|
|
15
15
|
implements ITreeMultisetNode<V, NEIGHBOR>
|
|
16
16
|
{
|
|
17
17
|
/**
|
|
18
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
19
|
-
* @param {
|
|
18
|
+
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
19
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
20
20
|
* of the binary tree node.
|
|
21
21
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
22
22
|
* tree node. If no value is provided, it will be `undefined`.
|
|
23
23
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
24
24
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
25
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class
|
|
25
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
26
26
|
*/
|
|
27
|
-
constructor(
|
|
28
|
-
super(
|
|
29
|
-
this.
|
|
27
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
|
|
28
|
+
super(key, val);
|
|
29
|
+
this.count = count;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
get count(): number {
|
|
35
|
-
return this._count;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
set count(v: number) {
|
|
39
|
-
this._count = v;
|
|
40
|
-
}
|
|
32
|
+
count: number;
|
|
41
33
|
}
|
|
42
34
|
|
|
43
35
|
/**
|
|
@@ -54,7 +46,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
54
46
|
* TreeMultiset.
|
|
55
47
|
*/
|
|
56
48
|
constructor(options?: TreeMultisetOptions) {
|
|
57
|
-
super(
|
|
49
|
+
super(options);
|
|
58
50
|
}
|
|
59
51
|
|
|
60
52
|
private _count = 0;
|
|
@@ -64,16 +56,16 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
64
56
|
}
|
|
65
57
|
|
|
66
58
|
/**
|
|
67
|
-
* The function creates a new BSTNode with the given
|
|
68
|
-
* @param {
|
|
59
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
60
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
69
61
|
* distinguish one node from another in the tree.
|
|
70
62
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
71
63
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
72
64
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
73
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
65
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
74
66
|
*/
|
|
75
|
-
override createNode(
|
|
76
|
-
return new TreeMultisetNode(
|
|
67
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
|
|
68
|
+
return new TreeMultisetNode(key, val, count) as N;
|
|
77
69
|
}
|
|
78
70
|
|
|
79
71
|
/**
|
|
@@ -84,17 +76,17 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
84
76
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
85
77
|
*/
|
|
86
78
|
override swapLocation(srcNode: N, destNode: N): N {
|
|
87
|
-
const {
|
|
88
|
-
const tempNode = this.createNode(
|
|
79
|
+
const {key, val, count, height} = destNode;
|
|
80
|
+
const tempNode = this.createNode(key, val, count);
|
|
89
81
|
if (tempNode) {
|
|
90
82
|
tempNode.height = height;
|
|
91
83
|
|
|
92
|
-
destNode.
|
|
84
|
+
destNode.key = srcNode.key;
|
|
93
85
|
destNode.val = srcNode.val;
|
|
94
86
|
destNode.count = srcNode.count;
|
|
95
87
|
destNode.height = srcNode.height;
|
|
96
88
|
|
|
97
|
-
srcNode.
|
|
89
|
+
srcNode.key = tempNode.key;
|
|
98
90
|
srcNode.val = tempNode.val;
|
|
99
91
|
srcNode.count = tempNode.count;
|
|
100
92
|
srcNode.height = tempNode.height;
|
|
@@ -106,23 +98,22 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
106
98
|
/**
|
|
107
99
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
108
100
|
* necessary.
|
|
109
|
-
* @param {
|
|
101
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
110
102
|
* represents a `BinaryTreeNode`).
|
|
111
103
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
112
104
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
113
105
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
114
106
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
115
107
|
*/
|
|
116
|
-
override add(
|
|
117
|
-
count = count ?? 1;
|
|
108
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
|
|
118
109
|
let inserted: N | null | undefined = undefined,
|
|
119
110
|
newNode: N | null;
|
|
120
|
-
if (
|
|
121
|
-
newNode = this.createNode(
|
|
122
|
-
} else if (
|
|
111
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
112
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
|
113
|
+
} else if (keyOrNode === null) {
|
|
123
114
|
newNode = null;
|
|
124
115
|
} else {
|
|
125
|
-
newNode = this.createNode(
|
|
116
|
+
newNode = this.createNode(keyOrNode, val, count);
|
|
126
117
|
}
|
|
127
118
|
if (!this.root) {
|
|
128
119
|
this._setRoot(newNode);
|
|
@@ -135,13 +126,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
135
126
|
while (traversing) {
|
|
136
127
|
if (cur) {
|
|
137
128
|
if (newNode) {
|
|
138
|
-
if (this._compare(cur.
|
|
129
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
139
130
|
cur.val = newNode.val;
|
|
140
131
|
cur.count += newNode.count;
|
|
141
132
|
this._setCount(this.count + newNode.count);
|
|
142
133
|
traversing = false;
|
|
143
134
|
inserted = cur;
|
|
144
|
-
} else if (this._compare(cur.
|
|
135
|
+
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
145
136
|
// Traverse left of the node
|
|
146
137
|
if (cur.left === undefined) {
|
|
147
138
|
//Add to the left of the current node
|
|
@@ -155,7 +146,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
155
146
|
//Traverse the left of the current node
|
|
156
147
|
if (cur.left) cur = cur.left;
|
|
157
148
|
}
|
|
158
|
-
} else if (this._compare(cur.
|
|
149
|
+
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
159
150
|
// Traverse right of the node
|
|
160
151
|
if (cur.right === undefined) {
|
|
161
152
|
//Add to the right of the current node
|
|
@@ -219,33 +210,33 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
219
210
|
/**
|
|
220
211
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
221
212
|
* the inserted nodes.
|
|
222
|
-
* @param {(
|
|
213
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
223
214
|
* objects, or null values.
|
|
224
215
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
225
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
216
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
226
217
|
* method. If provided, the `data` array should
|
|
227
218
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
228
219
|
*/
|
|
229
220
|
override addMany(
|
|
230
|
-
|
|
221
|
+
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
231
222
|
data?: N['val'][]
|
|
232
223
|
): (N | null | undefined)[] {
|
|
233
224
|
const inserted: (N | null | undefined)[] = [];
|
|
234
225
|
|
|
235
|
-
for (let i = 0; i <
|
|
236
|
-
const
|
|
226
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
227
|
+
const keyOrNode = keysOrNodes[i];
|
|
237
228
|
|
|
238
|
-
if (
|
|
239
|
-
inserted.push(this.add(
|
|
229
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
230
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
|
240
231
|
continue;
|
|
241
232
|
}
|
|
242
233
|
|
|
243
|
-
if (
|
|
234
|
+
if (keyOrNode === null) {
|
|
244
235
|
inserted.push(this.add(NaN, null, 0));
|
|
245
236
|
continue;
|
|
246
237
|
}
|
|
247
238
|
|
|
248
|
-
inserted.push(this.add(
|
|
239
|
+
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
249
240
|
}
|
|
250
241
|
return inserted;
|
|
251
242
|
}
|
|
@@ -256,7 +247,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
256
247
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
257
248
|
*/
|
|
258
249
|
override perfectlyBalance(): boolean {
|
|
259
|
-
const sorted = this.
|
|
250
|
+
const sorted = this.dfs('in', 'node'),
|
|
260
251
|
n = sorted.length;
|
|
261
252
|
if (sorted.length < 1) return false;
|
|
262
253
|
|
|
@@ -267,7 +258,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
267
258
|
if (l > r) return;
|
|
268
259
|
const m = l + Math.floor((r - l) / 2);
|
|
269
260
|
const midNode = sorted[m];
|
|
270
|
-
this.add(midNode.
|
|
261
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
271
262
|
buildBalanceBST(l, m - 1);
|
|
272
263
|
buildBalanceBST(m + 1, r);
|
|
273
264
|
};
|
|
@@ -283,7 +274,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
283
274
|
if (l <= r) {
|
|
284
275
|
const m = l + Math.floor((r - l) / 2);
|
|
285
276
|
const midNode = sorted[m];
|
|
286
|
-
this.add(midNode.
|
|
277
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
287
278
|
stack.push([m + 1, r]);
|
|
288
279
|
stack.push([l, m - 1]);
|
|
289
280
|
}
|
|
@@ -296,17 +287,17 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
296
287
|
/**
|
|
297
288
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
298
289
|
* node that needs to be balanced.
|
|
299
|
-
* @param {N |
|
|
290
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
300
291
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
301
292
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
302
293
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
303
294
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
304
295
|
*/
|
|
305
|
-
override remove(
|
|
296
|
+
override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
|
|
306
297
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
307
298
|
if (!this.root) return bstDeletedResult;
|
|
308
299
|
|
|
309
|
-
const curr: N | null = this.get(
|
|
300
|
+
const curr: N | null = this.get(nodeOrKey);
|
|
310
301
|
if (!curr) return bstDeletedResult;
|
|
311
302
|
|
|
312
303
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -397,13 +388,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
397
388
|
/**
|
|
398
389
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
399
390
|
* recursively or iteratively.
|
|
400
|
-
* @param {N |
|
|
401
|
-
* in a binary tree. It can be either a `
|
|
391
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
392
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
402
393
|
* `null` if the subtree is empty.
|
|
403
394
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
404
395
|
*/
|
|
405
|
-
subTreeSumCount(subTreeRoot: N |
|
|
406
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
396
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number {
|
|
397
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
407
398
|
|
|
408
399
|
if (!subTreeRoot) return 0;
|
|
409
400
|
|
|
@@ -434,15 +425,15 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
434
425
|
/**
|
|
435
426
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
436
427
|
* the `count` property of each node.
|
|
437
|
-
* @param {N |
|
|
438
|
-
* in a binary tree. It can be either a `
|
|
428
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
429
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
439
430
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
440
431
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
441
432
|
* in the subtree should be increased or decreased.
|
|
442
433
|
* @returns a boolean value.
|
|
443
434
|
*/
|
|
444
|
-
subTreeAddCount(subTreeRoot: N |
|
|
445
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, '
|
|
435
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean {
|
|
436
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
446
437
|
|
|
447
438
|
if (!subTreeRoot) return false;
|
|
448
439
|
|
|
@@ -476,14 +467,14 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
476
467
|
/**
|
|
477
468
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
478
469
|
* using a queue.
|
|
479
|
-
* @param {
|
|
470
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
480
471
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
481
472
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
482
473
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
483
474
|
* to `true`, the function will return only one node. If `onlyOne`
|
|
484
475
|
* @returns an array of nodes that match the given nodeProperty.
|
|
485
476
|
*/
|
|
486
|
-
getNodesByCount(nodeProperty:
|
|
477
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne = false): N[] {
|
|
487
478
|
if (!this.root) return [];
|
|
488
479
|
const result: N[] = [];
|
|
489
480
|
|
|
@@ -522,10 +513,10 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
522
513
|
/**
|
|
523
514
|
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
524
515
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
525
|
-
*
|
|
516
|
+
* bfs traversal.
|
|
526
517
|
*/
|
|
527
518
|
BFSCount(): number[] {
|
|
528
|
-
const nodes = super.
|
|
519
|
+
const nodes = super.bfs('node');
|
|
529
520
|
return nodes.map(node => node.count);
|
|
530
521
|
}
|
|
531
522
|
|
|
@@ -549,56 +540,53 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
549
540
|
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
550
541
|
* @returns The function `morrisCount` returns an array of numbers.
|
|
551
542
|
*/
|
|
552
|
-
morrisCount(pattern
|
|
553
|
-
pattern = pattern || 'in';
|
|
543
|
+
morrisCount(pattern: DFSOrderPattern = 'in'): number[] {
|
|
554
544
|
const nodes = super.morris(pattern, 'node');
|
|
555
545
|
return nodes.map(node => node.count);
|
|
556
546
|
}
|
|
557
547
|
|
|
558
548
|
/**
|
|
559
|
-
* The function
|
|
549
|
+
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
560
550
|
* the specified traversal pattern.
|
|
561
551
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
562
|
-
* the Depth-First Search (
|
|
563
|
-
* @returns The
|
|
564
|
-
* in the
|
|
552
|
+
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
553
|
+
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
554
|
+
* in the dfs traversal.
|
|
565
555
|
*/
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
const nodes = super.DFSIterative(pattern, 'node');
|
|
556
|
+
dfsCountIterative(pattern: DFSOrderPattern = 'in'): number[] {
|
|
557
|
+
const nodes = super.dfsIterative(pattern, 'node');
|
|
569
558
|
return nodes.map(node => node.count);
|
|
570
559
|
}
|
|
571
560
|
|
|
572
561
|
/**
|
|
573
|
-
* The
|
|
562
|
+
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
574
563
|
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
575
|
-
* the Depth-First Search (
|
|
576
|
-
* @returns The
|
|
564
|
+
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
565
|
+
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
577
566
|
* traversal.
|
|
578
567
|
*/
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
const nodes = super.DFS(pattern, 'node');
|
|
568
|
+
dfsCount(pattern: DFSOrderPattern = 'in'): number[] {
|
|
569
|
+
const nodes = super.dfs(pattern, 'node');
|
|
582
570
|
return nodes.map(node => node.count);
|
|
583
571
|
}
|
|
584
572
|
|
|
585
573
|
/**
|
|
586
574
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
587
575
|
* value than a given node.
|
|
588
|
-
* @param {N |
|
|
576
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
589
577
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
590
578
|
*/
|
|
591
|
-
lesserSumCount(beginNode: N |
|
|
592
|
-
if (typeof beginNode === 'number') beginNode = this.get(beginNode, '
|
|
579
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number {
|
|
580
|
+
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key');
|
|
593
581
|
if (!beginNode) return 0;
|
|
594
582
|
if (!this.root) return 0;
|
|
595
|
-
const
|
|
583
|
+
const key = beginNode.key;
|
|
596
584
|
|
|
597
585
|
let sum = 0;
|
|
598
586
|
|
|
599
587
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
600
588
|
const _traverse = (cur: N): void => {
|
|
601
|
-
const compared = this._compare(cur.
|
|
589
|
+
const compared = this._compare(cur.key, key);
|
|
602
590
|
if (compared === CP.eq) {
|
|
603
591
|
if (cur.right) sum += this.subTreeSumCount(cur.right);
|
|
604
592
|
return;
|
|
@@ -619,7 +607,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
619
607
|
while (queue.length > 0) {
|
|
620
608
|
const cur = queue.shift();
|
|
621
609
|
if (cur) {
|
|
622
|
-
const compared = this._compare(cur.
|
|
610
|
+
const compared = this._compare(cur.key, key);
|
|
623
611
|
if (compared === CP.eq) {
|
|
624
612
|
if (cur.right) sum += this.subTreeSumCount(cur.right);
|
|
625
613
|
return sum;
|
|
@@ -643,25 +631,25 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
643
631
|
/**
|
|
644
632
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
645
633
|
* greater than a given ID by a specified delta value.
|
|
646
|
-
* @param {N |
|
|
634
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
647
635
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
648
636
|
* of each node should be increased.
|
|
649
637
|
* @returns a boolean value.
|
|
650
638
|
*/
|
|
651
|
-
allGreaterNodesAddCount(node: N |
|
|
652
|
-
if (typeof node === 'number') node = this.get(node, '
|
|
639
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean {
|
|
640
|
+
if (typeof node === 'number') node = this.get(node, 'key');
|
|
653
641
|
if (!node) return false;
|
|
654
|
-
const
|
|
642
|
+
const key = node.key;
|
|
655
643
|
if (!this.root) return false;
|
|
656
644
|
|
|
657
645
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
658
646
|
const _traverse = (cur: N) => {
|
|
659
|
-
const compared = this._compare(cur.
|
|
647
|
+
const compared = this._compare(cur.key, key);
|
|
660
648
|
if (compared === CP.gt) cur.count += delta;
|
|
661
649
|
|
|
662
650
|
if (!cur.left && !cur.right) return;
|
|
663
|
-
if (cur.left && this._compare(cur.left.
|
|
664
|
-
if (cur.right && this._compare(cur.right.
|
|
651
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) _traverse(cur.left);
|
|
652
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) _traverse(cur.right);
|
|
665
653
|
};
|
|
666
654
|
|
|
667
655
|
_traverse(this.root);
|
|
@@ -671,11 +659,11 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
671
659
|
while (queue.length > 0) {
|
|
672
660
|
const cur = queue.shift();
|
|
673
661
|
if (cur) {
|
|
674
|
-
const compared = this._compare(cur.
|
|
662
|
+
const compared = this._compare(cur.key, key);
|
|
675
663
|
if (compared === CP.gt) cur.count += delta;
|
|
676
664
|
|
|
677
|
-
if (cur.left && this._compare(cur.left.
|
|
678
|
-
if (cur.right && this._compare(cur.right.
|
|
665
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) queue.push(cur.left);
|
|
666
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) queue.push(cur.right);
|
|
679
667
|
}
|
|
680
668
|
}
|
|
681
669
|
return true;
|