graph-typed 1.50.8 → 1.51.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-multi-map.d.ts +15 -3
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +14 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/binary-tree.js +11 -11
- package/dist/data-structures/binary-tree/bst.d.ts +19 -2
- package/dist/data-structures/binary-tree/bst.js +20 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/rb-tree.js +42 -44
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +40 -22
- package/dist/data-structures/binary-tree/tree-multi-map.js +43 -27
- 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 +1 -1
- 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 +17 -3
- package/src/data-structures/binary-tree/binary-tree.ts +36 -24
- package/src/data-structures/binary-tree/bst.ts +32 -11
- package/src/data-structures/binary-tree/rb-tree.ts +44 -44
- package/src/data-structures/binary-tree/tree-multi-map.ts +46 -27
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
|
@@ -5,21 +5,24 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
+
import { RBTNColor } from '../../types';
|
|
9
10
|
import { IBinaryTree } from '../../interfaces';
|
|
10
11
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
11
12
|
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
12
13
|
/**
|
|
13
|
-
* The constructor function initializes
|
|
14
|
-
* @param {K} key - The key parameter
|
|
15
|
-
*
|
|
16
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* 1.
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
15
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
16
|
+
* used to identify and locate the node within the tree.
|
|
17
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
18
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
19
|
+
* creating a new node.
|
|
20
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
21
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
22
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
23
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
24
|
+
*/
|
|
25
|
+
constructor(key: K, value?: V, count?: number, color?: RBTNColor);
|
|
23
26
|
protected _count: number;
|
|
24
27
|
/**
|
|
25
28
|
* The function returns the value of the private variable _count.
|
|
@@ -50,19 +53,34 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
50
53
|
* @returns the sum of the count property of all nodes in the tree.
|
|
51
54
|
*/
|
|
52
55
|
get count(): number;
|
|
53
|
-
getMutableCount(): number;
|
|
54
56
|
/**
|
|
55
|
-
*
|
|
57
|
+
* Time Complexity: O(n)
|
|
58
|
+
* Space Complexity: O(1)
|
|
59
|
+
*/
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(n)
|
|
62
|
+
* Space Complexity: O(1)
|
|
63
|
+
*
|
|
64
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
65
|
+
* search.
|
|
66
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
67
|
+
*/
|
|
68
|
+
getComputedCount(): number;
|
|
69
|
+
/**
|
|
70
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
56
71
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
57
|
-
* which is a generic type
|
|
58
|
-
* @param {V} [value] - The `value` parameter
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* default
|
|
63
|
-
* @
|
|
72
|
+
* which is a generic type representing the key type of the node.
|
|
73
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
74
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
75
|
+
* function. If provided, it should be of type `V`.
|
|
76
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
77
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
78
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
79
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
80
|
+
* with a key in the tree.
|
|
81
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
64
82
|
*/
|
|
65
|
-
createNode(key: K, value?: V, count?: number): NODE;
|
|
83
|
+
createNode(key: K, value?: V, color?: RBTNColor, count?: number): NODE;
|
|
66
84
|
/**
|
|
67
85
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
68
86
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
@@ -163,7 +181,7 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
163
181
|
* values:
|
|
164
182
|
* @returns a boolean value.
|
|
165
183
|
*/
|
|
166
|
-
perfectlyBalance(iterationType?:
|
|
184
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
167
185
|
/**
|
|
168
186
|
* Time complexity: O(n)
|
|
169
187
|
* Space complexity: O(n)
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const rb_tree_1 = require("./rb-tree");
|
|
6
5
|
class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
7
6
|
/**
|
|
8
|
-
* The constructor function initializes
|
|
9
|
-
* @param {K} key - The key parameter
|
|
10
|
-
*
|
|
11
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* 1.
|
|
7
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
8
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
9
|
+
* used to identify and locate the node within the tree.
|
|
10
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
11
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
12
|
+
* creating a new node.
|
|
13
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
14
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
15
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
16
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
16
17
|
*/
|
|
17
|
-
constructor(key, value, count = 1) {
|
|
18
|
-
super(key, value);
|
|
18
|
+
constructor(key, value, count = 1, color = 'BLACK') {
|
|
19
|
+
super(key, value, color);
|
|
19
20
|
this._count = 1;
|
|
20
21
|
this.count = count;
|
|
21
22
|
}
|
|
@@ -60,24 +61,39 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
60
61
|
get count() {
|
|
61
62
|
return this._count;
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(n)
|
|
66
|
+
* Space Complexity: O(1)
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Time Complexity: O(n)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*
|
|
72
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
73
|
+
* search.
|
|
74
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
75
|
+
*/
|
|
76
|
+
getComputedCount() {
|
|
64
77
|
let sum = 0;
|
|
65
78
|
this.dfs(node => (sum += node.count));
|
|
66
79
|
return sum;
|
|
67
80
|
}
|
|
68
81
|
/**
|
|
69
|
-
* The function creates a new TreeMultiMapNode
|
|
82
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
70
83
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
71
|
-
* which is a generic type
|
|
72
|
-
* @param {V} [value] - The `value` parameter
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* default
|
|
77
|
-
* @
|
|
84
|
+
* which is a generic type representing the key type of the node.
|
|
85
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
86
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
87
|
+
* function. If provided, it should be of type `V`.
|
|
88
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
89
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
90
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
91
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
92
|
+
* with a key in the tree.
|
|
93
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
78
94
|
*/
|
|
79
|
-
createNode(key, value, count) {
|
|
80
|
-
return new TreeMultiMapNode(key, value, count);
|
|
95
|
+
createNode(key, value, color = 'BLACK', count) {
|
|
96
|
+
return new TreeMultiMapNode(key, value, count, color);
|
|
81
97
|
}
|
|
82
98
|
/**
|
|
83
99
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
@@ -115,11 +131,11 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
115
131
|
return;
|
|
116
132
|
}
|
|
117
133
|
else {
|
|
118
|
-
node = this.createNode(key, value, count);
|
|
134
|
+
node = this.createNode(key, value, 'BLACK', count);
|
|
119
135
|
}
|
|
120
136
|
}
|
|
121
137
|
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
122
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
138
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
123
139
|
}
|
|
124
140
|
else {
|
|
125
141
|
return;
|
|
@@ -271,7 +287,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
271
287
|
}
|
|
272
288
|
this._size--;
|
|
273
289
|
// If the original color was black, fix the tree
|
|
274
|
-
if (originalColor ===
|
|
290
|
+
if (originalColor === 'BLACK') {
|
|
275
291
|
this._deleteFixup(replacementNode);
|
|
276
292
|
}
|
|
277
293
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
@@ -308,7 +324,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
308
324
|
* @returns a boolean value.
|
|
309
325
|
*/
|
|
310
326
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
311
|
-
const sorted = this.dfs(node => node, '
|
|
327
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
312
328
|
if (sorted.length < 1)
|
|
313
329
|
return false;
|
|
314
330
|
this.clear();
|
|
@@ -375,7 +391,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
375
391
|
destNode = this.ensureNode(destNode);
|
|
376
392
|
if (srcNode && destNode) {
|
|
377
393
|
const { key, value, count, color } = destNode;
|
|
378
|
-
const tempNode = this.createNode(key, value, count);
|
|
394
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
379
395
|
if (tempNode) {
|
|
380
396
|
tempNode.color = color;
|
|
381
397
|
destNode.key = srcNode.key;
|
|
@@ -159,7 +159,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
159
159
|
* Space Complexity: O(log n)
|
|
160
160
|
*
|
|
161
161
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
162
|
-
* @param order - Traverse order parameter: '
|
|
162
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
163
163
|
* @returns An array containing elements traversed in the specified order.
|
|
164
164
|
*/
|
|
165
165
|
dfs(order?: DFSOrderPattern): E[];
|
|
@@ -229,26 +229,26 @@ class Heap extends base_1.IterableElementBase {
|
|
|
229
229
|
* Space Complexity: O(log n)
|
|
230
230
|
*
|
|
231
231
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
232
|
-
* @param order - Traverse order parameter: '
|
|
232
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
233
233
|
* @returns An array containing elements traversed in the specified order.
|
|
234
234
|
*/
|
|
235
|
-
dfs(order = '
|
|
235
|
+
dfs(order = 'PRE') {
|
|
236
236
|
const result = [];
|
|
237
237
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
238
238
|
const _dfs = (index) => {
|
|
239
239
|
const left = 2 * index + 1, right = left + 1;
|
|
240
240
|
if (index < this.size) {
|
|
241
|
-
if (order === '
|
|
241
|
+
if (order === 'IN') {
|
|
242
242
|
_dfs(left);
|
|
243
243
|
result.push(this.elements[index]);
|
|
244
244
|
_dfs(right);
|
|
245
245
|
}
|
|
246
|
-
else if (order === '
|
|
246
|
+
else if (order === 'PRE') {
|
|
247
247
|
result.push(this.elements[index]);
|
|
248
248
|
_dfs(left);
|
|
249
249
|
_dfs(right);
|
|
250
250
|
}
|
|
251
|
-
else if (order === '
|
|
251
|
+
else if (order === 'POST') {
|
|
252
252
|
_dfs(left);
|
|
253
253
|
_dfs(right);
|
|
254
254
|
result.push(this.elements[index]);
|
package/dist/types/common.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type CP = 'LT' | 'EQ' | 'GT';
|
|
|
9
9
|
export type IterationType = 'ITERATIVE' | 'RECURSIVE';
|
|
10
10
|
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
|
|
11
11
|
export type Comparator<K> = (a: K, b: K) => number;
|
|
12
|
-
export type DFSOrderPattern = '
|
|
12
|
+
export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
13
13
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
14
14
|
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
15
15
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import type { BSTOptions } from "./bst";
|
|
3
|
-
export
|
|
4
|
-
RED = 1,
|
|
5
|
-
BLACK = 0
|
|
6
|
-
}
|
|
3
|
+
export type RBTNColor = 'RED' | 'BLACK';
|
|
7
4
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
5
|
export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
9
6
|
export type RBTreeOptions<K> = Omit<BSTOptions<K>, 'variant'> & {};
|
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RBTNColor = void 0;
|
|
4
|
-
var RBTNColor;
|
|
5
|
-
(function (RBTNColor) {
|
|
6
|
-
RBTNColor[RBTNColor["RED"] = 1] = "RED";
|
|
7
|
-
RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
|
|
8
|
-
})(RBTNColor = exports.RBTNColor || (exports.RBTNColor = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.51.0",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -136,6 +136,6 @@
|
|
|
136
136
|
"typescript": "^4.9.5"
|
|
137
137
|
},
|
|
138
138
|
"dependencies": {
|
|
139
|
-
"data-structure-typed": "^1.
|
|
139
|
+
"data-structure-typed": "^1.51.0"
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
14
|
BTNCallback,
|
|
15
|
+
IterationType,
|
|
15
16
|
KeyOrNodeOrEntry
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -85,7 +86,20 @@ export class AVLTreeMultiMap<
|
|
|
85
86
|
return this._count;
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(n)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Time Complexity: O(n)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
|
+
*
|
|
98
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
99
|
+
* search.
|
|
100
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
101
|
+
*/
|
|
102
|
+
getComputedCount(): number {
|
|
89
103
|
let sum = 0;
|
|
90
104
|
this.dfs(node => (sum += node.count));
|
|
91
105
|
return sum;
|
|
@@ -316,8 +330,8 @@ export class AVLTreeMultiMap<
|
|
|
316
330
|
* values:
|
|
317
331
|
* @returns a boolean value.
|
|
318
332
|
*/
|
|
319
|
-
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
320
|
-
const sorted = this.dfs(node => node, '
|
|
333
|
+
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
334
|
+
const sorted = this.dfs(node => node, 'IN'),
|
|
321
335
|
n = sorted.length;
|
|
322
336
|
if (sorted.length < 1) return false;
|
|
323
337
|
|
|
@@ -264,7 +264,10 @@ export class BinaryTree<
|
|
|
264
264
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
265
265
|
* itself if it is not a valid node key.
|
|
266
266
|
*/
|
|
267
|
-
ensureNode(
|
|
267
|
+
ensureNode(
|
|
268
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
269
|
+
iterationType: IterationType = 'ITERATIVE'
|
|
270
|
+
): NODE | null | undefined {
|
|
268
271
|
let res: NODE | null | undefined;
|
|
269
272
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
270
273
|
res = keyOrNodeOrEntry;
|
|
@@ -594,7 +597,7 @@ export class BinaryTree<
|
|
|
594
597
|
callback: C = this._defaultOneParamCallback as C,
|
|
595
598
|
onlyOne = false,
|
|
596
599
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
597
|
-
iterationType = this.iterationType
|
|
600
|
+
iterationType: IterationType = this.iterationType
|
|
598
601
|
): NODE[] {
|
|
599
602
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
600
603
|
callback = (node => node) as C;
|
|
@@ -684,7 +687,7 @@ export class BinaryTree<
|
|
|
684
687
|
identifier: ReturnType<C> | null | undefined,
|
|
685
688
|
callback: C = this._defaultOneParamCallback as C,
|
|
686
689
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
687
|
-
iterationType = this.iterationType
|
|
690
|
+
iterationType: IterationType = this.iterationType
|
|
688
691
|
): NODE | null | undefined {
|
|
689
692
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
690
693
|
callback = (node => node) as C;
|
|
@@ -711,7 +714,7 @@ export class BinaryTree<
|
|
|
711
714
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
712
715
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
713
716
|
*/
|
|
714
|
-
getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
|
|
717
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
715
718
|
if (!this.root) return undefined;
|
|
716
719
|
if (iterationType === 'RECURSIVE') {
|
|
717
720
|
const _dfs = (cur: NODE): NODE | undefined => {
|
|
@@ -788,7 +791,7 @@ export class BinaryTree<
|
|
|
788
791
|
identifier: ReturnType<C> | null | undefined,
|
|
789
792
|
callback: C = this._defaultOneParamCallback as C,
|
|
790
793
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
791
|
-
iterationType = this.iterationType
|
|
794
|
+
iterationType: IterationType = this.iterationType
|
|
792
795
|
): V | undefined {
|
|
793
796
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
794
797
|
callback = (node => node) as C;
|
|
@@ -847,7 +850,7 @@ export class BinaryTree<
|
|
|
847
850
|
identifier: ReturnType<C> | null | undefined,
|
|
848
851
|
callback: C = this._defaultOneParamCallback as C,
|
|
849
852
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
850
|
-
iterationType = this.iterationType
|
|
853
|
+
iterationType: IterationType = this.iterationType
|
|
851
854
|
): boolean {
|
|
852
855
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
853
856
|
callback = (node => node) as C;
|
|
@@ -924,7 +927,10 @@ export class BinaryTree<
|
|
|
924
927
|
* possible values:
|
|
925
928
|
* @returns a boolean value.
|
|
926
929
|
*/
|
|
927
|
-
isBST(
|
|
930
|
+
isBST(
|
|
931
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
932
|
+
iterationType: IterationType = this.iterationType
|
|
933
|
+
): boolean {
|
|
928
934
|
// TODO there is a bug
|
|
929
935
|
beginRoot = this.ensureNode(beginRoot);
|
|
930
936
|
if (!beginRoot) return true;
|
|
@@ -1016,7 +1022,10 @@ export class BinaryTree<
|
|
|
1016
1022
|
* values:
|
|
1017
1023
|
* @returns the height of the binary tree.
|
|
1018
1024
|
*/
|
|
1019
|
-
getHeight(
|
|
1025
|
+
getHeight(
|
|
1026
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1027
|
+
iterationType: IterationType = this.iterationType
|
|
1028
|
+
): number {
|
|
1020
1029
|
beginRoot = this.ensureNode(beginRoot);
|
|
1021
1030
|
if (!this.isRealNode(beginRoot)) return -1;
|
|
1022
1031
|
|
|
@@ -1064,7 +1073,10 @@ export class BinaryTree<
|
|
|
1064
1073
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
1065
1074
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
1066
1075
|
*/
|
|
1067
|
-
getMinHeight(
|
|
1076
|
+
getMinHeight(
|
|
1077
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1078
|
+
iterationType: IterationType = this.iterationType
|
|
1079
|
+
): number {
|
|
1068
1080
|
beginRoot = this.ensureNode(beginRoot);
|
|
1069
1081
|
if (!beginRoot) return -1;
|
|
1070
1082
|
|
|
@@ -1164,7 +1176,7 @@ export class BinaryTree<
|
|
|
1164
1176
|
*/
|
|
1165
1177
|
getLeftMost(
|
|
1166
1178
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1167
|
-
iterationType = this.iterationType
|
|
1179
|
+
iterationType: IterationType = this.iterationType
|
|
1168
1180
|
): NODE | null | undefined {
|
|
1169
1181
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1170
1182
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1211,7 +1223,7 @@ export class BinaryTree<
|
|
|
1211
1223
|
*/
|
|
1212
1224
|
getRightMost(
|
|
1213
1225
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1214
|
-
iterationType = this.iterationType
|
|
1226
|
+
iterationType: IterationType = this.iterationType
|
|
1215
1227
|
): NODE | null | undefined {
|
|
1216
1228
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1217
1229
|
// TODO support get right most by passing key in
|
|
@@ -1340,7 +1352,7 @@ export class BinaryTree<
|
|
|
1340
1352
|
*/
|
|
1341
1353
|
dfs<C extends BTNCallback<NODE | null | undefined>>(
|
|
1342
1354
|
callback: C = this._defaultOneParamCallback as C,
|
|
1343
|
-
pattern: DFSOrderPattern = '
|
|
1355
|
+
pattern: DFSOrderPattern = 'IN',
|
|
1344
1356
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1345
1357
|
iterationType: IterationType = 'ITERATIVE',
|
|
1346
1358
|
includeNull = false
|
|
@@ -1351,7 +1363,7 @@ export class BinaryTree<
|
|
|
1351
1363
|
if (iterationType === 'RECURSIVE') {
|
|
1352
1364
|
const _traverse = (node: NODE | null | undefined) => {
|
|
1353
1365
|
switch (pattern) {
|
|
1354
|
-
case '
|
|
1366
|
+
case 'IN':
|
|
1355
1367
|
if (includeNull) {
|
|
1356
1368
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1357
1369
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
@@ -1362,7 +1374,7 @@ export class BinaryTree<
|
|
|
1362
1374
|
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1363
1375
|
}
|
|
1364
1376
|
break;
|
|
1365
|
-
case '
|
|
1377
|
+
case 'PRE':
|
|
1366
1378
|
if (includeNull) {
|
|
1367
1379
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1368
1380
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
@@ -1373,7 +1385,7 @@ export class BinaryTree<
|
|
|
1373
1385
|
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1374
1386
|
}
|
|
1375
1387
|
break;
|
|
1376
|
-
case '
|
|
1388
|
+
case 'POST':
|
|
1377
1389
|
if (includeNull) {
|
|
1378
1390
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1379
1391
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
@@ -1405,17 +1417,17 @@ export class BinaryTree<
|
|
|
1405
1417
|
ans.push(callback(cur.node));
|
|
1406
1418
|
} else {
|
|
1407
1419
|
switch (pattern) {
|
|
1408
|
-
case '
|
|
1420
|
+
case 'IN':
|
|
1409
1421
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1410
1422
|
stack.push({ opt: 1, node: cur.node });
|
|
1411
1423
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1412
1424
|
break;
|
|
1413
|
-
case '
|
|
1425
|
+
case 'PRE':
|
|
1414
1426
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1415
1427
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1416
1428
|
stack.push({ opt: 1, node: cur.node });
|
|
1417
1429
|
break;
|
|
1418
|
-
case '
|
|
1430
|
+
case 'POST':
|
|
1419
1431
|
stack.push({ opt: 1, node: cur.node });
|
|
1420
1432
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1421
1433
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
@@ -1476,7 +1488,7 @@ export class BinaryTree<
|
|
|
1476
1488
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1477
1489
|
callback: C = this._defaultOneParamCallback as C,
|
|
1478
1490
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1479
|
-
iterationType = this.iterationType,
|
|
1491
|
+
iterationType: IterationType = this.iterationType,
|
|
1480
1492
|
includeNull = false
|
|
1481
1493
|
): ReturnType<C>[] {
|
|
1482
1494
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1570,7 +1582,7 @@ export class BinaryTree<
|
|
|
1570
1582
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1571
1583
|
callback: C = this._defaultOneParamCallback as C,
|
|
1572
1584
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1573
|
-
iterationType = this.iterationType,
|
|
1585
|
+
iterationType: IterationType = this.iterationType,
|
|
1574
1586
|
includeNull = false
|
|
1575
1587
|
): ReturnType<C>[][] {
|
|
1576
1588
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1640,7 +1652,7 @@ export class BinaryTree<
|
|
|
1640
1652
|
*/
|
|
1641
1653
|
morris<C extends BTNCallback<NODE>>(
|
|
1642
1654
|
callback: C = this._defaultOneParamCallback as C,
|
|
1643
|
-
pattern: DFSOrderPattern = '
|
|
1655
|
+
pattern: DFSOrderPattern = 'IN',
|
|
1644
1656
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1645
1657
|
): ReturnType<C>[] {
|
|
1646
1658
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1669,7 +1681,7 @@ export class BinaryTree<
|
|
|
1669
1681
|
_reverseEdge(tail);
|
|
1670
1682
|
};
|
|
1671
1683
|
switch (pattern) {
|
|
1672
|
-
case '
|
|
1684
|
+
case 'IN':
|
|
1673
1685
|
while (cur) {
|
|
1674
1686
|
if (cur.left) {
|
|
1675
1687
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1685,7 +1697,7 @@ export class BinaryTree<
|
|
|
1685
1697
|
cur = cur.right;
|
|
1686
1698
|
}
|
|
1687
1699
|
break;
|
|
1688
|
-
case '
|
|
1700
|
+
case 'PRE':
|
|
1689
1701
|
while (cur) {
|
|
1690
1702
|
if (cur.left) {
|
|
1691
1703
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1703,7 +1715,7 @@ export class BinaryTree<
|
|
|
1703
1715
|
cur = cur.right;
|
|
1704
1716
|
}
|
|
1705
1717
|
break;
|
|
1706
|
-
case '
|
|
1718
|
+
case 'POST':
|
|
1707
1719
|
while (cur) {
|
|
1708
1720
|
if (cur.left) {
|
|
1709
1721
|
const predecessor = this.getPredecessor(cur);
|