min-heap-typed 1.51.8 → 1.52.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/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/data-structures/binary-tree/bst.js +207 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +21 -20
- package/dist/data-structures/queue/deque.js +29 -23
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -60,31 +60,34 @@ exports.BSTNode = BSTNode;
|
|
|
60
60
|
*/
|
|
61
61
|
class BST extends binary_tree_1.BinaryTree {
|
|
62
62
|
/**
|
|
63
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
constructor(keysOrNodesOrEntries = [], options) {
|
|
63
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
64
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
65
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
66
|
+
* added to the binary search tree during the construction of the object.
|
|
67
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
68
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
69
|
+
*/
|
|
70
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
72
71
|
super([], options);
|
|
73
72
|
this._root = undefined;
|
|
74
|
-
this.
|
|
73
|
+
this._DEFAULT_COMPARATOR = (a, b) => {
|
|
74
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
75
|
+
throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
|
|
76
|
+
}
|
|
75
77
|
if (a > b)
|
|
76
78
|
return 1;
|
|
77
79
|
if (a < b)
|
|
78
80
|
return -1;
|
|
79
81
|
return 0;
|
|
80
82
|
};
|
|
83
|
+
this._comparator = this._DEFAULT_COMPARATOR;
|
|
81
84
|
if (options) {
|
|
82
85
|
const { comparator } = options;
|
|
83
86
|
if (comparator)
|
|
84
87
|
this._comparator = comparator;
|
|
85
88
|
}
|
|
86
|
-
if (
|
|
87
|
-
this.addMany(
|
|
89
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
90
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
88
91
|
}
|
|
89
92
|
/**
|
|
90
93
|
* The function returns the root node of a tree structure.
|
|
@@ -93,13 +96,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
93
96
|
get root() {
|
|
94
97
|
return this._root;
|
|
95
98
|
}
|
|
96
|
-
/**
|
|
97
|
-
* The function returns the value of the _comparator property.
|
|
98
|
-
* @returns The `_comparator` property is being returned.
|
|
99
|
-
*/
|
|
100
|
-
get comparator() {
|
|
101
|
-
return this._comparator;
|
|
102
|
-
}
|
|
103
99
|
/**
|
|
104
100
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
105
101
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -114,104 +110,68 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
114
110
|
/**
|
|
115
111
|
* The function creates a new binary search tree with the specified options.
|
|
116
112
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
117
|
-
* behavior of the `createTree` method. It
|
|
118
|
-
*
|
|
119
|
-
* @returns a new instance of the BST class
|
|
120
|
-
* options. The returned value is casted as TREE.
|
|
113
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
114
|
+
* following properties:
|
|
115
|
+
* @returns a new instance of the BST class with the provided options.
|
|
121
116
|
*/
|
|
122
117
|
createTree(options) {
|
|
123
118
|
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
124
119
|
}
|
|
125
120
|
/**
|
|
126
|
-
* The function
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* `
|
|
131
|
-
*
|
|
132
|
-
|
|
133
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
134
|
-
let node;
|
|
135
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
139
|
-
node = keyOrNodeOrEntry;
|
|
140
|
-
}
|
|
141
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
142
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
143
|
-
if (key === undefined || key === null) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
node = this.createNode(key, value);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
151
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
return node;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Time Complexity: O(log n)
|
|
160
|
-
* Space Complexity: O(log n)
|
|
121
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
122
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
123
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
124
|
+
* element.
|
|
125
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
126
|
+
* value associated with a key in a key-value pair.
|
|
127
|
+
* @returns either a NODE object or undefined.
|
|
161
128
|
*/
|
|
129
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
130
|
+
var _a;
|
|
131
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
|
|
132
|
+
}
|
|
162
133
|
/**
|
|
163
134
|
* Time Complexity: O(log n)
|
|
164
135
|
* Space Complexity: O(log n)
|
|
165
136
|
*
|
|
166
|
-
* The function
|
|
167
|
-
*
|
|
168
|
-
* @param {
|
|
169
|
-
* `
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
181
|
-
const key = keyOrNodeOrEntry[0];
|
|
182
|
-
if (key === null || key === undefined)
|
|
183
|
-
return;
|
|
184
|
-
return this.getNodeByKey(key, iterationType);
|
|
185
|
-
}
|
|
186
|
-
const key = keyOrNodeOrEntry;
|
|
187
|
-
if (key === null || key === undefined)
|
|
188
|
-
return;
|
|
189
|
-
return this.getNodeByKey(key, iterationType);
|
|
137
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
138
|
+
* it doesn't exist.
|
|
139
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
140
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
141
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
142
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
143
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
144
|
+
* value of `'ITERATIVE'`.
|
|
145
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
146
|
+
* not be ensured.
|
|
147
|
+
*/
|
|
148
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
149
|
+
var _a;
|
|
150
|
+
return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
190
151
|
}
|
|
191
152
|
/**
|
|
192
|
-
* The function checks if
|
|
193
|
-
* @param
|
|
194
|
-
*
|
|
153
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
154
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
155
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
156
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
157
|
+
* an instance of the `BSTNode` class.
|
|
195
158
|
*/
|
|
196
|
-
isNode(
|
|
197
|
-
return
|
|
159
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
160
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
198
161
|
}
|
|
199
|
-
/**
|
|
200
|
-
* Time Complexity: O(log n)
|
|
201
|
-
* Space Complexity: O(1)
|
|
202
|
-
*/
|
|
203
162
|
/**
|
|
204
163
|
* Time Complexity: O(log n)
|
|
205
164
|
* Space Complexity: O(1)
|
|
206
165
|
*
|
|
207
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* @param {V} [value] - The value
|
|
211
|
-
*
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
166
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
167
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
168
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
169
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
170
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
171
|
+
* @returns a boolean value.
|
|
172
|
+
*/
|
|
173
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
174
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
215
175
|
if (newNode === undefined)
|
|
216
176
|
return false;
|
|
217
177
|
if (this.root === undefined) {
|
|
@@ -222,15 +182,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
182
|
let current = this.root;
|
|
223
183
|
while (current !== undefined) {
|
|
224
184
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
225
|
-
// if (current !== newNode) {
|
|
226
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
227
185
|
this._replaceNode(current, newNode);
|
|
228
186
|
return true;
|
|
229
|
-
// } else {
|
|
230
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
231
|
-
// this._replaceNode(current, newNode);
|
|
232
|
-
// return;
|
|
233
|
-
// }
|
|
234
187
|
}
|
|
235
188
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
236
189
|
if (current.left === undefined) {
|
|
@@ -252,18 +205,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
205
|
return false;
|
|
253
206
|
}
|
|
254
207
|
/**
|
|
255
|
-
* Time Complexity: O(
|
|
256
|
-
* Space Complexity: O(
|
|
208
|
+
* Time Complexity: O(log n)
|
|
209
|
+
* Space Complexity: O(log n)
|
|
257
210
|
*/
|
|
258
211
|
/**
|
|
259
212
|
* Time Complexity: O(k log n)
|
|
260
213
|
* Space Complexity: O(k + log n)
|
|
261
214
|
*
|
|
262
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* data structure.
|
|
215
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
216
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
217
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
218
|
+
* elements to be added to the data structure.
|
|
267
219
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
268
220
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
269
221
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -272,20 +224,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
272
224
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
273
225
|
* value is true.
|
|
274
226
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
275
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* or entry was successfully inserted into the data structure.
|
|
227
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
228
|
+
* tree. It can have two possible values:
|
|
229
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
230
|
+
* successfully inserted into the data structure.
|
|
280
231
|
*/
|
|
281
|
-
addMany(
|
|
232
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
282
233
|
const inserted = [];
|
|
283
234
|
let valuesIterator;
|
|
284
235
|
if (values) {
|
|
285
236
|
valuesIterator = values[Symbol.iterator]();
|
|
286
237
|
}
|
|
287
238
|
if (!isBalanceAdd) {
|
|
288
|
-
for (const kve of
|
|
239
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
289
240
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
290
241
|
const nn = this.add(kve, value);
|
|
291
242
|
inserted.push(nn);
|
|
@@ -298,25 +249,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
249
|
return false;
|
|
299
250
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
300
251
|
};
|
|
301
|
-
for (const kve of
|
|
252
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
302
253
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
303
254
|
}
|
|
304
255
|
let sorted = [];
|
|
305
256
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
306
257
|
let keyA, keyB;
|
|
307
|
-
if (this.isEntry(a))
|
|
258
|
+
if (this.isEntry(a))
|
|
308
259
|
keyA = a[0];
|
|
309
|
-
}
|
|
310
260
|
else if (this.isRealNode(a))
|
|
311
261
|
keyA = a.key;
|
|
312
|
-
else
|
|
262
|
+
else if (this.toEntryFn) {
|
|
263
|
+
keyA = this.toEntryFn(a)[0];
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
313
266
|
keyA = a;
|
|
267
|
+
}
|
|
314
268
|
if (this.isEntry(b))
|
|
315
269
|
keyB = b[0];
|
|
316
270
|
else if (this.isRealNode(b))
|
|
317
271
|
keyB = b.key;
|
|
318
|
-
else
|
|
272
|
+
else if (this.toEntryFn) {
|
|
273
|
+
keyB = this.toEntryFn(b)[0];
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
319
276
|
keyB = b;
|
|
277
|
+
}
|
|
320
278
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
321
279
|
return this.comparator(keyA, keyB);
|
|
322
280
|
}
|
|
@@ -357,32 +315,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
357
315
|
return inserted;
|
|
358
316
|
}
|
|
359
317
|
/**
|
|
360
|
-
* Time Complexity: O(log n)
|
|
361
|
-
* Space Complexity: O(k + log n)
|
|
362
|
-
* /
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
318
|
* Time Complexity: O(log n)
|
|
366
319
|
* Space Complexity: O(k + log n)
|
|
367
320
|
*
|
|
368
|
-
* The
|
|
369
|
-
*
|
|
321
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
322
|
+
* identifier and callback function.
|
|
370
323
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
371
|
-
* want to search for in the
|
|
372
|
-
*
|
|
373
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
374
|
-
*
|
|
375
|
-
* function
|
|
376
|
-
* @param [onlyOne=false] - A boolean
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
324
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
325
|
+
* function.
|
|
326
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
327
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
328
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
329
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
330
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
331
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
332
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
333
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
334
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
335
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
336
|
+
* iteration to be performed. It can have two possible values:
|
|
337
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
386
338
|
*/
|
|
387
339
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
388
340
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -453,51 +405,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
405
|
* Time Complexity: O(log n)
|
|
454
406
|
* Space Complexity: O(1)
|
|
455
407
|
*
|
|
456
|
-
* The `getNode`
|
|
457
|
-
*
|
|
458
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
462
|
-
* the
|
|
463
|
-
*
|
|
464
|
-
*
|
|
408
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
409
|
+
* function in a binary search tree.
|
|
410
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
411
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
412
|
+
* type returned by the callback function.
|
|
413
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
414
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
415
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
416
|
+
* provided, the default callback will be
|
|
465
417
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
466
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
467
|
-
*
|
|
468
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
469
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
470
|
-
*
|
|
471
|
-
* @returns The method is returning a
|
|
418
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
419
|
+
* with that key. If it is a node, the search will start from that node.
|
|
420
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
421
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
422
|
+
* of the following values:
|
|
423
|
+
* @returns The method is returning a NODE object or undefined.
|
|
472
424
|
*/
|
|
473
425
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
474
426
|
var _a;
|
|
475
427
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
476
428
|
}
|
|
477
429
|
/**
|
|
478
|
-
* Time Complexity: O(log n)
|
|
479
|
-
* Space Complexity: O(
|
|
430
|
+
* Time Complexity: O(k log n)
|
|
431
|
+
* Space Complexity: O(k + log n)
|
|
480
432
|
*/
|
|
481
433
|
/**
|
|
482
434
|
* Time Complexity: O(log n)
|
|
483
435
|
* Space Complexity: O(1)
|
|
484
436
|
*
|
|
485
|
-
* The function `getNodeByKey`
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
490
|
-
* type of iteration to
|
|
491
|
-
*
|
|
492
|
-
* @returns The
|
|
493
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
437
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
438
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
439
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
440
|
+
* in the tree structure.
|
|
441
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
442
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
443
|
+
* It has a default value of `'ITERATIVE'`.
|
|
444
|
+
* @returns The method is returning a NODE object or undefined.
|
|
494
445
|
*/
|
|
495
446
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
496
447
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
497
448
|
}
|
|
498
449
|
/**
|
|
499
|
-
* Time
|
|
500
|
-
* Space
|
|
450
|
+
* Time Complexity: O(log n)
|
|
451
|
+
* Space Complexity: O(k + log n)
|
|
501
452
|
*/
|
|
502
453
|
/**
|
|
503
454
|
* Time complexity: O(n)
|
|
@@ -506,15 +457,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
506
457
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
507
458
|
* the callback function.
|
|
508
459
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
509
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
510
|
-
*
|
|
511
|
-
* @param {DFSOrderPattern} [pattern=
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
460
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
461
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
462
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
463
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
464
|
+
* take one of the following values:
|
|
465
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
466
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
467
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
468
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
469
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
518
470
|
* following values:
|
|
519
471
|
* @returns The method is returning an array of the return type of the callback function.
|
|
520
472
|
*/
|
|
@@ -522,8 +474,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
522
474
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
523
475
|
}
|
|
524
476
|
/**
|
|
525
|
-
* Time
|
|
526
|
-
* Space
|
|
477
|
+
* Time Complexity: O(log n)
|
|
478
|
+
* Space Complexity: O(1)
|
|
527
479
|
*/
|
|
528
480
|
/**
|
|
529
481
|
* Time complexity: O(n)
|
|
@@ -532,38 +484,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
532
484
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
533
485
|
* the callback function.
|
|
534
486
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
535
|
-
* visited during the breadth-first search
|
|
536
|
-
*
|
|
537
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
538
|
-
*
|
|
539
|
-
* the
|
|
540
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
541
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
542
|
-
*
|
|
543
|
-
* @returns
|
|
487
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
488
|
+
* node being visited, and it can return a value of any type.
|
|
489
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
490
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
491
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
492
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
493
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
494
|
+
* the following values:
|
|
495
|
+
* @returns an array of the return type of the callback function.
|
|
544
496
|
*/
|
|
545
497
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
546
498
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
547
499
|
}
|
|
548
500
|
/**
|
|
549
|
-
* Time
|
|
550
|
-
* Space
|
|
501
|
+
* Time Complexity: O(log n)
|
|
502
|
+
* Space Complexity: O(1)
|
|
551
503
|
*/
|
|
552
504
|
/**
|
|
553
505
|
* Time complexity: O(n)
|
|
554
506
|
* Space complexity: O(n)
|
|
555
507
|
*
|
|
556
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
557
|
-
*
|
|
508
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
509
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
558
510
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
559
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
560
|
-
* during the
|
|
561
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
562
|
-
* levels of
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* iteration.
|
|
511
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
512
|
+
* tree during the iteration process.
|
|
513
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
514
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
515
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
516
|
+
* value is provided, the root of
|
|
517
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
518
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
567
519
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
568
520
|
* function.
|
|
569
521
|
*/
|
|
@@ -571,27 +523,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
571
523
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
572
524
|
}
|
|
573
525
|
/**
|
|
574
|
-
* Time
|
|
575
|
-
* Space
|
|
526
|
+
* Time complexity: O(n)
|
|
527
|
+
* Space complexity: O(n)
|
|
576
528
|
*/
|
|
577
529
|
/**
|
|
578
|
-
* Time
|
|
579
|
-
* Space
|
|
530
|
+
* Time complexity: O(n)
|
|
531
|
+
* Space complexity: O(n)
|
|
580
532
|
*
|
|
581
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
582
|
-
*
|
|
533
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
534
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
583
535
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
584
|
-
* that
|
|
585
|
-
*
|
|
536
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
537
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
586
538
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
587
|
-
* traverse nodes that are lesser
|
|
588
|
-
*
|
|
589
|
-
* `
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
594
|
-
* performed on the binary tree. It can have two possible values:
|
|
539
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
540
|
+
* 0, or 1, where:
|
|
541
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
542
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
543
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
544
|
+
* `targetNode` is provided,
|
|
545
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
546
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
595
547
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
596
548
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
597
549
|
*/
|
|
@@ -634,18 +586,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
634
586
|
}
|
|
635
587
|
}
|
|
636
588
|
/**
|
|
637
|
-
* Time
|
|
638
|
-
* Space
|
|
589
|
+
* Time complexity: O(n)
|
|
590
|
+
* Space complexity: O(n)
|
|
639
591
|
*/
|
|
640
592
|
/**
|
|
641
|
-
* Time
|
|
642
|
-
* Space
|
|
593
|
+
* Time complexity: O(n)
|
|
594
|
+
* Space complexity: O(n)
|
|
643
595
|
*
|
|
644
|
-
* The `perfectlyBalance` function
|
|
645
|
-
*
|
|
646
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
647
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
648
|
-
*
|
|
596
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
597
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
598
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
599
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
600
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
601
|
+
* current instance of the class.
|
|
649
602
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
650
603
|
*/
|
|
651
604
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -685,25 +638,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
685
638
|
}
|
|
686
639
|
}
|
|
687
640
|
/**
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
* 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.
|
|
691
|
-
*
|
|
692
|
-
* Use Cases and Efficiency:
|
|
693
|
-
* 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.
|
|
694
|
-
* 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).
|
|
695
|
-
*/
|
|
696
|
-
/**
|
|
697
|
-
* Time Complexity: O(n)
|
|
698
|
-
* Space Complexity: O(log n)
|
|
641
|
+
* Time complexity: O(n)
|
|
642
|
+
* Space complexity: O(n)
|
|
699
643
|
*/
|
|
700
644
|
/**
|
|
701
645
|
* Time Complexity: O(n)
|
|
702
646
|
* Space Complexity: O(log n)
|
|
703
647
|
*
|
|
704
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
705
|
-
*
|
|
706
|
-
*
|
|
648
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
649
|
+
* iterative approach.
|
|
650
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
651
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
652
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
653
|
+
* instance of the AVL tree.
|
|
707
654
|
* @returns a boolean value.
|
|
708
655
|
*/
|
|
709
656
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -753,9 +700,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
753
700
|
return balanced;
|
|
754
701
|
}
|
|
755
702
|
/**
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
|
|
703
|
+
* Time Complexity: O(n)
|
|
704
|
+
* Space Complexity: O(log n)
|
|
705
|
+
*/
|
|
706
|
+
/**
|
|
707
|
+
* The function returns the value of the _comparator property.
|
|
708
|
+
* @returns The `_comparator` property is being returned.
|
|
709
|
+
*/
|
|
710
|
+
get comparator() {
|
|
711
|
+
return this._comparator;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
715
|
+
* root.
|
|
716
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
759
717
|
*/
|
|
760
718
|
_setRoot(v) {
|
|
761
719
|
if (v) {
|