priority-queue-typed 1.47.5 → 1.47.7
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 +36 -18
- package/dist/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/data-structures/binary-tree/bst.js +168 -169
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/heap/heap.d.ts +19 -21
- package/dist/data-structures/heap/heap.js +52 -34
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +1 -0
- package/dist/data-structures/queue/deque.js +3 -0
- package/dist/data-structures/queue/queue.d.ts +1 -0
- package/dist/data-structures/queue/queue.js +3 -0
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -2
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +4 -2
- package/dist/types/common.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -45,25 +45,37 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
exports.BSTNode = BSTNode;
|
|
48
|
+
/**
|
|
49
|
+
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
50
|
+
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
51
|
+
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
52
|
+
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
53
|
+
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
54
|
+
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
55
|
+
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
56
|
+
*/
|
|
48
57
|
class BST extends binary_tree_1.BinaryTree {
|
|
49
58
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
59
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
60
|
+
* the tree with optional elements and options.
|
|
61
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
62
|
+
* binary search tree.
|
|
63
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
64
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
53
65
|
*/
|
|
54
|
-
constructor(options) {
|
|
55
|
-
super(options);
|
|
66
|
+
constructor(elements, options) {
|
|
67
|
+
super([], options);
|
|
68
|
+
this.comparator = (a, b) => a - b;
|
|
56
69
|
if (options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
const { comparator } = options;
|
|
71
|
+
if (comparator) {
|
|
72
|
+
this.comparator = comparator;
|
|
73
|
+
}
|
|
61
74
|
}
|
|
62
75
|
this._root = undefined;
|
|
76
|
+
if (elements)
|
|
77
|
+
this.addMany(elements);
|
|
63
78
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Get the root node of the binary tree.
|
|
66
|
-
*/
|
|
67
79
|
get root() {
|
|
68
80
|
return this._root;
|
|
69
81
|
}
|
|
@@ -78,8 +90,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
78
90
|
createNode(key, value) {
|
|
79
91
|
return new BSTNode(key, value);
|
|
80
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* The function creates a new binary search tree with the specified options.
|
|
95
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
96
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
97
|
+
* that defines various options for creating a binary search tree.
|
|
98
|
+
* @returns a new instance of the BST class with the specified options.
|
|
99
|
+
*/
|
|
81
100
|
createTree(options) {
|
|
82
|
-
return new BST(Object.assign(
|
|
101
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
83
102
|
}
|
|
84
103
|
/**
|
|
85
104
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
@@ -89,158 +108,138 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
89
108
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
90
109
|
* Space Complexity: O(1) - Constant space is used.
|
|
91
110
|
*
|
|
92
|
-
* The `add` function adds a new node to a binary search tree
|
|
93
|
-
*
|
|
94
|
-
* following
|
|
95
|
-
* @
|
|
96
|
-
*
|
|
97
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
98
|
-
* no node was inserted, it returns `undefined`.
|
|
111
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
112
|
+
* object.
|
|
113
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
114
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
115
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
99
116
|
*/
|
|
100
|
-
add(
|
|
101
|
-
if (
|
|
117
|
+
add(keyOrNodeOrEntry) {
|
|
118
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
102
119
|
return undefined;
|
|
103
|
-
|
|
104
|
-
let inserted;
|
|
120
|
+
}
|
|
105
121
|
let newNode;
|
|
106
|
-
if (
|
|
107
|
-
newNode =
|
|
122
|
+
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
123
|
+
newNode = keyOrNodeOrEntry;
|
|
108
124
|
}
|
|
109
|
-
else if (this.isNodeKey(
|
|
110
|
-
newNode = this.createNode(
|
|
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
|
+
}
|
|
111
136
|
}
|
|
112
137
|
else {
|
|
113
|
-
|
|
138
|
+
return;
|
|
114
139
|
}
|
|
115
140
|
if (this.root === undefined) {
|
|
116
141
|
this._setRoot(newNode);
|
|
117
|
-
this._size
|
|
118
|
-
|
|
142
|
+
this._size++;
|
|
143
|
+
return this.root;
|
|
119
144
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
//Add to the left of the current node
|
|
140
|
-
cur.left = newNode;
|
|
141
|
-
this._size = this.size + 1;
|
|
142
|
-
traversing = false;
|
|
143
|
-
inserted = cur.left;
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
//Traverse the left of the current node
|
|
147
|
-
if (cur.left)
|
|
148
|
-
cur = cur.left;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
|
|
152
|
-
// Traverse right of the node
|
|
153
|
-
if (cur.right === undefined) {
|
|
154
|
-
if (newNode) {
|
|
155
|
-
newNode.parent = cur;
|
|
156
|
-
}
|
|
157
|
-
//Add to the right of the current node
|
|
158
|
-
cur.right = newNode;
|
|
159
|
-
this._size = this.size + 1;
|
|
160
|
-
traversing = false;
|
|
161
|
-
inserted = cur.right;
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
//Traverse the left of the current node
|
|
165
|
-
if (cur.right)
|
|
166
|
-
cur = cur.right;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
145
|
+
let current = this.root;
|
|
146
|
+
while (current !== undefined) {
|
|
147
|
+
if (this._compare(current.key, newNode.key) === types_1.CP.eq) {
|
|
148
|
+
// if (current !== newNode) {
|
|
149
|
+
// The key value is the same but the reference is different, update the value of the existing node
|
|
150
|
+
this._replaceNode(current, newNode);
|
|
151
|
+
return newNode;
|
|
152
|
+
// } else {
|
|
153
|
+
// The key value is the same and the reference is the same, replace the entire node
|
|
154
|
+
// this._replaceNode(current, newNode);
|
|
155
|
+
// return;
|
|
156
|
+
// }
|
|
157
|
+
}
|
|
158
|
+
else if (this._compare(current.key, newNode.key) === types_1.CP.gt) {
|
|
159
|
+
if (current.left === undefined) {
|
|
160
|
+
current.left = newNode;
|
|
161
|
+
newNode.parent = current;
|
|
162
|
+
this._size++;
|
|
163
|
+
return newNode;
|
|
169
164
|
}
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
current = current.left;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
if (current.right === undefined) {
|
|
169
|
+
current.right = newNode;
|
|
170
|
+
newNode.parent = current;
|
|
171
|
+
this._size++;
|
|
172
|
+
return newNode;
|
|
172
173
|
}
|
|
174
|
+
current = current.right;
|
|
173
175
|
}
|
|
174
176
|
}
|
|
175
|
-
return
|
|
177
|
+
return undefined;
|
|
176
178
|
}
|
|
177
179
|
/**
|
|
178
|
-
* Time Complexity: O(
|
|
179
|
-
* Space Complexity: O(
|
|
180
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
181
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
180
182
|
*/
|
|
181
183
|
/**
|
|
182
|
-
* Time Complexity: O(
|
|
183
|
-
* Space Complexity: O(
|
|
184
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
185
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
184
186
|
*
|
|
185
|
-
* The `addMany` function
|
|
186
|
-
*
|
|
187
|
-
* @param
|
|
188
|
-
* binary
|
|
189
|
-
* node), or `undefined`.
|
|
190
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
191
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
192
|
-
* the `keysOrNodes` array.
|
|
187
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
188
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
189
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
190
|
+
* binary tree.
|
|
193
191
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
194
|
-
* adding the nodes. The default value is
|
|
192
|
+
* adding the nodes. The default value is true.
|
|
195
193
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
196
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* @returns The
|
|
194
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
195
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
196
|
+
* tree instance.
|
|
197
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
200
198
|
*/
|
|
201
|
-
addMany(
|
|
202
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
203
|
-
function hasNoUndefined(arr) {
|
|
204
|
-
return arr.indexOf(undefined) === -1;
|
|
205
|
-
}
|
|
206
|
-
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
207
|
-
return super.addMany(keysOrNodes, data).map(n => n !== null && n !== void 0 ? n : undefined);
|
|
208
|
-
}
|
|
199
|
+
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
209
200
|
const inserted = [];
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
return false;
|
|
201
|
+
if (!isBalanceAdd) {
|
|
202
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
203
|
+
const nn = this.add(kve);
|
|
204
|
+
inserted.push(nn);
|
|
205
|
+
}
|
|
206
|
+
return inserted;
|
|
217
207
|
}
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return
|
|
208
|
+
const realBTNExemplars = [];
|
|
209
|
+
const isRealBTNExemplar = (kve) => {
|
|
210
|
+
if (kve === undefined || kve === null)
|
|
211
|
+
return false;
|
|
212
|
+
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
223
213
|
};
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
214
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
215
|
+
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
227
216
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
217
|
+
// TODO this addMany function is inefficient, it should be optimized
|
|
218
|
+
let sorted = [];
|
|
219
|
+
sorted = realBTNExemplars.sort((a, b) => {
|
|
220
|
+
let aR, bR;
|
|
221
|
+
if (this.isEntry(a))
|
|
222
|
+
aR = a[0];
|
|
223
|
+
else if (this.isRealNode(a))
|
|
224
|
+
aR = a.key;
|
|
225
|
+
else
|
|
226
|
+
aR = a;
|
|
227
|
+
if (this.isEntry(b))
|
|
228
|
+
bR = b[0];
|
|
229
|
+
else if (this.isRealNode(b))
|
|
230
|
+
bR = b.key;
|
|
231
|
+
else
|
|
232
|
+
bR = b;
|
|
233
|
+
return aR - bR;
|
|
234
|
+
});
|
|
235
|
+
const _dfs = (arr) => {
|
|
237
236
|
if (arr.length === 0)
|
|
238
237
|
return;
|
|
239
238
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
240
|
-
const newNode = this.add(arr[mid]
|
|
239
|
+
const newNode = this.add(arr[mid]);
|
|
241
240
|
inserted.push(newNode);
|
|
242
|
-
_dfs(arr.slice(0, mid)
|
|
243
|
-
_dfs(arr.slice(mid + 1)
|
|
241
|
+
_dfs(arr.slice(0, mid));
|
|
242
|
+
_dfs(arr.slice(mid + 1));
|
|
244
243
|
};
|
|
245
244
|
const _iterate = () => {
|
|
246
245
|
const n = sorted.length;
|
|
@@ -251,7 +250,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
251
250
|
const [l, r] = popped;
|
|
252
251
|
if (l <= r) {
|
|
253
252
|
const m = l + Math.floor((r - l) / 2);
|
|
254
|
-
const newNode = this.add(
|
|
253
|
+
const newNode = this.add(sorted[m]);
|
|
255
254
|
inserted.push(newNode);
|
|
256
255
|
stack.push([m + 1, r]);
|
|
257
256
|
stack.push([l, m - 1]);
|
|
@@ -260,7 +259,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
260
259
|
}
|
|
261
260
|
};
|
|
262
261
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
263
|
-
_dfs(
|
|
262
|
+
_dfs(sorted);
|
|
264
263
|
}
|
|
265
264
|
else {
|
|
266
265
|
_iterate();
|
|
@@ -268,8 +267,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
268
267
|
return inserted;
|
|
269
268
|
}
|
|
270
269
|
/**
|
|
271
|
-
* Time Complexity: O(log n) -
|
|
272
|
-
* Space Complexity: O(
|
|
270
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
271
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
273
272
|
*/
|
|
274
273
|
/**
|
|
275
274
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -286,7 +285,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
286
285
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
287
286
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
288
287
|
*/
|
|
289
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
288
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
290
289
|
var _a, _b, _c, _d, _e, _f;
|
|
291
290
|
if (this._compare(0, 1) === types_1.CP.lt)
|
|
292
291
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
@@ -297,7 +296,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
297
296
|
}
|
|
298
297
|
/**
|
|
299
298
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
300
|
-
* Space Complexity: O(
|
|
299
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
301
300
|
*/
|
|
302
301
|
/**
|
|
303
302
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -345,7 +344,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
345
344
|
}
|
|
346
345
|
}
|
|
347
346
|
/**
|
|
348
|
-
*
|
|
347
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
348
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
349
|
+
*/
|
|
350
|
+
/**
|
|
351
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
349
352
|
* otherwise it returns the key itself.
|
|
350
353
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
351
354
|
* `undefined`.
|
|
@@ -353,13 +356,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
353
356
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
354
357
|
* @returns either a node object (N) or undefined.
|
|
355
358
|
*/
|
|
356
|
-
|
|
359
|
+
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
357
360
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
358
361
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
361
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
362
|
-
*/
|
|
363
362
|
/**
|
|
364
363
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
365
364
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -383,8 +382,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
383
382
|
* performed on the binary tree. It can have two possible values:
|
|
384
383
|
* @returns The method returns an array of nodes (`N[]`).
|
|
385
384
|
*/
|
|
386
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
387
|
-
beginRoot = this.
|
|
385
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
386
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
388
387
|
if (!beginRoot)
|
|
389
388
|
return [];
|
|
390
389
|
const ans = [];
|
|
@@ -464,8 +463,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
464
463
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
465
464
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
466
465
|
*/
|
|
467
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.
|
|
468
|
-
targetNode = this.
|
|
466
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
467
|
+
targetNode = this.ensureNode(targetNode);
|
|
469
468
|
const ans = [];
|
|
470
469
|
if (!targetNode)
|
|
471
470
|
return ans;
|
|
@@ -505,17 +504,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
505
504
|
}
|
|
506
505
|
}
|
|
507
506
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
511
|
-
*
|
|
512
|
-
* Use Cases and Efficiency:
|
|
513
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
514
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
515
|
-
*/
|
|
516
|
-
/**
|
|
517
|
-
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
518
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
507
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
508
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
519
509
|
*/
|
|
520
510
|
/**
|
|
521
511
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -528,7 +518,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
528
518
|
* values:
|
|
529
519
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
530
520
|
*/
|
|
531
|
-
perfectlyBalance(iterationType = this.
|
|
521
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
532
522
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
533
523
|
this.clear();
|
|
534
524
|
if (sorted.length < 1)
|
|
@@ -539,7 +529,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
539
529
|
return;
|
|
540
530
|
const m = l + Math.floor((r - l) / 2);
|
|
541
531
|
const midNode = sorted[m];
|
|
542
|
-
this.add(midNode.key, midNode.value);
|
|
532
|
+
this.add([midNode.key, midNode.value]);
|
|
543
533
|
buildBalanceBST(l, m - 1);
|
|
544
534
|
buildBalanceBST(m + 1, r);
|
|
545
535
|
};
|
|
@@ -556,7 +546,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
556
546
|
const m = l + Math.floor((r - l) / 2);
|
|
557
547
|
const midNode = sorted[m];
|
|
558
548
|
debugger;
|
|
559
|
-
this.add(midNode.key, midNode.value);
|
|
549
|
+
this.add([midNode.key, midNode.value]);
|
|
560
550
|
stack.push([m + 1, r]);
|
|
561
551
|
stack.push([l, m - 1]);
|
|
562
552
|
}
|
|
@@ -566,8 +556,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
566
556
|
}
|
|
567
557
|
}
|
|
568
558
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
559
|
+
* Balancing Adjustment:
|
|
560
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
561
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
562
|
+
*
|
|
563
|
+
* Use Cases and Efficiency:
|
|
564
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
565
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
566
|
+
*/
|
|
567
|
+
/**
|
|
568
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
569
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
571
570
|
*/
|
|
572
571
|
/**
|
|
573
572
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -578,7 +577,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
578
577
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
579
578
|
* @returns a boolean value.
|
|
580
579
|
*/
|
|
581
|
-
isAVLBalanced(iterationType = this.
|
|
580
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
582
581
|
var _a, _b;
|
|
583
582
|
if (!this.root)
|
|
584
583
|
return true;
|
|
@@ -639,7 +638,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
639
638
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
640
639
|
*/
|
|
641
640
|
_compare(a, b) {
|
|
642
|
-
const compared = this.
|
|
641
|
+
const compared = this.comparator(a, b);
|
|
643
642
|
if (compared > 0)
|
|
644
643
|
return types_1.CP.gt;
|
|
645
644
|
else if (compared < 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 { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar, IterationType, 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<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -21,35 +21,54 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
21
21
|
*/
|
|
22
22
|
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
|
-
options: RBTreeOptions;
|
|
25
24
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
|
+
* initializes the tree with optional elements and options.
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
28
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
29
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
|
+
* elements to the
|
|
31
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
32
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
|
+
*/
|
|
35
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions>);
|
|
31
36
|
protected _root: N;
|
|
32
37
|
get root(): N;
|
|
33
38
|
protected _size: number;
|
|
34
39
|
get size(): number;
|
|
40
|
+
/**
|
|
41
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
43
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
44
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
45
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
46
|
+
* specific type when using the `createNode` method.
|
|
47
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
48
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
49
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
|
+
* value, and color.
|
|
51
|
+
*/
|
|
35
52
|
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
53
|
+
/**
|
|
54
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
56
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
57
|
+
* class.
|
|
58
|
+
* @returns a new instance of a RedBlackTree object.
|
|
59
|
+
*/
|
|
36
60
|
createTree(options?: RBTreeOptions): TREE;
|
|
37
61
|
/**
|
|
38
62
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
39
63
|
* Space Complexity: O(1)
|
|
40
64
|
*/
|
|
41
65
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
47
|
-
* following types:
|
|
48
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
49
|
-
* key in the node being added to the Red-Black Tree.
|
|
50
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
66
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
67
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
68
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
69
|
+
* `undefined`.
|
|
51
70
|
*/
|
|
52
|
-
add(
|
|
71
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
53
72
|
/**
|
|
54
73
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
55
74
|
* Space Complexity: O(1)
|
|
@@ -70,6 +89,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
70
89
|
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
71
90
|
*/
|
|
72
91
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
|
92
|
+
/**
|
|
93
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
94
|
+
* Space Complexity: O(1)
|
|
95
|
+
*/
|
|
73
96
|
isRealNode(node: N | undefined): node is N;
|
|
74
97
|
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
75
98
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
@@ -101,6 +124,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
101
124
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
102
125
|
*/
|
|
103
126
|
getPredecessor(x: N): N;
|
|
127
|
+
/**
|
|
128
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
129
|
+
* Space Complexity: O(1)
|
|
130
|
+
*/
|
|
104
131
|
clear(): void;
|
|
105
132
|
protected _setRoot(v: N): void;
|
|
106
133
|
/**
|
|
@@ -166,4 +193,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
166
193
|
* red-black tree.
|
|
167
194
|
*/
|
|
168
195
|
protected _fixInsert(k: N): void;
|
|
196
|
+
/**
|
|
197
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
198
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
199
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
200
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
201
|
+
* data structure.
|
|
202
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
203
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
204
|
+
*/
|
|
205
|
+
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
169
206
|
}
|