data-structure-typed 1.46.7 → 1.46.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 +140 -110
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +11 -422
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +10 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +19 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +121 -55
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +10 -9
- package/dist/cjs/data-structures/binary-tree/bst.js +16 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +32 -23
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -2
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +5 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +11 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +19 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +122 -55
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +10 -9
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -14
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +34 -24
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +12 -2
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +5 -4
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +198 -103
- package/dist/umd/data-structure-typed.min.js +4 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -6
- package/src/data-structures/binary-tree/avl-tree.ts +17 -5
- package/src/data-structures/binary-tree/binary-tree.ts +143 -62
- package/src/data-structures/binary-tree/bst.ts +23 -19
- package/src/data-structures/binary-tree/rb-tree.ts +38 -27
- package/src/data-structures/binary-tree/tree-multimap.ts +19 -6
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -5
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -7
- package/src/types/data-structures/binary-tree/bst.ts +3 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -1
- package/test/integration/index.html +30 -28
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +7 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +57 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +62 -7
- package/test/unit/data-structures/binary-tree/bst.test.ts +56 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +98 -42
- package/tsconfig-base.json +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binary-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/binary-tree.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AAEH,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,wCAAuB,CAAA;IACvB,wCAAuB,CAAA;AACzB,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB;AAED,IAAY,cAQX;AARD,WAAY,cAAc;IACxB,+BAAa,CAAA;IACb,+BAAa,CAAA;IACb,iCAAe,CAAA;IACf,yCAAuB,CAAA;IACvB,2CAAyB,CAAA;IACzB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;AACvB,CAAC,EARW,cAAc,8BAAd,cAAc,QAQzB
|
|
1
|
+
{"version":3,"file":"binary-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/binary-tree.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AAEH,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,wCAAuB,CAAA;IACvB,wCAAuB,CAAA;AACzB,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB;AAED,IAAY,cAQX;AARD,WAAY,cAAc;IACxB,+BAAa,CAAA;IACb,+BAAa,CAAA;IACb,iCAAe,CAAA;IACf,yCAAuB,CAAA;IACvB,2CAAyB,CAAA;IACzB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;AACvB,CAAC,EARW,cAAc,8BAAd,cAAc,QAQzB"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { BSTNode } from '../../../data-structures';
|
|
1
|
+
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
3
|
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
6
|
export type BSTOptions = BinaryTreeOptions & {
|
|
6
7
|
comparator?: BSTComparator;
|
|
7
8
|
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { RedBlackTreeNode } from '../../../data-structures';
|
|
1
|
+
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from "./bst";
|
|
3
3
|
export declare enum RBTNColor {
|
|
4
4
|
RED = 1,
|
|
5
5
|
BLACK = 0
|
|
6
6
|
}
|
|
7
7
|
export type RedBlackTreeNodeNested<T> = RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
|
+
export type RedBlackTreeNested<T, N extends RedBlackTreeNode<T, N>> = RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
9
|
export type RBTreeOptions = BSTOptions & {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TreeMultimapNode } from '../../../data-structures';
|
|
1
|
+
import { TreeMultimap, TreeMultimapNode } from '../../../data-structures';
|
|
2
2
|
import { AVLTreeOptions } from './avl-tree';
|
|
3
3
|
export type TreeMultimapNodeNested<T> = TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type TreeMultimapNested<T, N extends TreeMultimapNode<T, N>> = TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
5
|
export type TreeMultimapOptions = Omit<AVLTreeOptions, 'isMergeDuplicatedNodeByKey'> & {};
|
|
@@ -6,14 +6,15 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
10
|
import { BTNCallback } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
14
14
|
constructor(key: BTNKey, value?: V);
|
|
15
15
|
}
|
|
16
|
-
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
16
|
+
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
17
|
+
options: AVLTreeOptions;
|
|
17
18
|
/**
|
|
18
19
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
19
20
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -31,6 +32,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
31
32
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
32
33
|
*/
|
|
33
34
|
createNode(key: BTNKey, value?: V): N;
|
|
35
|
+
createTree(options?: AVLTreeOptions): TREE;
|
|
34
36
|
/**
|
|
35
37
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
36
38
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
+
import { IterationType } from '../../types';
|
|
9
10
|
export class AVLTreeNode extends BSTNode {
|
|
10
11
|
height;
|
|
11
12
|
constructor(key, value) {
|
|
@@ -14,6 +15,7 @@ export class AVLTreeNode extends BSTNode {
|
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
export class AVLTree extends BST {
|
|
18
|
+
options;
|
|
17
19
|
/**
|
|
18
20
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
19
21
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -22,6 +24,12 @@ export class AVLTree extends BST {
|
|
|
22
24
|
*/
|
|
23
25
|
constructor(options) {
|
|
24
26
|
super(options);
|
|
27
|
+
if (options) {
|
|
28
|
+
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
32
|
+
}
|
|
25
33
|
}
|
|
26
34
|
/**
|
|
27
35
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -35,6 +43,9 @@ export class AVLTree extends BST {
|
|
|
35
43
|
createNode(key, value) {
|
|
36
44
|
return new AVLTreeNode(key, value);
|
|
37
45
|
}
|
|
46
|
+
createTree(options) {
|
|
47
|
+
return new AVLTree({ ...this.options, ...options });
|
|
48
|
+
}
|
|
38
49
|
/**
|
|
39
50
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
40
51
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
9
|
+
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
12
12
|
* Represents a node in a binary tree.
|
|
@@ -62,8 +62,8 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
62
62
|
* Represents a binary tree data structure.
|
|
63
63
|
* @template N - The type of the binary tree's nodes.
|
|
64
64
|
*/
|
|
65
|
-
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
|
|
66
|
-
|
|
65
|
+
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> {
|
|
66
|
+
options: BinaryTreeOptions;
|
|
67
67
|
/**
|
|
68
68
|
* Creates a new instance of BinaryTree.
|
|
69
69
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -86,6 +86,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
86
86
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
87
87
|
*/
|
|
88
88
|
createNode(key: BTNKey, value?: V): N;
|
|
89
|
+
createTree(options?: BinaryTreeOptions): TREE;
|
|
89
90
|
/**
|
|
90
91
|
* Time Complexity: O(n)
|
|
91
92
|
* Space Complexity: O(1)
|
|
@@ -181,7 +182,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
181
182
|
* values:
|
|
182
183
|
* @returns the height of the binary tree.
|
|
183
184
|
*/
|
|
184
|
-
getHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): number;
|
|
185
|
+
getHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType | undefined): number;
|
|
185
186
|
/**
|
|
186
187
|
* Time Complexity: O(n)
|
|
187
188
|
* Space Complexity: O(log n)
|
|
@@ -200,7 +201,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
200
201
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
201
202
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
202
203
|
*/
|
|
203
|
-
getMinHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): number;
|
|
204
|
+
getMinHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType | undefined): number;
|
|
204
205
|
/**
|
|
205
206
|
* Time Complexity: O(n)
|
|
206
207
|
* Space Complexity: O(log n)
|
|
@@ -306,7 +307,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
306
307
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
307
308
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
308
309
|
*/
|
|
309
|
-
getLeftMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
310
|
+
getLeftMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType | undefined): N | null | undefined;
|
|
310
311
|
/**
|
|
311
312
|
* Time Complexity: O(log n)
|
|
312
313
|
* Space Complexity: O(1)
|
|
@@ -326,7 +327,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
326
327
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
327
328
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
328
329
|
*/
|
|
329
|
-
getRightMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
330
|
+
getRightMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType | undefined): N | null | undefined;
|
|
330
331
|
/**
|
|
331
332
|
* Time Complexity: O(n)
|
|
332
333
|
* Space Complexity: O(1)
|
|
@@ -343,7 +344,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
343
344
|
* possible values:
|
|
344
345
|
* @returns a boolean value.
|
|
345
346
|
*/
|
|
346
|
-
isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType?: IterationType): boolean;
|
|
347
|
+
isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType?: IterationType | undefined): boolean;
|
|
347
348
|
/**
|
|
348
349
|
* Time Complexity: O(n)
|
|
349
350
|
* Space Complexity: O(1)
|
|
@@ -359,7 +360,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
359
360
|
* expected to be
|
|
360
361
|
* @returns a boolean value.
|
|
361
362
|
*/
|
|
362
|
-
isBST(iterationType?: IterationType): boolean;
|
|
363
|
+
isBST(iterationType?: IterationType | undefined): boolean;
|
|
363
364
|
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
364
365
|
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
365
366
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
@@ -429,6 +430,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
429
430
|
* by the return type of the `callback` function.
|
|
430
431
|
*/
|
|
431
432
|
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKey | N | null | undefined): ReturnType<C>[];
|
|
433
|
+
forEach(callback: (entry: [BTNKey, V | undefined], tree: typeof this) => void): void;
|
|
434
|
+
filter(predicate: (entry: [BTNKey, V | undefined], tree: typeof this) => boolean): TREE;
|
|
435
|
+
map(callback: (entry: [BTNKey, V | undefined], tree: typeof this) => V): TREE;
|
|
436
|
+
reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: typeof this) => T, initialValue: T): T;
|
|
432
437
|
/**
|
|
433
438
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
434
439
|
* either an iterative or recursive manner.
|
|
@@ -438,15 +443,16 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
438
443
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
439
444
|
* binary tree nodes in a specific order.
|
|
440
445
|
*/
|
|
441
|
-
[Symbol.iterator](node?: N | null | undefined): Generator<BTNKey, void, undefined>;
|
|
446
|
+
[Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>;
|
|
442
447
|
/**
|
|
443
448
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
444
|
-
* @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
|
|
449
|
+
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
445
450
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
446
451
|
* following types:
|
|
452
|
+
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
447
453
|
*/
|
|
448
|
-
print(beginRoot?: BTNKey | N | null | undefined): void;
|
|
449
|
-
protected _displayAux(node: N | null | undefined
|
|
454
|
+
print(beginRoot?: BTNKey | N | null | undefined, options?: BinaryTreePrintOptions): void;
|
|
455
|
+
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
450
456
|
protected _defaultOneParamCallback: (node: N) => number;
|
|
451
457
|
/**
|
|
452
458
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -92,15 +92,17 @@ export class BinaryTreeNode {
|
|
|
92
92
|
* @template N - The type of the binary tree's nodes.
|
|
93
93
|
*/
|
|
94
94
|
export class BinaryTree {
|
|
95
|
-
|
|
95
|
+
options;
|
|
96
96
|
/**
|
|
97
97
|
* Creates a new instance of BinaryTree.
|
|
98
98
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
99
99
|
*/
|
|
100
100
|
constructor(options) {
|
|
101
101
|
if (options) {
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
this.options = { iterationType: IterationType.ITERATIVE, ...options };
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
this.options = { iterationType: IterationType.ITERATIVE };
|
|
104
106
|
}
|
|
105
107
|
this._size = 0;
|
|
106
108
|
}
|
|
@@ -127,6 +129,9 @@ export class BinaryTree {
|
|
|
127
129
|
createNode(key, value) {
|
|
128
130
|
return new BinaryTreeNode(key, value);
|
|
129
131
|
}
|
|
132
|
+
createTree(options) {
|
|
133
|
+
return new BinaryTree({ ...this.options, ...options });
|
|
134
|
+
}
|
|
130
135
|
/**
|
|
131
136
|
* Time Complexity: O(n)
|
|
132
137
|
* Space Complexity: O(1)
|
|
@@ -358,7 +363,7 @@ export class BinaryTree {
|
|
|
358
363
|
* values:
|
|
359
364
|
* @returns the height of the binary tree.
|
|
360
365
|
*/
|
|
361
|
-
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
366
|
+
getHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
362
367
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
363
368
|
if (!beginRoot)
|
|
364
369
|
return -1;
|
|
@@ -404,7 +409,7 @@ export class BinaryTree {
|
|
|
404
409
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
405
410
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
406
411
|
*/
|
|
407
|
-
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
412
|
+
getMinHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
408
413
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
409
414
|
if (!beginRoot)
|
|
410
415
|
return -1;
|
|
@@ -495,7 +500,7 @@ export class BinaryTree {
|
|
|
495
500
|
* traverse the binary tree. It can have two possible values:
|
|
496
501
|
* @returns an array of nodes of type `N`.
|
|
497
502
|
*/
|
|
498
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
503
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
499
504
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
500
505
|
callback = (node => node);
|
|
501
506
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -557,7 +562,7 @@ export class BinaryTree {
|
|
|
557
562
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
558
563
|
* @returns a boolean value.
|
|
559
564
|
*/
|
|
560
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
565
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
561
566
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
562
567
|
callback = (node => node);
|
|
563
568
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
@@ -587,7 +592,7 @@ export class BinaryTree {
|
|
|
587
592
|
* nodes are visited during the search.
|
|
588
593
|
* @returns a value of type `N | null | undefined`.
|
|
589
594
|
*/
|
|
590
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
595
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
591
596
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
592
597
|
callback = (node => node);
|
|
593
598
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -679,7 +684,7 @@ export class BinaryTree {
|
|
|
679
684
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
680
685
|
* found, `undefined` is returned.
|
|
681
686
|
*/
|
|
682
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
687
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
683
688
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
684
689
|
callback = (node => node);
|
|
685
690
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
@@ -749,7 +754,7 @@ export class BinaryTree {
|
|
|
749
754
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
750
755
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
751
756
|
*/
|
|
752
|
-
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
757
|
+
getLeftMost(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
753
758
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
754
759
|
if (!beginRoot)
|
|
755
760
|
return beginRoot;
|
|
@@ -790,7 +795,7 @@ export class BinaryTree {
|
|
|
790
795
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
791
796
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
792
797
|
*/
|
|
793
|
-
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
798
|
+
getRightMost(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
794
799
|
// TODO support get right most by passing key in
|
|
795
800
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
796
801
|
if (!beginRoot)
|
|
@@ -829,7 +834,7 @@ export class BinaryTree {
|
|
|
829
834
|
* possible values:
|
|
830
835
|
* @returns a boolean value.
|
|
831
836
|
*/
|
|
832
|
-
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
837
|
+
isSubtreeBST(beginRoot, iterationType = this.options.iterationType) {
|
|
833
838
|
// TODO there is a bug
|
|
834
839
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
835
840
|
if (!beginRoot)
|
|
@@ -876,7 +881,7 @@ export class BinaryTree {
|
|
|
876
881
|
* expected to be
|
|
877
882
|
* @returns a boolean value.
|
|
878
883
|
*/
|
|
879
|
-
isBST(iterationType = this.iterationType) {
|
|
884
|
+
isBST(iterationType = this.options.iterationType) {
|
|
880
885
|
if (this.root === null)
|
|
881
886
|
return true;
|
|
882
887
|
return this.isSubtreeBST(this.root, iterationType);
|
|
@@ -901,13 +906,13 @@ export class BinaryTree {
|
|
|
901
906
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
902
907
|
* performed on the subtree. It can have two possible values:
|
|
903
908
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
904
|
-
* whether
|
|
909
|
+
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
905
910
|
* traversal will include null values, otherwise it will skip them.
|
|
906
911
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
907
912
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
908
913
|
* by the return type of the `callback` function.
|
|
909
914
|
*/
|
|
910
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
915
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
911
916
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
912
917
|
const ans = [];
|
|
913
918
|
if (!beginRoot)
|
|
@@ -1134,12 +1139,12 @@ export class BinaryTree {
|
|
|
1134
1139
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1135
1140
|
* performed during the breadth-first search (BFS). It can have two possible values:
|
|
1136
1141
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
|
|
1137
|
-
*
|
|
1142
|
+
* to include null values in the breadth-first search traversal. If `includeNull` is set to
|
|
1138
1143
|
* `true`, null values will be included in the traversal, otherwise they will be skipped.
|
|
1139
1144
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1140
1145
|
* the breadth-first traversal of a binary tree.
|
|
1141
1146
|
*/
|
|
1142
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1147
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
1143
1148
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1144
1149
|
if (!beginRoot)
|
|
1145
1150
|
return [];
|
|
@@ -1211,12 +1216,12 @@ export class BinaryTree {
|
|
|
1211
1216
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1212
1217
|
* performed on the tree. It can have two possible values:
|
|
1213
1218
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1214
|
-
* whether
|
|
1219
|
+
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1215
1220
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1216
1221
|
* be excluded
|
|
1217
1222
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1218
1223
|
*/
|
|
1219
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1224
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
1220
1225
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1221
1226
|
const levelsNodes = [];
|
|
1222
1227
|
if (!beginRoot)
|
|
@@ -1415,6 +1420,42 @@ export class BinaryTree {
|
|
|
1415
1420
|
}
|
|
1416
1421
|
return ans;
|
|
1417
1422
|
}
|
|
1423
|
+
forEach(callback) {
|
|
1424
|
+
for (const entry of this) {
|
|
1425
|
+
callback(entry, this);
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
filter(predicate) {
|
|
1429
|
+
const newTree = this.createTree();
|
|
1430
|
+
for (const [key, value] of this) {
|
|
1431
|
+
if (predicate([key, value], this)) {
|
|
1432
|
+
newTree.add(key, value);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
return newTree;
|
|
1436
|
+
}
|
|
1437
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1438
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: typeof this) => NV) {
|
|
1439
|
+
// const newTree = this.createTree();
|
|
1440
|
+
// for (const [key, value] of this) {
|
|
1441
|
+
// newTree.add(key, callback([key, value], this));
|
|
1442
|
+
// }
|
|
1443
|
+
// return newTree;
|
|
1444
|
+
// }
|
|
1445
|
+
map(callback) {
|
|
1446
|
+
const newTree = this.createTree();
|
|
1447
|
+
for (const [key, value] of this) {
|
|
1448
|
+
newTree.add(key, callback([key, value], this));
|
|
1449
|
+
}
|
|
1450
|
+
return newTree;
|
|
1451
|
+
}
|
|
1452
|
+
reduce(callback, initialValue) {
|
|
1453
|
+
let accumulator = initialValue;
|
|
1454
|
+
for (const [key, value] of this) {
|
|
1455
|
+
accumulator = callback(accumulator, [key, value], this);
|
|
1456
|
+
}
|
|
1457
|
+
return accumulator;
|
|
1458
|
+
}
|
|
1418
1459
|
/**
|
|
1419
1460
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1420
1461
|
* either an iterative or recursive manner.
|
|
@@ -1425,78 +1466,104 @@ export class BinaryTree {
|
|
|
1425
1466
|
* binary tree nodes in a specific order.
|
|
1426
1467
|
*/
|
|
1427
1468
|
*[Symbol.iterator](node = this.root) {
|
|
1428
|
-
if (!node)
|
|
1469
|
+
if (!node)
|
|
1429
1470
|
return;
|
|
1430
|
-
|
|
1431
|
-
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1471
|
+
if (this.options.iterationType === IterationType.ITERATIVE) {
|
|
1432
1472
|
const stack = [];
|
|
1433
1473
|
let current = node;
|
|
1434
1474
|
while (current || stack.length > 0) {
|
|
1435
|
-
while (current) {
|
|
1475
|
+
while (current && !isNaN(current.key)) {
|
|
1436
1476
|
stack.push(current);
|
|
1437
1477
|
current = current.left;
|
|
1438
1478
|
}
|
|
1439
1479
|
current = stack.pop();
|
|
1440
|
-
if (current)
|
|
1441
|
-
yield current.key;
|
|
1442
|
-
if (current)
|
|
1480
|
+
if (current && !isNaN(current.key)) {
|
|
1481
|
+
yield [current.key, current.value];
|
|
1443
1482
|
current = current.right;
|
|
1483
|
+
}
|
|
1444
1484
|
}
|
|
1445
1485
|
}
|
|
1446
1486
|
else {
|
|
1447
|
-
if (node.left) {
|
|
1487
|
+
if (node.left && !isNaN(node.key)) {
|
|
1448
1488
|
yield* this[Symbol.iterator](node.left);
|
|
1449
1489
|
}
|
|
1450
|
-
yield node.key;
|
|
1451
|
-
if (node.right) {
|
|
1490
|
+
yield [node.key, node.value];
|
|
1491
|
+
if (node.right && !isNaN(node.key)) {
|
|
1452
1492
|
yield* this[Symbol.iterator](node.right);
|
|
1453
1493
|
}
|
|
1454
1494
|
}
|
|
1455
1495
|
}
|
|
1456
1496
|
/**
|
|
1457
1497
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1458
|
-
* @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
|
|
1498
|
+
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
1459
1499
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1460
1500
|
* following types:
|
|
1501
|
+
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
1461
1502
|
*/
|
|
1462
|
-
print(beginRoot = this.root) {
|
|
1503
|
+
print(beginRoot = this.root, options) {
|
|
1504
|
+
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1463
1505
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1464
1506
|
if (!beginRoot)
|
|
1465
1507
|
return;
|
|
1508
|
+
if (opts.isShowUndefined)
|
|
1509
|
+
console.log(`U for undefined
|
|
1510
|
+
`);
|
|
1511
|
+
if (opts.isShowNull)
|
|
1512
|
+
console.log(`N for null
|
|
1513
|
+
`);
|
|
1514
|
+
if (opts.isShowRedBlackNIL)
|
|
1515
|
+
console.log(`S for Sentinel Node
|
|
1516
|
+
`);
|
|
1466
1517
|
const display = (root) => {
|
|
1467
|
-
const [lines, , ,] = this._displayAux(root);
|
|
1518
|
+
const [lines, , ,] = this._displayAux(root, opts);
|
|
1468
1519
|
for (const line of lines) {
|
|
1469
1520
|
console.log(line);
|
|
1470
1521
|
}
|
|
1471
1522
|
};
|
|
1472
1523
|
display(beginRoot);
|
|
1473
1524
|
}
|
|
1474
|
-
_displayAux(node) {
|
|
1475
|
-
|
|
1476
|
-
|
|
1525
|
+
_displayAux(node, options) {
|
|
1526
|
+
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1527
|
+
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
1528
|
+
// Check if node is null or undefined or key is NaN
|
|
1529
|
+
if (node === null && !isShowNull) {
|
|
1530
|
+
return emptyDisplayLayout;
|
|
1531
|
+
}
|
|
1532
|
+
else if (node === undefined && !isShowUndefined) {
|
|
1533
|
+
return emptyDisplayLayout;
|
|
1477
1534
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
if (!node.left && !node.right) {
|
|
1481
|
-
return [[line], width, 1, Math.floor(width / 2)];
|
|
1535
|
+
else if (node !== null && node !== undefined && isNaN(node.key) && !isShowRedBlackNIL) {
|
|
1536
|
+
return emptyDisplayLayout;
|
|
1482
1537
|
}
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
const
|
|
1496
|
-
const
|
|
1497
|
-
|
|
1538
|
+
else if (node !== null && node !== undefined) {
|
|
1539
|
+
// Display logic of normal nodes
|
|
1540
|
+
const key = node.key, line = isNaN(key) ? 'S' : key.toString(), width = line.length;
|
|
1541
|
+
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1542
|
+
}
|
|
1543
|
+
else {
|
|
1544
|
+
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1545
|
+
const line = node === undefined ? 'U' : 'N', width = line.length;
|
|
1546
|
+
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
1547
|
+
}
|
|
1548
|
+
function _buildNodeDisplay(line, width, left, right) {
|
|
1549
|
+
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
1550
|
+
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
1551
|
+
const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
|
|
1552
|
+
+ '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
|
|
1553
|
+
+ line
|
|
1554
|
+
+ '_'.repeat(Math.max(0, rightMiddle))
|
|
1555
|
+
+ ' '.repeat(Math.max(0, rightWidth - rightMiddle));
|
|
1556
|
+
const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
|
|
1557
|
+
+ ' '.repeat(width)
|
|
1558
|
+
+ (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
|
|
1559
|
+
const mergedLines = [firstLine, secondLine];
|
|
1560
|
+
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
1561
|
+
const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
|
|
1562
|
+
const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
|
|
1563
|
+
mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
|
|
1564
|
+
}
|
|
1565
|
+
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
1498
1566
|
}
|
|
1499
|
-
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
1500
1567
|
}
|
|
1501
1568
|
_defaultOneParamCallback = (node) => node.key;
|
|
1502
1569
|
/**
|