undirected-graph-typed 1.47.9 → 1.48.0
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.d.ts +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +14 -0
- package/dist/data-structures/binary-tree/binary-tree.js +52 -28
- package/dist/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/data-structures/binary-tree/bst.js +41 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +44 -21
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -23
- package/src/data-structures/binary-tree/bst.ts +38 -19
- package/src/data-structures/binary-tree/rb-tree.ts +55 -31
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -17
|
@@ -52,6 +52,12 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
52
52
|
* @returns a new AVLTree object.
|
|
53
53
|
*/
|
|
54
54
|
createTree(options?: AVLTreeOptions): TREE;
|
|
55
|
+
/**
|
|
56
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
57
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
58
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
59
|
+
*/
|
|
60
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
55
61
|
/**
|
|
56
62
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
57
63
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -63,6 +63,14 @@ class AVLTree extends bst_1.BST {
|
|
|
63
63
|
createTree(options) {
|
|
64
64
|
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
68
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
69
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
70
|
+
*/
|
|
71
|
+
isNode(exemplar) {
|
|
72
|
+
return exemplar instanceof AVLTreeNode;
|
|
73
|
+
}
|
|
66
74
|
/**
|
|
67
75
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
68
76
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -72,6 +72,20 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
72
72
|
* @returns a new instance of a binary tree.
|
|
73
73
|
*/
|
|
74
74
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
78
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
79
|
+
*/
|
|
80
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
81
|
+
/**
|
|
82
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
83
|
+
* object.
|
|
84
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
85
|
+
* function. It can be any type.
|
|
86
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
87
|
+
*/
|
|
88
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined;
|
|
75
89
|
/**
|
|
76
90
|
* The function checks if a given value is an entry in a binary tree node.
|
|
77
91
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -117,6 +117,51 @@ class BinaryTree {
|
|
|
117
117
|
createTree(options) {
|
|
118
118
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
119
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
122
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
123
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
124
|
+
*/
|
|
125
|
+
isNode(exemplar) {
|
|
126
|
+
return exemplar instanceof BinaryTreeNode;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
130
|
+
* object.
|
|
131
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
132
|
+
* function. It can be any type.
|
|
133
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
134
|
+
*/
|
|
135
|
+
exemplarToNode(exemplar) {
|
|
136
|
+
if (exemplar === undefined)
|
|
137
|
+
return;
|
|
138
|
+
let node;
|
|
139
|
+
if (exemplar === null) {
|
|
140
|
+
node = null;
|
|
141
|
+
}
|
|
142
|
+
else if (this.isEntry(exemplar)) {
|
|
143
|
+
const [key, value] = exemplar;
|
|
144
|
+
if (key === undefined) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
else if (key === null) {
|
|
148
|
+
node = null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
node = this.createNode(key, value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (this.isNode(exemplar)) {
|
|
155
|
+
node = exemplar;
|
|
156
|
+
}
|
|
157
|
+
else if (this.isNodeKey(exemplar)) {
|
|
158
|
+
node = this.createNode(exemplar);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
return node;
|
|
164
|
+
}
|
|
120
165
|
/**
|
|
121
166
|
* The function checks if a given value is an entry in a binary tree node.
|
|
122
167
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -139,7 +184,10 @@ class BinaryTree {
|
|
|
139
184
|
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
140
185
|
*/
|
|
141
186
|
add(keyOrNodeOrEntry) {
|
|
142
|
-
let inserted
|
|
187
|
+
let inserted;
|
|
188
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
189
|
+
if (newNode === undefined)
|
|
190
|
+
return;
|
|
143
191
|
const _bfs = (root, newNode) => {
|
|
144
192
|
const queue = new queue_1.Queue([root]);
|
|
145
193
|
while (queue.size > 0) {
|
|
@@ -157,36 +205,12 @@ class BinaryTree {
|
|
|
157
205
|
queue.push(cur.right);
|
|
158
206
|
}
|
|
159
207
|
};
|
|
160
|
-
if (keyOrNodeOrEntry === null) {
|
|
161
|
-
needInsert = null;
|
|
162
|
-
}
|
|
163
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
164
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
165
|
-
}
|
|
166
|
-
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
167
|
-
needInsert = keyOrNodeOrEntry;
|
|
168
|
-
}
|
|
169
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
170
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
171
|
-
if (key === undefined) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
else if (key === null) {
|
|
175
|
-
needInsert = null;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
needInsert = this.createNode(key, value);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
208
|
if (this.root) {
|
|
185
|
-
inserted = _bfs(this.root,
|
|
209
|
+
inserted = _bfs(this.root, newNode);
|
|
186
210
|
}
|
|
187
211
|
else {
|
|
188
|
-
this._setRoot(
|
|
189
|
-
if (
|
|
212
|
+
this._setRoot(newNode);
|
|
213
|
+
if (newNode) {
|
|
190
214
|
this._size = 1;
|
|
191
215
|
}
|
|
192
216
|
else {
|
|
@@ -72,6 +72,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
72
72
|
* @returns a new instance of the BST class with the specified options.
|
|
73
73
|
*/
|
|
74
74
|
createTree(options?: Partial<BSTOptions>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
78
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
79
|
+
*/
|
|
80
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
81
|
+
/**
|
|
82
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
83
|
+
* is valid, otherwise it returns undefined.
|
|
84
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
85
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
86
|
+
*/
|
|
87
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
75
88
|
/**
|
|
76
89
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
77
90
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -100,6 +100,45 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
100
100
|
createTree(options) {
|
|
101
101
|
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
102
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
105
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
106
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
107
|
+
*/
|
|
108
|
+
isNode(exemplar) {
|
|
109
|
+
return exemplar instanceof BSTNode;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
113
|
+
* is valid, otherwise it returns undefined.
|
|
114
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
115
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
116
|
+
*/
|
|
117
|
+
exemplarToNode(exemplar) {
|
|
118
|
+
let node;
|
|
119
|
+
if (exemplar === null || exemplar === undefined) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
else if (this.isNode(exemplar)) {
|
|
123
|
+
node = exemplar;
|
|
124
|
+
}
|
|
125
|
+
else if (this.isEntry(exemplar)) {
|
|
126
|
+
const [key, value] = exemplar;
|
|
127
|
+
if (key === undefined || key === null) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
node = this.createNode(key, value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (this.isNodeKey(exemplar)) {
|
|
135
|
+
node = this.createNode(exemplar);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
return node;
|
|
141
|
+
}
|
|
103
142
|
/**
|
|
104
143
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
105
144
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -115,28 +154,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
115
154
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
116
155
|
*/
|
|
117
156
|
add(keyOrNodeOrEntry) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
let newNode;
|
|
122
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
123
|
-
newNode = keyOrNodeOrEntry;
|
|
124
|
-
}
|
|
125
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
126
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
127
|
-
}
|
|
128
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
129
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
130
|
-
if (key === undefined || key === null) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
newNode = this.createNode(key, value);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
157
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
158
|
+
if (newNode === undefined)
|
|
138
159
|
return;
|
|
139
|
-
}
|
|
140
160
|
if (this.root === undefined) {
|
|
141
161
|
this._setRoot(newNode);
|
|
142
162
|
this._size++;
|
|
@@ -58,6 +58,22 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
58
58
|
* @returns a new instance of a RedBlackTree object.
|
|
59
59
|
*/
|
|
60
60
|
createTree(options?: RBTreeOptions): TREE;
|
|
61
|
+
/**
|
|
62
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
63
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
64
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
65
|
+
* class.
|
|
66
|
+
*/
|
|
67
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
68
|
+
/**
|
|
69
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
70
|
+
* otherwise it returns undefined.
|
|
71
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
72
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
73
|
+
* that is not a valid exemplar.
|
|
74
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
75
|
+
*/
|
|
76
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
61
77
|
/**
|
|
62
78
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
63
79
|
* Space Complexity: O(1)
|
|
@@ -77,28 +77,32 @@ class RedBlackTree extends bst_1.BST {
|
|
|
77
77
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
80
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
81
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
82
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
83
|
+
* class.
|
|
82
84
|
*/
|
|
85
|
+
isNode(exemplar) {
|
|
86
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
87
|
+
}
|
|
83
88
|
/**
|
|
84
|
-
* The function
|
|
85
|
-
*
|
|
86
|
-
* @
|
|
87
|
-
*
|
|
89
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
90
|
+
* otherwise it returns undefined.
|
|
91
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
92
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
93
|
+
* that is not a valid exemplar.
|
|
94
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
88
95
|
*/
|
|
89
|
-
|
|
96
|
+
exemplarToNode(exemplar) {
|
|
90
97
|
let node;
|
|
91
|
-
if (
|
|
92
|
-
node = this.createNode(keyOrNodeOrEntry, undefined, types_1.RBTNColor.RED);
|
|
93
|
-
}
|
|
94
|
-
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
95
|
-
node = keyOrNodeOrEntry;
|
|
96
|
-
}
|
|
97
|
-
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
98
|
+
if (exemplar === null || exemplar === undefined) {
|
|
98
99
|
return;
|
|
99
100
|
}
|
|
100
|
-
else if (this.
|
|
101
|
-
|
|
101
|
+
else if (this.isNode(exemplar)) {
|
|
102
|
+
node = exemplar;
|
|
103
|
+
}
|
|
104
|
+
else if (this.isEntry(exemplar)) {
|
|
105
|
+
const [key, value] = exemplar;
|
|
102
106
|
if (key === undefined || key === null) {
|
|
103
107
|
return;
|
|
104
108
|
}
|
|
@@ -106,50 +110,69 @@ class RedBlackTree extends bst_1.BST {
|
|
|
106
110
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
107
111
|
}
|
|
108
112
|
}
|
|
113
|
+
else if (this.isNodeKey(exemplar)) {
|
|
114
|
+
node = this.createNode(exemplar, undefined, types_1.RBTNColor.RED);
|
|
115
|
+
}
|
|
109
116
|
else {
|
|
110
117
|
return;
|
|
111
118
|
}
|
|
112
|
-
node
|
|
113
|
-
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
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
|
+
/**
|
|
126
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
127
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
128
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
129
|
+
* `undefined`.
|
|
130
|
+
*/
|
|
131
|
+
add(keyOrNodeOrEntry) {
|
|
132
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
133
|
+
if (newNode === undefined)
|
|
134
|
+
return;
|
|
135
|
+
newNode.left = this.Sentinel;
|
|
136
|
+
newNode.right = this.Sentinel;
|
|
114
137
|
let y = undefined;
|
|
115
138
|
let x = this.root;
|
|
116
139
|
while (x !== this.Sentinel) {
|
|
117
140
|
y = x;
|
|
118
141
|
if (x) {
|
|
119
|
-
if (
|
|
142
|
+
if (newNode.key < x.key) {
|
|
120
143
|
x = x.left;
|
|
121
144
|
}
|
|
122
|
-
else if (
|
|
145
|
+
else if (newNode.key > x.key) {
|
|
123
146
|
x = x === null || x === void 0 ? void 0 : x.right;
|
|
124
147
|
}
|
|
125
148
|
else {
|
|
126
|
-
if (
|
|
127
|
-
this._replaceNode(x,
|
|
149
|
+
if (newNode !== x) {
|
|
150
|
+
this._replaceNode(x, newNode);
|
|
128
151
|
}
|
|
129
152
|
return;
|
|
130
153
|
}
|
|
131
154
|
}
|
|
132
155
|
}
|
|
133
|
-
|
|
156
|
+
newNode.parent = y;
|
|
134
157
|
if (y === undefined) {
|
|
135
|
-
this._setRoot(
|
|
158
|
+
this._setRoot(newNode);
|
|
136
159
|
}
|
|
137
|
-
else if (
|
|
138
|
-
y.left =
|
|
160
|
+
else if (newNode.key < y.key) {
|
|
161
|
+
y.left = newNode;
|
|
139
162
|
}
|
|
140
163
|
else {
|
|
141
|
-
y.right =
|
|
164
|
+
y.right = newNode;
|
|
142
165
|
}
|
|
143
|
-
if (
|
|
144
|
-
|
|
166
|
+
if (newNode.parent === undefined) {
|
|
167
|
+
newNode.color = types_1.RBTNColor.BLACK;
|
|
145
168
|
this._size++;
|
|
146
169
|
return;
|
|
147
170
|
}
|
|
148
|
-
if (
|
|
171
|
+
if (newNode.parent.parent === undefined) {
|
|
149
172
|
this._size++;
|
|
150
173
|
return;
|
|
151
174
|
}
|
|
152
|
-
this._fixInsert(
|
|
175
|
+
this._fixInsert(newNode);
|
|
153
176
|
this._size++;
|
|
154
177
|
}
|
|
155
178
|
/**
|
|
@@ -41,6 +41,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
41
41
|
*/
|
|
42
42
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
43
43
|
createTree(options?: TreeMultimapOptions): TREE;
|
|
44
|
+
/**
|
|
45
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
46
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
47
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
|
+
* class.
|
|
49
|
+
*/
|
|
50
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
51
|
+
/**
|
|
52
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
53
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
54
|
+
* the value type and `N` represents the node type.
|
|
55
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
56
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
57
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
58
|
+
*/
|
|
59
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
44
60
|
/**
|
|
45
61
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
46
62
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -51,6 +51,48 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
51
51
|
createTree(options) {
|
|
52
52
|
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
56
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
57
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
|
+
* class.
|
|
59
|
+
*/
|
|
60
|
+
isNode(exemplar) {
|
|
61
|
+
return exemplar instanceof TreeMultimapNode;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
65
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
66
|
+
* the value type and `N` represents the node type.
|
|
67
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
68
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
69
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
70
|
+
*/
|
|
71
|
+
exemplarToNode(exemplar, count = 1) {
|
|
72
|
+
let node;
|
|
73
|
+
if (exemplar === undefined || exemplar === null) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
else if (this.isNode(exemplar)) {
|
|
77
|
+
node = exemplar;
|
|
78
|
+
}
|
|
79
|
+
else if (this.isEntry(exemplar)) {
|
|
80
|
+
const [key, value] = exemplar;
|
|
81
|
+
if (key === undefined || key === null) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
node = this.createNode(key, value, count);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (this.isNodeKey(exemplar)) {
|
|
89
|
+
node = this.createNode(exemplar, undefined, count);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
54
96
|
/**
|
|
55
97
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
56
98
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -68,28 +110,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
68
110
|
* @returns either a node (`N`) or `undefined`.
|
|
69
111
|
*/
|
|
70
112
|
add(keyOrNodeOrEntry, count = 1) {
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
76
|
-
newNode = keyOrNodeOrEntry;
|
|
77
|
-
}
|
|
78
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
79
|
-
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
80
|
-
}
|
|
81
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
82
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
83
|
-
if (key === undefined || key === null) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
newNode = this.createNode(key, value, count);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
113
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
114
|
+
if (newNode === undefined)
|
|
91
115
|
return;
|
|
92
|
-
}
|
|
93
116
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
94
117
|
const inserted = super.add(newNode);
|
|
95
118
|
if (inserted) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "undirected-graph-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.48.0",
|
|
4
4
|
"description": "Undirected Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -145,6 +145,6 @@
|
|
|
145
145
|
"typescript": "^4.9.5"
|
|
146
146
|
},
|
|
147
147
|
"dependencies": {
|
|
148
|
-
"data-structure-typed": "^1.
|
|
148
|
+
"data-structure-typed": "^1.48.0"
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -82,6 +82,15 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
82
82
|
}) as TREE;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
87
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
88
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
89
|
+
*/
|
|
90
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
91
|
+
return exemplar instanceof AVLTreeNode;
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
/**
|
|
86
95
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
87
96
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -163,6 +163,47 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
163
163
|
return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/**
|
|
167
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
168
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
169
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
170
|
+
*/
|
|
171
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
172
|
+
return exemplar instanceof BinaryTreeNode;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
177
|
+
* object.
|
|
178
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
179
|
+
* function. It can be any type.
|
|
180
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
181
|
+
*/
|
|
182
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined {
|
|
183
|
+
if (exemplar === undefined) return;
|
|
184
|
+
|
|
185
|
+
let node: N | null | undefined;
|
|
186
|
+
if (exemplar === null) {
|
|
187
|
+
node = null;
|
|
188
|
+
} else if (this.isEntry(exemplar)) {
|
|
189
|
+
const [key, value] = exemplar;
|
|
190
|
+
if (key === undefined) {
|
|
191
|
+
return;
|
|
192
|
+
} else if (key === null) {
|
|
193
|
+
node = null;
|
|
194
|
+
} else {
|
|
195
|
+
node = this.createNode(key, value);
|
|
196
|
+
}
|
|
197
|
+
} else if (this.isNode(exemplar)) {
|
|
198
|
+
node = exemplar;
|
|
199
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
200
|
+
node = this.createNode(exemplar);
|
|
201
|
+
} else {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
|
|
166
207
|
/**
|
|
167
208
|
* The function checks if a given value is an entry in a binary tree node.
|
|
168
209
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -188,7 +229,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
188
229
|
*/
|
|
189
230
|
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | null | undefined {
|
|
190
231
|
|
|
191
|
-
let inserted: N | null | undefined
|
|
232
|
+
let inserted: N | null | undefined;
|
|
233
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
234
|
+
if (newNode === undefined) return;
|
|
192
235
|
|
|
193
236
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
194
237
|
const queue = new Queue<N>([root]);
|
|
@@ -205,30 +248,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
205
248
|
}
|
|
206
249
|
};
|
|
207
250
|
|
|
208
|
-
if (keyOrNodeOrEntry === null) {
|
|
209
|
-
needInsert = null;
|
|
210
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
211
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
212
|
-
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
213
|
-
needInsert = keyOrNodeOrEntry;
|
|
214
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
215
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
216
|
-
if (key === undefined) {
|
|
217
|
-
return;
|
|
218
|
-
} else if (key === null) {
|
|
219
|
-
needInsert = null;
|
|
220
|
-
} else {
|
|
221
|
-
needInsert = this.createNode(key, value);
|
|
222
|
-
}
|
|
223
|
-
} else {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
251
|
if (this.root) {
|
|
228
|
-
inserted = _bfs(this.root,
|
|
252
|
+
inserted = _bfs(this.root, newNode);
|
|
229
253
|
} else {
|
|
230
|
-
this._setRoot(
|
|
231
|
-
if (
|
|
254
|
+
this._setRoot(newNode);
|
|
255
|
+
if (newNode) {
|
|
232
256
|
this._size = 1;
|
|
233
257
|
} else {
|
|
234
258
|
this._size = 0;
|
|
@@ -143,6 +143,42 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
143
143
|
}) as TREE;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
148
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
149
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
150
|
+
*/
|
|
151
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
152
|
+
return exemplar instanceof BSTNode;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
157
|
+
* is valid, otherwise it returns undefined.
|
|
158
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
159
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
160
|
+
*/
|
|
161
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
+
let node: N | undefined;
|
|
163
|
+
if (exemplar === null || exemplar === undefined) {
|
|
164
|
+
return;
|
|
165
|
+
} else if (this.isNode(exemplar)) {
|
|
166
|
+
node = exemplar;
|
|
167
|
+
} else if (this.isEntry(exemplar)) {
|
|
168
|
+
const [key, value] = exemplar;
|
|
169
|
+
if (key === undefined || key === null) {
|
|
170
|
+
return;
|
|
171
|
+
} else {
|
|
172
|
+
node = this.createNode(key, value);
|
|
173
|
+
}
|
|
174
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
175
|
+
node = this.createNode(exemplar);
|
|
176
|
+
} else {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
|
|
146
182
|
/**
|
|
147
183
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
148
184
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -159,25 +195,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
159
195
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
160
196
|
*/
|
|
161
197
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let newNode: N | undefined;
|
|
167
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
168
|
-
newNode = keyOrNodeOrEntry;
|
|
169
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
170
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
171
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
-
if (key === undefined || key === null) {
|
|
174
|
-
return;
|
|
175
|
-
} else {
|
|
176
|
-
newNode = this.createNode(key, value);
|
|
177
|
-
}
|
|
178
|
-
} else {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
198
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
199
|
+
if (newNode === undefined) return;
|
|
181
200
|
|
|
182
201
|
if (this.root === undefined) {
|
|
183
202
|
this._setRoot(newNode);
|
|
@@ -107,38 +107,62 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
110
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
111
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
112
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
113
|
+
* class.
|
|
112
114
|
*/
|
|
113
|
-
|
|
115
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
116
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
117
|
+
}
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
*
|
|
120
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
121
|
+
* otherwise it returns undefined.
|
|
122
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
123
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
124
|
+
* that is not a valid exemplar.
|
|
125
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
120
126
|
*/
|
|
121
|
-
override
|
|
122
|
-
let node: N;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
126
|
-
node = keyOrNodeOrEntry;
|
|
127
|
-
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
127
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
128
|
+
let node: N | undefined;
|
|
129
|
+
|
|
130
|
+
if (exemplar === null || exemplar === undefined) {
|
|
128
131
|
return;
|
|
129
|
-
} else if (this.
|
|
130
|
-
|
|
132
|
+
} else if (this.isNode(exemplar)) {
|
|
133
|
+
node = exemplar;
|
|
134
|
+
} else if (this.isEntry(exemplar)) {
|
|
135
|
+
const [key, value] = exemplar;
|
|
131
136
|
if (key === undefined || key === null) {
|
|
132
137
|
return;
|
|
133
138
|
} else {
|
|
134
139
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
135
140
|
}
|
|
141
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
142
|
+
node = this.createNode(exemplar, undefined, RBTNColor.RED);
|
|
136
143
|
} else {
|
|
137
144
|
return;
|
|
138
145
|
}
|
|
146
|
+
return node;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
151
|
+
* Space Complexity: O(1)
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
156
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
157
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
158
|
+
* `undefined`.
|
|
159
|
+
*/
|
|
160
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
161
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
162
|
+
if (newNode === undefined) return;
|
|
139
163
|
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
newNode.left = this.Sentinel;
|
|
165
|
+
newNode.right = this.Sentinel;
|
|
142
166
|
|
|
143
167
|
let y: N | undefined = undefined;
|
|
144
168
|
let x: N | undefined = this.root;
|
|
@@ -146,13 +170,13 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
146
170
|
while (x !== this.Sentinel) {
|
|
147
171
|
y = x;
|
|
148
172
|
if (x) {
|
|
149
|
-
if (
|
|
173
|
+
if (newNode.key < x.key) {
|
|
150
174
|
x = x.left;
|
|
151
|
-
} else if (
|
|
175
|
+
} else if (newNode.key > x.key) {
|
|
152
176
|
x = x?.right;
|
|
153
177
|
} else {
|
|
154
|
-
if (
|
|
155
|
-
this._replaceNode(x,
|
|
178
|
+
if (newNode !== x) {
|
|
179
|
+
this._replaceNode(x, newNode)
|
|
156
180
|
}
|
|
157
181
|
return;
|
|
158
182
|
}
|
|
@@ -160,27 +184,27 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
160
184
|
|
|
161
185
|
}
|
|
162
186
|
|
|
163
|
-
|
|
187
|
+
newNode.parent = y;
|
|
164
188
|
if (y === undefined) {
|
|
165
|
-
this._setRoot(
|
|
166
|
-
} else if (
|
|
167
|
-
y.left =
|
|
189
|
+
this._setRoot(newNode);
|
|
190
|
+
} else if (newNode.key < y.key) {
|
|
191
|
+
y.left = newNode;
|
|
168
192
|
} else {
|
|
169
|
-
y.right =
|
|
193
|
+
y.right = newNode;
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
if (
|
|
173
|
-
|
|
196
|
+
if (newNode.parent === undefined) {
|
|
197
|
+
newNode.color = RBTNColor.BLACK;
|
|
174
198
|
this._size++;
|
|
175
199
|
return;
|
|
176
200
|
}
|
|
177
201
|
|
|
178
|
-
if (
|
|
202
|
+
if (newNode.parent.parent === undefined) {
|
|
179
203
|
this._size++;
|
|
180
204
|
return;
|
|
181
205
|
}
|
|
182
206
|
|
|
183
|
-
this._fixInsert(
|
|
207
|
+
this._fixInsert(newNode);
|
|
184
208
|
this._size++;
|
|
185
209
|
}
|
|
186
210
|
|
|
@@ -80,6 +80,45 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
80
80
|
}) as TREE;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
86
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
87
|
+
* class.
|
|
88
|
+
*/
|
|
89
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
90
|
+
return exemplar instanceof TreeMultimapNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
95
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
96
|
+
* the value type and `N` represents the node type.
|
|
97
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
98
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
99
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
100
|
+
*/
|
|
101
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
102
|
+
let node: N | undefined;
|
|
103
|
+
if (exemplar === undefined || exemplar === null) {
|
|
104
|
+
return;
|
|
105
|
+
} else if (this.isNode(exemplar)) {
|
|
106
|
+
node = exemplar;
|
|
107
|
+
} else if (this.isEntry(exemplar)) {
|
|
108
|
+
const [key, value] = exemplar;
|
|
109
|
+
if (key === undefined || key === null) {
|
|
110
|
+
return;
|
|
111
|
+
} else {
|
|
112
|
+
node = this.createNode(key, value, count);
|
|
113
|
+
}
|
|
114
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
115
|
+
node = this.createNode(exemplar, undefined, count);
|
|
116
|
+
} else {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
|
|
83
122
|
/**
|
|
84
123
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
85
124
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -98,23 +137,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
98
137
|
* @returns either a node (`N`) or `undefined`.
|
|
99
138
|
*/
|
|
100
139
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
105
|
-
newNode = keyOrNodeOrEntry;
|
|
106
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
107
|
-
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
108
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
-
if (key === undefined || key === null) {
|
|
111
|
-
return;
|
|
112
|
-
} else {
|
|
113
|
-
newNode = this.createNode(key, value, count);
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
140
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
141
|
+
if (newNode === undefined) return;
|
|
142
|
+
|
|
118
143
|
const orgNodeCount = newNode?.count || 0;
|
|
119
144
|
const inserted = super.add(newNode);
|
|
120
145
|
if (inserted) {
|