data-structure-typed 1.49.5 → 1.49.6
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 +14 -23
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/umd/data-structure-typed.js +497 -419
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BinaryTreeDeleteResult, BTNCallback,
|
|
8
|
+
import { BinaryTreeDeleteResult, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -23,16 +23,16 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
23
23
|
Sentinel: N;
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
|
-
* initializes the tree with optional
|
|
27
|
-
* @param [
|
|
28
|
-
* objects. It represents the initial
|
|
26
|
+
* initializes the tree with optional nodes and options.
|
|
27
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
28
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
|
-
*
|
|
30
|
+
* nodes to the
|
|
31
31
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(
|
|
35
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
@@ -59,34 +59,40 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
59
59
|
*/
|
|
60
60
|
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
|
-
* The function
|
|
63
|
-
* @param
|
|
64
|
-
* @
|
|
62
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
63
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
64
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
65
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
66
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
67
|
+
* @returns a node of type N or undefined.
|
|
68
|
+
*/
|
|
69
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
72
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
73
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
65
74
|
* class.
|
|
66
75
|
*/
|
|
67
|
-
isNode(
|
|
76
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*/
|
|
81
|
+
isRealNode(node: N | undefined): node is N;
|
|
68
82
|
/**
|
|
69
83
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
70
84
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
71
85
|
* data type.
|
|
72
86
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
73
87
|
*/
|
|
74
|
-
isNotNodeInstance(potentialKey:
|
|
88
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
75
89
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
78
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
79
|
-
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
80
|
-
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
81
|
-
* @returns a node of type N or undefined.
|
|
82
|
-
*/
|
|
83
|
-
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
|
+
* Time Complexity: O(log n)
|
|
86
91
|
* Space Complexity: O(1)
|
|
92
|
+
* on average (where n is the number of nodes in the tree)
|
|
87
93
|
*/
|
|
88
94
|
/**
|
|
89
|
-
* Time Complexity: O(log n)
|
|
95
|
+
* Time Complexity: O(log n)
|
|
90
96
|
* Space Complexity: O(1)
|
|
91
97
|
*
|
|
92
98
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -97,13 +103,14 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
97
103
|
* being added to the binary search tree.
|
|
98
104
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
99
105
|
*/
|
|
100
|
-
add(keyOrNodeOrEntry:
|
|
106
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
|
|
101
107
|
/**
|
|
102
|
-
* Time Complexity: O(log n)
|
|
108
|
+
* Time Complexity: O(log n)
|
|
103
109
|
* Space Complexity: O(1)
|
|
110
|
+
* on average (where n is the number of nodes in the tree)
|
|
104
111
|
*/
|
|
105
112
|
/**
|
|
106
|
-
* Time Complexity: O(log n)
|
|
113
|
+
* Time Complexity: O(log n)
|
|
107
114
|
* Space Complexity: O(1)
|
|
108
115
|
*
|
|
109
116
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -118,11 +125,6 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
118
125
|
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
119
126
|
*/
|
|
120
127
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
121
|
-
/**
|
|
122
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
123
|
-
* Space Complexity: O(1)
|
|
124
|
-
*/
|
|
125
|
-
isRealNode(node: N | undefined): node is N;
|
|
126
128
|
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
127
129
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
128
130
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
@@ -141,7 +143,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
141
143
|
*/
|
|
142
144
|
getPredecessor(x: N): N;
|
|
143
145
|
/**
|
|
144
|
-
* Time Complexity: O(
|
|
146
|
+
* Time Complexity: O(1)
|
|
145
147
|
* Space Complexity: O(1)
|
|
146
148
|
*/
|
|
147
149
|
clear(): void;
|
|
@@ -172,11 +174,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
172
174
|
*/
|
|
173
175
|
protected _rightRotate(x: N): void;
|
|
174
176
|
/**
|
|
175
|
-
* Time Complexity: O(log n)
|
|
177
|
+
* Time Complexity: O(log n)
|
|
176
178
|
* Space Complexity: O(1)
|
|
177
179
|
*/
|
|
178
180
|
/**
|
|
179
|
-
* Time Complexity: O(log n)
|
|
181
|
+
* Time Complexity: O(log n)
|
|
180
182
|
* Space Complexity: O(1)
|
|
181
183
|
*
|
|
182
184
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -197,11 +199,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
197
199
|
*/
|
|
198
200
|
protected _rbTransplant(u: N, v: N): void;
|
|
199
201
|
/**
|
|
200
|
-
* Time Complexity: O(log n)
|
|
202
|
+
* Time Complexity: O(log n)
|
|
201
203
|
* Space Complexity: O(1)
|
|
202
204
|
*/
|
|
203
205
|
/**
|
|
204
|
-
* Time Complexity: O(log n)
|
|
206
|
+
* Time Complexity: O(log n)
|
|
205
207
|
* Space Complexity: O(1)
|
|
206
208
|
*
|
|
207
209
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -25,20 +25,20 @@ export class RedBlackTree extends BST {
|
|
|
25
25
|
Sentinel = new RedBlackTreeNode(NaN);
|
|
26
26
|
/**
|
|
27
27
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
28
|
-
* initializes the tree with optional
|
|
29
|
-
* @param [
|
|
30
|
-
* objects. It represents the initial
|
|
28
|
+
* initializes the tree with optional nodes and options.
|
|
29
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
30
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
31
31
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
32
|
-
*
|
|
32
|
+
* nodes to the
|
|
33
33
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
34
34
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
35
35
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
36
36
|
*/
|
|
37
|
-
constructor(
|
|
37
|
+
constructor(nodes, options) {
|
|
38
38
|
super([], options);
|
|
39
39
|
this._root = this.Sentinel;
|
|
40
|
-
if (
|
|
41
|
-
super.addMany(
|
|
40
|
+
if (nodes)
|
|
41
|
+
super.addMany(nodes);
|
|
42
42
|
}
|
|
43
43
|
_root;
|
|
44
44
|
get root() {
|
|
@@ -78,41 +78,23 @@ export class RedBlackTree extends BST {
|
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
|
-
* The function
|
|
82
|
-
* @param
|
|
83
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
84
|
-
* class.
|
|
85
|
-
*/
|
|
86
|
-
isNode(exemplar) {
|
|
87
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
91
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
92
|
-
* data type.
|
|
93
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
94
|
-
*/
|
|
95
|
-
isNotNodeInstance(potentialKey) {
|
|
96
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
100
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
81
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
82
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
101
83
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
102
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
84
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
103
85
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
104
86
|
* @returns a node of type N or undefined.
|
|
105
87
|
*/
|
|
106
|
-
exemplarToNode(
|
|
88
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
107
89
|
let node;
|
|
108
|
-
if (
|
|
90
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
109
91
|
return;
|
|
110
92
|
}
|
|
111
|
-
else if (this.isNode(
|
|
112
|
-
node =
|
|
93
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
94
|
+
node = keyOrNodeOrEntry;
|
|
113
95
|
}
|
|
114
|
-
else if (this.isEntry(
|
|
115
|
-
const [key, value] =
|
|
96
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
97
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
116
98
|
if (key === undefined || key === null) {
|
|
117
99
|
return;
|
|
118
100
|
}
|
|
@@ -120,20 +102,48 @@ export class RedBlackTree extends BST {
|
|
|
120
102
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
121
103
|
}
|
|
122
104
|
}
|
|
123
|
-
else if (this.isNotNodeInstance(
|
|
124
|
-
node = this.createNode(
|
|
105
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
106
|
+
node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
|
|
125
107
|
}
|
|
126
108
|
else {
|
|
127
109
|
return;
|
|
128
110
|
}
|
|
129
111
|
return node;
|
|
130
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
115
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
116
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
117
|
+
* class.
|
|
118
|
+
*/
|
|
119
|
+
isNode(keyOrNodeOrEntry) {
|
|
120
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
121
|
+
}
|
|
131
122
|
/**
|
|
132
123
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
133
124
|
* Space Complexity: O(1)
|
|
134
125
|
*/
|
|
126
|
+
isRealNode(node) {
|
|
127
|
+
if (node === this.Sentinel || node === undefined)
|
|
128
|
+
return false;
|
|
129
|
+
return node instanceof RedBlackTreeNode;
|
|
130
|
+
}
|
|
135
131
|
/**
|
|
136
|
-
*
|
|
132
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
133
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
134
|
+
* data type.
|
|
135
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
136
|
+
*/
|
|
137
|
+
isNotNodeInstance(potentialKey) {
|
|
138
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Time Complexity: O(log n)
|
|
142
|
+
* Space Complexity: O(1)
|
|
143
|
+
* on average (where n is the number of nodes in the tree)
|
|
144
|
+
*/
|
|
145
|
+
/**
|
|
146
|
+
* Time Complexity: O(log n)
|
|
137
147
|
* Space Complexity: O(1)
|
|
138
148
|
*
|
|
139
149
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -147,7 +157,7 @@ export class RedBlackTree extends BST {
|
|
|
147
157
|
add(keyOrNodeOrEntry, value) {
|
|
148
158
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
149
159
|
if (newNode === undefined)
|
|
150
|
-
return;
|
|
160
|
+
return false;
|
|
151
161
|
newNode.left = this.Sentinel;
|
|
152
162
|
newNode.right = this.Sentinel;
|
|
153
163
|
let y = undefined;
|
|
@@ -165,7 +175,7 @@ export class RedBlackTree extends BST {
|
|
|
165
175
|
if (newNode !== x) {
|
|
166
176
|
this._replaceNode(x, newNode);
|
|
167
177
|
}
|
|
168
|
-
return;
|
|
178
|
+
return false;
|
|
169
179
|
}
|
|
170
180
|
}
|
|
171
181
|
}
|
|
@@ -182,21 +192,23 @@ export class RedBlackTree extends BST {
|
|
|
182
192
|
if (newNode.parent === undefined) {
|
|
183
193
|
newNode.color = RBTNColor.BLACK;
|
|
184
194
|
this._size++;
|
|
185
|
-
return;
|
|
195
|
+
return false;
|
|
186
196
|
}
|
|
187
197
|
if (newNode.parent.parent === undefined) {
|
|
188
198
|
this._size++;
|
|
189
|
-
return;
|
|
199
|
+
return false;
|
|
190
200
|
}
|
|
191
201
|
this._fixInsert(newNode);
|
|
192
202
|
this._size++;
|
|
203
|
+
return true;
|
|
193
204
|
}
|
|
194
205
|
/**
|
|
195
|
-
* Time Complexity: O(log n)
|
|
206
|
+
* Time Complexity: O(log n)
|
|
196
207
|
* Space Complexity: O(1)
|
|
208
|
+
* on average (where n is the number of nodes in the tree)
|
|
197
209
|
*/
|
|
198
210
|
/**
|
|
199
|
-
* Time Complexity: O(log n)
|
|
211
|
+
* Time Complexity: O(log n)
|
|
200
212
|
* Space Complexity: O(1)
|
|
201
213
|
*
|
|
202
214
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -269,20 +281,11 @@ export class RedBlackTree extends BST {
|
|
|
269
281
|
return ans;
|
|
270
282
|
}
|
|
271
283
|
/**
|
|
272
|
-
* Time Complexity: O(log n)
|
|
273
|
-
* Space Complexity: O(1)
|
|
274
|
-
*/
|
|
275
|
-
isRealNode(node) {
|
|
276
|
-
if (node === this.Sentinel || node === undefined)
|
|
277
|
-
return false;
|
|
278
|
-
return node instanceof RedBlackTreeNode;
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
284
|
+
* Time Complexity: O(log n)
|
|
282
285
|
* Space Complexity: O(1)
|
|
283
286
|
*/
|
|
284
287
|
/**
|
|
285
|
-
* Time Complexity: O(log n)
|
|
288
|
+
* Time Complexity: O(log n)
|
|
286
289
|
* Space Complexity: O(1)
|
|
287
290
|
*
|
|
288
291
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -333,7 +336,7 @@ export class RedBlackTree extends BST {
|
|
|
333
336
|
return y;
|
|
334
337
|
}
|
|
335
338
|
/**
|
|
336
|
-
* Time Complexity: O(
|
|
339
|
+
* Time Complexity: O(1)
|
|
337
340
|
* Space Complexity: O(1)
|
|
338
341
|
*/
|
|
339
342
|
clear() {
|
|
@@ -414,11 +417,11 @@ export class RedBlackTree extends BST {
|
|
|
414
417
|
}
|
|
415
418
|
}
|
|
416
419
|
/**
|
|
417
|
-
* Time Complexity: O(log n)
|
|
420
|
+
* Time Complexity: O(log n)
|
|
418
421
|
* Space Complexity: O(1)
|
|
419
422
|
*/
|
|
420
423
|
/**
|
|
421
|
-
* Time Complexity: O(log n)
|
|
424
|
+
* Time Complexity: O(log n)
|
|
422
425
|
* Space Complexity: O(1)
|
|
423
426
|
*
|
|
424
427
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -513,11 +516,11 @@ export class RedBlackTree extends BST {
|
|
|
513
516
|
v.parent = u.parent;
|
|
514
517
|
}
|
|
515
518
|
/**
|
|
516
|
-
* Time Complexity: O(log n)
|
|
519
|
+
* Time Complexity: O(log n)
|
|
517
520
|
* Space Complexity: O(1)
|
|
518
521
|
*/
|
|
519
522
|
/**
|
|
520
|
-
* Time Complexity: O(log n)
|
|
523
|
+
* Time Complexity: O(log n)
|
|
521
524
|
* Space Complexity: O(1)
|
|
522
525
|
*
|
|
523
526
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback,
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
9
|
import { IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
@@ -27,7 +27,7 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
-
constructor(
|
|
30
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
@@ -42,22 +42,8 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
42
42
|
createNode(key: K, value?: V, count?: number): N;
|
|
43
43
|
createTree(options?: TreeMultimapOptions<K>): TREE;
|
|
44
44
|
/**
|
|
45
|
-
* The function
|
|
46
|
-
* @param
|
|
47
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
|
-
* class.
|
|
49
|
-
*/
|
|
50
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
51
|
-
/**
|
|
52
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
53
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
54
|
-
* data type.
|
|
55
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
56
|
-
*/
|
|
57
|
-
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
|
|
58
|
-
/**
|
|
59
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
60
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
45
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
46
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
61
47
|
* can be one of the following:
|
|
62
48
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
63
49
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -66,14 +52,29 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
66
52
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
67
53
|
* @returns a node of type `N` or `undefined`.
|
|
68
54
|
*/
|
|
69
|
-
exemplarToNode(
|
|
55
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): N | undefined;
|
|
70
56
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
57
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
58
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
59
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
60
|
+
* class.
|
|
73
61
|
*/
|
|
62
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
74
63
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
64
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
+
* data type.
|
|
67
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
+
*/
|
|
69
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
70
|
+
/**
|
|
71
|
+
* Time Complexity: O(log n)
|
|
72
|
+
* Space Complexity: O(1)
|
|
73
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Time Complexity: O(log n)
|
|
77
|
+
* Space Complexity: O(1)
|
|
77
78
|
*
|
|
78
79
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
79
80
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -87,14 +88,15 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
87
88
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
88
89
|
* was not successful.
|
|
89
90
|
*/
|
|
90
|
-
add(keyOrNodeOrEntry:
|
|
91
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
91
92
|
/**
|
|
92
|
-
* Time Complexity: O(k log n)
|
|
93
|
-
* Space Complexity: O(1)
|
|
93
|
+
* Time Complexity: O(k log n)
|
|
94
|
+
* Space Complexity: O(1)
|
|
95
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
94
96
|
*/
|
|
95
97
|
/**
|
|
96
|
-
* Time Complexity: O(k log n)
|
|
97
|
-
* Space Complexity: O(1)
|
|
98
|
+
* Time Complexity: O(k log n)
|
|
99
|
+
* Space Complexity: O(1)
|
|
98
100
|
*
|
|
99
101
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
100
102
|
* structure.
|
|
@@ -102,14 +104,15 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
102
104
|
* either keys, nodes, or entries.
|
|
103
105
|
* @returns The method is returning an array of type `N | undefined`.
|
|
104
106
|
*/
|
|
105
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
107
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[];
|
|
106
108
|
/**
|
|
107
|
-
* Time Complexity: O(
|
|
108
|
-
* Space Complexity: O(
|
|
109
|
+
* Time Complexity: O(n log n)
|
|
110
|
+
* Space Complexity: O(n)
|
|
111
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
109
112
|
*/
|
|
110
113
|
/**
|
|
111
|
-
* Time Complexity: O(n log n)
|
|
112
|
-
* Space Complexity: O(n)
|
|
114
|
+
* Time Complexity: O(n log n)
|
|
115
|
+
* Space Complexity: O(n)
|
|
113
116
|
*
|
|
114
117
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
115
118
|
* tree using either a recursive or iterative approach.
|
|
@@ -120,12 +123,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
120
123
|
*/
|
|
121
124
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
122
125
|
/**
|
|
123
|
-
* Time Complexity: O(k log n)
|
|
124
|
-
* Space Complexity: O(1)
|
|
126
|
+
* Time Complexity: O(k log n)
|
|
127
|
+
* Space Complexity: O(1)
|
|
128
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
125
129
|
*/
|
|
126
130
|
/**
|
|
127
|
-
* Time Complexity: O(log n)
|
|
128
|
-
* Space Complexity: O(1)
|
|
131
|
+
* Time Complexity: O(k log n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
129
133
|
*
|
|
130
134
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
131
135
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -144,10 +148,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
144
148
|
*/
|
|
145
149
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
|
|
146
150
|
/**
|
|
147
|
-
* Time Complexity: O(
|
|
148
|
-
* Space Complexity: O(
|
|
151
|
+
* Time Complexity: O(1)
|
|
152
|
+
* Space Complexity: O(1)
|
|
149
153
|
*/
|
|
150
154
|
/**
|
|
155
|
+
* Time Complexity: O(1)
|
|
156
|
+
* Space Complexity: O(1)
|
|
157
|
+
*
|
|
151
158
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
152
159
|
*/
|
|
153
160
|
clear(): void;
|