data-structure-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/CHANGELOG.md +1 -1
- package/README.md +100 -106
- package/README_zh-CN.md +93 -100
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +23 -458
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +0 -4
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +36 -34
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/cjs/data-structures/queue/deque.js +1 -172
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +0 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +36 -34
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/mjs/data-structures/queue/deque.js +0 -170
- package/dist/umd/data-structure-typed.js +32 -195
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +43 -33
- package/src/data-structures/queue/deque.ts +1 -191
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +15 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +5 -5
- package/test/unit/data-structures/queue/deque.test.ts +265 -367
- package/test/unit/data-structures/queue/queue.test.ts +158 -158
|
@@ -66,10 +66,6 @@ export class BinaryTreeNode {
|
|
|
66
66
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
67
67
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
68
68
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
69
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
70
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
71
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
72
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
73
69
|
*/
|
|
74
70
|
export class BinaryTree extends IterableEntryBase {
|
|
75
71
|
iterationType = IterationType.ITERATIVE;
|
|
@@ -198,44 +194,50 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
198
194
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
199
195
|
*/
|
|
200
196
|
add(keyOrNodeOrEntry, value) {
|
|
201
|
-
let inserted;
|
|
202
197
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
203
198
|
if (newNode === undefined)
|
|
204
199
|
return;
|
|
205
|
-
//
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
200
|
+
// If the tree is empty, directly set the new node as the root node
|
|
201
|
+
if (!this.root) {
|
|
202
|
+
this._root = newNode;
|
|
203
|
+
this._size = 1;
|
|
204
|
+
return newNode;
|
|
205
|
+
}
|
|
206
|
+
const queue = new Queue([this.root]);
|
|
207
|
+
let potentialParent; // Record the parent node of the potential insertion location
|
|
208
|
+
while (queue.size > 0) {
|
|
209
|
+
const cur = queue.shift();
|
|
210
|
+
if (!cur)
|
|
211
|
+
continue;
|
|
212
|
+
// Check for duplicate keys when newNode is not null
|
|
213
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
214
|
+
this._replaceNode(cur, newNode);
|
|
215
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
216
|
+
}
|
|
217
|
+
// Record the first possible insertion location found
|
|
218
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
219
|
+
potentialParent = cur;
|
|
220
|
+
}
|
|
221
|
+
// Continue traversing the left and right subtrees
|
|
222
|
+
if (cur.left !== null) {
|
|
223
|
+
cur.left && queue.push(cur.left);
|
|
224
|
+
}
|
|
225
|
+
if (cur.right !== null) {
|
|
226
|
+
cur.right && queue.push(cur.right);
|
|
223
227
|
}
|
|
224
|
-
};
|
|
225
|
-
if (this.root) {
|
|
226
|
-
inserted = _bfs(this.root, newNode);
|
|
227
228
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
|
|
229
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
230
|
+
if (potentialParent) {
|
|
231
|
+
if (potentialParent.left === undefined) {
|
|
232
|
+
potentialParent.left = newNode;
|
|
232
233
|
}
|
|
233
|
-
else {
|
|
234
|
-
|
|
234
|
+
else if (potentialParent.right === undefined) {
|
|
235
|
+
potentialParent.right = newNode;
|
|
235
236
|
}
|
|
236
|
-
|
|
237
|
+
this._size++;
|
|
238
|
+
return newNode;
|
|
237
239
|
}
|
|
238
|
-
return
|
|
240
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
239
241
|
}
|
|
240
242
|
/**
|
|
241
243
|
* 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
|
-
}
|
|
@@ -789,173 +789,3 @@ export class Deque extends IterableElementBase {
|
|
|
789
789
|
return { bucketIndex, indexInBucket };
|
|
790
790
|
}
|
|
791
791
|
}
|
|
792
|
-
// O(1) time complexity of obtaining the element
|
|
793
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
794
|
-
// todo tested slowest one
|
|
795
|
-
export class ObjectDeque {
|
|
796
|
-
constructor(capacity) {
|
|
797
|
-
if (capacity !== undefined)
|
|
798
|
-
this._capacity = capacity;
|
|
799
|
-
}
|
|
800
|
-
_nodes = {};
|
|
801
|
-
get nodes() {
|
|
802
|
-
return this._nodes;
|
|
803
|
-
}
|
|
804
|
-
_capacity = Number.MAX_SAFE_INTEGER;
|
|
805
|
-
get capacity() {
|
|
806
|
-
return this._capacity;
|
|
807
|
-
}
|
|
808
|
-
_first = -1;
|
|
809
|
-
get first() {
|
|
810
|
-
return this._first;
|
|
811
|
-
}
|
|
812
|
-
_last = -1;
|
|
813
|
-
get last() {
|
|
814
|
-
return this._last;
|
|
815
|
-
}
|
|
816
|
-
_size = 0;
|
|
817
|
-
get size() {
|
|
818
|
-
return this._size;
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Time Complexity: O(1)
|
|
822
|
-
* Space Complexity: O(1)
|
|
823
|
-
*/
|
|
824
|
-
/**
|
|
825
|
-
* Time Complexity: O(1)
|
|
826
|
-
* Space Complexity: O(1)
|
|
827
|
-
*
|
|
828
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
829
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
830
|
-
* structure.
|
|
831
|
-
*/
|
|
832
|
-
addFirst(element) {
|
|
833
|
-
if (this.size === 0) {
|
|
834
|
-
const mid = Math.floor(this.capacity / 2);
|
|
835
|
-
this._first = mid;
|
|
836
|
-
this._last = mid;
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
this._first--;
|
|
840
|
-
}
|
|
841
|
-
this.nodes[this.first] = element;
|
|
842
|
-
this._size++;
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Time Complexity: O(1)
|
|
846
|
-
* Space Complexity: O(1)
|
|
847
|
-
*/
|
|
848
|
-
/**
|
|
849
|
-
* Time Complexity: O(1)
|
|
850
|
-
* Space Complexity: O(1)
|
|
851
|
-
*
|
|
852
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
853
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
854
|
-
*/
|
|
855
|
-
addLast(element) {
|
|
856
|
-
if (this.size === 0) {
|
|
857
|
-
const mid = Math.floor(this.capacity / 2);
|
|
858
|
-
this._first = mid;
|
|
859
|
-
this._last = mid;
|
|
860
|
-
}
|
|
861
|
-
else {
|
|
862
|
-
this._last++;
|
|
863
|
-
}
|
|
864
|
-
this.nodes[this.last] = element;
|
|
865
|
-
this._size++;
|
|
866
|
-
}
|
|
867
|
-
/**
|
|
868
|
-
* Time Complexity: O(1)
|
|
869
|
-
* Space Complexity: O(1)
|
|
870
|
-
*/
|
|
871
|
-
/**
|
|
872
|
-
* Time Complexity: O(1)
|
|
873
|
-
* Space Complexity: O(1)
|
|
874
|
-
*
|
|
875
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
876
|
-
* @returns The element of the first element in the data structure.
|
|
877
|
-
*/
|
|
878
|
-
pollFirst() {
|
|
879
|
-
if (!this.size)
|
|
880
|
-
return;
|
|
881
|
-
const element = this.getFirst();
|
|
882
|
-
delete this.nodes[this.first];
|
|
883
|
-
this._first++;
|
|
884
|
-
this._size--;
|
|
885
|
-
return element;
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Time Complexity: O(1)
|
|
889
|
-
* Space Complexity: O(1)
|
|
890
|
-
*/
|
|
891
|
-
/**
|
|
892
|
-
* Time Complexity: O(1)
|
|
893
|
-
* Space Complexity: O(1)
|
|
894
|
-
*
|
|
895
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
896
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
897
|
-
*/
|
|
898
|
-
getFirst() {
|
|
899
|
-
if (this.size)
|
|
900
|
-
return this.nodes[this.first];
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Time Complexity: O(1)
|
|
904
|
-
* Space Complexity: O(1)
|
|
905
|
-
*/
|
|
906
|
-
/**
|
|
907
|
-
* Time Complexity: O(1)
|
|
908
|
-
* Space Complexity: O(1)
|
|
909
|
-
*
|
|
910
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
911
|
-
* @returns The element that was removed from the data structure.
|
|
912
|
-
*/
|
|
913
|
-
pollLast() {
|
|
914
|
-
if (!this.size)
|
|
915
|
-
return;
|
|
916
|
-
const element = this.getLast();
|
|
917
|
-
delete this.nodes[this.last];
|
|
918
|
-
this._last--;
|
|
919
|
-
this._size--;
|
|
920
|
-
return element;
|
|
921
|
-
}
|
|
922
|
-
/**
|
|
923
|
-
* Time Complexity: O(1)
|
|
924
|
-
* Space Complexity: O(1)
|
|
925
|
-
*/
|
|
926
|
-
/**
|
|
927
|
-
* Time Complexity: O(1)
|
|
928
|
-
* Space Complexity: O(1)
|
|
929
|
-
*
|
|
930
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
931
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
932
|
-
*/
|
|
933
|
-
getLast() {
|
|
934
|
-
if (this.size)
|
|
935
|
-
return this.nodes[this.last];
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
* Time Complexity: O(1)
|
|
939
|
-
* Space Complexity: O(1)
|
|
940
|
-
*/
|
|
941
|
-
/**
|
|
942
|
-
* Time Complexity: O(1)
|
|
943
|
-
* Space Complexity: O(1)
|
|
944
|
-
*
|
|
945
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
946
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
947
|
-
* retrieve from the array.
|
|
948
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
949
|
-
* index, `undefined` is returned.
|
|
950
|
-
*/
|
|
951
|
-
get(index) {
|
|
952
|
-
return this.nodes[this.first + index] || undefined;
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
956
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
957
|
-
*/
|
|
958
|
-
isEmpty() {
|
|
959
|
-
return this.size <= 0;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
@@ -142,7 +142,6 @@ var dataStructureTyped = (() => {
|
|
|
142
142
|
MinHeap: () => MinHeap,
|
|
143
143
|
MinPriorityQueue: () => MinPriorityQueue,
|
|
144
144
|
Navigator: () => Navigator,
|
|
145
|
-
ObjectDeque: () => ObjectDeque,
|
|
146
145
|
PriorityQueue: () => PriorityQueue,
|
|
147
146
|
Queue: () => Queue,
|
|
148
147
|
RBTNColor: () => RBTNColor,
|
|
@@ -4374,171 +4373,6 @@ var dataStructureTyped = (() => {
|
|
|
4374
4373
|
return { bucketIndex, indexInBucket };
|
|
4375
4374
|
}
|
|
4376
4375
|
};
|
|
4377
|
-
var ObjectDeque = class {
|
|
4378
|
-
constructor(capacity) {
|
|
4379
|
-
__publicField(this, "_nodes", {});
|
|
4380
|
-
__publicField(this, "_capacity", Number.MAX_SAFE_INTEGER);
|
|
4381
|
-
__publicField(this, "_first", -1);
|
|
4382
|
-
__publicField(this, "_last", -1);
|
|
4383
|
-
__publicField(this, "_size", 0);
|
|
4384
|
-
if (capacity !== void 0)
|
|
4385
|
-
this._capacity = capacity;
|
|
4386
|
-
}
|
|
4387
|
-
get nodes() {
|
|
4388
|
-
return this._nodes;
|
|
4389
|
-
}
|
|
4390
|
-
get capacity() {
|
|
4391
|
-
return this._capacity;
|
|
4392
|
-
}
|
|
4393
|
-
get first() {
|
|
4394
|
-
return this._first;
|
|
4395
|
-
}
|
|
4396
|
-
get last() {
|
|
4397
|
-
return this._last;
|
|
4398
|
-
}
|
|
4399
|
-
get size() {
|
|
4400
|
-
return this._size;
|
|
4401
|
-
}
|
|
4402
|
-
/**
|
|
4403
|
-
* Time Complexity: O(1)
|
|
4404
|
-
* Space Complexity: O(1)
|
|
4405
|
-
*/
|
|
4406
|
-
/**
|
|
4407
|
-
* Time Complexity: O(1)
|
|
4408
|
-
* Space Complexity: O(1)
|
|
4409
|
-
*
|
|
4410
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
4411
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
4412
|
-
* structure.
|
|
4413
|
-
*/
|
|
4414
|
-
addFirst(element) {
|
|
4415
|
-
if (this.size === 0) {
|
|
4416
|
-
const mid = Math.floor(this.capacity / 2);
|
|
4417
|
-
this._first = mid;
|
|
4418
|
-
this._last = mid;
|
|
4419
|
-
} else {
|
|
4420
|
-
this._first--;
|
|
4421
|
-
}
|
|
4422
|
-
this.nodes[this.first] = element;
|
|
4423
|
-
this._size++;
|
|
4424
|
-
}
|
|
4425
|
-
/**
|
|
4426
|
-
* Time Complexity: O(1)
|
|
4427
|
-
* Space Complexity: O(1)
|
|
4428
|
-
*/
|
|
4429
|
-
/**
|
|
4430
|
-
* Time Complexity: O(1)
|
|
4431
|
-
* Space Complexity: O(1)
|
|
4432
|
-
*
|
|
4433
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
4434
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
4435
|
-
*/
|
|
4436
|
-
addLast(element) {
|
|
4437
|
-
if (this.size === 0) {
|
|
4438
|
-
const mid = Math.floor(this.capacity / 2);
|
|
4439
|
-
this._first = mid;
|
|
4440
|
-
this._last = mid;
|
|
4441
|
-
} else {
|
|
4442
|
-
this._last++;
|
|
4443
|
-
}
|
|
4444
|
-
this.nodes[this.last] = element;
|
|
4445
|
-
this._size++;
|
|
4446
|
-
}
|
|
4447
|
-
/**
|
|
4448
|
-
* Time Complexity: O(1)
|
|
4449
|
-
* Space Complexity: O(1)
|
|
4450
|
-
*/
|
|
4451
|
-
/**
|
|
4452
|
-
* Time Complexity: O(1)
|
|
4453
|
-
* Space Complexity: O(1)
|
|
4454
|
-
*
|
|
4455
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
4456
|
-
* @returns The element of the first element in the data structure.
|
|
4457
|
-
*/
|
|
4458
|
-
pollFirst() {
|
|
4459
|
-
if (!this.size)
|
|
4460
|
-
return;
|
|
4461
|
-
const element = this.getFirst();
|
|
4462
|
-
delete this.nodes[this.first];
|
|
4463
|
-
this._first++;
|
|
4464
|
-
this._size--;
|
|
4465
|
-
return element;
|
|
4466
|
-
}
|
|
4467
|
-
/**
|
|
4468
|
-
* Time Complexity: O(1)
|
|
4469
|
-
* Space Complexity: O(1)
|
|
4470
|
-
*/
|
|
4471
|
-
/**
|
|
4472
|
-
* Time Complexity: O(1)
|
|
4473
|
-
* Space Complexity: O(1)
|
|
4474
|
-
*
|
|
4475
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
4476
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
4477
|
-
*/
|
|
4478
|
-
getFirst() {
|
|
4479
|
-
if (this.size)
|
|
4480
|
-
return this.nodes[this.first];
|
|
4481
|
-
}
|
|
4482
|
-
/**
|
|
4483
|
-
* Time Complexity: O(1)
|
|
4484
|
-
* Space Complexity: O(1)
|
|
4485
|
-
*/
|
|
4486
|
-
/**
|
|
4487
|
-
* Time Complexity: O(1)
|
|
4488
|
-
* Space Complexity: O(1)
|
|
4489
|
-
*
|
|
4490
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
4491
|
-
* @returns The element that was removed from the data structure.
|
|
4492
|
-
*/
|
|
4493
|
-
pollLast() {
|
|
4494
|
-
if (!this.size)
|
|
4495
|
-
return;
|
|
4496
|
-
const element = this.getLast();
|
|
4497
|
-
delete this.nodes[this.last];
|
|
4498
|
-
this._last--;
|
|
4499
|
-
this._size--;
|
|
4500
|
-
return element;
|
|
4501
|
-
}
|
|
4502
|
-
/**
|
|
4503
|
-
* Time Complexity: O(1)
|
|
4504
|
-
* Space Complexity: O(1)
|
|
4505
|
-
*/
|
|
4506
|
-
/**
|
|
4507
|
-
* Time Complexity: O(1)
|
|
4508
|
-
* Space Complexity: O(1)
|
|
4509
|
-
*
|
|
4510
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
4511
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
4512
|
-
*/
|
|
4513
|
-
getLast() {
|
|
4514
|
-
if (this.size)
|
|
4515
|
-
return this.nodes[this.last];
|
|
4516
|
-
}
|
|
4517
|
-
/**
|
|
4518
|
-
* Time Complexity: O(1)
|
|
4519
|
-
* Space Complexity: O(1)
|
|
4520
|
-
*/
|
|
4521
|
-
/**
|
|
4522
|
-
* Time Complexity: O(1)
|
|
4523
|
-
* Space Complexity: O(1)
|
|
4524
|
-
*
|
|
4525
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
4526
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
4527
|
-
* retrieve from the array.
|
|
4528
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
4529
|
-
* index, `undefined` is returned.
|
|
4530
|
-
*/
|
|
4531
|
-
get(index) {
|
|
4532
|
-
return this.nodes[this.first + index] || void 0;
|
|
4533
|
-
}
|
|
4534
|
-
/**
|
|
4535
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
4536
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
4537
|
-
*/
|
|
4538
|
-
isEmpty() {
|
|
4539
|
-
return this.size <= 0;
|
|
4540
|
-
}
|
|
4541
|
-
};
|
|
4542
4376
|
|
|
4543
4377
|
// src/data-structures/heap/heap.ts
|
|
4544
4378
|
var Heap = class _Heap extends IterableElementBase {
|
|
@@ -7656,41 +7490,44 @@ var dataStructureTyped = (() => {
|
|
|
7656
7490
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
7657
7491
|
*/
|
|
7658
7492
|
add(keyOrNodeOrEntry, value) {
|
|
7659
|
-
let inserted;
|
|
7660
7493
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
7661
7494
|
if (newNode === void 0)
|
|
7662
7495
|
return;
|
|
7663
|
-
if (
|
|
7664
|
-
|
|
7665
|
-
|
|
7666
|
-
|
|
7667
|
-
|
|
7668
|
-
|
|
7669
|
-
|
|
7670
|
-
|
|
7671
|
-
|
|
7672
|
-
|
|
7673
|
-
|
|
7674
|
-
|
|
7675
|
-
|
|
7676
|
-
|
|
7677
|
-
queue.push(cur.left);
|
|
7678
|
-
if (cur.right)
|
|
7679
|
-
queue.push(cur.right);
|
|
7496
|
+
if (!this.root) {
|
|
7497
|
+
this._root = newNode;
|
|
7498
|
+
this._size = 1;
|
|
7499
|
+
return newNode;
|
|
7500
|
+
}
|
|
7501
|
+
const queue = new Queue([this.root]);
|
|
7502
|
+
let potentialParent;
|
|
7503
|
+
while (queue.size > 0) {
|
|
7504
|
+
const cur = queue.shift();
|
|
7505
|
+
if (!cur)
|
|
7506
|
+
continue;
|
|
7507
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
7508
|
+
this._replaceNode(cur, newNode);
|
|
7509
|
+
return newNode;
|
|
7680
7510
|
}
|
|
7681
|
-
|
|
7682
|
-
|
|
7683
|
-
|
|
7684
|
-
|
|
7685
|
-
|
|
7686
|
-
|
|
7687
|
-
|
|
7688
|
-
|
|
7689
|
-
this._size = 0;
|
|
7511
|
+
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
7512
|
+
potentialParent = cur;
|
|
7513
|
+
}
|
|
7514
|
+
if (cur.left !== null) {
|
|
7515
|
+
cur.left && queue.push(cur.left);
|
|
7516
|
+
}
|
|
7517
|
+
if (cur.right !== null) {
|
|
7518
|
+
cur.right && queue.push(cur.right);
|
|
7690
7519
|
}
|
|
7691
|
-
inserted = this.root;
|
|
7692
7520
|
}
|
|
7693
|
-
|
|
7521
|
+
if (potentialParent) {
|
|
7522
|
+
if (potentialParent.left === void 0) {
|
|
7523
|
+
potentialParent.left = newNode;
|
|
7524
|
+
} else if (potentialParent.right === void 0) {
|
|
7525
|
+
potentialParent.right = newNode;
|
|
7526
|
+
}
|
|
7527
|
+
this._size++;
|
|
7528
|
+
return newNode;
|
|
7529
|
+
}
|
|
7530
|
+
return void 0;
|
|
7694
7531
|
}
|
|
7695
7532
|
/**
|
|
7696
7533
|
* Time Complexity: O(k log n) - O(k * n)
|