undirected-graph-typed 1.51.7 → 1.51.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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -10
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +12 -12
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -12
- package/dist/data-structures/binary-tree/binary-tree.js +22 -32
- package/dist/data-structures/binary-tree/bst.d.ts +32 -77
- package/dist/data-structures/binary-tree/bst.js +68 -136
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -13
- package/dist/data-structures/binary-tree/rb-tree.js +3 -20
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +29 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +4 -12
- package/src/data-structures/binary-tree/avl-tree.ts +15 -14
- package/src/data-structures/binary-tree/binary-tree.ts +29 -38
- package/src/data-structures/binary-tree/bst.ts +78 -148
- package/src/data-structures/binary-tree/rb-tree.ts +8 -22
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -3
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
-
export declare class AVLTreeMultiMapNode<K
|
|
11
|
+
export declare class AVLTreeMultiMapNode<K extends Comparable, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
14
14
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -36,7 +36,7 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
36
36
|
/**
|
|
37
37
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
38
38
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K
|
|
39
|
+
export declare class AVLTreeMultiMap<K extends Comparable, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>> extends AVLTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
40
40
|
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeMultiMapOptions<K>);
|
|
41
41
|
protected _count: number;
|
|
42
42
|
/**
|
|
@@ -68,15 +68,6 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
68
68
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
69
69
|
*/
|
|
70
70
|
createNode(key: K, value?: V, count?: number): NODE;
|
|
71
|
-
/**
|
|
72
|
-
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
73
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
74
|
-
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
75
|
-
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
76
|
-
* the tree, respectively. These properties can be
|
|
77
|
-
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
78
|
-
* default options. The returned value is casted as `TREE`.
|
|
79
|
-
*/
|
|
80
71
|
createTree(options?: AVLTreeMultiMapOptions<K>): TREE;
|
|
81
72
|
/**
|
|
82
73
|
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
@@ -83,17 +83,8 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
83
83
|
createNode(key, value, count) {
|
|
84
84
|
return new AVLTreeMultiMapNode(key, value, count);
|
|
85
85
|
}
|
|
86
|
-
/**
|
|
87
|
-
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
88
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
89
|
-
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
90
|
-
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
91
|
-
* the tree, respectively. These properties can be
|
|
92
|
-
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
93
|
-
* default options. The returned value is casted as `TREE`.
|
|
94
|
-
*/
|
|
95
86
|
createTree(options) {
|
|
96
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType,
|
|
87
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
97
88
|
}
|
|
98
89
|
/**
|
|
99
90
|
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, KeyOrNodeOrEntry } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class AVLTreeNode<K
|
|
11
|
+
export declare class AVLTreeNode<K extends Comparable, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes a new instance of a class with a key and an optional value,
|
|
14
14
|
* and sets the height property to 0.
|
|
@@ -40,7 +40,7 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
40
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
41
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
42
42
|
*/
|
|
43
|
-
export declare class AVLTree<K
|
|
43
|
+
export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
44
44
|
/**
|
|
45
45
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
46
46
|
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
@@ -83,7 +83,7 @@ class AVLTree extends bst_1.BST {
|
|
|
83
83
|
* @returns a new AVLTree object.
|
|
84
84
|
*/
|
|
85
85
|
createTree(options) {
|
|
86
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType,
|
|
86
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
@@ -157,21 +157,21 @@ class AVLTree extends bst_1.BST {
|
|
|
157
157
|
* if either `srcNode` or `destNode` is undefined.
|
|
158
158
|
*/
|
|
159
159
|
_swapProperties(srcNode, destNode) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (
|
|
163
|
-
const { key, value, height } =
|
|
160
|
+
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
161
|
+
const destNodeEnsured = this.ensureNode(destNode);
|
|
162
|
+
if (srcNodeEnsured && destNodeEnsured) {
|
|
163
|
+
const { key, value, height } = destNodeEnsured;
|
|
164
164
|
const tempNode = this.createNode(key, value);
|
|
165
165
|
if (tempNode) {
|
|
166
166
|
tempNode.height = height;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
167
|
+
destNodeEnsured.key = srcNodeEnsured.key;
|
|
168
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
169
|
+
destNodeEnsured.height = srcNodeEnsured.height;
|
|
170
|
+
srcNodeEnsured.key = tempNode.key;
|
|
171
|
+
srcNodeEnsured.value = tempNode.value;
|
|
172
|
+
srcNodeEnsured.height = tempNode.height;
|
|
173
173
|
}
|
|
174
|
-
return
|
|
174
|
+
return destNodeEnsured;
|
|
175
175
|
}
|
|
176
176
|
return undefined;
|
|
177
177
|
}
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, DFSOrderPattern, EntryCallback, KeyOrNodeOrEntry, NodeDisplayLayout } from '../../types';
|
|
9
|
-
import { FamilyPosition, IterationType } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, Comparable, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, KeyOrNodeOrEntry, NodeDisplayLayout } from '../../types';
|
|
10
9
|
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { IterableEntryBase } from '../base';
|
|
12
11
|
/**
|
|
@@ -14,7 +13,7 @@ import { IterableEntryBase } from '../base';
|
|
|
14
13
|
* @template V - The type of data stored in the node.
|
|
15
14
|
* @template NODE - The type of the family relationship in the binary tree.
|
|
16
15
|
*/
|
|
17
|
-
export declare class BinaryTreeNode<K
|
|
16
|
+
export declare class BinaryTreeNode<K extends Comparable, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> {
|
|
18
17
|
key: K;
|
|
19
18
|
value?: V;
|
|
20
19
|
parent?: NODE;
|
|
@@ -66,7 +65,7 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
66
65
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
67
66
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
68
67
|
*/
|
|
69
|
-
export declare class BinaryTree<K
|
|
68
|
+
export declare class BinaryTree<K extends Comparable, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, NODE, TREE> {
|
|
70
69
|
iterationType: IterationType;
|
|
71
70
|
/**
|
|
72
71
|
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
|
|
@@ -77,13 +76,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
77
76
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
78
77
|
* required.
|
|
79
78
|
*/
|
|
80
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: BinaryTreeOptions
|
|
81
|
-
protected _extractor: (key: K) => number;
|
|
82
|
-
/**
|
|
83
|
-
* The function returns the value of the `_extractor` property.
|
|
84
|
-
* @returns The `_extractor` property is being returned.
|
|
85
|
-
*/
|
|
86
|
-
get extractor(): (key: K) => number;
|
|
79
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: BinaryTreeOptions);
|
|
87
80
|
protected _root?: NODE | null;
|
|
88
81
|
/**
|
|
89
82
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
@@ -117,7 +110,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
117
110
|
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
118
111
|
* @returns a new instance of a binary tree.
|
|
119
112
|
*/
|
|
120
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
113
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
121
114
|
/**
|
|
122
115
|
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
123
116
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
@@ -106,27 +106,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
106
106
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
|
-
this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
110
109
|
this._NIL = new BinaryTreeNode(NaN);
|
|
111
110
|
this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
112
111
|
if (options) {
|
|
113
|
-
const { iterationType
|
|
112
|
+
const { iterationType } = options;
|
|
114
113
|
if (iterationType)
|
|
115
114
|
this.iterationType = iterationType;
|
|
116
|
-
if (extractor)
|
|
117
|
-
this._extractor = extractor;
|
|
118
115
|
}
|
|
119
116
|
this._size = 0;
|
|
120
117
|
if (keysOrNodesOrEntries)
|
|
121
118
|
this.addMany(keysOrNodesOrEntries);
|
|
122
119
|
}
|
|
123
|
-
/**
|
|
124
|
-
* The function returns the value of the `_extractor` property.
|
|
125
|
-
* @returns The `_extractor` property is being returned.
|
|
126
|
-
*/
|
|
127
|
-
get extractor() {
|
|
128
|
-
return this._extractor;
|
|
129
|
-
}
|
|
130
120
|
/**
|
|
131
121
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
132
122
|
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
@@ -715,7 +705,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
715
705
|
const dfs = (cur, min, max) => {
|
|
716
706
|
if (!this.isRealNode(cur))
|
|
717
707
|
return true;
|
|
718
|
-
const numKey =
|
|
708
|
+
const numKey = Number(cur.key);
|
|
719
709
|
if (numKey <= min || numKey >= max)
|
|
720
710
|
return false;
|
|
721
711
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -736,7 +726,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
736
726
|
curr = curr.left;
|
|
737
727
|
}
|
|
738
728
|
curr = stack.pop();
|
|
739
|
-
const numKey =
|
|
729
|
+
const numKey = Number(curr.key);
|
|
740
730
|
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
741
731
|
return false;
|
|
742
732
|
prev = numKey;
|
|
@@ -766,15 +756,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
766
756
|
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
767
757
|
*/
|
|
768
758
|
getDepth(dist, beginRoot = this.root) {
|
|
769
|
-
|
|
770
|
-
|
|
759
|
+
let distEnsured = this.ensureNode(dist);
|
|
760
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
771
761
|
let depth = 0;
|
|
772
|
-
while (
|
|
773
|
-
if (
|
|
762
|
+
while (distEnsured === null || distEnsured === void 0 ? void 0 : distEnsured.parent) {
|
|
763
|
+
if (distEnsured === beginRootEnsured) {
|
|
774
764
|
return depth;
|
|
775
765
|
}
|
|
776
766
|
depth++;
|
|
777
|
-
|
|
767
|
+
distEnsured = distEnsured.parent;
|
|
778
768
|
}
|
|
779
769
|
return depth;
|
|
780
770
|
}
|
|
@@ -908,16 +898,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
908
898
|
getPathToRoot(beginNode, isReverse = true) {
|
|
909
899
|
// TODO to support get path through passing key
|
|
910
900
|
const result = [];
|
|
911
|
-
|
|
912
|
-
if (!
|
|
901
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
902
|
+
if (!beginNodeEnsured)
|
|
913
903
|
return result;
|
|
914
|
-
while (
|
|
904
|
+
while (beginNodeEnsured.parent) {
|
|
915
905
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
916
906
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
917
|
-
result.push(
|
|
918
|
-
|
|
907
|
+
result.push(beginNodeEnsured);
|
|
908
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
919
909
|
}
|
|
920
|
-
result.push(
|
|
910
|
+
result.push(beginNodeEnsured);
|
|
921
911
|
return isReverse ? result.reverse() : result;
|
|
922
912
|
}
|
|
923
913
|
/**
|
|
@@ -1563,10 +1553,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1563
1553
|
console.log(`U for undefined
|
|
1564
1554
|
`);
|
|
1565
1555
|
if (opts.isShowNull)
|
|
1566
|
-
console.log(`
|
|
1556
|
+
console.log(`N for null
|
|
1567
1557
|
`);
|
|
1568
1558
|
if (opts.isShowRedBlackNIL)
|
|
1569
|
-
console.log(`S for Sentinel Node
|
|
1559
|
+
console.log(`S for Sentinel Node(NIL)
|
|
1570
1560
|
`);
|
|
1571
1561
|
const display = (root) => {
|
|
1572
1562
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1592,23 +1582,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1592
1582
|
const stack = [];
|
|
1593
1583
|
let current = node;
|
|
1594
1584
|
while (current || stack.length > 0) {
|
|
1595
|
-
while (
|
|
1585
|
+
while (this.isRealNode(current)) {
|
|
1596
1586
|
stack.push(current);
|
|
1597
1587
|
current = current.left;
|
|
1598
1588
|
}
|
|
1599
1589
|
current = stack.pop();
|
|
1600
|
-
if (
|
|
1590
|
+
if (this.isRealNode(current)) {
|
|
1601
1591
|
yield [current.key, current.value];
|
|
1602
1592
|
current = current.right;
|
|
1603
1593
|
}
|
|
1604
1594
|
}
|
|
1605
1595
|
}
|
|
1606
1596
|
else {
|
|
1607
|
-
if (node.left &&
|
|
1597
|
+
if (node.left && this.isRealNode(node)) {
|
|
1608
1598
|
yield* this[Symbol.iterator](node.left);
|
|
1609
1599
|
}
|
|
1610
1600
|
yield [node.key, node.value];
|
|
1611
|
-
if (node.right &&
|
|
1601
|
+
if (node.right && this.isRealNode(node)) {
|
|
1612
1602
|
yield* this[Symbol.iterator](node.right);
|
|
1613
1603
|
}
|
|
1614
1604
|
}
|
|
@@ -1637,12 +1627,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1637
1627
|
else if (node === undefined && !isShowUndefined) {
|
|
1638
1628
|
return emptyDisplayLayout;
|
|
1639
1629
|
}
|
|
1640
|
-
else if (
|
|
1630
|
+
else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1641
1631
|
return emptyDisplayLayout;
|
|
1642
1632
|
}
|
|
1643
1633
|
else if (node !== null && node !== undefined) {
|
|
1644
1634
|
// Display logic of normal nodes
|
|
1645
|
-
const key = node.key, line =
|
|
1635
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
|
|
1646
1636
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1647
1637
|
}
|
|
1648
1638
|
else {
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
8
|
+
import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, Comparable, Comparator, CP, DFSOrderPattern, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
10
9
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<K
|
|
11
|
+
export declare class BSTNode<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
13
12
|
parent?: NODE;
|
|
14
13
|
constructor(key: K, value?: V);
|
|
15
14
|
protected _left?: NODE;
|
|
@@ -47,12 +46,13 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
47
46
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
48
47
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
49
48
|
*/
|
|
50
|
-
export declare class BST<K
|
|
49
|
+
export declare class BST<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
51
50
|
/**
|
|
52
|
-
* This is the constructor function for a
|
|
53
|
-
*
|
|
54
|
-
* @param keysOrNodesOrEntries -
|
|
55
|
-
* to initialize the binary search tree with the provided
|
|
51
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
|
|
52
|
+
* the tree with keys, nodes, or entries and optional options.
|
|
53
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
54
|
+
* contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
|
|
55
|
+
* keys, nodes, or entries.
|
|
56
56
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
57
57
|
* configuration options for the binary search tree. It can have the following properties:
|
|
58
58
|
*/
|
|
@@ -63,12 +63,12 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
63
63
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
64
64
|
*/
|
|
65
65
|
get root(): NODE | undefined;
|
|
66
|
-
protected
|
|
66
|
+
protected _comparator: Comparator<K>;
|
|
67
67
|
/**
|
|
68
|
-
* The function returns the value of the
|
|
69
|
-
* @returns The
|
|
68
|
+
* The function returns the value of the _comparator property.
|
|
69
|
+
* @returns The `_comparator` property is being returned.
|
|
70
70
|
*/
|
|
71
|
-
get
|
|
71
|
+
get comparator(): Comparator<K>;
|
|
72
72
|
/**
|
|
73
73
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
74
74
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -127,13 +127,11 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
127
127
|
* Time Complexity: O(log n)
|
|
128
128
|
* Space Complexity: O(1)
|
|
129
129
|
*
|
|
130
|
-
* The `add` function adds a new node to a binary tree
|
|
131
|
-
*
|
|
132
|
-
* @param keyOrNodeOrEntry -
|
|
133
|
-
* @param {V} [value] - The
|
|
134
|
-
*
|
|
135
|
-
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
136
|
-
* node was not added.
|
|
130
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
|
|
131
|
+
* updating the value if the key already exists.
|
|
132
|
+
* @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
|
|
133
|
+
* @param {V} [value] - The value to be added to the binary search tree.
|
|
134
|
+
* @returns The method returns a boolean value.
|
|
137
135
|
*/
|
|
138
136
|
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
139
137
|
/**
|
|
@@ -144,21 +142,24 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
144
142
|
* Time Complexity: O(k log n)
|
|
145
143
|
* Space Complexity: O(k + log n)
|
|
146
144
|
*
|
|
147
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
145
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
|
|
146
|
+
* the structure if specified, and returns an array indicating whether each key or node was
|
|
147
|
+
* successfully inserted.
|
|
148
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
149
|
+
* data structure.
|
|
151
150
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
152
151
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
153
152
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
154
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
155
|
-
*
|
|
156
|
-
* algorithm. If set to false, the
|
|
157
|
-
*
|
|
158
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
159
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
160
|
-
* `this.iterationType`, which
|
|
161
|
-
*
|
|
153
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
154
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
155
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
156
|
+
* value is true.
|
|
157
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
158
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
|
|
159
|
+
* has a default value of `this.iterationType`, which means it will use the iteration type specified
|
|
160
|
+
* in the binary tree instance.
|
|
161
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each key or node
|
|
162
|
+
* or entry was successfully inserted into the data structure.
|
|
162
163
|
*/
|
|
163
164
|
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
164
165
|
/**
|
|
@@ -304,24 +305,6 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
304
305
|
* function.
|
|
305
306
|
*/
|
|
306
307
|
listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[][];
|
|
307
|
-
/**
|
|
308
|
-
* Time Complexity: O(log n)
|
|
309
|
-
* Space Complexity: O(1)
|
|
310
|
-
*/
|
|
311
|
-
/**
|
|
312
|
-
* Time Complexity: O(log n)
|
|
313
|
-
* Space Complexity: O(1)
|
|
314
|
-
*
|
|
315
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
316
|
-
* leftmost node if the comparison result is greater than.
|
|
317
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
318
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
319
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
320
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
321
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
322
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
323
|
-
*/
|
|
324
|
-
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, NODE>): K | undefined;
|
|
325
308
|
/**
|
|
326
309
|
* Time Complexity: O(log n)
|
|
327
310
|
* Space Complexity: O(log n)
|
|
@@ -393,32 +376,4 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
393
376
|
* can either be an object of type `NODE` or it can be `undefined`.
|
|
394
377
|
*/
|
|
395
378
|
protected _setRoot(v: NODE | undefined): void;
|
|
396
|
-
/**
|
|
397
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
398
|
-
* is greater than, less than, or equal to the second value.
|
|
399
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
400
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
401
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
402
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
403
|
-
*/
|
|
404
|
-
protected _compare(a: K, b: K): CP;
|
|
405
|
-
/**
|
|
406
|
-
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
407
|
-
* `a` is less than `b` based on the specified variant.
|
|
408
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
409
|
-
* first value to be compared in the function.
|
|
410
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
411
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
412
|
-
* @returns a boolean value.
|
|
413
|
-
*/
|
|
414
|
-
protected _lt(a: K, b: K): boolean;
|
|
415
|
-
/**
|
|
416
|
-
* The function compares two values using a custom extractor function and returns true if the first
|
|
417
|
-
* value is greater than the second value.
|
|
418
|
-
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
419
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
420
|
-
* of the arguments for the comparison in the function.
|
|
421
|
-
* @returns a boolean value.
|
|
422
|
-
*/
|
|
423
|
-
protected _gt(a: K, b: K): boolean;
|
|
424
379
|
}
|