graph-typed 1.51.5 → 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 +2 -10
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +12 -14
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -13
- package/dist/data-structures/binary-tree/binary-tree.js +46 -78
- package/dist/data-structures/binary-tree/bst.d.ts +51 -96
- package/dist/data-structures/binary-tree/bst.js +120 -218
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js +4 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/data-structures/binary-tree/tree-multi-map.js +1 -0
- 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 +5 -12
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +56 -76
- package/src/data-structures/binary-tree/bst.ts +132 -224
- package/src/data-structures/binary-tree/rb-tree.ts +9 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +5 -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.
|
|
@@ -199,6 +190,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
199
190
|
const deletedResult = [];
|
|
200
191
|
if (!this.root)
|
|
201
192
|
return deletedResult;
|
|
193
|
+
callback = this._ensureCallback(identifier, callback);
|
|
202
194
|
const curr = (_a = this.getNode(identifier, callback)) !== null && _a !== void 0 ? _a : undefined;
|
|
203
195
|
if (!curr)
|
|
204
196
|
return deletedResult;
|
|
@@ -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.
|
|
@@ -138,8 +138,6 @@ class AVLTree extends bst_1.BST {
|
|
|
138
138
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
139
139
|
*/
|
|
140
140
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
141
|
-
if (identifier instanceof AVLTreeNode)
|
|
142
|
-
callback = (node => node);
|
|
143
141
|
const deletedResults = super.delete(identifier, callback);
|
|
144
142
|
for (const { needBalanced } of deletedResults) {
|
|
145
143
|
if (needBalanced) {
|
|
@@ -159,21 +157,21 @@ class AVLTree extends bst_1.BST {
|
|
|
159
157
|
* if either `srcNode` or `destNode` is undefined.
|
|
160
158
|
*/
|
|
161
159
|
_swapProperties(srcNode, destNode) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (
|
|
165
|
-
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;
|
|
166
164
|
const tempNode = this.createNode(key, value);
|
|
167
165
|
if (tempNode) {
|
|
168
166
|
tempNode.height = height;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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;
|
|
175
173
|
}
|
|
176
|
-
return
|
|
174
|
+
return destNodeEnsured;
|
|
177
175
|
}
|
|
178
176
|
return undefined;
|
|
179
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>`.
|
|
@@ -254,7 +247,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
254
247
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
255
248
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
256
249
|
*/
|
|
257
|
-
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
250
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | null | undefined;
|
|
258
251
|
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
259
252
|
get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
260
253
|
get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
@@ -606,4 +599,5 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
606
599
|
* type `NODE` or `null`.
|
|
607
600
|
*/
|
|
608
601
|
protected _setRoot(v: NODE | null | undefined): void;
|
|
602
|
+
protected _ensureCallback<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): C;
|
|
609
603
|
}
|
|
@@ -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`,
|
|
@@ -225,23 +215,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
225
215
|
* itself if it is not a valid node key.
|
|
226
216
|
*/
|
|
227
217
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
218
|
+
if (keyOrNodeOrEntry === this.NIL)
|
|
219
|
+
return;
|
|
228
220
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
229
221
|
return keyOrNodeOrEntry;
|
|
230
222
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (keyOrNodeOrEntry[0] === undefined)
|
|
235
|
-
return;
|
|
236
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
if (keyOrNodeOrEntry === null)
|
|
223
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
224
|
+
const key = keyOrNodeOrEntry[0];
|
|
225
|
+
if (key === null)
|
|
240
226
|
return null;
|
|
241
|
-
if (
|
|
227
|
+
if (key === undefined)
|
|
242
228
|
return;
|
|
243
|
-
return this.getNodeByKey(
|
|
229
|
+
return this.getNodeByKey(key, iterationType);
|
|
244
230
|
}
|
|
231
|
+
if (keyOrNodeOrEntry === null)
|
|
232
|
+
return null;
|
|
233
|
+
if (keyOrNodeOrEntry === undefined)
|
|
234
|
+
return;
|
|
235
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
245
236
|
}
|
|
246
237
|
/**
|
|
247
238
|
* The function checks if a given node is a real node or null.
|
|
@@ -426,8 +417,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
426
417
|
const deletedResult = [];
|
|
427
418
|
if (!this.root)
|
|
428
419
|
return deletedResult;
|
|
429
|
-
|
|
430
|
-
callback = (node => node);
|
|
420
|
+
callback = this._ensureCallback(identifier, callback);
|
|
431
421
|
const curr = this.getNode(identifier, callback);
|
|
432
422
|
if (!curr)
|
|
433
423
|
return deletedResult;
|
|
@@ -499,11 +489,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
499
489
|
* @returns an array of nodes of type `NODE`.
|
|
500
490
|
*/
|
|
501
491
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
502
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
503
|
-
callback = (node => node);
|
|
504
492
|
beginRoot = this.ensureNode(beginRoot);
|
|
505
493
|
if (!beginRoot)
|
|
506
494
|
return [];
|
|
495
|
+
callback = this._ensureCallback(identifier, callback);
|
|
507
496
|
const ans = [];
|
|
508
497
|
if (iterationType === 'RECURSIVE') {
|
|
509
498
|
const dfs = (cur) => {
|
|
@@ -584,33 +573,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
584
573
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
585
574
|
*/
|
|
586
575
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
587
|
-
|
|
588
|
-
return undefined;
|
|
589
|
-
if (iterationType === 'RECURSIVE') {
|
|
590
|
-
const dfs = (cur) => {
|
|
591
|
-
if (cur.key === key)
|
|
592
|
-
return cur;
|
|
593
|
-
if (!cur.left && !cur.right)
|
|
594
|
-
return;
|
|
595
|
-
if (cur.left)
|
|
596
|
-
return dfs(cur.left);
|
|
597
|
-
if (cur.right)
|
|
598
|
-
return dfs(cur.right);
|
|
599
|
-
};
|
|
600
|
-
return dfs(this.root);
|
|
601
|
-
}
|
|
602
|
-
else {
|
|
603
|
-
const stack = [this.root];
|
|
604
|
-
while (stack.length > 0) {
|
|
605
|
-
const cur = stack.pop();
|
|
606
|
-
if (cur) {
|
|
607
|
-
if (cur.key === key)
|
|
608
|
-
return cur;
|
|
609
|
-
cur.left && stack.push(cur.left);
|
|
610
|
-
cur.right && stack.push(cur.right);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
576
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
614
577
|
}
|
|
615
578
|
/**
|
|
616
579
|
* Time Complexity: O(n)
|
|
@@ -639,8 +602,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
639
602
|
* found, `undefined` is returned.
|
|
640
603
|
*/
|
|
641
604
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
642
|
-
var _a
|
|
643
|
-
return (
|
|
605
|
+
var _a;
|
|
606
|
+
return (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
644
607
|
}
|
|
645
608
|
/**
|
|
646
609
|
* Time Complexity: O(n)
|
|
@@ -668,8 +631,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
668
631
|
* @returns a boolean value.
|
|
669
632
|
*/
|
|
670
633
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
671
|
-
|
|
672
|
-
callback = (node => node);
|
|
634
|
+
callback = this._ensureCallback(identifier, callback);
|
|
673
635
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
674
636
|
}
|
|
675
637
|
/**
|
|
@@ -743,7 +705,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
743
705
|
const dfs = (cur, min, max) => {
|
|
744
706
|
if (!this.isRealNode(cur))
|
|
745
707
|
return true;
|
|
746
|
-
const numKey =
|
|
708
|
+
const numKey = Number(cur.key);
|
|
747
709
|
if (numKey <= min || numKey >= max)
|
|
748
710
|
return false;
|
|
749
711
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -764,7 +726,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
764
726
|
curr = curr.left;
|
|
765
727
|
}
|
|
766
728
|
curr = stack.pop();
|
|
767
|
-
const numKey =
|
|
729
|
+
const numKey = Number(curr.key);
|
|
768
730
|
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
769
731
|
return false;
|
|
770
732
|
prev = numKey;
|
|
@@ -794,15 +756,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
794
756
|
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
795
757
|
*/
|
|
796
758
|
getDepth(dist, beginRoot = this.root) {
|
|
797
|
-
|
|
798
|
-
|
|
759
|
+
let distEnsured = this.ensureNode(dist);
|
|
760
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
799
761
|
let depth = 0;
|
|
800
|
-
while (
|
|
801
|
-
if (
|
|
762
|
+
while (distEnsured === null || distEnsured === void 0 ? void 0 : distEnsured.parent) {
|
|
763
|
+
if (distEnsured === beginRootEnsured) {
|
|
802
764
|
return depth;
|
|
803
765
|
}
|
|
804
766
|
depth++;
|
|
805
|
-
|
|
767
|
+
distEnsured = distEnsured.parent;
|
|
806
768
|
}
|
|
807
769
|
return depth;
|
|
808
770
|
}
|
|
@@ -936,16 +898,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
936
898
|
getPathToRoot(beginNode, isReverse = true) {
|
|
937
899
|
// TODO to support get path through passing key
|
|
938
900
|
const result = [];
|
|
939
|
-
|
|
940
|
-
if (!
|
|
901
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
902
|
+
if (!beginNodeEnsured)
|
|
941
903
|
return result;
|
|
942
|
-
while (
|
|
904
|
+
while (beginNodeEnsured.parent) {
|
|
943
905
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
944
906
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
945
|
-
result.push(
|
|
946
|
-
|
|
907
|
+
result.push(beginNodeEnsured);
|
|
908
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
947
909
|
}
|
|
948
|
-
result.push(
|
|
910
|
+
result.push(beginNodeEnsured);
|
|
949
911
|
return isReverse ? result.reverse() : result;
|
|
950
912
|
}
|
|
951
913
|
/**
|
|
@@ -1591,10 +1553,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1591
1553
|
console.log(`U for undefined
|
|
1592
1554
|
`);
|
|
1593
1555
|
if (opts.isShowNull)
|
|
1594
|
-
console.log(`
|
|
1556
|
+
console.log(`N for null
|
|
1595
1557
|
`);
|
|
1596
1558
|
if (opts.isShowRedBlackNIL)
|
|
1597
|
-
console.log(`S for Sentinel Node
|
|
1559
|
+
console.log(`S for Sentinel Node(NIL)
|
|
1598
1560
|
`);
|
|
1599
1561
|
const display = (root) => {
|
|
1600
1562
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1620,23 +1582,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1620
1582
|
const stack = [];
|
|
1621
1583
|
let current = node;
|
|
1622
1584
|
while (current || stack.length > 0) {
|
|
1623
|
-
while (
|
|
1585
|
+
while (this.isRealNode(current)) {
|
|
1624
1586
|
stack.push(current);
|
|
1625
1587
|
current = current.left;
|
|
1626
1588
|
}
|
|
1627
1589
|
current = stack.pop();
|
|
1628
|
-
if (
|
|
1590
|
+
if (this.isRealNode(current)) {
|
|
1629
1591
|
yield [current.key, current.value];
|
|
1630
1592
|
current = current.right;
|
|
1631
1593
|
}
|
|
1632
1594
|
}
|
|
1633
1595
|
}
|
|
1634
1596
|
else {
|
|
1635
|
-
if (node.left &&
|
|
1597
|
+
if (node.left && this.isRealNode(node)) {
|
|
1636
1598
|
yield* this[Symbol.iterator](node.left);
|
|
1637
1599
|
}
|
|
1638
1600
|
yield [node.key, node.value];
|
|
1639
|
-
if (node.right &&
|
|
1601
|
+
if (node.right && this.isRealNode(node)) {
|
|
1640
1602
|
yield* this[Symbol.iterator](node.right);
|
|
1641
1603
|
}
|
|
1642
1604
|
}
|
|
@@ -1665,12 +1627,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1665
1627
|
else if (node === undefined && !isShowUndefined) {
|
|
1666
1628
|
return emptyDisplayLayout;
|
|
1667
1629
|
}
|
|
1668
|
-
else if (
|
|
1630
|
+
else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1669
1631
|
return emptyDisplayLayout;
|
|
1670
1632
|
}
|
|
1671
1633
|
else if (node !== null && node !== undefined) {
|
|
1672
1634
|
// Display logic of normal nodes
|
|
1673
|
-
const key = node.key, line =
|
|
1635
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
|
|
1674
1636
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1675
1637
|
}
|
|
1676
1638
|
else {
|
|
@@ -1766,5 +1728,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1766
1728
|
}
|
|
1767
1729
|
this._root = v;
|
|
1768
1730
|
}
|
|
1731
|
+
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1732
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1733
|
+
callback = (node => node);
|
|
1734
|
+
}
|
|
1735
|
+
return callback;
|
|
1736
|
+
}
|
|
1769
1737
|
}
|
|
1770
1738
|
exports.BinaryTree = BinaryTree;
|