data-structure-typed 1.42.3 → 1.42.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +17 -17
- package/benchmark/report.html +12 -12
- package/benchmark/report.json +106 -106
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/cjs/src/data-structures/binary-tree/bst.js +77 -21
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/cjs/src/data-structures/binary-tree/index.js +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/{tree-multiset.d.ts → tree-multimap.d.ts} +20 -20
- package/dist/cjs/src/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +35 -32
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -0
- package/dist/cjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/index.js +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multimap.d.ts +4 -0
- package/dist/cjs/src/types/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multimap.js.map +1 -0
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/mjs/src/data-structures/binary-tree/bst.js +79 -21
- package/dist/mjs/src/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/mjs/src/data-structures/binary-tree/index.js +1 -1
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +148 -184
- package/dist/mjs/src/data-structures/binary-tree/{tree-multiset.d.ts → tree-multimap.d.ts} +20 -20
- package/dist/mjs/src/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +33 -31
- package/dist/mjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/index.js +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/mjs/src/types/data-structures/binary-tree/tree-multimap.d.ts +4 -0
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -3
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +201 -131
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/{tree-multiset.ts → tree-multimap.ts} +38 -37
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/index.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +6 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -31
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +205 -159
- package/test/unit/data-structures/binary-tree/{tree-multiset.test.ts → tree-multimap.test.ts} +182 -182
- package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js.map +0 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multiset.d.ts +0 -4
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multiset.js.map +0 -1
- package/dist/mjs/src/types/data-structures/binary-tree/tree-multiset.d.ts +0 -4
- package/src/types/data-structures/binary-tree/tree-multiset.ts +0 -6
- /package/dist/mjs/src/types/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +0 -0
- /package/test/performance/data-structures/binary-tree/{tree-multiset.test.ts → tree-multimap.test.ts} +0 -0
|
@@ -10,7 +10,28 @@ import { CP, IterationType } from '../../types';
|
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
|
+
parent: N | undefined;
|
|
13
14
|
constructor(key: BTNKey, value?: V);
|
|
15
|
+
protected _left: N | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Get the left child node.
|
|
18
|
+
*/
|
|
19
|
+
get left(): N | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Set the left child node.
|
|
22
|
+
* @param {N | undefined} v - The left child node.
|
|
23
|
+
*/
|
|
24
|
+
set left(v: N | undefined);
|
|
25
|
+
protected _right: N | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Get the right child node.
|
|
28
|
+
*/
|
|
29
|
+
get right(): N | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Set the right child node.
|
|
32
|
+
* @param {N | undefined} v - The right child node.
|
|
33
|
+
*/
|
|
34
|
+
set right(v: N | undefined);
|
|
14
35
|
}
|
|
15
36
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
37
|
/**
|
|
@@ -20,6 +41,11 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
20
41
|
* binary search tree.
|
|
21
42
|
*/
|
|
22
43
|
constructor(options?: BSTOptions);
|
|
44
|
+
protected _root: N | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Get the root node of the binary tree.
|
|
47
|
+
*/
|
|
48
|
+
get root(): N | undefined;
|
|
23
49
|
/**
|
|
24
50
|
* The function creates a new binary search tree node with the given key and value.
|
|
25
51
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -32,33 +58,33 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
32
58
|
/**
|
|
33
59
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
60
|
* into the tree.
|
|
35
|
-
* @param {BTNKey | N |
|
|
36
|
-
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `
|
|
61
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
|
|
62
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
|
|
37
63
|
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
38
64
|
* binary search tree.
|
|
39
65
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
|
-
* was not added or if the parameters were invalid, it returns
|
|
66
|
+
* was not added or if the parameters were invalid, it returns undefined or undefined.
|
|
41
67
|
*/
|
|
42
|
-
add(keyOrNode: BTNKey | N | null, value?: V): N |
|
|
68
|
+
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
43
69
|
/**
|
|
44
70
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
71
|
* maintaining balance.
|
|
46
72
|
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
73
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
74
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
|
-
* `
|
|
75
|
+
* `undefined
|
|
50
76
|
* @param {V[]} data - The values of tree nodes
|
|
51
77
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
52
78
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
53
79
|
* It can have two possible values:
|
|
54
|
-
* @returns The `addMany` function returns an array of `N`, `
|
|
80
|
+
* @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
|
|
55
81
|
*/
|
|
56
|
-
addMany(keysOrNodes: (BTNKey |
|
|
82
|
+
addMany(keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
57
83
|
/**
|
|
58
84
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
59
85
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
60
86
|
* rightmost node otherwise.
|
|
61
|
-
* @param {N |
|
|
87
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
62
88
|
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
63
89
|
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
64
90
|
* of the entire binary
|
|
@@ -69,7 +95,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
69
95
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
70
96
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
71
97
|
*/
|
|
72
|
-
lastKey(beginRoot?: N |
|
|
98
|
+
lastKey(beginRoot?: N | undefined, iterationType?: IterationType): BTNKey;
|
|
73
99
|
/**
|
|
74
100
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
75
101
|
* using either recursive or iterative traversal.
|
|
@@ -84,14 +110,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
84
110
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
85
111
|
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
86
112
|
* return an array containing all nodes that match the node
|
|
87
|
-
* @param {N |
|
|
113
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
88
114
|
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
89
|
-
* is `
|
|
115
|
+
* is `undefined`, an empty array will be returned.
|
|
90
116
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
91
117
|
* traverse the binary tree. It can have one of the following values:
|
|
92
118
|
* @returns an array of nodes (N[]).
|
|
93
119
|
*/
|
|
94
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> |
|
|
120
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: N | undefined, iterationType?: IterationType): N[];
|
|
95
121
|
/**
|
|
96
122
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
97
123
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -101,15 +127,15 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
101
127
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
102
128
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
103
129
|
* of the following values:
|
|
104
|
-
* @param {BTNKey | N |
|
|
130
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
|
|
105
131
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
106
132
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
107
|
-
* (`BTNKey`), or `
|
|
133
|
+
* (`BTNKey`), or `undefined` to
|
|
108
134
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
109
135
|
* done recursively or iteratively. It can have two possible values:
|
|
110
136
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
111
137
|
*/
|
|
112
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N |
|
|
138
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
113
139
|
/**
|
|
114
140
|
* Balancing Adjustment:
|
|
115
141
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
@@ -136,6 +162,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
136
162
|
*/
|
|
137
163
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
138
164
|
protected _comparator: BSTComparator;
|
|
165
|
+
protected _setRoot(v: N | undefined): void;
|
|
139
166
|
/**
|
|
140
167
|
* The function compares two values using a comparator function and returns whether the first value
|
|
141
168
|
* is greater than, less than, or equal to the second value.
|
|
@@ -5,8 +5,46 @@ const types_1 = require("../../types");
|
|
|
5
5
|
const binary_tree_1 = require("./binary-tree");
|
|
6
6
|
const queue_1 = require("../queue");
|
|
7
7
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
8
|
+
parent;
|
|
8
9
|
constructor(key, value) {
|
|
9
10
|
super(key, value);
|
|
11
|
+
this.parent = undefined;
|
|
12
|
+
this._left = undefined;
|
|
13
|
+
this._right = undefined;
|
|
14
|
+
}
|
|
15
|
+
_left;
|
|
16
|
+
/**
|
|
17
|
+
* Get the left child node.
|
|
18
|
+
*/
|
|
19
|
+
get left() {
|
|
20
|
+
return this._left;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Set the left child node.
|
|
24
|
+
* @param {N | undefined} v - The left child node.
|
|
25
|
+
*/
|
|
26
|
+
set left(v) {
|
|
27
|
+
if (v) {
|
|
28
|
+
v.parent = this;
|
|
29
|
+
}
|
|
30
|
+
this._left = v;
|
|
31
|
+
}
|
|
32
|
+
_right;
|
|
33
|
+
/**
|
|
34
|
+
* Get the right child node.
|
|
35
|
+
*/
|
|
36
|
+
get right() {
|
|
37
|
+
return this._right;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Set the right child node.
|
|
41
|
+
* @param {N | undefined} v - The right child node.
|
|
42
|
+
*/
|
|
43
|
+
set right(v) {
|
|
44
|
+
if (v) {
|
|
45
|
+
v.parent = this;
|
|
46
|
+
}
|
|
47
|
+
this._right = v;
|
|
10
48
|
}
|
|
11
49
|
}
|
|
12
50
|
exports.BSTNode = BSTNode;
|
|
@@ -19,6 +57,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
19
57
|
*/
|
|
20
58
|
constructor(options) {
|
|
21
59
|
super(options);
|
|
60
|
+
this._root = undefined;
|
|
22
61
|
if (options !== undefined) {
|
|
23
62
|
const { comparator } = options;
|
|
24
63
|
if (comparator !== undefined) {
|
|
@@ -26,6 +65,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
26
65
|
}
|
|
27
66
|
}
|
|
28
67
|
}
|
|
68
|
+
_root = undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Get the root node of the binary tree.
|
|
71
|
+
*/
|
|
72
|
+
get root() {
|
|
73
|
+
return this._root;
|
|
74
|
+
}
|
|
29
75
|
/**
|
|
30
76
|
* The function creates a new binary search tree node with the given key and value.
|
|
31
77
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -40,27 +86,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
40
86
|
/**
|
|
41
87
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
42
88
|
* into the tree.
|
|
43
|
-
* @param {BTNKey | N |
|
|
44
|
-
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `
|
|
89
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
|
|
90
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
|
|
45
91
|
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
46
92
|
* binary search tree.
|
|
47
93
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
48
|
-
* was not added or if the parameters were invalid, it returns
|
|
94
|
+
* was not added or if the parameters were invalid, it returns undefined or undefined.
|
|
49
95
|
*/
|
|
50
96
|
add(keyOrNode, value) {
|
|
97
|
+
if (keyOrNode === 8) {
|
|
98
|
+
debugger;
|
|
99
|
+
}
|
|
100
|
+
if (keyOrNode === null)
|
|
101
|
+
return undefined;
|
|
51
102
|
// TODO support node as a parameter
|
|
52
|
-
let inserted
|
|
53
|
-
let newNode
|
|
103
|
+
let inserted;
|
|
104
|
+
let newNode;
|
|
54
105
|
if (keyOrNode instanceof BSTNode) {
|
|
55
106
|
newNode = keyOrNode;
|
|
56
107
|
}
|
|
57
108
|
else if (typeof keyOrNode === 'number') {
|
|
58
109
|
newNode = this.createNode(keyOrNode, value);
|
|
59
110
|
}
|
|
60
|
-
else
|
|
61
|
-
newNode =
|
|
111
|
+
else {
|
|
112
|
+
newNode = undefined;
|
|
62
113
|
}
|
|
63
|
-
if (this.root ===
|
|
114
|
+
if (this.root === undefined) {
|
|
64
115
|
this._setRoot(newNode);
|
|
65
116
|
this._size = this.size + 1;
|
|
66
117
|
inserted = this.root;
|
|
@@ -69,7 +120,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
69
120
|
let cur = this.root;
|
|
70
121
|
let traversing = true;
|
|
71
122
|
while (traversing) {
|
|
72
|
-
if (cur !==
|
|
123
|
+
if (cur !== undefined && newNode !== undefined) {
|
|
73
124
|
if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
|
|
74
125
|
if (newNode) {
|
|
75
126
|
cur.value = newNode.value;
|
|
@@ -128,20 +179,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
128
179
|
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
129
180
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
130
181
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
131
|
-
* `
|
|
182
|
+
* `undefined
|
|
132
183
|
* @param {V[]} data - The values of tree nodes
|
|
133
184
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
134
185
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
135
186
|
* It can have two possible values:
|
|
136
|
-
* @returns The `addMany` function returns an array of `N`, `
|
|
187
|
+
* @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
|
|
137
188
|
*/
|
|
138
189
|
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
139
190
|
// TODO this addMany function is inefficient, it should be optimized
|
|
140
191
|
function hasNoNull(arr) {
|
|
141
|
-
return arr.indexOf(
|
|
192
|
+
return arr.indexOf(undefined) === -1;
|
|
142
193
|
}
|
|
143
194
|
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
144
|
-
return super.addMany(keysOrNodes, data);
|
|
195
|
+
return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
|
|
145
196
|
}
|
|
146
197
|
const inserted = [];
|
|
147
198
|
const combinedArr = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
@@ -208,7 +259,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
208
259
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
209
260
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
210
261
|
* rightmost node otherwise.
|
|
211
|
-
* @param {N |
|
|
262
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
212
263
|
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
213
264
|
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
214
265
|
* of the entire binary
|
|
@@ -241,9 +292,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
241
292
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
242
293
|
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
243
294
|
* return an array containing all nodes that match the node
|
|
244
|
-
* @param {N |
|
|
295
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
245
296
|
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
246
|
-
* is `
|
|
297
|
+
* is `undefined`, an empty array will be returned.
|
|
247
298
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
248
299
|
* traverse the binary tree. It can have one of the following values:
|
|
249
300
|
* @returns an array of nodes (N[]).
|
|
@@ -313,17 +364,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
313
364
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
314
365
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
315
366
|
* of the following values:
|
|
316
|
-
* @param {BTNKey | N |
|
|
367
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
|
|
317
368
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
318
369
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
319
|
-
* (`BTNKey`), or `
|
|
370
|
+
* (`BTNKey`), or `undefined` to
|
|
320
371
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
321
372
|
* done recursively or iteratively. It can have two possible values:
|
|
322
373
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
323
374
|
*/
|
|
324
375
|
lesserOrGreaterTraverse(callback = this.defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
325
376
|
if (typeof targetNode === 'number')
|
|
326
|
-
targetNode = this.getNode(targetNode);
|
|
377
|
+
targetNode = this.getNode(targetNode) ?? undefined;
|
|
327
378
|
const ans = [];
|
|
328
379
|
if (!targetNode)
|
|
329
380
|
return ans;
|
|
@@ -406,6 +457,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
406
457
|
if (l <= r) {
|
|
407
458
|
const m = l + Math.floor((r - l) / 2);
|
|
408
459
|
const midNode = sorted[m];
|
|
460
|
+
debugger;
|
|
409
461
|
this.add(midNode.key, midNode.value);
|
|
410
462
|
stack.push([m + 1, r]);
|
|
411
463
|
stack.push([l, m - 1]);
|
|
@@ -438,7 +490,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
438
490
|
}
|
|
439
491
|
else {
|
|
440
492
|
const stack = [];
|
|
441
|
-
let node = this.root, last =
|
|
493
|
+
let node = this.root, last = undefined;
|
|
442
494
|
const depths = new Map();
|
|
443
495
|
while (stack.length > 0 || node) {
|
|
444
496
|
if (node) {
|
|
@@ -456,7 +508,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
456
508
|
return false;
|
|
457
509
|
depths.set(node, 1 + Math.max(left, right));
|
|
458
510
|
last = node;
|
|
459
|
-
node =
|
|
511
|
+
node = undefined;
|
|
460
512
|
}
|
|
461
513
|
}
|
|
462
514
|
else
|
|
@@ -467,6 +519,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
467
519
|
return balanced;
|
|
468
520
|
}
|
|
469
521
|
_comparator = (a, b) => a - b;
|
|
522
|
+
_setRoot(v) {
|
|
523
|
+
if (v) {
|
|
524
|
+
v.parent = undefined;
|
|
525
|
+
}
|
|
526
|
+
this._root = v;
|
|
527
|
+
}
|
|
470
528
|
/**
|
|
471
529
|
* The function compares two values using a comparator function and returns whether the first value
|
|
472
530
|
* is greater than, less than, or equal to the second value.
|
|
@@ -20,4 +20,4 @@ __exportStar(require("./binary-indexed-tree"), exports);
|
|
|
20
20
|
__exportStar(require("./segment-tree"), exports);
|
|
21
21
|
__exportStar(require("./avl-tree"), exports);
|
|
22
22
|
__exportStar(require("./rb-tree"), exports);
|
|
23
|
-
__exportStar(require("./tree-
|
|
23
|
+
__exportStar(require("./tree-multimap"), exports);
|
|
@@ -5,16 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { RBTNColor } from '../../types';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
color: number;
|
|
15
|
-
constructor(key: number, color?: RBTNColor);
|
|
8
|
+
import { BinaryTreeDeletedResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
9
|
+
import { BST, BSTNode } from "./bst";
|
|
10
|
+
import { IBinaryTree } from "../../interfaces";
|
|
11
|
+
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
12
|
+
color: RBTNColor;
|
|
13
|
+
constructor(key: BTNKey, value?: V, color?: RBTNColor);
|
|
16
14
|
}
|
|
17
|
-
export declare const NIL: RBTreeNode;
|
|
18
15
|
/**
|
|
19
16
|
* 1. Each node is either red or black.
|
|
20
17
|
* 2. The root node is always black.
|
|
@@ -22,95 +19,75 @@ export declare const NIL: RBTreeNode;
|
|
|
22
19
|
* 4. Red nodes must have black children.
|
|
23
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
24
21
|
*/
|
|
25
|
-
export declare class RedBlackTree {
|
|
26
|
-
constructor();
|
|
27
|
-
protected _root:
|
|
28
|
-
get root():
|
|
22
|
+
export declare class RedBlackTree<V = any, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
23
|
+
constructor(options?: RBTreeOptions);
|
|
24
|
+
protected _root: N;
|
|
25
|
+
get root(): N;
|
|
29
26
|
protected _size: number;
|
|
30
27
|
get size(): number;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
|
|
41
|
-
* tree.
|
|
42
|
-
* @param {number} key - The `node` parameter is of type `RBTreeNode` and represents the current
|
|
43
|
-
* node being processed in the delete operation.
|
|
44
|
-
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
45
|
-
*/
|
|
46
|
-
delete(key: number): void;
|
|
47
|
-
isRealNode(node: RBTreeNode | null | undefined): node is RBTreeNode;
|
|
48
|
-
/**
|
|
49
|
-
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
50
|
-
* given key in a red-black tree.
|
|
51
|
-
* @param {number} key - The key parameter is a number that represents the value we are searching for
|
|
52
|
-
* in the RBTree.
|
|
53
|
-
* @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
|
|
54
|
-
* point for the search in the binary search tree. If no value is provided for `beginRoot`, it
|
|
55
|
-
* defaults to the root of the binary search tree (`this.root`).
|
|
56
|
-
* @returns a RBTreeNode.
|
|
57
|
-
*/
|
|
58
|
-
getNode(key: number, beginRoot?: RBTreeNode): RBTreeNode;
|
|
28
|
+
NIL: N;
|
|
29
|
+
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
30
|
+
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
31
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
32
|
+
isNode(node: N | undefined): node is N;
|
|
33
|
+
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
34
|
+
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
35
|
+
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
59
36
|
/**
|
|
60
37
|
* The function returns the leftmost node in a red-black tree.
|
|
61
38
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
62
39
|
* a Red-Black Tree.
|
|
63
40
|
* @returns The leftmost node in the given RBTreeNode.
|
|
64
41
|
*/
|
|
65
|
-
getLeftMost(node?:
|
|
42
|
+
getLeftMost(node?: N): N;
|
|
66
43
|
/**
|
|
67
44
|
* The function returns the rightmost node in a red-black tree.
|
|
68
45
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
69
46
|
* @returns the rightmost node in a red-black tree.
|
|
70
47
|
*/
|
|
71
|
-
getRightMost(node:
|
|
48
|
+
getRightMost(node: N): N;
|
|
72
49
|
/**
|
|
73
50
|
* The function returns the successor of a given node in a red-black tree.
|
|
74
51
|
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
75
52
|
* @returns the successor of the given RBTreeNode.
|
|
76
53
|
*/
|
|
77
|
-
getSuccessor(x:
|
|
54
|
+
getSuccessor(x: N): N | undefined;
|
|
78
55
|
/**
|
|
79
56
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
80
57
|
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
81
58
|
* Red-Black Tree.
|
|
82
59
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
83
60
|
*/
|
|
84
|
-
getPredecessor(x:
|
|
61
|
+
getPredecessor(x: N): N;
|
|
85
62
|
clear(): void;
|
|
86
|
-
|
|
63
|
+
protected _setRoot(v: N): void;
|
|
87
64
|
/**
|
|
88
65
|
* The function performs a left rotation on a red-black tree node.
|
|
89
66
|
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
90
67
|
*/
|
|
91
|
-
protected _leftRotate(x:
|
|
68
|
+
protected _leftRotate(x: N): void;
|
|
92
69
|
/**
|
|
93
70
|
* The function performs a right rotation on a red-black tree node.
|
|
94
71
|
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
95
72
|
* rotated.
|
|
96
73
|
*/
|
|
97
|
-
protected _rightRotate(x:
|
|
74
|
+
protected _rightRotate(x: N): void;
|
|
98
75
|
/**
|
|
99
76
|
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
100
77
|
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
101
78
|
* red-black tree.
|
|
102
79
|
*/
|
|
103
|
-
protected _fixDelete(x:
|
|
80
|
+
protected _fixDelete(x: N): void;
|
|
104
81
|
/**
|
|
105
82
|
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
106
83
|
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
107
84
|
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
108
85
|
*/
|
|
109
|
-
protected _rbTransplant(u:
|
|
86
|
+
protected _rbTransplant(u: N, v: N): void;
|
|
110
87
|
/**
|
|
111
88
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
112
89
|
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
113
90
|
* red-black tree.
|
|
114
91
|
*/
|
|
115
|
-
protected _fixInsert(k:
|
|
92
|
+
protected _fixInsert(k: N): void;
|
|
116
93
|
}
|