bst-typed 1.50.7 → 1.50.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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +14 -3
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +33 -34
- package/dist/data-structures/binary-tree/bst.d.ts +22 -3
- package/dist/data-structures/binary-tree/bst.js +78 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/rb-tree.js +47 -50
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -28
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +5 -5
- package/dist/types/common.d.ts +6 -29
- package/dist/types/common.js +0 -40
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +20 -7
- package/src/data-structures/binary-tree/binary-tree.ts +54 -45
- package/src/data-structures/binary-tree/bst.ts +86 -42
- package/src/data-structures/binary-tree/rb-tree.ts +49 -49
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -29
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/types/common.ts +6 -30
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
|
@@ -9,12 +9,13 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
+
IterationType,
|
|
12
13
|
KeyOrNodeOrEntry,
|
|
13
14
|
TreeMultiMapNested,
|
|
14
15
|
TreeMultiMapNodeNested,
|
|
15
16
|
TreeMultiMapOptions
|
|
16
17
|
} from '../../types';
|
|
17
|
-
import {
|
|
18
|
+
import { RBTNColor } from '../../types';
|
|
18
19
|
import { IBinaryTree } from '../../interfaces';
|
|
19
20
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
20
21
|
|
|
@@ -24,17 +25,19 @@ export class TreeMultiMapNode<
|
|
|
24
25
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
|
|
25
26
|
> extends RedBlackTreeNode<K, V, NODE> {
|
|
26
27
|
/**
|
|
27
|
-
* The constructor function initializes
|
|
28
|
-
* @param {K} key - The key parameter
|
|
29
|
-
*
|
|
30
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* 1.
|
|
28
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
29
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
30
|
+
* used to identify and locate the node within the tree.
|
|
31
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
32
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
33
|
+
* creating a new node.
|
|
34
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
35
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
36
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
37
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
35
38
|
*/
|
|
36
|
-
constructor(key: K, value?: V, count = 1) {
|
|
37
|
-
super(key, value);
|
|
39
|
+
constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
|
|
40
|
+
super(key, value, color);
|
|
38
41
|
this.count = count;
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -91,25 +94,41 @@ export class TreeMultiMap<
|
|
|
91
94
|
return this._count;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Time Complexity: O(n)
|
|
99
|
+
* Space Complexity: O(1)
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(n)
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
106
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
107
|
+
* search.
|
|
108
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
109
|
+
*/
|
|
110
|
+
getComputedCount(): number {
|
|
95
111
|
let sum = 0;
|
|
96
112
|
this.dfs(node => (sum += node.count));
|
|
97
113
|
return sum;
|
|
98
114
|
}
|
|
99
115
|
|
|
100
116
|
/**
|
|
101
|
-
* The function creates a new TreeMultiMapNode
|
|
117
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
102
118
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
103
|
-
* which is a generic type
|
|
104
|
-
* @param {V} [value] - The `value` parameter
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* default
|
|
109
|
-
* @
|
|
119
|
+
* which is a generic type representing the key type of the node.
|
|
120
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
121
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
122
|
+
* function. If provided, it should be of type `V`.
|
|
123
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
124
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
125
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
126
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
127
|
+
* with a key in the tree.
|
|
128
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
110
129
|
*/
|
|
111
|
-
override createNode(key: K, value?: V, count?: number): NODE {
|
|
112
|
-
return new TreeMultiMapNode(key, value, count) as NODE;
|
|
130
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
|
|
131
|
+
return new TreeMultiMapNode(key, value, count, color) as NODE;
|
|
113
132
|
}
|
|
114
133
|
|
|
115
134
|
/**
|
|
@@ -153,10 +172,10 @@ export class TreeMultiMap<
|
|
|
153
172
|
if (key === undefined || key === null) {
|
|
154
173
|
return;
|
|
155
174
|
} else {
|
|
156
|
-
node = this.createNode(key, value, count);
|
|
175
|
+
node = this.createNode(key, value, 'BLACK', count);
|
|
157
176
|
}
|
|
158
177
|
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
159
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
178
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
160
179
|
} else {
|
|
161
180
|
return;
|
|
162
181
|
}
|
|
@@ -314,7 +333,7 @@ export class TreeMultiMap<
|
|
|
314
333
|
this._size--;
|
|
315
334
|
|
|
316
335
|
// If the original color was black, fix the tree
|
|
317
|
-
if (originalColor ===
|
|
336
|
+
if (originalColor === 'BLACK') {
|
|
318
337
|
this._deleteFixup(replacementNode);
|
|
319
338
|
}
|
|
320
339
|
|
|
@@ -356,14 +375,14 @@ export class TreeMultiMap<
|
|
|
356
375
|
* values:
|
|
357
376
|
* @returns a boolean value.
|
|
358
377
|
*/
|
|
359
|
-
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
360
|
-
const sorted = this.dfs(node => node, '
|
|
378
|
+
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
379
|
+
const sorted = this.dfs(node => node, 'IN'),
|
|
361
380
|
n = sorted.length;
|
|
362
381
|
if (sorted.length < 1) return false;
|
|
363
382
|
|
|
364
383
|
this.clear();
|
|
365
384
|
|
|
366
|
-
if (iterationType ===
|
|
385
|
+
if (iterationType === 'RECURSIVE') {
|
|
367
386
|
const buildBalanceBST = (l: number, r: number) => {
|
|
368
387
|
if (l > r) return;
|
|
369
388
|
const m = l + Math.floor((r - l) / 2);
|
|
@@ -431,7 +450,7 @@ export class TreeMultiMap<
|
|
|
431
450
|
destNode = this.ensureNode(destNode);
|
|
432
451
|
if (srcNode && destNode) {
|
|
433
452
|
const { key, value, count, color } = destNode;
|
|
434
|
-
const tempNode = this.createNode(key, value, count);
|
|
453
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
435
454
|
if (tempNode) {
|
|
436
455
|
tempNode.color = color;
|
|
437
456
|
|
|
@@ -247,10 +247,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
247
247
|
* Space Complexity: O(log n)
|
|
248
248
|
*
|
|
249
249
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
250
|
-
* @param order - Traverse order parameter: '
|
|
250
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
251
251
|
* @returns An array containing elements traversed in the specified order.
|
|
252
252
|
*/
|
|
253
|
-
dfs(order: DFSOrderPattern = '
|
|
253
|
+
dfs(order: DFSOrderPattern = 'PRE'): E[] {
|
|
254
254
|
const result: E[] = [];
|
|
255
255
|
|
|
256
256
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
@@ -258,15 +258,15 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
258
258
|
const left = 2 * index + 1,
|
|
259
259
|
right = left + 1;
|
|
260
260
|
if (index < this.size) {
|
|
261
|
-
if (order === '
|
|
261
|
+
if (order === 'IN') {
|
|
262
262
|
_dfs(left);
|
|
263
263
|
result.push(this.elements[index]);
|
|
264
264
|
_dfs(right);
|
|
265
|
-
} else if (order === '
|
|
265
|
+
} else if (order === 'PRE') {
|
|
266
266
|
result.push(this.elements[index]);
|
|
267
267
|
_dfs(left);
|
|
268
268
|
_dfs(right);
|
|
269
|
-
} else if (order === '
|
|
269
|
+
} else if (order === 'POST') {
|
|
270
270
|
_dfs(left);
|
|
271
271
|
_dfs(right);
|
|
272
272
|
result.push(this.elements[index]);
|
package/src/types/common.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
INVERSE = 'INVERSE'
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export enum CP {
|
|
7
|
-
lt = 'lt',
|
|
8
|
-
eq = 'eq',
|
|
9
|
-
gt = 'gt'
|
|
10
|
-
}
|
|
1
|
+
export type BSTVariant = 'STANDARD' | 'INVERSE';
|
|
2
|
+
export type CP = 'LT' | 'EQ' | 'GT';
|
|
11
3
|
|
|
12
4
|
/**
|
|
13
5
|
* Enum representing different loop types.
|
|
@@ -15,24 +7,13 @@ export enum CP {
|
|
|
15
7
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
16
8
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
17
9
|
*/
|
|
18
|
-
export
|
|
19
|
-
ITERATIVE = 'ITERATIVE',
|
|
20
|
-
RECURSIVE = 'RECURSIVE'
|
|
21
|
-
}
|
|
10
|
+
export type IterationType = 'ITERATIVE' | 'RECURSIVE';
|
|
22
11
|
|
|
23
|
-
export
|
|
24
|
-
ROOT = 'ROOT',
|
|
25
|
-
LEFT = 'LEFT',
|
|
26
|
-
RIGHT = 'RIGHT',
|
|
27
|
-
ROOT_LEFT = 'ROOT_LEFT',
|
|
28
|
-
ROOT_RIGHT = 'ROOT_RIGHT',
|
|
29
|
-
ISOLATED = 'ISOLATED',
|
|
30
|
-
MAL_NODE = 'MAL_NODE'
|
|
31
|
-
}
|
|
12
|
+
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
|
|
32
13
|
|
|
33
14
|
export type Comparator<K> = (a: K, b: K) => number;
|
|
34
15
|
|
|
35
|
-
export type DFSOrderPattern = '
|
|
16
|
+
export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
36
17
|
|
|
37
18
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
38
19
|
|
|
@@ -64,9 +45,4 @@ export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
|
|
64
45
|
|
|
65
46
|
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
|
66
47
|
|
|
67
|
-
export
|
|
68
|
-
CREATED = 'CREATED',
|
|
69
|
-
READ = 'READ',
|
|
70
|
-
UPDATED = 'UPDATED',
|
|
71
|
-
DELETED = 'DELETED'
|
|
72
|
-
}
|
|
48
|
+
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import type { BSTOptions } from "./bst";
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export type RBTNColor = 'RED' | 'BLACK';
|
|
5
5
|
|
|
6
6
|
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
7
|
|