data-structure-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/CHANGELOG.md +1 -1
- package/README.md +73 -67
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +15 -393
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +14 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +33 -34
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +22 -3
- package/dist/cjs/data-structures/binary-tree/bst.js +78 -39
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +47 -50
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -28
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/data-structures/heap/heap.js +5 -5
- package/dist/cjs/types/common.d.ts +6 -29
- package/dist/cjs/types/common.js +0 -40
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +14 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +33 -34
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +22 -3
- package/dist/mjs/data-structures/binary-tree/bst.js +78 -39
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +47 -50
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +44 -28
- package/dist/mjs/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/data-structures/heap/heap.js +5 -5
- package/dist/mjs/types/common.d.ts +6 -29
- package/dist/mjs/types/common.js +1 -39
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +212 -206
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- 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
- package/test/integration/all-in-one.test.ts +4 -4
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +20 -15
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +13 -22
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +15 -23
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +127 -74
- package/test/unit/data-structures/binary-tree/bst.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/overall.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +31 -26
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +26 -34
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
|
@@ -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]);
|
|
@@ -1,33 +1,15 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
INVERSE = "INVERSE"
|
|
4
|
-
}
|
|
5
|
-
export declare enum CP {
|
|
6
|
-
lt = "lt",
|
|
7
|
-
eq = "eq",
|
|
8
|
-
gt = "gt"
|
|
9
|
-
}
|
|
1
|
+
export type BSTVariant = 'STANDARD' | 'INVERSE';
|
|
2
|
+
export type CP = 'LT' | 'EQ' | 'GT';
|
|
10
3
|
/**
|
|
11
4
|
* Enum representing different loop types.
|
|
12
5
|
*
|
|
13
6
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
14
7
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
15
8
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
RECURSIVE = "RECURSIVE"
|
|
19
|
-
}
|
|
20
|
-
export declare enum FamilyPosition {
|
|
21
|
-
ROOT = "ROOT",
|
|
22
|
-
LEFT = "LEFT",
|
|
23
|
-
RIGHT = "RIGHT",
|
|
24
|
-
ROOT_LEFT = "ROOT_LEFT",
|
|
25
|
-
ROOT_RIGHT = "ROOT_RIGHT",
|
|
26
|
-
ISOLATED = "ISOLATED",
|
|
27
|
-
MAL_NODE = "MAL_NODE"
|
|
28
|
-
}
|
|
9
|
+
export type IterationType = 'ITERATIVE' | 'RECURSIVE';
|
|
10
|
+
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
|
|
29
11
|
export type Comparator<K> = (a: K, b: K) => number;
|
|
30
|
-
export type DFSOrderPattern = '
|
|
12
|
+
export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
31
13
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
32
14
|
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
33
15
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
@@ -52,9 +34,4 @@ export type BinaryTreeDeleteResult<N> = {
|
|
|
52
34
|
deleted: N | null | undefined;
|
|
53
35
|
needBalanced: N | null | undefined;
|
|
54
36
|
};
|
|
55
|
-
export
|
|
56
|
-
CREATED = "CREATED",
|
|
57
|
-
READ = "READ",
|
|
58
|
-
UPDATED = "UPDATED",
|
|
59
|
-
DELETED = "DELETED"
|
|
60
|
-
}
|
|
37
|
+
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
|
package/dist/cjs/types/common.js
CHANGED
|
@@ -1,43 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CRUD = exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0;
|
|
4
|
-
var BSTVariant;
|
|
5
|
-
(function (BSTVariant) {
|
|
6
|
-
BSTVariant["STANDARD"] = "STANDARD";
|
|
7
|
-
BSTVariant["INVERSE"] = "INVERSE";
|
|
8
|
-
})(BSTVariant || (exports.BSTVariant = BSTVariant = {}));
|
|
9
|
-
var CP;
|
|
10
|
-
(function (CP) {
|
|
11
|
-
CP["lt"] = "lt";
|
|
12
|
-
CP["eq"] = "eq";
|
|
13
|
-
CP["gt"] = "gt";
|
|
14
|
-
})(CP || (exports.CP = CP = {}));
|
|
15
|
-
/**
|
|
16
|
-
* Enum representing different loop types.
|
|
17
|
-
*
|
|
18
|
-
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
19
|
-
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
20
|
-
*/
|
|
21
|
-
var IterationType;
|
|
22
|
-
(function (IterationType) {
|
|
23
|
-
IterationType["ITERATIVE"] = "ITERATIVE";
|
|
24
|
-
IterationType["RECURSIVE"] = "RECURSIVE";
|
|
25
|
-
})(IterationType || (exports.IterationType = IterationType = {}));
|
|
26
|
-
var FamilyPosition;
|
|
27
|
-
(function (FamilyPosition) {
|
|
28
|
-
FamilyPosition["ROOT"] = "ROOT";
|
|
29
|
-
FamilyPosition["LEFT"] = "LEFT";
|
|
30
|
-
FamilyPosition["RIGHT"] = "RIGHT";
|
|
31
|
-
FamilyPosition["ROOT_LEFT"] = "ROOT_LEFT";
|
|
32
|
-
FamilyPosition["ROOT_RIGHT"] = "ROOT_RIGHT";
|
|
33
|
-
FamilyPosition["ISOLATED"] = "ISOLATED";
|
|
34
|
-
FamilyPosition["MAL_NODE"] = "MAL_NODE";
|
|
35
|
-
})(FamilyPosition || (exports.FamilyPosition = FamilyPosition = {}));
|
|
36
|
-
var CRUD;
|
|
37
|
-
(function (CRUD) {
|
|
38
|
-
CRUD["CREATED"] = "CREATED";
|
|
39
|
-
CRUD["READ"] = "READ";
|
|
40
|
-
CRUD["UPDATED"] = "UPDATED";
|
|
41
|
-
CRUD["DELETED"] = "DELETED";
|
|
42
|
-
})(CRUD || (exports.CRUD = CRUD = {}));
|
|
43
3
|
//# sourceMappingURL=common.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/types/common.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/types/common.ts"],"names":[],"mappings":""}
|
|
@@ -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,9 +1,3 @@
|
|
|
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 = RBTNColor = {}));
|
|
9
3
|
//# sourceMappingURL=rb-tree.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":""}
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { IterationType } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
10
9
|
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -46,7 +45,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
46
45
|
* @returns the sum of the count property of all nodes in the tree.
|
|
47
46
|
*/
|
|
48
47
|
get count(): number;
|
|
49
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(n)
|
|
50
|
+
* Space Complexity: O(1)
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(n)
|
|
54
|
+
* Space Complexity: O(1)
|
|
55
|
+
*
|
|
56
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
57
|
+
* search.
|
|
58
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
59
|
+
*/
|
|
60
|
+
getComputedCount(): number;
|
|
50
61
|
/**
|
|
51
62
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
52
63
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { FamilyPosition, IterationType } from '../../types';
|
|
2
1
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
2
|
export class AVLTreeMultiMapNode extends AVLTreeNode {
|
|
4
3
|
/**
|
|
@@ -51,7 +50,19 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
51
50
|
get count() {
|
|
52
51
|
return this._count;
|
|
53
52
|
}
|
|
54
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(n)
|
|
55
|
+
* Space Complexity: O(1)
|
|
56
|
+
*/
|
|
57
|
+
/**
|
|
58
|
+
* Time Complexity: O(n)
|
|
59
|
+
* Space Complexity: O(1)
|
|
60
|
+
*
|
|
61
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
62
|
+
* search.
|
|
63
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
64
|
+
*/
|
|
65
|
+
getComputedCount() {
|
|
55
66
|
let sum = 0;
|
|
56
67
|
this.dfs(node => (sum += node.count));
|
|
57
68
|
return sum;
|
|
@@ -204,10 +215,10 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
204
215
|
}
|
|
205
216
|
else {
|
|
206
217
|
const { familyPosition: fp } = curr;
|
|
207
|
-
if (fp ===
|
|
218
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
208
219
|
parent.left = curr.right;
|
|
209
220
|
}
|
|
210
|
-
else if (fp ===
|
|
221
|
+
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
211
222
|
parent.right = curr.right;
|
|
212
223
|
}
|
|
213
224
|
needBalanced = parent;
|
|
@@ -270,11 +281,11 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
270
281
|
* @returns a boolean value.
|
|
271
282
|
*/
|
|
272
283
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
273
|
-
const sorted = this.dfs(node => node, '
|
|
284
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
274
285
|
if (sorted.length < 1)
|
|
275
286
|
return false;
|
|
276
287
|
this.clear();
|
|
277
|
-
if (iterationType ===
|
|
288
|
+
if (iterationType === 'RECURSIVE') {
|
|
278
289
|
const buildBalanceBST = (l, r) => {
|
|
279
290
|
if (l > r)
|
|
280
291
|
return;
|
|
@@ -135,7 +135,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
135
135
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
136
136
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
137
137
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
138
|
-
* `
|
|
138
|
+
* `'ITERATIVE'`.
|
|
139
139
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
140
140
|
* itself if it is not a valid node key.
|
|
141
141
|
*/
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { FamilyPosition, IterationType } from '../../types';
|
|
9
8
|
import { trampoline } from '../../utils';
|
|
10
9
|
import { Queue } from '../queue';
|
|
11
10
|
import { IterableEntryBase } from '../base';
|
|
@@ -77,15 +76,15 @@ export class BinaryTreeNode {
|
|
|
77
76
|
get familyPosition() {
|
|
78
77
|
const that = this;
|
|
79
78
|
if (!this.parent) {
|
|
80
|
-
return this.left || this.right ?
|
|
79
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
81
80
|
}
|
|
82
81
|
if (this.parent.left === that) {
|
|
83
|
-
return this.left || this.right ?
|
|
82
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
84
83
|
}
|
|
85
84
|
else if (this.parent.right === that) {
|
|
86
|
-
return this.left || this.right ?
|
|
85
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
87
86
|
}
|
|
88
|
-
return
|
|
87
|
+
return 'MAL_NODE';
|
|
89
88
|
}
|
|
90
89
|
}
|
|
91
90
|
/**
|
|
@@ -96,7 +95,7 @@ export class BinaryTreeNode {
|
|
|
96
95
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
97
96
|
*/
|
|
98
97
|
export class BinaryTree extends IterableEntryBase {
|
|
99
|
-
iterationType =
|
|
98
|
+
iterationType = 'ITERATIVE';
|
|
100
99
|
/**
|
|
101
100
|
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
|
|
102
101
|
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
@@ -119,7 +118,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
119
118
|
if (keysOrNodesOrEntries)
|
|
120
119
|
this.addMany(keysOrNodesOrEntries);
|
|
121
120
|
}
|
|
122
|
-
_extractor = (key) => Number(key);
|
|
121
|
+
_extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
123
122
|
/**
|
|
124
123
|
* The function returns the value of the `_extractor` property.
|
|
125
124
|
* @returns The `_extractor` property is being returned.
|
|
@@ -215,11 +214,11 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
215
214
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
216
215
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
217
216
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
218
|
-
* `
|
|
217
|
+
* `'ITERATIVE'`.
|
|
219
218
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
220
219
|
* itself if it is not a valid node key.
|
|
221
220
|
*/
|
|
222
|
-
ensureNode(keyOrNodeOrEntry, iterationType =
|
|
221
|
+
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
223
222
|
let res;
|
|
224
223
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
225
224
|
res = keyOrNodeOrEntry;
|
|
@@ -446,10 +445,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
446
445
|
}
|
|
447
446
|
else if (parent) {
|
|
448
447
|
const { familyPosition: fp } = curr;
|
|
449
|
-
if (fp ===
|
|
448
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
450
449
|
parent.left = curr.right;
|
|
451
450
|
}
|
|
452
|
-
else if (fp ===
|
|
451
|
+
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
453
452
|
parent.right = curr.right;
|
|
454
453
|
}
|
|
455
454
|
needBalanced = parent;
|
|
@@ -498,7 +497,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
498
497
|
if (!beginRoot)
|
|
499
498
|
return [];
|
|
500
499
|
const ans = [];
|
|
501
|
-
if (iterationType ===
|
|
500
|
+
if (iterationType === 'RECURSIVE') {
|
|
502
501
|
const _traverse = (cur) => {
|
|
503
502
|
if (callback(cur) === identifier) {
|
|
504
503
|
ans.push(cur);
|
|
@@ -577,10 +576,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
577
576
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
578
577
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
579
578
|
*/
|
|
580
|
-
getNodeByKey(key, iterationType =
|
|
579
|
+
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
581
580
|
if (!this.root)
|
|
582
581
|
return undefined;
|
|
583
|
-
if (iterationType ===
|
|
582
|
+
if (iterationType === 'RECURSIVE') {
|
|
584
583
|
const _dfs = (cur) => {
|
|
585
584
|
if (cur.key === key)
|
|
586
585
|
return cur;
|
|
@@ -734,7 +733,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
734
733
|
beginRoot = this.ensureNode(beginRoot);
|
|
735
734
|
if (!beginRoot)
|
|
736
735
|
return true;
|
|
737
|
-
if (iterationType ===
|
|
736
|
+
if (iterationType === 'RECURSIVE') {
|
|
738
737
|
const dfs = (cur, min, max) => {
|
|
739
738
|
if (!this.isRealNode(cur))
|
|
740
739
|
return true;
|
|
@@ -823,7 +822,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
823
822
|
beginRoot = this.ensureNode(beginRoot);
|
|
824
823
|
if (!this.isRealNode(beginRoot))
|
|
825
824
|
return -1;
|
|
826
|
-
if (iterationType ===
|
|
825
|
+
if (iterationType === 'RECURSIVE') {
|
|
827
826
|
const _getMaxHeight = (cur) => {
|
|
828
827
|
if (!this.isRealNode(cur))
|
|
829
828
|
return -1;
|
|
@@ -868,7 +867,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
868
867
|
beginRoot = this.ensureNode(beginRoot);
|
|
869
868
|
if (!beginRoot)
|
|
870
869
|
return -1;
|
|
871
|
-
if (iterationType ===
|
|
870
|
+
if (iterationType === 'RECURSIVE') {
|
|
872
871
|
const _getMinHeight = (cur) => {
|
|
873
872
|
if (!this.isRealNode(cur))
|
|
874
873
|
return 0;
|
|
@@ -966,7 +965,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
966
965
|
beginRoot = this.ensureNode(beginRoot);
|
|
967
966
|
if (!this.isRealNode(beginRoot))
|
|
968
967
|
return beginRoot;
|
|
969
|
-
if (iterationType ===
|
|
968
|
+
if (iterationType === 'RECURSIVE') {
|
|
970
969
|
const _traverse = (cur) => {
|
|
971
970
|
if (!this.isRealNode(cur.left))
|
|
972
971
|
return cur;
|
|
@@ -1010,7 +1009,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1010
1009
|
beginRoot = this.ensureNode(beginRoot);
|
|
1011
1010
|
if (!beginRoot)
|
|
1012
1011
|
return beginRoot;
|
|
1013
|
-
if (iterationType ===
|
|
1012
|
+
if (iterationType === 'RECURSIVE') {
|
|
1014
1013
|
const _traverse = (cur) => {
|
|
1015
1014
|
if (!this.isRealNode(cur.right))
|
|
1016
1015
|
return cur;
|
|
@@ -1110,15 +1109,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1110
1109
|
* `false`, null or undefined
|
|
1111
1110
|
* @returns an array of values that are the return values of the callback function.
|
|
1112
1111
|
*/
|
|
1113
|
-
dfs(callback = this._defaultOneParamCallback, pattern = '
|
|
1112
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1114
1113
|
beginRoot = this.ensureNode(beginRoot);
|
|
1115
1114
|
if (!beginRoot)
|
|
1116
1115
|
return [];
|
|
1117
1116
|
const ans = [];
|
|
1118
|
-
if (iterationType ===
|
|
1117
|
+
if (iterationType === 'RECURSIVE') {
|
|
1119
1118
|
const _traverse = (node) => {
|
|
1120
1119
|
switch (pattern) {
|
|
1121
|
-
case '
|
|
1120
|
+
case 'IN':
|
|
1122
1121
|
if (includeNull) {
|
|
1123
1122
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1124
1123
|
_traverse(node.left);
|
|
@@ -1134,7 +1133,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1134
1133
|
_traverse(node.right);
|
|
1135
1134
|
}
|
|
1136
1135
|
break;
|
|
1137
|
-
case '
|
|
1136
|
+
case 'PRE':
|
|
1138
1137
|
if (includeNull) {
|
|
1139
1138
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1140
1139
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
@@ -1150,7 +1149,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1150
1149
|
_traverse(node.right);
|
|
1151
1150
|
}
|
|
1152
1151
|
break;
|
|
1153
|
-
case '
|
|
1152
|
+
case 'POST':
|
|
1154
1153
|
if (includeNull) {
|
|
1155
1154
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1156
1155
|
_traverse(node.left);
|
|
@@ -1190,17 +1189,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1190
1189
|
}
|
|
1191
1190
|
else {
|
|
1192
1191
|
switch (pattern) {
|
|
1193
|
-
case '
|
|
1192
|
+
case 'IN':
|
|
1194
1193
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1195
1194
|
stack.push({ opt: 1, node: cur.node });
|
|
1196
1195
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1197
1196
|
break;
|
|
1198
|
-
case '
|
|
1197
|
+
case 'PRE':
|
|
1199
1198
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1200
1199
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1201
1200
|
stack.push({ opt: 1, node: cur.node });
|
|
1202
1201
|
break;
|
|
1203
|
-
case '
|
|
1202
|
+
case 'POST':
|
|
1204
1203
|
stack.push({ opt: 1, node: cur.node });
|
|
1205
1204
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1206
1205
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
@@ -1246,7 +1245,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1246
1245
|
if (!beginRoot)
|
|
1247
1246
|
return [];
|
|
1248
1247
|
const ans = [];
|
|
1249
|
-
if (iterationType ===
|
|
1248
|
+
if (iterationType === 'RECURSIVE') {
|
|
1250
1249
|
const queue = new Queue([beginRoot]);
|
|
1251
1250
|
const traverse = (level) => {
|
|
1252
1251
|
if (queue.size === 0)
|
|
@@ -1323,7 +1322,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1323
1322
|
const levelsNodes = [];
|
|
1324
1323
|
if (!beginRoot)
|
|
1325
1324
|
return levelsNodes;
|
|
1326
|
-
if (iterationType ===
|
|
1325
|
+
if (iterationType === 'RECURSIVE') {
|
|
1327
1326
|
const _recursive = (node, level) => {
|
|
1328
1327
|
if (!levelsNodes[level])
|
|
1329
1328
|
levelsNodes[level] = [];
|
|
@@ -1390,7 +1389,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1390
1389
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1391
1390
|
* by the return type of the `callback` function.
|
|
1392
1391
|
*/
|
|
1393
|
-
morris(callback = this._defaultOneParamCallback, pattern = '
|
|
1392
|
+
morris(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root) {
|
|
1394
1393
|
beginRoot = this.ensureNode(beginRoot);
|
|
1395
1394
|
if (beginRoot === null)
|
|
1396
1395
|
return [];
|
|
@@ -1417,7 +1416,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1417
1416
|
_reverseEdge(tail);
|
|
1418
1417
|
};
|
|
1419
1418
|
switch (pattern) {
|
|
1420
|
-
case '
|
|
1419
|
+
case 'IN':
|
|
1421
1420
|
while (cur) {
|
|
1422
1421
|
if (cur.left) {
|
|
1423
1422
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1434,7 +1433,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1434
1433
|
cur = cur.right;
|
|
1435
1434
|
}
|
|
1436
1435
|
break;
|
|
1437
|
-
case '
|
|
1436
|
+
case 'PRE':
|
|
1438
1437
|
while (cur) {
|
|
1439
1438
|
if (cur.left) {
|
|
1440
1439
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1454,7 +1453,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1454
1453
|
cur = cur.right;
|
|
1455
1454
|
}
|
|
1456
1455
|
break;
|
|
1457
|
-
case '
|
|
1456
|
+
case 'POST':
|
|
1458
1457
|
while (cur) {
|
|
1459
1458
|
if (cur.left) {
|
|
1460
1459
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1610,7 +1609,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1610
1609
|
*_getIterator(node = this.root) {
|
|
1611
1610
|
if (!node)
|
|
1612
1611
|
return;
|
|
1613
|
-
if (this.iterationType ===
|
|
1612
|
+
if (this.iterationType === 'ITERATIVE') {
|
|
1614
1613
|
const stack = [];
|
|
1615
1614
|
let current = node;
|
|
1616
1615
|
while (current || stack.length > 0) {
|
|
@@ -109,7 +109,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
109
109
|
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
110
110
|
* `undefined`.
|
|
111
111
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
112
|
-
* type of iteration to be performed. It has a default value of `
|
|
112
|
+
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
|
|
113
113
|
* @returns either a node object (NODE) or undefined.
|
|
114
114
|
*/
|
|
115
115
|
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
@@ -372,8 +372,27 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
372
372
|
* is greater than, less than, or equal to the second value.
|
|
373
373
|
* @param {K} a - The parameter "a" is of type K.
|
|
374
374
|
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
375
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are
|
|
376
|
-
* than),
|
|
375
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
376
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
377
377
|
*/
|
|
378
378
|
protected _compare(a: K, b: K): CP;
|
|
379
|
+
/**
|
|
380
|
+
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
381
|
+
* `a` is less than `b` based on the specified variant.
|
|
382
|
+
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
383
|
+
* first value to be compared in the function.
|
|
384
|
+
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
385
|
+
* of the arguments for the comparison in the `_lt` function.
|
|
386
|
+
* @returns a boolean value.
|
|
387
|
+
*/
|
|
388
|
+
protected _lt(a: K, b: K): boolean;
|
|
389
|
+
/**
|
|
390
|
+
* The function compares two values using a custom extractor function and returns true if the first
|
|
391
|
+
* value is greater than the second value.
|
|
392
|
+
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
393
|
+
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
394
|
+
* of the arguments for the comparison in the function.
|
|
395
|
+
* @returns a boolean value.
|
|
396
|
+
*/
|
|
397
|
+
protected _gt(a: K, b: K): boolean;
|
|
379
398
|
}
|