data-structure-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/CHANGELOG.md +1 -1
- package/README.md +33 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +52 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/cjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +44 -21
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +52 -28
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +44 -21
- package/dist/umd/data-structure-typed.js +186 -89
- 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 +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
|
@@ -119,6 +119,51 @@ export class BinaryTree {
|
|
|
119
119
|
createTree(options) {
|
|
120
120
|
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
121
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
124
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
125
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
126
|
+
*/
|
|
127
|
+
isNode(exemplar) {
|
|
128
|
+
return exemplar instanceof BinaryTreeNode;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
132
|
+
* object.
|
|
133
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
134
|
+
* function. It can be any type.
|
|
135
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
136
|
+
*/
|
|
137
|
+
exemplarToNode(exemplar) {
|
|
138
|
+
if (exemplar === undefined)
|
|
139
|
+
return;
|
|
140
|
+
let node;
|
|
141
|
+
if (exemplar === null) {
|
|
142
|
+
node = null;
|
|
143
|
+
}
|
|
144
|
+
else if (this.isEntry(exemplar)) {
|
|
145
|
+
const [key, value] = exemplar;
|
|
146
|
+
if (key === undefined) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
else if (key === null) {
|
|
150
|
+
node = null;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
node = this.createNode(key, value);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (this.isNode(exemplar)) {
|
|
157
|
+
node = exemplar;
|
|
158
|
+
}
|
|
159
|
+
else if (this.isNodeKey(exemplar)) {
|
|
160
|
+
node = this.createNode(exemplar);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
return node;
|
|
166
|
+
}
|
|
122
167
|
/**
|
|
123
168
|
* The function checks if a given value is an entry in a binary tree node.
|
|
124
169
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -141,7 +186,10 @@ export class BinaryTree {
|
|
|
141
186
|
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
142
187
|
*/
|
|
143
188
|
add(keyOrNodeOrEntry) {
|
|
144
|
-
let inserted
|
|
189
|
+
let inserted;
|
|
190
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
191
|
+
if (newNode === undefined)
|
|
192
|
+
return;
|
|
145
193
|
const _bfs = (root, newNode) => {
|
|
146
194
|
const queue = new Queue([root]);
|
|
147
195
|
while (queue.size > 0) {
|
|
@@ -159,36 +207,12 @@ export class BinaryTree {
|
|
|
159
207
|
queue.push(cur.right);
|
|
160
208
|
}
|
|
161
209
|
};
|
|
162
|
-
if (keyOrNodeOrEntry === null) {
|
|
163
|
-
needInsert = null;
|
|
164
|
-
}
|
|
165
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
166
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
167
|
-
}
|
|
168
|
-
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
169
|
-
needInsert = keyOrNodeOrEntry;
|
|
170
|
-
}
|
|
171
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
-
if (key === undefined) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
else if (key === null) {
|
|
177
|
-
needInsert = null;
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
needInsert = this.createNode(key, value);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
210
|
if (this.root) {
|
|
187
|
-
inserted = _bfs(this.root,
|
|
211
|
+
inserted = _bfs(this.root, newNode);
|
|
188
212
|
}
|
|
189
213
|
else {
|
|
190
|
-
this._setRoot(
|
|
191
|
-
if (
|
|
214
|
+
this._setRoot(newNode);
|
|
215
|
+
if (newNode) {
|
|
192
216
|
this._size = 1;
|
|
193
217
|
}
|
|
194
218
|
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.
|
|
@@ -103,6 +103,45 @@ export class BST extends BinaryTree {
|
|
|
103
103
|
comparator: this.comparator, ...options
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
108
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
109
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
110
|
+
*/
|
|
111
|
+
isNode(exemplar) {
|
|
112
|
+
return exemplar instanceof BSTNode;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
116
|
+
* is valid, otherwise it returns undefined.
|
|
117
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
118
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
119
|
+
*/
|
|
120
|
+
exemplarToNode(exemplar) {
|
|
121
|
+
let node;
|
|
122
|
+
if (exemplar === null || exemplar === undefined) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
else if (this.isNode(exemplar)) {
|
|
126
|
+
node = exemplar;
|
|
127
|
+
}
|
|
128
|
+
else if (this.isEntry(exemplar)) {
|
|
129
|
+
const [key, value] = exemplar;
|
|
130
|
+
if (key === undefined || key === null) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
node = this.createNode(key, value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (this.isNodeKey(exemplar)) {
|
|
138
|
+
node = this.createNode(exemplar);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
return node;
|
|
144
|
+
}
|
|
106
145
|
/**
|
|
107
146
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
108
147
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -118,28 +157,9 @@ export class BST extends BinaryTree {
|
|
|
118
157
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
119
158
|
*/
|
|
120
159
|
add(keyOrNodeOrEntry) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
let newNode;
|
|
125
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
126
|
-
newNode = keyOrNodeOrEntry;
|
|
127
|
-
}
|
|
128
|
-
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
129
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
130
|
-
}
|
|
131
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
132
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
133
|
-
if (key === undefined || key === null) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
newNode = this.createNode(key, value);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
160
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
161
|
+
if (newNode === undefined)
|
|
141
162
|
return;
|
|
142
|
-
}
|
|
143
163
|
if (this.root === undefined) {
|
|
144
164
|
this._setRoot(newNode);
|
|
145
165
|
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)
|
|
@@ -78,28 +78,32 @@ export class RedBlackTree extends BST {
|
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
81
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
82
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
83
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
84
|
+
* class.
|
|
83
85
|
*/
|
|
86
|
+
isNode(exemplar) {
|
|
87
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
88
|
+
}
|
|
84
89
|
/**
|
|
85
|
-
* The function
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
*
|
|
90
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
91
|
+
* otherwise it returns undefined.
|
|
92
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
93
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
94
|
+
* that is not a valid exemplar.
|
|
95
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
89
96
|
*/
|
|
90
|
-
|
|
97
|
+
exemplarToNode(exemplar) {
|
|
91
98
|
let node;
|
|
92
|
-
if (
|
|
93
|
-
node = this.createNode(keyOrNodeOrEntry, undefined, RBTNColor.RED);
|
|
94
|
-
}
|
|
95
|
-
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
96
|
-
node = keyOrNodeOrEntry;
|
|
97
|
-
}
|
|
98
|
-
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
99
|
+
if (exemplar === null || exemplar === undefined) {
|
|
99
100
|
return;
|
|
100
101
|
}
|
|
101
|
-
else if (this.
|
|
102
|
-
|
|
102
|
+
else if (this.isNode(exemplar)) {
|
|
103
|
+
node = exemplar;
|
|
104
|
+
}
|
|
105
|
+
else if (this.isEntry(exemplar)) {
|
|
106
|
+
const [key, value] = exemplar;
|
|
103
107
|
if (key === undefined || key === null) {
|
|
104
108
|
return;
|
|
105
109
|
}
|
|
@@ -107,50 +111,69 @@ export class RedBlackTree extends BST {
|
|
|
107
111
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
108
112
|
}
|
|
109
113
|
}
|
|
114
|
+
else if (this.isNodeKey(exemplar)) {
|
|
115
|
+
node = this.createNode(exemplar, undefined, RBTNColor.RED);
|
|
116
|
+
}
|
|
110
117
|
else {
|
|
111
118
|
return;
|
|
112
119
|
}
|
|
113
|
-
node
|
|
114
|
-
|
|
120
|
+
return node;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
124
|
+
* Space Complexity: O(1)
|
|
125
|
+
*/
|
|
126
|
+
/**
|
|
127
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
128
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
129
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
130
|
+
* `undefined`.
|
|
131
|
+
*/
|
|
132
|
+
add(keyOrNodeOrEntry) {
|
|
133
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
134
|
+
if (newNode === undefined)
|
|
135
|
+
return;
|
|
136
|
+
newNode.left = this.Sentinel;
|
|
137
|
+
newNode.right = this.Sentinel;
|
|
115
138
|
let y = undefined;
|
|
116
139
|
let x = this.root;
|
|
117
140
|
while (x !== this.Sentinel) {
|
|
118
141
|
y = x;
|
|
119
142
|
if (x) {
|
|
120
|
-
if (
|
|
143
|
+
if (newNode.key < x.key) {
|
|
121
144
|
x = x.left;
|
|
122
145
|
}
|
|
123
|
-
else if (
|
|
146
|
+
else if (newNode.key > x.key) {
|
|
124
147
|
x = x?.right;
|
|
125
148
|
}
|
|
126
149
|
else {
|
|
127
|
-
if (
|
|
128
|
-
this._replaceNode(x,
|
|
150
|
+
if (newNode !== x) {
|
|
151
|
+
this._replaceNode(x, newNode);
|
|
129
152
|
}
|
|
130
153
|
return;
|
|
131
154
|
}
|
|
132
155
|
}
|
|
133
156
|
}
|
|
134
|
-
|
|
157
|
+
newNode.parent = y;
|
|
135
158
|
if (y === undefined) {
|
|
136
|
-
this._setRoot(
|
|
159
|
+
this._setRoot(newNode);
|
|
137
160
|
}
|
|
138
|
-
else if (
|
|
139
|
-
y.left =
|
|
161
|
+
else if (newNode.key < y.key) {
|
|
162
|
+
y.left = newNode;
|
|
140
163
|
}
|
|
141
164
|
else {
|
|
142
|
-
y.right =
|
|
165
|
+
y.right = newNode;
|
|
143
166
|
}
|
|
144
|
-
if (
|
|
145
|
-
|
|
167
|
+
if (newNode.parent === undefined) {
|
|
168
|
+
newNode.color = RBTNColor.BLACK;
|
|
146
169
|
this._size++;
|
|
147
170
|
return;
|
|
148
171
|
}
|
|
149
|
-
if (
|
|
172
|
+
if (newNode.parent.parent === undefined) {
|
|
150
173
|
this._size++;
|
|
151
174
|
return;
|
|
152
175
|
}
|
|
153
|
-
this._fixInsert(
|
|
176
|
+
this._fixInsert(newNode);
|
|
154
177
|
this._size++;
|
|
155
178
|
}
|
|
156
179
|
/**
|
|
@@ -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 @@ export class TreeMultimap extends AVLTree {
|
|
|
51
51
|
comparator: this.comparator, ...options
|
|
52
52
|
});
|
|
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 @@ export class TreeMultimap extends 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?.count || 0;
|
|
94
117
|
const inserted = super.add(newNode);
|
|
95
118
|
if (inserted) {
|