min-heap-typed 1.48.7 → 1.48.9
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/binary-tree.d.ts +0 -4
- package/dist/data-structures/binary-tree/binary-tree.js +36 -34
- package/dist/data-structures/queue/deque.d.ts +0 -110
- package/dist/data-structures/queue/deque.js +1 -172
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +43 -33
- package/src/data-structures/queue/deque.ts +1 -191
|
@@ -37,10 +37,6 @@ export declare class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K
|
|
|
37
37
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
38
38
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
39
39
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
40
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
41
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
42
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
43
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
44
40
|
*/
|
|
45
41
|
export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, N, TREE> {
|
|
46
42
|
iterationType: IterationType;
|
|
@@ -65,10 +65,6 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
65
65
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
66
66
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
67
67
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
68
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
69
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
70
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
72
68
|
*/
|
|
73
69
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
74
70
|
/**
|
|
@@ -196,44 +192,50 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
196
192
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
197
193
|
*/
|
|
198
194
|
add(keyOrNodeOrEntry, value) {
|
|
199
|
-
let inserted;
|
|
200
195
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
201
196
|
if (newNode === undefined)
|
|
202
197
|
return;
|
|
203
|
-
//
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
198
|
+
// If the tree is empty, directly set the new node as the root node
|
|
199
|
+
if (!this.root) {
|
|
200
|
+
this._root = newNode;
|
|
201
|
+
this._size = 1;
|
|
202
|
+
return newNode;
|
|
203
|
+
}
|
|
204
|
+
const queue = new queue_1.Queue([this.root]);
|
|
205
|
+
let potentialParent; // Record the parent node of the potential insertion location
|
|
206
|
+
while (queue.size > 0) {
|
|
207
|
+
const cur = queue.shift();
|
|
208
|
+
if (!cur)
|
|
209
|
+
continue;
|
|
210
|
+
// Check for duplicate keys when newNode is not null
|
|
211
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
212
|
+
this._replaceNode(cur, newNode);
|
|
213
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
214
|
+
}
|
|
215
|
+
// Record the first possible insertion location found
|
|
216
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
217
|
+
potentialParent = cur;
|
|
218
|
+
}
|
|
219
|
+
// Continue traversing the left and right subtrees
|
|
220
|
+
if (cur.left !== null) {
|
|
221
|
+
cur.left && queue.push(cur.left);
|
|
222
|
+
}
|
|
223
|
+
if (cur.right !== null) {
|
|
224
|
+
cur.right && queue.push(cur.right);
|
|
221
225
|
}
|
|
222
|
-
};
|
|
223
|
-
if (this.root) {
|
|
224
|
-
inserted = _bfs(this.root, newNode);
|
|
225
226
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (
|
|
229
|
-
|
|
227
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
228
|
+
if (potentialParent) {
|
|
229
|
+
if (potentialParent.left === undefined) {
|
|
230
|
+
potentialParent.left = newNode;
|
|
230
231
|
}
|
|
231
|
-
else {
|
|
232
|
-
|
|
232
|
+
else if (potentialParent.right === undefined) {
|
|
233
|
+
potentialParent.right = newNode;
|
|
233
234
|
}
|
|
234
|
-
|
|
235
|
+
this._size++;
|
|
236
|
+
return newNode;
|
|
235
237
|
}
|
|
236
|
-
return
|
|
238
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
237
239
|
}
|
|
238
240
|
/**
|
|
239
241
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -439,113 +439,3 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
439
439
|
indexInBucket: number;
|
|
440
440
|
};
|
|
441
441
|
}
|
|
442
|
-
export declare class ObjectDeque<E = number> {
|
|
443
|
-
constructor(capacity?: number);
|
|
444
|
-
protected _nodes: {
|
|
445
|
-
[key: number]: E;
|
|
446
|
-
};
|
|
447
|
-
get nodes(): {
|
|
448
|
-
[p: number]: E;
|
|
449
|
-
};
|
|
450
|
-
protected _capacity: number;
|
|
451
|
-
get capacity(): number;
|
|
452
|
-
protected _first: number;
|
|
453
|
-
get first(): number;
|
|
454
|
-
protected _last: number;
|
|
455
|
-
get last(): number;
|
|
456
|
-
protected _size: number;
|
|
457
|
-
get size(): number;
|
|
458
|
-
/**
|
|
459
|
-
* Time Complexity: O(1)
|
|
460
|
-
* Space Complexity: O(1)
|
|
461
|
-
*/
|
|
462
|
-
/**
|
|
463
|
-
* Time Complexity: O(1)
|
|
464
|
-
* Space Complexity: O(1)
|
|
465
|
-
*
|
|
466
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
467
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
468
|
-
* structure.
|
|
469
|
-
*/
|
|
470
|
-
addFirst(element: E): void;
|
|
471
|
-
/**
|
|
472
|
-
* Time Complexity: O(1)
|
|
473
|
-
* Space Complexity: O(1)
|
|
474
|
-
*/
|
|
475
|
-
/**
|
|
476
|
-
* Time Complexity: O(1)
|
|
477
|
-
* Space Complexity: O(1)
|
|
478
|
-
*
|
|
479
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
480
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
481
|
-
*/
|
|
482
|
-
addLast(element: E): void;
|
|
483
|
-
/**
|
|
484
|
-
* Time Complexity: O(1)
|
|
485
|
-
* Space Complexity: O(1)
|
|
486
|
-
*/
|
|
487
|
-
/**
|
|
488
|
-
* Time Complexity: O(1)
|
|
489
|
-
* Space Complexity: O(1)
|
|
490
|
-
*
|
|
491
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
492
|
-
* @returns The element of the first element in the data structure.
|
|
493
|
-
*/
|
|
494
|
-
pollFirst(): E | undefined;
|
|
495
|
-
/**
|
|
496
|
-
* Time Complexity: O(1)
|
|
497
|
-
* Space Complexity: O(1)
|
|
498
|
-
*/
|
|
499
|
-
/**
|
|
500
|
-
* Time Complexity: O(1)
|
|
501
|
-
* Space Complexity: O(1)
|
|
502
|
-
*
|
|
503
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
504
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
505
|
-
*/
|
|
506
|
-
getFirst(): E | undefined;
|
|
507
|
-
/**
|
|
508
|
-
* Time Complexity: O(1)
|
|
509
|
-
* Space Complexity: O(1)
|
|
510
|
-
*/
|
|
511
|
-
/**
|
|
512
|
-
* Time Complexity: O(1)
|
|
513
|
-
* Space Complexity: O(1)
|
|
514
|
-
*
|
|
515
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
516
|
-
* @returns The element that was removed from the data structure.
|
|
517
|
-
*/
|
|
518
|
-
pollLast(): E | undefined;
|
|
519
|
-
/**
|
|
520
|
-
* Time Complexity: O(1)
|
|
521
|
-
* Space Complexity: O(1)
|
|
522
|
-
*/
|
|
523
|
-
/**
|
|
524
|
-
* Time Complexity: O(1)
|
|
525
|
-
* Space Complexity: O(1)
|
|
526
|
-
*
|
|
527
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
528
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
529
|
-
*/
|
|
530
|
-
getLast(): E | undefined;
|
|
531
|
-
/**
|
|
532
|
-
* Time Complexity: O(1)
|
|
533
|
-
* Space Complexity: O(1)
|
|
534
|
-
*/
|
|
535
|
-
/**
|
|
536
|
-
* Time Complexity: O(1)
|
|
537
|
-
* Space Complexity: O(1)
|
|
538
|
-
*
|
|
539
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
540
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
541
|
-
* retrieve from the array.
|
|
542
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
543
|
-
* index, `undefined` is returned.
|
|
544
|
-
*/
|
|
545
|
-
get(index: number): NonNullable<E> | undefined;
|
|
546
|
-
/**
|
|
547
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
548
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
549
|
-
*/
|
|
550
|
-
isEmpty(): boolean;
|
|
551
|
-
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.
|
|
10
|
+
exports.Deque = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
const base_1 = require("../base");
|
|
13
13
|
/**
|
|
@@ -792,174 +792,3 @@ class Deque extends base_1.IterableElementBase {
|
|
|
792
792
|
}
|
|
793
793
|
}
|
|
794
794
|
exports.Deque = Deque;
|
|
795
|
-
// O(1) time complexity of obtaining the element
|
|
796
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
797
|
-
// todo tested slowest one
|
|
798
|
-
class ObjectDeque {
|
|
799
|
-
constructor(capacity) {
|
|
800
|
-
this._nodes = {};
|
|
801
|
-
this._capacity = Number.MAX_SAFE_INTEGER;
|
|
802
|
-
this._first = -1;
|
|
803
|
-
this._last = -1;
|
|
804
|
-
this._size = 0;
|
|
805
|
-
if (capacity !== undefined)
|
|
806
|
-
this._capacity = capacity;
|
|
807
|
-
}
|
|
808
|
-
get nodes() {
|
|
809
|
-
return this._nodes;
|
|
810
|
-
}
|
|
811
|
-
get capacity() {
|
|
812
|
-
return this._capacity;
|
|
813
|
-
}
|
|
814
|
-
get first() {
|
|
815
|
-
return this._first;
|
|
816
|
-
}
|
|
817
|
-
get last() {
|
|
818
|
-
return this._last;
|
|
819
|
-
}
|
|
820
|
-
get size() {
|
|
821
|
-
return this._size;
|
|
822
|
-
}
|
|
823
|
-
/**
|
|
824
|
-
* Time Complexity: O(1)
|
|
825
|
-
* Space Complexity: O(1)
|
|
826
|
-
*/
|
|
827
|
-
/**
|
|
828
|
-
* Time Complexity: O(1)
|
|
829
|
-
* Space Complexity: O(1)
|
|
830
|
-
*
|
|
831
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
832
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
833
|
-
* structure.
|
|
834
|
-
*/
|
|
835
|
-
addFirst(element) {
|
|
836
|
-
if (this.size === 0) {
|
|
837
|
-
const mid = Math.floor(this.capacity / 2);
|
|
838
|
-
this._first = mid;
|
|
839
|
-
this._last = mid;
|
|
840
|
-
}
|
|
841
|
-
else {
|
|
842
|
-
this._first--;
|
|
843
|
-
}
|
|
844
|
-
this.nodes[this.first] = element;
|
|
845
|
-
this._size++;
|
|
846
|
-
}
|
|
847
|
-
/**
|
|
848
|
-
* Time Complexity: O(1)
|
|
849
|
-
* Space Complexity: O(1)
|
|
850
|
-
*/
|
|
851
|
-
/**
|
|
852
|
-
* Time Complexity: O(1)
|
|
853
|
-
* Space Complexity: O(1)
|
|
854
|
-
*
|
|
855
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
856
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
857
|
-
*/
|
|
858
|
-
addLast(element) {
|
|
859
|
-
if (this.size === 0) {
|
|
860
|
-
const mid = Math.floor(this.capacity / 2);
|
|
861
|
-
this._first = mid;
|
|
862
|
-
this._last = mid;
|
|
863
|
-
}
|
|
864
|
-
else {
|
|
865
|
-
this._last++;
|
|
866
|
-
}
|
|
867
|
-
this.nodes[this.last] = element;
|
|
868
|
-
this._size++;
|
|
869
|
-
}
|
|
870
|
-
/**
|
|
871
|
-
* Time Complexity: O(1)
|
|
872
|
-
* Space Complexity: O(1)
|
|
873
|
-
*/
|
|
874
|
-
/**
|
|
875
|
-
* Time Complexity: O(1)
|
|
876
|
-
* Space Complexity: O(1)
|
|
877
|
-
*
|
|
878
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
879
|
-
* @returns The element of the first element in the data structure.
|
|
880
|
-
*/
|
|
881
|
-
pollFirst() {
|
|
882
|
-
if (!this.size)
|
|
883
|
-
return;
|
|
884
|
-
const element = this.getFirst();
|
|
885
|
-
delete this.nodes[this.first];
|
|
886
|
-
this._first++;
|
|
887
|
-
this._size--;
|
|
888
|
-
return element;
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* Time Complexity: O(1)
|
|
892
|
-
* Space Complexity: O(1)
|
|
893
|
-
*/
|
|
894
|
-
/**
|
|
895
|
-
* Time Complexity: O(1)
|
|
896
|
-
* Space Complexity: O(1)
|
|
897
|
-
*
|
|
898
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
899
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
900
|
-
*/
|
|
901
|
-
getFirst() {
|
|
902
|
-
if (this.size)
|
|
903
|
-
return this.nodes[this.first];
|
|
904
|
-
}
|
|
905
|
-
/**
|
|
906
|
-
* Time Complexity: O(1)
|
|
907
|
-
* Space Complexity: O(1)
|
|
908
|
-
*/
|
|
909
|
-
/**
|
|
910
|
-
* Time Complexity: O(1)
|
|
911
|
-
* Space Complexity: O(1)
|
|
912
|
-
*
|
|
913
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
914
|
-
* @returns The element that was removed from the data structure.
|
|
915
|
-
*/
|
|
916
|
-
pollLast() {
|
|
917
|
-
if (!this.size)
|
|
918
|
-
return;
|
|
919
|
-
const element = this.getLast();
|
|
920
|
-
delete this.nodes[this.last];
|
|
921
|
-
this._last--;
|
|
922
|
-
this._size--;
|
|
923
|
-
return element;
|
|
924
|
-
}
|
|
925
|
-
/**
|
|
926
|
-
* Time Complexity: O(1)
|
|
927
|
-
* Space Complexity: O(1)
|
|
928
|
-
*/
|
|
929
|
-
/**
|
|
930
|
-
* Time Complexity: O(1)
|
|
931
|
-
* Space Complexity: O(1)
|
|
932
|
-
*
|
|
933
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
934
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
935
|
-
*/
|
|
936
|
-
getLast() {
|
|
937
|
-
if (this.size)
|
|
938
|
-
return this.nodes[this.last];
|
|
939
|
-
}
|
|
940
|
-
/**
|
|
941
|
-
* Time Complexity: O(1)
|
|
942
|
-
* Space Complexity: O(1)
|
|
943
|
-
*/
|
|
944
|
-
/**
|
|
945
|
-
* Time Complexity: O(1)
|
|
946
|
-
* Space Complexity: O(1)
|
|
947
|
-
*
|
|
948
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
949
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
950
|
-
* retrieve from the array.
|
|
951
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
952
|
-
* index, `undefined` is returned.
|
|
953
|
-
*/
|
|
954
|
-
get(index) {
|
|
955
|
-
return this.nodes[this.first + index] || undefined;
|
|
956
|
-
}
|
|
957
|
-
/**
|
|
958
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
959
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
960
|
-
*/
|
|
961
|
-
isEmpty() {
|
|
962
|
-
return this.size <= 0;
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
exports.ObjectDeque = ObjectDeque;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.9",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.48.
|
|
135
|
+
"data-structure-typed": "^1.48.9"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -98,10 +98,6 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
|
|
|
98
98
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
99
99
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
100
100
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
101
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
102
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
103
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
104
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
105
101
|
*/
|
|
106
102
|
|
|
107
103
|
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
|
|
@@ -231,7 +227,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
231
227
|
* Space Complexity O(1)
|
|
232
228
|
*/
|
|
233
229
|
|
|
234
|
-
|
|
235
230
|
/**
|
|
236
231
|
* Time Complexity O(log n) - O(n)
|
|
237
232
|
* Space Complexity O(1)
|
|
@@ -243,50 +238,65 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
243
238
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
244
239
|
*/
|
|
245
240
|
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
246
|
-
|
|
247
|
-
let inserted: N | null | undefined;
|
|
248
241
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
249
242
|
if (newNode === undefined) return;
|
|
250
243
|
|
|
251
|
-
//
|
|
252
|
-
if (
|
|
244
|
+
// If the tree is empty, directly set the new node as the root node
|
|
245
|
+
if (!this.root) {
|
|
246
|
+
this._root = newNode;
|
|
247
|
+
this._size = 1;
|
|
248
|
+
return newNode;
|
|
249
|
+
}
|
|
253
250
|
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
251
|
+
const queue = new Queue<N>([this.root]);
|
|
252
|
+
let potentialParent: N | undefined; // Record the parent node of the potential insertion location
|
|
253
|
+
|
|
254
|
+
while (queue.size > 0) {
|
|
255
|
+
const cur = queue.shift();
|
|
256
|
+
|
|
257
|
+
if (!cur) continue;
|
|
258
|
+
|
|
259
|
+
// Check for duplicate keys when newNode is not null
|
|
260
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
261
|
+
this._replaceNode(cur, newNode);
|
|
262
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
266
263
|
}
|
|
267
|
-
};
|
|
268
264
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
265
|
+
// Record the first possible insertion location found
|
|
266
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
267
|
+
potentialParent = cur;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Continue traversing the left and right subtrees
|
|
271
|
+
if (cur.left !== null) {
|
|
272
|
+
cur.left && queue.push(cur.left);
|
|
273
|
+
}
|
|
274
|
+
if (cur.right !== null) {
|
|
275
|
+
cur.right && queue.push(cur.right);
|
|
277
276
|
}
|
|
278
|
-
inserted = this.root;
|
|
279
277
|
}
|
|
280
|
-
|
|
278
|
+
|
|
279
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
280
|
+
if (potentialParent) {
|
|
281
|
+
if (potentialParent.left === undefined) {
|
|
282
|
+
potentialParent.left = newNode;
|
|
283
|
+
} else if (potentialParent.right === undefined) {
|
|
284
|
+
potentialParent.right = newNode;
|
|
285
|
+
}
|
|
286
|
+
this._size++;
|
|
287
|
+
return newNode;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
281
291
|
}
|
|
282
292
|
|
|
293
|
+
|
|
283
294
|
/**
|
|
284
295
|
* Time Complexity: O(k log n) - O(k * n)
|
|
285
296
|
* Space Complexity: O(1)
|
|
286
297
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
287
298
|
*/
|
|
288
299
|
|
|
289
|
-
|
|
290
300
|
/**
|
|
291
301
|
* Time Complexity: O(k log n) - O(k * n)
|
|
292
302
|
* Space Complexity: O(1)
|
|
@@ -849,194 +849,4 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
849
849
|
|
|
850
850
|
return { bucketIndex, indexInBucket };
|
|
851
851
|
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// O(1) time complexity of obtaining the element
|
|
855
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
856
|
-
// todo tested slowest one
|
|
857
|
-
export class ObjectDeque<E = number> {
|
|
858
|
-
constructor(capacity?: number) {
|
|
859
|
-
if (capacity !== undefined) this._capacity = capacity;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
protected _nodes: { [key: number]: E } = {};
|
|
863
|
-
|
|
864
|
-
get nodes(): { [p: number]: E } {
|
|
865
|
-
return this._nodes;
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
869
|
-
|
|
870
|
-
get capacity(): number {
|
|
871
|
-
return this._capacity;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
protected _first = -1;
|
|
875
|
-
|
|
876
|
-
get first(): number {
|
|
877
|
-
return this._first;
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
protected _last = -1;
|
|
881
|
-
|
|
882
|
-
get last(): number {
|
|
883
|
-
return this._last;
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
protected _size = 0;
|
|
887
|
-
|
|
888
|
-
get size(): number {
|
|
889
|
-
return this._size;
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
/**
|
|
893
|
-
* Time Complexity: O(1)
|
|
894
|
-
* Space Complexity: O(1)
|
|
895
|
-
*/
|
|
896
|
-
|
|
897
|
-
/**
|
|
898
|
-
* Time Complexity: O(1)
|
|
899
|
-
* Space Complexity: O(1)
|
|
900
|
-
*
|
|
901
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
902
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
903
|
-
* structure.
|
|
904
|
-
*/
|
|
905
|
-
addFirst(element: E) {
|
|
906
|
-
if (this.size === 0) {
|
|
907
|
-
const mid = Math.floor(this.capacity / 2);
|
|
908
|
-
this._first = mid;
|
|
909
|
-
this._last = mid;
|
|
910
|
-
} else {
|
|
911
|
-
this._first--;
|
|
912
|
-
}
|
|
913
|
-
this.nodes[this.first] = element;
|
|
914
|
-
this._size++;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
/**
|
|
918
|
-
* Time Complexity: O(1)
|
|
919
|
-
* Space Complexity: O(1)
|
|
920
|
-
*/
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
* Time Complexity: O(1)
|
|
924
|
-
* Space Complexity: O(1)
|
|
925
|
-
*
|
|
926
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
927
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
928
|
-
*/
|
|
929
|
-
addLast(element: E) {
|
|
930
|
-
if (this.size === 0) {
|
|
931
|
-
const mid = Math.floor(this.capacity / 2);
|
|
932
|
-
this._first = mid;
|
|
933
|
-
this._last = mid;
|
|
934
|
-
} else {
|
|
935
|
-
this._last++;
|
|
936
|
-
}
|
|
937
|
-
this.nodes[this.last] = element;
|
|
938
|
-
this._size++;
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
/**
|
|
942
|
-
* Time Complexity: O(1)
|
|
943
|
-
* Space Complexity: O(1)
|
|
944
|
-
*/
|
|
945
|
-
|
|
946
|
-
/**
|
|
947
|
-
* Time Complexity: O(1)
|
|
948
|
-
* Space Complexity: O(1)
|
|
949
|
-
*
|
|
950
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
951
|
-
* @returns The element of the first element in the data structure.
|
|
952
|
-
*/
|
|
953
|
-
pollFirst() {
|
|
954
|
-
if (!this.size) return;
|
|
955
|
-
const element = this.getFirst();
|
|
956
|
-
delete this.nodes[this.first];
|
|
957
|
-
this._first++;
|
|
958
|
-
this._size--;
|
|
959
|
-
return element;
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
/**
|
|
963
|
-
* Time Complexity: O(1)
|
|
964
|
-
* Space Complexity: O(1)
|
|
965
|
-
*/
|
|
966
|
-
|
|
967
|
-
/**
|
|
968
|
-
* Time Complexity: O(1)
|
|
969
|
-
* Space Complexity: O(1)
|
|
970
|
-
*
|
|
971
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
972
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
973
|
-
*/
|
|
974
|
-
getFirst() {
|
|
975
|
-
if (this.size) return this.nodes[this.first];
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
/**
|
|
979
|
-
* Time Complexity: O(1)
|
|
980
|
-
* Space Complexity: O(1)
|
|
981
|
-
*/
|
|
982
|
-
|
|
983
|
-
/**
|
|
984
|
-
* Time Complexity: O(1)
|
|
985
|
-
* Space Complexity: O(1)
|
|
986
|
-
*
|
|
987
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
988
|
-
* @returns The element that was removed from the data structure.
|
|
989
|
-
*/
|
|
990
|
-
pollLast() {
|
|
991
|
-
if (!this.size) return;
|
|
992
|
-
const element = this.getLast();
|
|
993
|
-
delete this.nodes[this.last];
|
|
994
|
-
this._last--;
|
|
995
|
-
this._size--;
|
|
996
|
-
|
|
997
|
-
return element;
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
/**
|
|
1001
|
-
* Time Complexity: O(1)
|
|
1002
|
-
* Space Complexity: O(1)
|
|
1003
|
-
*/
|
|
1004
|
-
|
|
1005
|
-
/**
|
|
1006
|
-
* Time Complexity: O(1)
|
|
1007
|
-
* Space Complexity: O(1)
|
|
1008
|
-
*
|
|
1009
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
1010
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
1011
|
-
*/
|
|
1012
|
-
getLast() {
|
|
1013
|
-
if (this.size) return this.nodes[this.last];
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
/**
|
|
1017
|
-
* Time Complexity: O(1)
|
|
1018
|
-
* Space Complexity: O(1)
|
|
1019
|
-
*/
|
|
1020
|
-
|
|
1021
|
-
/**
|
|
1022
|
-
* Time Complexity: O(1)
|
|
1023
|
-
* Space Complexity: O(1)
|
|
1024
|
-
*
|
|
1025
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
1026
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
1027
|
-
* retrieve from the array.
|
|
1028
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
1029
|
-
* index, `undefined` is returned.
|
|
1030
|
-
*/
|
|
1031
|
-
get(index: number) {
|
|
1032
|
-
return this.nodes[this.first + index] || undefined;
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
/**
|
|
1036
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
1037
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
1038
|
-
*/
|
|
1039
|
-
isEmpty() {
|
|
1040
|
-
return this.size <= 0;
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
852
|
+
}
|