min-heap-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
package/dist/types/common.js
CHANGED
|
@@ -1,42 +1,2 @@
|
|
|
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 || (exports.BSTVariant = {}));
|
|
9
|
-
var CP;
|
|
10
|
-
(function (CP) {
|
|
11
|
-
CP["lt"] = "lt";
|
|
12
|
-
CP["eq"] = "eq";
|
|
13
|
-
CP["gt"] = "gt";
|
|
14
|
-
})(CP = exports.CP || (exports.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 || (exports.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 || (exports.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 || (exports.CRUD = {}));
|
|
@@ -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": "min-heap-typed",
|
|
3
|
-
"version": "1.50.
|
|
3
|
+
"version": "1.50.9",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.50.
|
|
135
|
+
"data-structure-typed": "^1.50.9"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -12,9 +12,9 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
14
|
BTNCallback,
|
|
15
|
+
IterationType,
|
|
15
16
|
KeyOrNodeOrEntry
|
|
16
17
|
} from '../../types';
|
|
17
|
-
import { FamilyPosition, IterationType } from '../../types';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
20
20
|
|
|
@@ -86,7 +86,20 @@ export class AVLTreeMultiMap<
|
|
|
86
86
|
return this._count;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
|
|
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 {
|
|
90
103
|
let sum = 0;
|
|
91
104
|
this.dfs(node => (sum += node.count));
|
|
92
105
|
return sum;
|
|
@@ -249,9 +262,9 @@ export class AVLTreeMultiMap<
|
|
|
249
262
|
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
250
263
|
} else {
|
|
251
264
|
const { familyPosition: fp } = curr;
|
|
252
|
-
if (fp ===
|
|
265
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
253
266
|
parent.left = curr.right;
|
|
254
|
-
} else if (fp ===
|
|
267
|
+
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
255
268
|
parent.right = curr.right;
|
|
256
269
|
}
|
|
257
270
|
needBalanced = parent;
|
|
@@ -317,14 +330,14 @@ export class AVLTreeMultiMap<
|
|
|
317
330
|
* values:
|
|
318
331
|
* @returns a boolean value.
|
|
319
332
|
*/
|
|
320
|
-
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
321
|
-
const sorted = this.dfs(node => node, '
|
|
333
|
+
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
334
|
+
const sorted = this.dfs(node => node, 'IN'),
|
|
322
335
|
n = sorted.length;
|
|
323
336
|
if (sorted.length < 1) return false;
|
|
324
337
|
|
|
325
338
|
this.clear();
|
|
326
339
|
|
|
327
|
-
if (iterationType ===
|
|
340
|
+
if (iterationType === 'RECURSIVE') {
|
|
328
341
|
const buildBalanceBST = (l: number, r: number) => {
|
|
329
342
|
if (l > r) return;
|
|
330
343
|
const m = l + Math.floor((r - l) / 2);
|
|
@@ -107,16 +107,16 @@ export class BinaryTreeNode<
|
|
|
107
107
|
get familyPosition(): FamilyPosition {
|
|
108
108
|
const that = this as unknown as NODE;
|
|
109
109
|
if (!this.parent) {
|
|
110
|
-
return this.left || this.right ?
|
|
110
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
if (this.parent.left === that) {
|
|
114
|
-
return this.left || this.right ?
|
|
114
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
115
115
|
} else if (this.parent.right === that) {
|
|
116
|
-
return this.left || this.right ?
|
|
116
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
return
|
|
119
|
+
return 'MAL_NODE';
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -136,7 +136,7 @@ export class BinaryTree<
|
|
|
136
136
|
>
|
|
137
137
|
extends IterableEntryBase<K, V | undefined>
|
|
138
138
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
139
|
-
iterationType =
|
|
139
|
+
iterationType: IterationType = 'ITERATIVE';
|
|
140
140
|
|
|
141
141
|
/**
|
|
142
142
|
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
|
|
@@ -160,7 +160,7 @@ export class BinaryTree<
|
|
|
160
160
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
protected _extractor = (key: K) => Number(key);
|
|
163
|
+
protected _extractor = (key: K) => (typeof key === 'number' ? key : Number(key));
|
|
164
164
|
|
|
165
165
|
/**
|
|
166
166
|
* The function returns the value of the `_extractor` property.
|
|
@@ -260,13 +260,13 @@ export class BinaryTree<
|
|
|
260
260
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
261
261
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
262
262
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
263
|
-
* `
|
|
263
|
+
* `'ITERATIVE'`.
|
|
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
267
|
ensureNode(
|
|
268
268
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
269
|
-
iterationType =
|
|
269
|
+
iterationType: IterationType = 'ITERATIVE'
|
|
270
270
|
): NODE | null | undefined {
|
|
271
271
|
let res: NODE | null | undefined;
|
|
272
272
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
@@ -521,9 +521,9 @@ export class BinaryTree<
|
|
|
521
521
|
}
|
|
522
522
|
} else if (parent) {
|
|
523
523
|
const { familyPosition: fp } = curr;
|
|
524
|
-
if (fp ===
|
|
524
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
525
525
|
parent.left = curr.right;
|
|
526
|
-
} else if (fp ===
|
|
526
|
+
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
527
527
|
parent.right = curr.right;
|
|
528
528
|
}
|
|
529
529
|
needBalanced = parent;
|
|
@@ -597,7 +597,7 @@ export class BinaryTree<
|
|
|
597
597
|
callback: C = this._defaultOneParamCallback as C,
|
|
598
598
|
onlyOne = false,
|
|
599
599
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
600
|
-
iterationType = this.iterationType
|
|
600
|
+
iterationType: IterationType = this.iterationType
|
|
601
601
|
): NODE[] {
|
|
602
602
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
603
603
|
callback = (node => node) as C;
|
|
@@ -606,7 +606,7 @@ export class BinaryTree<
|
|
|
606
606
|
|
|
607
607
|
const ans: NODE[] = [];
|
|
608
608
|
|
|
609
|
-
if (iterationType ===
|
|
609
|
+
if (iterationType === 'RECURSIVE') {
|
|
610
610
|
const _traverse = (cur: NODE) => {
|
|
611
611
|
if (callback(cur) === identifier) {
|
|
612
612
|
ans.push(cur);
|
|
@@ -687,7 +687,7 @@ export class BinaryTree<
|
|
|
687
687
|
identifier: ReturnType<C> | null | undefined,
|
|
688
688
|
callback: C = this._defaultOneParamCallback as C,
|
|
689
689
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
690
|
-
iterationType = this.iterationType
|
|
690
|
+
iterationType: IterationType = this.iterationType
|
|
691
691
|
): NODE | null | undefined {
|
|
692
692
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
693
693
|
callback = (node => node) as C;
|
|
@@ -714,9 +714,9 @@ export class BinaryTree<
|
|
|
714
714
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
715
715
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
716
716
|
*/
|
|
717
|
-
getNodeByKey(key: K, iterationType =
|
|
717
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
718
718
|
if (!this.root) return undefined;
|
|
719
|
-
if (iterationType ===
|
|
719
|
+
if (iterationType === 'RECURSIVE') {
|
|
720
720
|
const _dfs = (cur: NODE): NODE | undefined => {
|
|
721
721
|
if (cur.key === key) return cur;
|
|
722
722
|
|
|
@@ -791,7 +791,7 @@ export class BinaryTree<
|
|
|
791
791
|
identifier: ReturnType<C> | null | undefined,
|
|
792
792
|
callback: C = this._defaultOneParamCallback as C,
|
|
793
793
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
794
|
-
iterationType = this.iterationType
|
|
794
|
+
iterationType: IterationType = this.iterationType
|
|
795
795
|
): V | undefined {
|
|
796
796
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
797
797
|
callback = (node => node) as C;
|
|
@@ -850,7 +850,7 @@ export class BinaryTree<
|
|
|
850
850
|
identifier: ReturnType<C> | null | undefined,
|
|
851
851
|
callback: C = this._defaultOneParamCallback as C,
|
|
852
852
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
853
|
-
iterationType = this.iterationType
|
|
853
|
+
iterationType: IterationType = this.iterationType
|
|
854
854
|
): boolean {
|
|
855
855
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
856
856
|
callback = (node => node) as C;
|
|
@@ -927,12 +927,15 @@ export class BinaryTree<
|
|
|
927
927
|
* possible values:
|
|
928
928
|
* @returns a boolean value.
|
|
929
929
|
*/
|
|
930
|
-
isBST(
|
|
930
|
+
isBST(
|
|
931
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
932
|
+
iterationType: IterationType = this.iterationType
|
|
933
|
+
): boolean {
|
|
931
934
|
// TODO there is a bug
|
|
932
935
|
beginRoot = this.ensureNode(beginRoot);
|
|
933
936
|
if (!beginRoot) return true;
|
|
934
937
|
|
|
935
|
-
if (iterationType ===
|
|
938
|
+
if (iterationType === 'RECURSIVE') {
|
|
936
939
|
const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
|
|
937
940
|
if (!this.isRealNode(cur)) return true;
|
|
938
941
|
const numKey = this.extractor(cur.key);
|
|
@@ -1019,11 +1022,14 @@ export class BinaryTree<
|
|
|
1019
1022
|
* values:
|
|
1020
1023
|
* @returns the height of the binary tree.
|
|
1021
1024
|
*/
|
|
1022
|
-
getHeight(
|
|
1025
|
+
getHeight(
|
|
1026
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1027
|
+
iterationType: IterationType = this.iterationType
|
|
1028
|
+
): number {
|
|
1023
1029
|
beginRoot = this.ensureNode(beginRoot);
|
|
1024
1030
|
if (!this.isRealNode(beginRoot)) return -1;
|
|
1025
1031
|
|
|
1026
|
-
if (iterationType ===
|
|
1032
|
+
if (iterationType === 'RECURSIVE') {
|
|
1027
1033
|
const _getMaxHeight = (cur: NODE | null | undefined): number => {
|
|
1028
1034
|
if (!this.isRealNode(cur)) return -1;
|
|
1029
1035
|
const leftHeight = _getMaxHeight(cur.left);
|
|
@@ -1067,11 +1073,14 @@ export class BinaryTree<
|
|
|
1067
1073
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
1068
1074
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
1069
1075
|
*/
|
|
1070
|
-
getMinHeight(
|
|
1076
|
+
getMinHeight(
|
|
1077
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1078
|
+
iterationType: IterationType = this.iterationType
|
|
1079
|
+
): number {
|
|
1071
1080
|
beginRoot = this.ensureNode(beginRoot);
|
|
1072
1081
|
if (!beginRoot) return -1;
|
|
1073
1082
|
|
|
1074
|
-
if (iterationType ===
|
|
1083
|
+
if (iterationType === 'RECURSIVE') {
|
|
1075
1084
|
const _getMinHeight = (cur: NODE | null | undefined): number => {
|
|
1076
1085
|
if (!this.isRealNode(cur)) return 0;
|
|
1077
1086
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
|
|
@@ -1167,14 +1176,14 @@ export class BinaryTree<
|
|
|
1167
1176
|
*/
|
|
1168
1177
|
getLeftMost(
|
|
1169
1178
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1170
|
-
iterationType = this.iterationType
|
|
1179
|
+
iterationType: IterationType = this.iterationType
|
|
1171
1180
|
): NODE | null | undefined {
|
|
1172
1181
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1173
1182
|
beginRoot = this.ensureNode(beginRoot);
|
|
1174
1183
|
|
|
1175
1184
|
if (!this.isRealNode(beginRoot)) return beginRoot;
|
|
1176
1185
|
|
|
1177
|
-
if (iterationType ===
|
|
1186
|
+
if (iterationType === 'RECURSIVE') {
|
|
1178
1187
|
const _traverse = (cur: NODE): NODE => {
|
|
1179
1188
|
if (!this.isRealNode(cur.left)) return cur;
|
|
1180
1189
|
return _traverse(cur.left);
|
|
@@ -1214,14 +1223,14 @@ export class BinaryTree<
|
|
|
1214
1223
|
*/
|
|
1215
1224
|
getRightMost(
|
|
1216
1225
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1217
|
-
iterationType = this.iterationType
|
|
1226
|
+
iterationType: IterationType = this.iterationType
|
|
1218
1227
|
): NODE | null | undefined {
|
|
1219
1228
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1220
1229
|
// TODO support get right most by passing key in
|
|
1221
1230
|
beginRoot = this.ensureNode(beginRoot);
|
|
1222
1231
|
if (!beginRoot) return beginRoot;
|
|
1223
1232
|
|
|
1224
|
-
if (iterationType ===
|
|
1233
|
+
if (iterationType === 'RECURSIVE') {
|
|
1225
1234
|
const _traverse = (cur: NODE): NODE => {
|
|
1226
1235
|
if (!this.isRealNode(cur.right)) return cur;
|
|
1227
1236
|
return _traverse(cur.right);
|
|
@@ -1343,18 +1352,18 @@ export class BinaryTree<
|
|
|
1343
1352
|
*/
|
|
1344
1353
|
dfs<C extends BTNCallback<NODE | null | undefined>>(
|
|
1345
1354
|
callback: C = this._defaultOneParamCallback as C,
|
|
1346
|
-
pattern: DFSOrderPattern = '
|
|
1355
|
+
pattern: DFSOrderPattern = 'IN',
|
|
1347
1356
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1348
|
-
iterationType: IterationType =
|
|
1357
|
+
iterationType: IterationType = 'ITERATIVE',
|
|
1349
1358
|
includeNull = false
|
|
1350
1359
|
): ReturnType<C>[] {
|
|
1351
1360
|
beginRoot = this.ensureNode(beginRoot);
|
|
1352
1361
|
if (!beginRoot) return [];
|
|
1353
1362
|
const ans: ReturnType<C>[] = [];
|
|
1354
|
-
if (iterationType ===
|
|
1363
|
+
if (iterationType === 'RECURSIVE') {
|
|
1355
1364
|
const _traverse = (node: NODE | null | undefined) => {
|
|
1356
1365
|
switch (pattern) {
|
|
1357
|
-
case '
|
|
1366
|
+
case 'IN':
|
|
1358
1367
|
if (includeNull) {
|
|
1359
1368
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1360
1369
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
@@ -1365,7 +1374,7 @@ export class BinaryTree<
|
|
|
1365
1374
|
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1366
1375
|
}
|
|
1367
1376
|
break;
|
|
1368
|
-
case '
|
|
1377
|
+
case 'PRE':
|
|
1369
1378
|
if (includeNull) {
|
|
1370
1379
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1371
1380
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
@@ -1376,7 +1385,7 @@ export class BinaryTree<
|
|
|
1376
1385
|
if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
|
|
1377
1386
|
}
|
|
1378
1387
|
break;
|
|
1379
|
-
case '
|
|
1388
|
+
case 'POST':
|
|
1380
1389
|
if (includeNull) {
|
|
1381
1390
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
|
|
1382
1391
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
|
|
@@ -1408,17 +1417,17 @@ export class BinaryTree<
|
|
|
1408
1417
|
ans.push(callback(cur.node));
|
|
1409
1418
|
} else {
|
|
1410
1419
|
switch (pattern) {
|
|
1411
|
-
case '
|
|
1420
|
+
case 'IN':
|
|
1412
1421
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1413
1422
|
stack.push({ opt: 1, node: cur.node });
|
|
1414
1423
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1415
1424
|
break;
|
|
1416
|
-
case '
|
|
1425
|
+
case 'PRE':
|
|
1417
1426
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1418
1427
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1419
1428
|
stack.push({ opt: 1, node: cur.node });
|
|
1420
1429
|
break;
|
|
1421
|
-
case '
|
|
1430
|
+
case 'POST':
|
|
1422
1431
|
stack.push({ opt: 1, node: cur.node });
|
|
1423
1432
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1424
1433
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
@@ -1479,7 +1488,7 @@ export class BinaryTree<
|
|
|
1479
1488
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1480
1489
|
callback: C = this._defaultOneParamCallback as C,
|
|
1481
1490
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1482
|
-
iterationType = this.iterationType,
|
|
1491
|
+
iterationType: IterationType = this.iterationType,
|
|
1483
1492
|
includeNull = false
|
|
1484
1493
|
): ReturnType<C>[] {
|
|
1485
1494
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1487,7 +1496,7 @@ export class BinaryTree<
|
|
|
1487
1496
|
|
|
1488
1497
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1489
1498
|
|
|
1490
|
-
if (iterationType ===
|
|
1499
|
+
if (iterationType === 'RECURSIVE') {
|
|
1491
1500
|
const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
|
|
1492
1501
|
|
|
1493
1502
|
const traverse = (level: number) => {
|
|
@@ -1573,14 +1582,14 @@ export class BinaryTree<
|
|
|
1573
1582
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1574
1583
|
callback: C = this._defaultOneParamCallback as C,
|
|
1575
1584
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1576
|
-
iterationType = this.iterationType,
|
|
1585
|
+
iterationType: IterationType = this.iterationType,
|
|
1577
1586
|
includeNull = false
|
|
1578
1587
|
): ReturnType<C>[][] {
|
|
1579
1588
|
beginRoot = this.ensureNode(beginRoot);
|
|
1580
1589
|
const levelsNodes: ReturnType<C>[][] = [];
|
|
1581
1590
|
if (!beginRoot) return levelsNodes;
|
|
1582
1591
|
|
|
1583
|
-
if (iterationType ===
|
|
1592
|
+
if (iterationType === 'RECURSIVE') {
|
|
1584
1593
|
const _recursive = (node: NODE | null, level: number) => {
|
|
1585
1594
|
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1586
1595
|
levelsNodes[level].push(callback(node));
|
|
@@ -1643,7 +1652,7 @@ export class BinaryTree<
|
|
|
1643
1652
|
*/
|
|
1644
1653
|
morris<C extends BTNCallback<NODE>>(
|
|
1645
1654
|
callback: C = this._defaultOneParamCallback as C,
|
|
1646
|
-
pattern: DFSOrderPattern = '
|
|
1655
|
+
pattern: DFSOrderPattern = 'IN',
|
|
1647
1656
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1648
1657
|
): ReturnType<C>[] {
|
|
1649
1658
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1672,7 +1681,7 @@ export class BinaryTree<
|
|
|
1672
1681
|
_reverseEdge(tail);
|
|
1673
1682
|
};
|
|
1674
1683
|
switch (pattern) {
|
|
1675
|
-
case '
|
|
1684
|
+
case 'IN':
|
|
1676
1685
|
while (cur) {
|
|
1677
1686
|
if (cur.left) {
|
|
1678
1687
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1688,7 +1697,7 @@ export class BinaryTree<
|
|
|
1688
1697
|
cur = cur.right;
|
|
1689
1698
|
}
|
|
1690
1699
|
break;
|
|
1691
|
-
case '
|
|
1700
|
+
case 'PRE':
|
|
1692
1701
|
while (cur) {
|
|
1693
1702
|
if (cur.left) {
|
|
1694
1703
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1706,7 +1715,7 @@ export class BinaryTree<
|
|
|
1706
1715
|
cur = cur.right;
|
|
1707
1716
|
}
|
|
1708
1717
|
break;
|
|
1709
|
-
case '
|
|
1718
|
+
case 'POST':
|
|
1710
1719
|
while (cur) {
|
|
1711
1720
|
if (cur.left) {
|
|
1712
1721
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1876,7 +1885,7 @@ export class BinaryTree<
|
|
|
1876
1885
|
protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1877
1886
|
if (!node) return;
|
|
1878
1887
|
|
|
1879
|
-
if (this.iterationType ===
|
|
1888
|
+
if (this.iterationType === 'ITERATIVE') {
|
|
1880
1889
|
const stack: (NODE | null | undefined)[] = [];
|
|
1881
1890
|
let current: NODE | null | undefined = node;
|
|
1882
1891
|
|