data-structure-typed 1.50.6 → 1.50.8
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 +27 -24
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +17 -395
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +36 -33
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -47
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -17
- 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 +1 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/common.d.ts +5 -22
- package/dist/cjs/types/common.js +0 -33
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +36 -33
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -47
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -17
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/mjs/types/common.d.ts +5 -22
- package/dist/mjs/types/common.js +1 -32
- package/dist/umd/data-structure-typed.js +112 -138
- 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 +8 -5
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +35 -36
- package/src/data-structures/binary-tree/bst.ts +67 -44
- package/src/data-structures/binary-tree/rb-tree.ts +11 -22
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/types/common.ts +6 -23
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- 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 +12 -17
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +118 -65
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/overall.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +272 -267
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +249 -245
|
@@ -1,31 +1,13 @@
|
|
|
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
12
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
31
13
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -52,3 +34,4 @@ export type BinaryTreeDeleteResult<N> = {
|
|
|
52
34
|
deleted: N | null | undefined;
|
|
53
35
|
needBalanced: N | null | undefined;
|
|
54
36
|
};
|
|
37
|
+
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
|
package/dist/cjs/types/common.js
CHANGED
|
@@ -1,36 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
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
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":""}
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { IterationType } 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,6 +45,7 @@ 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;
|
|
48
|
+
getMutableCount(): number;
|
|
49
49
|
/**
|
|
50
50
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
51
51
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -156,7 +156,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
156
156
|
* values:
|
|
157
157
|
* @returns a boolean value.
|
|
158
158
|
*/
|
|
159
|
-
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
159
|
+
perfectlyBalance(iterationType?: import("../../types").IterationType): boolean;
|
|
160
160
|
/**
|
|
161
161
|
* Time complexity: O(n)
|
|
162
162
|
* Space complexity: O(n)
|
|
@@ -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
|
/**
|
|
@@ -49,6 +48,9 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
49
48
|
* @returns the sum of the count property of all nodes in the tree.
|
|
50
49
|
*/
|
|
51
50
|
get count() {
|
|
51
|
+
return this._count;
|
|
52
|
+
}
|
|
53
|
+
getMutableCount() {
|
|
52
54
|
let sum = 0;
|
|
53
55
|
this.dfs(node => (sum += node.count));
|
|
54
56
|
return sum;
|
|
@@ -201,10 +203,10 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
201
203
|
}
|
|
202
204
|
else {
|
|
203
205
|
const { familyPosition: fp } = curr;
|
|
204
|
-
if (fp ===
|
|
206
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
205
207
|
parent.left = curr.right;
|
|
206
208
|
}
|
|
207
|
-
else if (fp ===
|
|
209
|
+
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
208
210
|
parent.right = curr.right;
|
|
209
211
|
}
|
|
210
212
|
needBalanced = parent;
|
|
@@ -271,7 +273,7 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
271
273
|
if (sorted.length < 1)
|
|
272
274
|
return false;
|
|
273
275
|
this.clear();
|
|
274
|
-
if (iterationType ===
|
|
276
|
+
if (iterationType === 'RECURSIVE') {
|
|
275
277
|
const buildBalanceBST = (l, r) => {
|
|
276
278
|
if (l > r)
|
|
277
279
|
return;
|
|
@@ -135,11 +135,11 @@ 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
|
*/
|
|
142
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
142
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | null | undefined;
|
|
143
143
|
/**
|
|
144
144
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
145
145
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
@@ -248,7 +248,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
248
248
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
249
249
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
250
250
|
*/
|
|
251
|
-
getNodeByKey(key: K, iterationType?:
|
|
251
|
+
getNodeByKey(key: K, iterationType?: string): NODE | undefined;
|
|
252
252
|
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
253
253
|
get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
254
254
|
get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
@@ -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;
|
|
@@ -821,9 +820,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
821
820
|
*/
|
|
822
821
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
823
822
|
beginRoot = this.ensureNode(beginRoot);
|
|
824
|
-
if (!beginRoot)
|
|
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,11 +867,11 @@ 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
|
-
if (!cur)
|
|
872
|
+
if (!this.isRealNode(cur))
|
|
874
873
|
return 0;
|
|
875
|
-
if (!cur.left && !cur.right)
|
|
874
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
876
875
|
return 0;
|
|
877
876
|
const leftMinHeight = _getMinHeight(cur.left);
|
|
878
877
|
const rightMinHeight = _getMinHeight(cur.right);
|
|
@@ -885,17 +884,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
885
884
|
let node = beginRoot, last = null;
|
|
886
885
|
const depths = new Map();
|
|
887
886
|
while (stack.length > 0 || node) {
|
|
888
|
-
if (node) {
|
|
887
|
+
if (this.isRealNode(node)) {
|
|
889
888
|
stack.push(node);
|
|
890
889
|
node = node.left;
|
|
891
890
|
}
|
|
892
891
|
else {
|
|
893
892
|
node = stack[stack.length - 1];
|
|
894
|
-
if (!node.right || last === node.right) {
|
|
893
|
+
if (!this.isRealNode(node.right) || last === node.right) {
|
|
895
894
|
node = stack.pop();
|
|
896
|
-
if (node) {
|
|
897
|
-
const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
898
|
-
const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
895
|
+
if (this.isRealNode(node)) {
|
|
896
|
+
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) ?? -1 : -1;
|
|
897
|
+
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) ?? -1 : -1;
|
|
899
898
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
900
899
|
last = node;
|
|
901
900
|
node = null;
|
|
@@ -961,10 +960,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
961
960
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
962
961
|
*/
|
|
963
962
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
963
|
+
if (this.isNIL(beginRoot))
|
|
964
|
+
return beginRoot;
|
|
964
965
|
beginRoot = this.ensureNode(beginRoot);
|
|
965
|
-
if (!beginRoot)
|
|
966
|
+
if (!this.isRealNode(beginRoot))
|
|
966
967
|
return beginRoot;
|
|
967
|
-
if (iterationType ===
|
|
968
|
+
if (iterationType === 'RECURSIVE') {
|
|
968
969
|
const _traverse = (cur) => {
|
|
969
970
|
if (!this.isRealNode(cur.left))
|
|
970
971
|
return cur;
|
|
@@ -1002,11 +1003,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1002
1003
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1003
1004
|
*/
|
|
1004
1005
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
1006
|
+
if (this.isNIL(beginRoot))
|
|
1007
|
+
return beginRoot;
|
|
1005
1008
|
// TODO support get right most by passing key in
|
|
1006
1009
|
beginRoot = this.ensureNode(beginRoot);
|
|
1007
1010
|
if (!beginRoot)
|
|
1008
1011
|
return beginRoot;
|
|
1009
|
-
if (iterationType ===
|
|
1012
|
+
if (iterationType === 'RECURSIVE') {
|
|
1010
1013
|
const _traverse = (cur) => {
|
|
1011
1014
|
if (!this.isRealNode(cur.right))
|
|
1012
1015
|
return cur;
|
|
@@ -1106,12 +1109,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1106
1109
|
* `false`, null or undefined
|
|
1107
1110
|
* @returns an array of values that are the return values of the callback function.
|
|
1108
1111
|
*/
|
|
1109
|
-
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType =
|
|
1112
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1110
1113
|
beginRoot = this.ensureNode(beginRoot);
|
|
1111
1114
|
if (!beginRoot)
|
|
1112
1115
|
return [];
|
|
1113
1116
|
const ans = [];
|
|
1114
|
-
if (iterationType ===
|
|
1117
|
+
if (iterationType === 'RECURSIVE') {
|
|
1115
1118
|
const _traverse = (node) => {
|
|
1116
1119
|
switch (pattern) {
|
|
1117
1120
|
case 'in':
|
|
@@ -1242,7 +1245,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1242
1245
|
if (!beginRoot)
|
|
1243
1246
|
return [];
|
|
1244
1247
|
const ans = [];
|
|
1245
|
-
if (iterationType ===
|
|
1248
|
+
if (iterationType === 'RECURSIVE') {
|
|
1246
1249
|
const queue = new Queue([beginRoot]);
|
|
1247
1250
|
const traverse = (level) => {
|
|
1248
1251
|
if (queue.size === 0)
|
|
@@ -1319,7 +1322,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1319
1322
|
const levelsNodes = [];
|
|
1320
1323
|
if (!beginRoot)
|
|
1321
1324
|
return levelsNodes;
|
|
1322
|
-
if (iterationType ===
|
|
1325
|
+
if (iterationType === 'RECURSIVE') {
|
|
1323
1326
|
const _recursive = (node, level) => {
|
|
1324
1327
|
if (!levelsNodes[level])
|
|
1325
1328
|
levelsNodes[level] = [];
|
|
@@ -1606,7 +1609,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1606
1609
|
*_getIterator(node = this.root) {
|
|
1607
1610
|
if (!node)
|
|
1608
1611
|
return;
|
|
1609
|
-
if (this.iterationType ===
|
|
1612
|
+
if (this.iterationType === 'ITERATIVE') {
|
|
1610
1613
|
const stack = [];
|
|
1611
1614
|
let current = node;
|
|
1612
1615
|
while (current || stack.length > 0) {
|
|
@@ -109,10 +109,10 @@ 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
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
115
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | undefined;
|
|
116
116
|
/**
|
|
117
117
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
118
118
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
@@ -179,7 +179,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
179
179
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
180
180
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
181
181
|
*/
|
|
182
|
-
getNodeByKey(key: K, iterationType?:
|
|
182
|
+
getNodeByKey(key: K, iterationType?: string): NODE | undefined;
|
|
183
183
|
/**
|
|
184
184
|
* Time Complexity: O(log n)
|
|
185
185
|
* Space Complexity: O(k + log n)
|
|
@@ -372,8 +372,10 @@ 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
|
+
protected _lt(a: K, b: K): boolean;
|
|
380
|
+
protected _gt(a: K, b: K): boolean;
|
|
379
381
|
}
|