directed-graph-typed 1.50.6 → 1.50.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.js +36 -33
- package/dist/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/data-structures/binary-tree/bst.js +68 -47
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -8
- package/dist/data-structures/binary-tree/rb-tree.js +7 -17
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +1 -1
- package/dist/types/common.d.ts +5 -22
- package/dist/types/common.js +0 -33
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +8 -5
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +35 -36
- package/src/data-structures/binary-tree/bst.ts +67 -44
- package/src/data-structures/binary-tree/rb-tree.ts +11 -22
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/types/common.ts +6 -23
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { IterationType } from '../../types';
|
|
10
9
|
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -46,6 +45,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
46
45
|
* @returns the sum of the count property of all nodes in the tree.
|
|
47
46
|
*/
|
|
48
47
|
get count(): number;
|
|
48
|
+
getMutableCount(): number;
|
|
49
49
|
/**
|
|
50
50
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
51
51
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -156,7 +156,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
156
156
|
* values:
|
|
157
157
|
* @returns a boolean value.
|
|
158
158
|
*/
|
|
159
|
-
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
159
|
+
perfectlyBalance(iterationType?: import("../../types").IterationType): boolean;
|
|
160
160
|
/**
|
|
161
161
|
* Time complexity: O(n)
|
|
162
162
|
* Space complexity: O(n)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const avl_tree_1 = require("./avl-tree");
|
|
6
5
|
class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
|
|
7
6
|
/**
|
|
@@ -53,6 +52,9 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
53
52
|
* @returns the sum of the count property of all nodes in the tree.
|
|
54
53
|
*/
|
|
55
54
|
get count() {
|
|
55
|
+
return this._count;
|
|
56
|
+
}
|
|
57
|
+
getMutableCount() {
|
|
56
58
|
let sum = 0;
|
|
57
59
|
this.dfs(node => (sum += node.count));
|
|
58
60
|
return sum;
|
|
@@ -202,10 +204,10 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
202
204
|
}
|
|
203
205
|
else {
|
|
204
206
|
const { familyPosition: fp } = curr;
|
|
205
|
-
if (fp ===
|
|
207
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
206
208
|
parent.left = curr.right;
|
|
207
209
|
}
|
|
208
|
-
else if (fp ===
|
|
210
|
+
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
209
211
|
parent.right = curr.right;
|
|
210
212
|
}
|
|
211
213
|
needBalanced = parent;
|
|
@@ -272,7 +274,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
272
274
|
if (sorted.length < 1)
|
|
273
275
|
return false;
|
|
274
276
|
this.clear();
|
|
275
|
-
if (iterationType ===
|
|
277
|
+
if (iterationType === 'RECURSIVE') {
|
|
276
278
|
const buildBalanceBST = (l, r) => {
|
|
277
279
|
if (l > r)
|
|
278
280
|
return;
|
|
@@ -135,11 +135,11 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
135
135
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
136
136
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
137
137
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
138
|
-
* `
|
|
138
|
+
* `'ITERATIVE'`.
|
|
139
139
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
140
140
|
* itself if it is not a valid node key.
|
|
141
141
|
*/
|
|
142
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
142
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | null | undefined;
|
|
143
143
|
/**
|
|
144
144
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
145
145
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
@@ -248,7 +248,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
248
248
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
249
249
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
250
250
|
*/
|
|
251
|
-
getNodeByKey(key: K, iterationType?:
|
|
251
|
+
getNodeByKey(key: K, iterationType?: string): NODE | undefined;
|
|
252
252
|
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
253
253
|
get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
254
254
|
get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
11
|
-
const types_1 = require("../../types");
|
|
12
11
|
const utils_1 = require("../../utils");
|
|
13
12
|
const queue_1 = require("../queue");
|
|
14
13
|
const base_1 = require("../base");
|
|
@@ -75,15 +74,15 @@ class BinaryTreeNode {
|
|
|
75
74
|
get familyPosition() {
|
|
76
75
|
const that = this;
|
|
77
76
|
if (!this.parent) {
|
|
78
|
-
return this.left || this.right ?
|
|
77
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
79
78
|
}
|
|
80
79
|
if (this.parent.left === that) {
|
|
81
|
-
return this.left || this.right ?
|
|
80
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
82
81
|
}
|
|
83
82
|
else if (this.parent.right === that) {
|
|
84
|
-
return this.left || this.right ?
|
|
83
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
85
84
|
}
|
|
86
|
-
return
|
|
85
|
+
return 'MAL_NODE';
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
@@ -106,8 +105,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
106
105
|
*/
|
|
107
106
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
108
107
|
super();
|
|
109
|
-
this.iterationType =
|
|
110
|
-
this._extractor = (key) => Number(key);
|
|
108
|
+
this.iterationType = 'ITERATIVE';
|
|
109
|
+
this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
111
110
|
this._defaultOneParamCallback = (node) => (node ? node.key : undefined);
|
|
112
111
|
if (options) {
|
|
113
112
|
const { iterationType, extractor } = options;
|
|
@@ -213,11 +212,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
213
212
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
214
213
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
215
214
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
216
|
-
* `
|
|
215
|
+
* `'ITERATIVE'`.
|
|
217
216
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
218
217
|
* itself if it is not a valid node key.
|
|
219
218
|
*/
|
|
220
|
-
ensureNode(keyOrNodeOrEntry, iterationType =
|
|
219
|
+
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
221
220
|
let res;
|
|
222
221
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
223
222
|
res = keyOrNodeOrEntry;
|
|
@@ -444,10 +443,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
444
443
|
}
|
|
445
444
|
else if (parent) {
|
|
446
445
|
const { familyPosition: fp } = curr;
|
|
447
|
-
if (fp ===
|
|
446
|
+
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
448
447
|
parent.left = curr.right;
|
|
449
448
|
}
|
|
450
|
-
else if (fp ===
|
|
449
|
+
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
451
450
|
parent.right = curr.right;
|
|
452
451
|
}
|
|
453
452
|
needBalanced = parent;
|
|
@@ -496,7 +495,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
496
495
|
if (!beginRoot)
|
|
497
496
|
return [];
|
|
498
497
|
const ans = [];
|
|
499
|
-
if (iterationType ===
|
|
498
|
+
if (iterationType === 'RECURSIVE') {
|
|
500
499
|
const _traverse = (cur) => {
|
|
501
500
|
if (callback(cur) === identifier) {
|
|
502
501
|
ans.push(cur);
|
|
@@ -576,10 +575,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
576
575
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
577
576
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
578
577
|
*/
|
|
579
|
-
getNodeByKey(key, iterationType =
|
|
578
|
+
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
580
579
|
if (!this.root)
|
|
581
580
|
return undefined;
|
|
582
|
-
if (iterationType ===
|
|
581
|
+
if (iterationType === 'RECURSIVE') {
|
|
583
582
|
const _dfs = (cur) => {
|
|
584
583
|
if (cur.key === key)
|
|
585
584
|
return cur;
|
|
@@ -734,7 +733,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
734
733
|
beginRoot = this.ensureNode(beginRoot);
|
|
735
734
|
if (!beginRoot)
|
|
736
735
|
return true;
|
|
737
|
-
if (iterationType ===
|
|
736
|
+
if (iterationType === 'RECURSIVE') {
|
|
738
737
|
const dfs = (cur, min, max) => {
|
|
739
738
|
if (!this.isRealNode(cur))
|
|
740
739
|
return true;
|
|
@@ -821,9 +820,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
821
820
|
*/
|
|
822
821
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
823
822
|
beginRoot = this.ensureNode(beginRoot);
|
|
824
|
-
if (!beginRoot)
|
|
823
|
+
if (!this.isRealNode(beginRoot))
|
|
825
824
|
return -1;
|
|
826
|
-
if (iterationType ===
|
|
825
|
+
if (iterationType === 'RECURSIVE') {
|
|
827
826
|
const _getMaxHeight = (cur) => {
|
|
828
827
|
if (!this.isRealNode(cur))
|
|
829
828
|
return -1;
|
|
@@ -869,11 +868,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
869
868
|
beginRoot = this.ensureNode(beginRoot);
|
|
870
869
|
if (!beginRoot)
|
|
871
870
|
return -1;
|
|
872
|
-
if (iterationType ===
|
|
871
|
+
if (iterationType === 'RECURSIVE') {
|
|
873
872
|
const _getMinHeight = (cur) => {
|
|
874
|
-
if (!cur)
|
|
873
|
+
if (!this.isRealNode(cur))
|
|
875
874
|
return 0;
|
|
876
|
-
if (!cur.left && !cur.right)
|
|
875
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
877
876
|
return 0;
|
|
878
877
|
const leftMinHeight = _getMinHeight(cur.left);
|
|
879
878
|
const rightMinHeight = _getMinHeight(cur.right);
|
|
@@ -886,17 +885,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
886
885
|
let node = beginRoot, last = null;
|
|
887
886
|
const depths = new Map();
|
|
888
887
|
while (stack.length > 0 || node) {
|
|
889
|
-
if (node) {
|
|
888
|
+
if (this.isRealNode(node)) {
|
|
890
889
|
stack.push(node);
|
|
891
890
|
node = node.left;
|
|
892
891
|
}
|
|
893
892
|
else {
|
|
894
893
|
node = stack[stack.length - 1];
|
|
895
|
-
if (!node.right || last === node.right) {
|
|
894
|
+
if (!this.isRealNode(node.right) || last === node.right) {
|
|
896
895
|
node = stack.pop();
|
|
897
|
-
if (node) {
|
|
898
|
-
const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
899
|
-
const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
896
|
+
if (this.isRealNode(node)) {
|
|
897
|
+
const leftMinHeight = this.isRealNode(node.left) ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
898
|
+
const rightMinHeight = this.isRealNode(node.right) ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
900
899
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
901
900
|
last = node;
|
|
902
901
|
node = null;
|
|
@@ -962,10 +961,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
962
961
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
963
962
|
*/
|
|
964
963
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
964
|
+
if (this.isNIL(beginRoot))
|
|
965
|
+
return beginRoot;
|
|
965
966
|
beginRoot = this.ensureNode(beginRoot);
|
|
966
|
-
if (!beginRoot)
|
|
967
|
+
if (!this.isRealNode(beginRoot))
|
|
967
968
|
return beginRoot;
|
|
968
|
-
if (iterationType ===
|
|
969
|
+
if (iterationType === 'RECURSIVE') {
|
|
969
970
|
const _traverse = (cur) => {
|
|
970
971
|
if (!this.isRealNode(cur.left))
|
|
971
972
|
return cur;
|
|
@@ -1003,11 +1004,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1003
1004
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1004
1005
|
*/
|
|
1005
1006
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
1007
|
+
if (this.isNIL(beginRoot))
|
|
1008
|
+
return beginRoot;
|
|
1006
1009
|
// TODO support get right most by passing key in
|
|
1007
1010
|
beginRoot = this.ensureNode(beginRoot);
|
|
1008
1011
|
if (!beginRoot)
|
|
1009
1012
|
return beginRoot;
|
|
1010
|
-
if (iterationType ===
|
|
1013
|
+
if (iterationType === 'RECURSIVE') {
|
|
1011
1014
|
const _traverse = (cur) => {
|
|
1012
1015
|
if (!this.isRealNode(cur.right))
|
|
1013
1016
|
return cur;
|
|
@@ -1107,12 +1110,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1107
1110
|
* `false`, null or undefined
|
|
1108
1111
|
* @returns an array of values that are the return values of the callback function.
|
|
1109
1112
|
*/
|
|
1110
|
-
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType =
|
|
1113
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1111
1114
|
beginRoot = this.ensureNode(beginRoot);
|
|
1112
1115
|
if (!beginRoot)
|
|
1113
1116
|
return [];
|
|
1114
1117
|
const ans = [];
|
|
1115
|
-
if (iterationType ===
|
|
1118
|
+
if (iterationType === 'RECURSIVE') {
|
|
1116
1119
|
const _traverse = (node) => {
|
|
1117
1120
|
switch (pattern) {
|
|
1118
1121
|
case 'in':
|
|
@@ -1243,7 +1246,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1243
1246
|
if (!beginRoot)
|
|
1244
1247
|
return [];
|
|
1245
1248
|
const ans = [];
|
|
1246
|
-
if (iterationType ===
|
|
1249
|
+
if (iterationType === 'RECURSIVE') {
|
|
1247
1250
|
const queue = new queue_1.Queue([beginRoot]);
|
|
1248
1251
|
const traverse = (level) => {
|
|
1249
1252
|
if (queue.size === 0)
|
|
@@ -1320,7 +1323,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1320
1323
|
const levelsNodes = [];
|
|
1321
1324
|
if (!beginRoot)
|
|
1322
1325
|
return levelsNodes;
|
|
1323
|
-
if (iterationType ===
|
|
1326
|
+
if (iterationType === 'RECURSIVE') {
|
|
1324
1327
|
const _recursive = (node, level) => {
|
|
1325
1328
|
if (!levelsNodes[level])
|
|
1326
1329
|
levelsNodes[level] = [];
|
|
@@ -1607,7 +1610,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1607
1610
|
*_getIterator(node = this.root) {
|
|
1608
1611
|
if (!node)
|
|
1609
1612
|
return;
|
|
1610
|
-
if (this.iterationType ===
|
|
1613
|
+
if (this.iterationType === 'ITERATIVE') {
|
|
1611
1614
|
const stack = [];
|
|
1612
1615
|
let current = node;
|
|
1613
1616
|
while (current || stack.length > 0) {
|
|
@@ -109,10 +109,10 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
109
109
|
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
110
110
|
* `undefined`.
|
|
111
111
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
112
|
-
* type of iteration to be performed. It has a default value of `
|
|
112
|
+
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
|
|
113
113
|
* @returns either a node object (NODE) or undefined.
|
|
114
114
|
*/
|
|
115
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
115
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | undefined;
|
|
116
116
|
/**
|
|
117
117
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
118
118
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
@@ -179,7 +179,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
179
179
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
180
180
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
181
181
|
*/
|
|
182
|
-
getNodeByKey(key: K, iterationType?:
|
|
182
|
+
getNodeByKey(key: K, iterationType?: string): NODE | undefined;
|
|
183
183
|
/**
|
|
184
184
|
* Time Complexity: O(log n)
|
|
185
185
|
* Space Complexity: O(k + log n)
|
|
@@ -372,8 +372,10 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
372
372
|
* is greater than, less than, or equal to the second value.
|
|
373
373
|
* @param {K} a - The parameter "a" is of type K.
|
|
374
374
|
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
375
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are
|
|
376
|
-
* than),
|
|
375
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
376
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
377
377
|
*/
|
|
378
378
|
protected _compare(a: K, b: K): CP;
|
|
379
|
+
protected _lt(a: K, b: K): boolean;
|
|
380
|
+
protected _gt(a: K, b: K): boolean;
|
|
379
381
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BST = exports.BSTNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const binary_tree_1 = require("./binary-tree");
|
|
6
5
|
const queue_1 = require("../queue");
|
|
7
6
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
@@ -70,7 +69,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
70
69
|
*/
|
|
71
70
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
72
71
|
super([], options);
|
|
73
|
-
this._variant =
|
|
72
|
+
this._variant = 'STANDARD';
|
|
74
73
|
if (options) {
|
|
75
74
|
const { variant } = options;
|
|
76
75
|
if (variant)
|
|
@@ -162,10 +161,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
162
161
|
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
163
162
|
* `undefined`.
|
|
164
163
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
165
|
-
* type of iteration to be performed. It has a default value of `
|
|
164
|
+
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
|
|
166
165
|
* @returns either a node object (NODE) or undefined.
|
|
167
166
|
*/
|
|
168
|
-
ensureNode(keyOrNodeOrEntry, iterationType =
|
|
167
|
+
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
169
168
|
let res;
|
|
170
169
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
171
170
|
res = keyOrNodeOrEntry;
|
|
@@ -215,7 +214,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
215
214
|
}
|
|
216
215
|
let current = this.root;
|
|
217
216
|
while (current !== undefined) {
|
|
218
|
-
if (this._compare(current.key, newNode.key) ===
|
|
217
|
+
if (this._compare(current.key, newNode.key) === 'EQ') {
|
|
219
218
|
// if (current !== newNode) {
|
|
220
219
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
221
220
|
this._replaceNode(current, newNode);
|
|
@@ -226,7 +225,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
226
225
|
// return;
|
|
227
226
|
// }
|
|
228
227
|
}
|
|
229
|
-
else if (this._compare(current.key, newNode.key) ===
|
|
228
|
+
else if (this._compare(current.key, newNode.key) === 'GT') {
|
|
230
229
|
if (current.left === undefined) {
|
|
231
230
|
current.left = newNode;
|
|
232
231
|
this._size++;
|
|
@@ -335,7 +334,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
335
334
|
}
|
|
336
335
|
}
|
|
337
336
|
};
|
|
338
|
-
if (iterationType ===
|
|
337
|
+
if (iterationType === 'RECURSIVE') {
|
|
339
338
|
_dfs(sorted);
|
|
340
339
|
}
|
|
341
340
|
else {
|
|
@@ -361,18 +360,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
361
360
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
362
361
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
363
362
|
*/
|
|
364
|
-
getNodeByKey(key, iterationType =
|
|
365
|
-
if (!this.root)
|
|
363
|
+
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
364
|
+
if (!this.isRealNode(this.root))
|
|
366
365
|
return undefined;
|
|
367
|
-
if (iterationType ===
|
|
366
|
+
if (iterationType === 'RECURSIVE') {
|
|
368
367
|
const _dfs = (cur) => {
|
|
369
368
|
if (cur.key === key)
|
|
370
369
|
return cur;
|
|
371
|
-
if (!cur.left && !cur.right)
|
|
370
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
371
|
return;
|
|
373
|
-
if (this._compare(cur.key, key) ===
|
|
372
|
+
if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left))
|
|
374
373
|
return _dfs(cur.left);
|
|
375
|
-
if (this._compare(cur.key, key) ===
|
|
374
|
+
if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right))
|
|
376
375
|
return _dfs(cur.right);
|
|
377
376
|
};
|
|
378
377
|
return _dfs(this.root);
|
|
@@ -381,13 +380,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
381
380
|
const queue = new queue_1.Queue([this.root]);
|
|
382
381
|
while (queue.size > 0) {
|
|
383
382
|
const cur = queue.shift();
|
|
384
|
-
if (cur) {
|
|
385
|
-
if (this._compare(cur.key, key) ===
|
|
383
|
+
if (this.isRealNode(cur)) {
|
|
384
|
+
if (this._compare(cur.key, key) === 'EQ')
|
|
386
385
|
return cur;
|
|
387
|
-
if (this._compare(cur.key, key) ===
|
|
388
|
-
cur.left && queue.push(cur.left);
|
|
389
|
-
if (this._compare(cur.key, key) ===
|
|
390
|
-
cur.right && queue.push(cur.right);
|
|
386
|
+
if (this._compare(cur.key, key) === 'GT')
|
|
387
|
+
this.isRealNode(cur.left) && queue.push(cur.left);
|
|
388
|
+
if (this._compare(cur.key, key) === 'LT')
|
|
389
|
+
this.isRealNode(cur.right) && queue.push(cur.right);
|
|
391
390
|
}
|
|
392
391
|
}
|
|
393
392
|
}
|
|
@@ -425,7 +424,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
425
424
|
if (!beginRoot)
|
|
426
425
|
return [];
|
|
427
426
|
const ans = [];
|
|
428
|
-
if (iterationType ===
|
|
427
|
+
if (iterationType === 'RECURSIVE') {
|
|
429
428
|
const _traverse = (cur) => {
|
|
430
429
|
const callbackResult = callback(cur);
|
|
431
430
|
if (callbackResult === identifier) {
|
|
@@ -433,27 +432,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
433
432
|
if (onlyOne)
|
|
434
433
|
return;
|
|
435
434
|
}
|
|
436
|
-
if (!cur.left && !cur.right)
|
|
435
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
437
436
|
return;
|
|
438
437
|
// TODO potential bug
|
|
439
438
|
if (callback === this._defaultOneParamCallback) {
|
|
440
|
-
if (this._compare(cur.key, identifier) ===
|
|
441
|
-
|
|
442
|
-
if (this._compare(cur.key, identifier) ===
|
|
443
|
-
|
|
439
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
440
|
+
_traverse(cur.left);
|
|
441
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
442
|
+
_traverse(cur.right);
|
|
444
443
|
}
|
|
445
444
|
else {
|
|
446
|
-
cur.left && _traverse(cur.left);
|
|
447
|
-
cur.right && _traverse(cur.right);
|
|
445
|
+
this.isRealNode(cur.left) && _traverse(cur.left);
|
|
446
|
+
this.isRealNode(cur.right) && _traverse(cur.right);
|
|
448
447
|
}
|
|
449
448
|
};
|
|
450
449
|
_traverse(beginRoot);
|
|
451
450
|
}
|
|
452
451
|
else {
|
|
453
|
-
const
|
|
454
|
-
while (
|
|
455
|
-
const cur =
|
|
456
|
-
if (cur) {
|
|
452
|
+
const stack = [beginRoot];
|
|
453
|
+
while (stack.length > 0) {
|
|
454
|
+
const cur = stack.pop();
|
|
455
|
+
if (this.isRealNode(cur)) {
|
|
457
456
|
const callbackResult = callback(cur);
|
|
458
457
|
if (callbackResult === identifier) {
|
|
459
458
|
ans.push(cur);
|
|
@@ -462,14 +461,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
462
461
|
}
|
|
463
462
|
// TODO potential bug
|
|
464
463
|
if (callback === this._defaultOneParamCallback) {
|
|
465
|
-
if (this._compare(cur.key, identifier) ===
|
|
466
|
-
|
|
467
|
-
if (this._compare(cur.key, identifier) ===
|
|
468
|
-
|
|
464
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
465
|
+
stack.push(cur.right);
|
|
466
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
467
|
+
stack.push(cur.left);
|
|
468
|
+
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
469
|
+
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
470
|
+
// // @ts-ignore
|
|
471
|
+
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
472
|
+
// // @ts-ignore
|
|
473
|
+
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
469
474
|
}
|
|
470
475
|
else {
|
|
471
|
-
cur.
|
|
472
|
-
cur.
|
|
476
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
477
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
473
478
|
}
|
|
474
479
|
}
|
|
475
480
|
}
|
|
@@ -499,7 +504,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
499
504
|
* following values:
|
|
500
505
|
* @returns The method is returning an array of the return type of the callback function.
|
|
501
506
|
*/
|
|
502
|
-
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType =
|
|
507
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
503
508
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
504
509
|
}
|
|
505
510
|
/**
|
|
@@ -572,7 +577,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
572
577
|
let current = this.ensureNode(beginRoot);
|
|
573
578
|
if (!current)
|
|
574
579
|
return undefined;
|
|
575
|
-
if (this._variant ===
|
|
580
|
+
if (this._variant === 'STANDARD') {
|
|
576
581
|
// For BSTVariant.MIN, find the rightmost node
|
|
577
582
|
while (current.right !== undefined) {
|
|
578
583
|
current = current.right;
|
|
@@ -611,7 +616,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
611
616
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
612
617
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
613
618
|
*/
|
|
614
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater =
|
|
619
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
|
|
615
620
|
targetNode = this.ensureNode(targetNode);
|
|
616
621
|
const ans = [];
|
|
617
622
|
if (!targetNode)
|
|
@@ -619,7 +624,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
619
624
|
if (!this.root)
|
|
620
625
|
return ans;
|
|
621
626
|
const targetKey = targetNode.key;
|
|
622
|
-
if (iterationType ===
|
|
627
|
+
if (iterationType === 'RECURSIVE') {
|
|
623
628
|
const _traverse = (cur) => {
|
|
624
629
|
const compared = this._compare(cur.key, targetKey);
|
|
625
630
|
if (compared === lesserOrGreater)
|
|
@@ -669,7 +674,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
669
674
|
this.clear();
|
|
670
675
|
if (sorted.length < 1)
|
|
671
676
|
return false;
|
|
672
|
-
if (iterationType ===
|
|
677
|
+
if (iterationType === 'RECURSIVE') {
|
|
673
678
|
const buildBalanceBST = (l, r) => {
|
|
674
679
|
if (l > r)
|
|
675
680
|
return;
|
|
@@ -727,7 +732,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
727
732
|
if (!this.root)
|
|
728
733
|
return true;
|
|
729
734
|
let balanced = true;
|
|
730
|
-
if (iterationType ===
|
|
735
|
+
if (iterationType === 'RECURSIVE') {
|
|
731
736
|
const _height = (cur) => {
|
|
732
737
|
if (!cur)
|
|
733
738
|
return 0;
|
|
@@ -784,14 +789,30 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
784
789
|
* is greater than, less than, or equal to the second value.
|
|
785
790
|
* @param {K} a - The parameter "a" is of type K.
|
|
786
791
|
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
787
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are
|
|
788
|
-
* than),
|
|
792
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
793
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
789
794
|
*/
|
|
790
795
|
_compare(a, b) {
|
|
791
796
|
const extractedA = this.extractor(a);
|
|
792
797
|
const extractedB = this.extractor(b);
|
|
793
|
-
const compared = this.variant ===
|
|
794
|
-
return compared > 0 ?
|
|
798
|
+
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
799
|
+
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
|
|
800
|
+
}
|
|
801
|
+
_lt(a, b) {
|
|
802
|
+
const extractedA = this.extractor(a);
|
|
803
|
+
const extractedB = this.extractor(b);
|
|
804
|
+
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
|
|
805
|
+
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
806
|
+
// return extractedA < extractedB;
|
|
807
|
+
// return a < b;
|
|
808
|
+
}
|
|
809
|
+
_gt(a, b) {
|
|
810
|
+
const extractedA = this.extractor(a);
|
|
811
|
+
const extractedB = this.extractor(b);
|
|
812
|
+
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
|
|
813
|
+
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
814
|
+
// return extractedA > extractedB;
|
|
815
|
+
// return a > b;
|
|
795
816
|
}
|
|
796
817
|
}
|
|
797
818
|
exports.BST = BST;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
-
import { RBTNColor } from '../../types';
|
|
2
|
+
import { CRUD, RBTNColor } from '../../types';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
import { IBinaryTree } from '../../interfaces';
|
|
5
5
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -51,12 +51,6 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
51
51
|
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
52
52
|
*/
|
|
53
53
|
get root(): NODE | undefined;
|
|
54
|
-
protected _size: number;
|
|
55
|
-
/**
|
|
56
|
-
* The function returns the size of an object.
|
|
57
|
-
* @returns The size of the object, which is a number.
|
|
58
|
-
*/
|
|
59
|
-
get size(): number;
|
|
60
54
|
/**
|
|
61
55
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
62
56
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
@@ -236,7 +230,7 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
236
230
|
* node in the tree.
|
|
237
231
|
* @returns {'inserted' | 'updated'} - The result of the insertion.
|
|
238
232
|
*/
|
|
239
|
-
protected _insert(node: NODE):
|
|
233
|
+
protected _insert(node: NODE): CRUD;
|
|
240
234
|
/**
|
|
241
235
|
* Time Complexity: O(1)
|
|
242
236
|
* Space Complexity: O(1)
|