data-structure-typed 1.39.1 → 1.39.2
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/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +11 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +38 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +11 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +38 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +47 -5
- package/src/data-structures/binary-tree/bst.ts +1 -2
- package/src/data-structures/binary-tree/rb-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -2
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +119 -14
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +27 -26
- package/src/data-structures/queue/queue.ts +4 -4
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +23 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +70 -7
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +60 -13
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +43 -43
- package/test/unit/data-structures/queue/queue.test.ts +7 -7
- package/test/utils/big-o.ts +1 -1
|
@@ -87,17 +87,13 @@ export class SinglyLinkedList<E = any> {
|
|
|
87
87
|
return singlyLinkedList;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
getLength(): number {
|
|
91
|
-
return this._length;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
90
|
/**
|
|
95
|
-
* The `push` function adds a new node with the given
|
|
96
|
-
* @param {E}
|
|
91
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
92
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
97
93
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
98
94
|
*/
|
|
99
|
-
push(
|
|
100
|
-
const newNode = new SinglyLinkedListNode(
|
|
95
|
+
push(val: E): void {
|
|
96
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
101
97
|
if (!this.head) {
|
|
102
98
|
this.head = newNode;
|
|
103
99
|
this.tail = newNode;
|
|
@@ -108,6 +104,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
108
104
|
this._length++;
|
|
109
105
|
}
|
|
110
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
109
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
110
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
111
|
+
*/
|
|
112
|
+
addLast(val: E): void {
|
|
113
|
+
this.push(val);
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
/**
|
|
112
117
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
113
118
|
* pointers accordingly.
|
|
@@ -135,6 +140,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
135
140
|
return val;
|
|
136
141
|
}
|
|
137
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
145
|
+
* pointers accordingly.
|
|
146
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
147
|
+
* the linked list is empty, it returns `null`.
|
|
148
|
+
*/
|
|
149
|
+
popLast(): E | undefined {
|
|
150
|
+
return this.pop();
|
|
151
|
+
}
|
|
152
|
+
|
|
138
153
|
/**
|
|
139
154
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
140
155
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -147,6 +162,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
147
162
|
return removedNode.val;
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
/**
|
|
166
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
167
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
168
|
+
*/
|
|
169
|
+
popFirst(): E | undefined {
|
|
170
|
+
return this.shift();
|
|
171
|
+
}
|
|
172
|
+
|
|
150
173
|
/**
|
|
151
174
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
152
175
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -164,6 +187,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
164
187
|
this._length++;
|
|
165
188
|
}
|
|
166
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
192
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
193
|
+
* linked list.
|
|
194
|
+
*/
|
|
195
|
+
addFirst(val: E): void {
|
|
196
|
+
this.unshift(val);
|
|
197
|
+
}
|
|
198
|
+
|
|
167
199
|
/**
|
|
168
200
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
169
201
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -382,7 +414,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
382
414
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
383
415
|
* the specified value is found, the function returns `null`.
|
|
384
416
|
*/
|
|
385
|
-
|
|
417
|
+
getNode(value: E): SinglyLinkedListNode<E> | null {
|
|
386
418
|
let current = this.head;
|
|
387
419
|
|
|
388
420
|
while (current) {
|
|
@@ -432,9 +464,6 @@ export class SinglyLinkedList<E = any> {
|
|
|
432
464
|
return false;
|
|
433
465
|
}
|
|
434
466
|
|
|
435
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
436
|
-
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
437
|
-
|
|
438
467
|
/**
|
|
439
468
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
440
469
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -449,7 +478,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
449
478
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
450
479
|
existingNode = existingValueOrNode;
|
|
451
480
|
} else {
|
|
452
|
-
existingNode = this.
|
|
481
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
453
482
|
}
|
|
454
483
|
|
|
455
484
|
if (existingNode) {
|
|
@@ -485,7 +514,83 @@ export class SinglyLinkedList<E = any> {
|
|
|
485
514
|
return count;
|
|
486
515
|
}
|
|
487
516
|
|
|
488
|
-
|
|
517
|
+
/**
|
|
518
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
519
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
520
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
521
|
+
* current node in the linked list.
|
|
522
|
+
*/
|
|
523
|
+
forEach(callback: (val: E, index: number) => void): void {
|
|
524
|
+
let current = this.head;
|
|
525
|
+
let index = 0;
|
|
526
|
+
while (current) {
|
|
527
|
+
callback(current.val, index);
|
|
528
|
+
current = current.next;
|
|
529
|
+
index++;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
535
|
+
* SinglyLinkedList with the transformed values.
|
|
536
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
537
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
538
|
+
* SinglyLinkedList).
|
|
539
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
540
|
+
*/
|
|
541
|
+
map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
|
|
542
|
+
const mappedList = new SinglyLinkedList<U>();
|
|
543
|
+
let current = this.head;
|
|
544
|
+
while (current) {
|
|
545
|
+
mappedList.push(callback(current.val));
|
|
546
|
+
current = current.next;
|
|
547
|
+
}
|
|
548
|
+
return mappedList;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
553
|
+
* elements that satisfy the given callback function.
|
|
554
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
555
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
556
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
557
|
+
*/
|
|
558
|
+
filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
|
|
559
|
+
const filteredList = new SinglyLinkedList<E>();
|
|
560
|
+
let current = this.head;
|
|
561
|
+
while (current) {
|
|
562
|
+
if (callback(current.val)) {
|
|
563
|
+
filteredList.push(current.val);
|
|
564
|
+
}
|
|
565
|
+
current = current.next;
|
|
566
|
+
}
|
|
567
|
+
return filteredList;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
572
|
+
* single value.
|
|
573
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
574
|
+
* used to perform a specific operation on each element of the linked list.
|
|
575
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
576
|
+
* point for the reduction operation.
|
|
577
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
578
|
+
* elements in the linked list.
|
|
579
|
+
*/
|
|
580
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
|
|
581
|
+
let accumulator = initialValue;
|
|
582
|
+
let current = this.head;
|
|
583
|
+
while (current) {
|
|
584
|
+
accumulator = callback(accumulator, current.val);
|
|
585
|
+
current = current.next;
|
|
586
|
+
}
|
|
587
|
+
return accumulator;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
|
+
*/
|
|
593
|
+
* [Symbol.iterator]() {
|
|
489
594
|
let current = this.head;
|
|
490
595
|
|
|
491
596
|
while (current) {
|
|
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
private _nodes: {[key: number]: E} = {};
|
|
23
|
+
private _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -95,12 +96,12 @@ export class ObjectDeque<E = number> {
|
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
/**
|
|
98
|
-
* The function `
|
|
99
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
99
100
|
* @returns The value of the first element in the data structure.
|
|
100
101
|
*/
|
|
101
|
-
|
|
102
|
+
popFirst() {
|
|
102
103
|
if (!this._size) return;
|
|
103
|
-
const value = this.
|
|
104
|
+
const value = this.getFirst();
|
|
104
105
|
delete this._nodes[this._first];
|
|
105
106
|
this._first++;
|
|
106
107
|
this._size--;
|
|
@@ -108,20 +109,20 @@ export class ObjectDeque<E = number> {
|
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
/**
|
|
111
|
-
* The `
|
|
112
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
112
113
|
* @returns The element at the first position of the `_nodes` array.
|
|
113
114
|
*/
|
|
114
|
-
|
|
115
|
+
getFirst() {
|
|
115
116
|
if (this._size) return this._nodes[this._first];
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
/**
|
|
119
|
-
* The `
|
|
120
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
120
121
|
* @returns The value that was removed from the data structure.
|
|
121
122
|
*/
|
|
122
|
-
|
|
123
|
+
popLast() {
|
|
123
124
|
if (!this._size) return;
|
|
124
|
-
const value = this.
|
|
125
|
+
const value = this.getLast();
|
|
125
126
|
delete this._nodes[this._last];
|
|
126
127
|
this._last--;
|
|
127
128
|
this._size--;
|
|
@@ -130,10 +131,10 @@ export class ObjectDeque<E = number> {
|
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
/**
|
|
133
|
-
* The `
|
|
134
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
134
135
|
* @returns The last element in the array "_nodes" is being returned.
|
|
135
136
|
*/
|
|
136
|
-
|
|
137
|
+
getLast() {
|
|
137
138
|
if (this._size) return this._nodes[this._last];
|
|
138
139
|
}
|
|
139
140
|
|
|
@@ -156,7 +157,7 @@ export class ObjectDeque<E = number> {
|
|
|
156
157
|
return this._size <= 0;
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
+
protected _seNodes(value: { [p: number]: E }) {
|
|
160
161
|
this._nodes = value;
|
|
161
162
|
}
|
|
162
163
|
|
|
@@ -188,19 +189,19 @@ export class ArrayDeque<E> {
|
|
|
188
189
|
}
|
|
189
190
|
|
|
190
191
|
/**
|
|
191
|
-
* The function "
|
|
192
|
-
* @returns The method `
|
|
192
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
193
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
193
194
|
*/
|
|
194
|
-
|
|
195
|
+
popLast(): E | null {
|
|
195
196
|
return this._nodes.pop() ?? null;
|
|
196
197
|
}
|
|
197
198
|
|
|
198
199
|
/**
|
|
199
|
-
* The `
|
|
200
|
-
* @returns The `
|
|
200
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
201
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
201
202
|
* empty.
|
|
202
203
|
*/
|
|
203
|
-
|
|
204
|
+
popFirst(): E | null {
|
|
204
205
|
return this._nodes.shift() ?? null;
|
|
205
206
|
}
|
|
206
207
|
|
|
@@ -219,19 +220,19 @@ export class ArrayDeque<E> {
|
|
|
219
220
|
}
|
|
220
221
|
|
|
221
222
|
/**
|
|
222
|
-
* The `
|
|
223
|
-
* @returns The function `
|
|
223
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
224
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
224
225
|
* empty, it will return `null`.
|
|
225
226
|
*/
|
|
226
|
-
|
|
227
|
+
getFirst(): E | null {
|
|
227
228
|
return this._nodes[0] ?? null;
|
|
228
229
|
}
|
|
229
230
|
|
|
230
231
|
/**
|
|
231
|
-
* The `
|
|
232
|
-
* @returns The method `
|
|
232
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
233
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
233
234
|
*/
|
|
234
|
-
|
|
235
|
+
getLast(): E | null {
|
|
235
236
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
236
237
|
}
|
|
237
238
|
|
|
@@ -123,11 +123,11 @@ export class Queue<E = any> {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
|
-
* The `
|
|
127
|
-
* @returns The method `
|
|
126
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
127
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
128
128
|
* array is empty, it returns `null`.
|
|
129
129
|
*/
|
|
130
|
-
|
|
130
|
+
getLast(): E | undefined {
|
|
131
131
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -183,7 +183,7 @@ export class Queue<E = any> {
|
|
|
183
183
|
return new Queue(this.nodes.slice(this.offset));
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
*[Symbol.iterator]() {
|
|
186
|
+
* [Symbol.iterator]() {
|
|
187
187
|
for (const item of this.nodes) {
|
|
188
188
|
yield item;
|
|
189
189
|
}
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
|
|
3
3
|
export type TrlFn = (...args: any[]) => any;
|
|
4
4
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type KeyValueObject = {[key: string]: any};
|
|
1
|
+
export type KeyValueObject = { [key: string]: any };
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
|
|
3
|
+
export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
|
|
4
4
|
|
|
5
5
|
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
|
6
6
|
|
|
@@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => {
|
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
186
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
186
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
187
187
|
expect(objBST).toBeInstanceOf(BST);
|
|
188
188
|
objBST.add(11, {key: 11, keyA: 11});
|
|
189
189
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -219,7 +219,7 @@ describe('AVL Tree Test recursively', () => {
|
|
|
219
219
|
});
|
|
220
220
|
|
|
221
221
|
describe('AVLTree APIs test', () => {
|
|
222
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
222
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
223
223
|
beforeEach(() => {
|
|
224
224
|
avl.clear();
|
|
225
225
|
});
|
|
@@ -125,11 +125,19 @@ describe('BinaryTree', () => {
|
|
|
125
125
|
});
|
|
126
126
|
|
|
127
127
|
it('should traverse in-order', () => {
|
|
128
|
+
tree.add(null);
|
|
129
|
+
tree.delete(1);
|
|
130
|
+
expect(tree.getHeight()).toBe(-1);
|
|
128
131
|
tree.add(4);
|
|
129
132
|
tree.add(2);
|
|
133
|
+
expect(tree.getHeight()).toBe(1);
|
|
134
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
135
|
+
expect(tree.getHeight()).toBe(1);
|
|
136
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
137
|
+
|
|
130
138
|
tree.add(6);
|
|
131
139
|
tree.add(1);
|
|
132
|
-
tree.add(3);
|
|
140
|
+
tree.add(new BinaryTreeNode(3));
|
|
133
141
|
tree.add(5);
|
|
134
142
|
tree.add(7);
|
|
135
143
|
|
|
@@ -160,6 +168,7 @@ describe('BinaryTree', () => {
|
|
|
160
168
|
]);
|
|
161
169
|
|
|
162
170
|
expect(tree.isSubtreeBST(tree.get(4), IterationType.RECURSIVE)).toBe(true);
|
|
171
|
+
expect(tree.isSubtreeBST(tree.get(4), IterationType.ITERATIVE)).toBe(true);
|
|
163
172
|
});
|
|
164
173
|
|
|
165
174
|
it('should subTreeTraverse', () => {
|
|
@@ -197,6 +206,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
197
206
|
|
|
198
207
|
expect(result).toEqual(expected);
|
|
199
208
|
expect(tree.dfs(node => node.key, 'in')).toEqual(expected);
|
|
209
|
+
expect(tree.dfs(node => node.key, 'in', tree.root, IterationType.RECURSIVE)).toEqual(expected);
|
|
200
210
|
});
|
|
201
211
|
|
|
202
212
|
it('should perform pre-order Morris traversal correctly as dfs traversal', () => {
|
|
@@ -231,7 +241,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
231
241
|
});
|
|
232
242
|
|
|
233
243
|
describe('BinaryTree APIs test', () => {
|
|
234
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
244
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
235
245
|
beforeEach(() => {
|
|
236
246
|
avl.clear();
|
|
237
247
|
});
|
|
@@ -255,8 +265,10 @@ describe('BinaryTree traversals', () => {
|
|
|
255
265
|
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
|
|
256
266
|
tree.refill(arr);
|
|
257
267
|
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
|
268
|
+
expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
|
258
269
|
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
|
259
270
|
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
|
|
271
|
+
expect(tree.dfs(node => node.key, 'post', tree.root, IterationType.RECURSIVE)).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
|
|
260
272
|
expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
|
|
261
273
|
35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
|
|
262
274
|
]);
|
|
@@ -281,7 +293,7 @@ describe('BinaryTree', () => {
|
|
|
281
293
|
let tree: BinaryTree<string>;
|
|
282
294
|
|
|
283
295
|
beforeEach(() => {
|
|
284
|
-
tree = new BinaryTree<string>();
|
|
296
|
+
tree = new BinaryTree<string>({iterationType: IterationType.RECURSIVE});
|
|
285
297
|
});
|
|
286
298
|
|
|
287
299
|
afterEach(() => {
|
|
@@ -441,6 +453,14 @@ describe('BinaryTree', () => {
|
|
|
441
453
|
tree.add(3, 'B');
|
|
442
454
|
tree.add(7, 'C');
|
|
443
455
|
|
|
456
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
457
|
+
// @ts-ignore
|
|
458
|
+
expect([...tree]).toEqual([3, 5, 7]);
|
|
459
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
460
|
+
// @ts-ignore
|
|
461
|
+
expect([...tree]).toEqual([3, 5, 7]);
|
|
462
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
463
|
+
|
|
444
464
|
const result = tree.morris();
|
|
445
465
|
expect(result).toEqual([3, 5, 7]);
|
|
446
466
|
// Add assertions for the result of Morris traversal
|
|
@@ -189,7 +189,7 @@ describe('BST operations test', () => {
|
|
|
189
189
|
});
|
|
190
190
|
|
|
191
191
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
192
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
192
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
193
193
|
expect(objBST).toBeInstanceOf(BST);
|
|
194
194
|
objBST.add(11, {key: 11, keyA: 11});
|
|
195
195
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -260,7 +260,7 @@ describe('BST operations test', () => {
|
|
|
260
260
|
objBST.perfectlyBalance();
|
|
261
261
|
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
262
262
|
|
|
263
|
-
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
|
263
|
+
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
264
264
|
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
265
265
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
266
266
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
@@ -385,7 +385,7 @@ describe('BST operations test', () => {
|
|
|
385
385
|
expect(bfsIDs[1]).toBe(12);
|
|
386
386
|
expect(bfsIDs[2]).toBe(16);
|
|
387
387
|
|
|
388
|
-
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
|
388
|
+
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
389
389
|
objBST.bfs(node => bfsNodes.push(node));
|
|
390
390
|
expect(bfsNodes[0].key).toBe(2);
|
|
391
391
|
expect(bfsNodes[1].key).toBe(12);
|
|
@@ -579,7 +579,7 @@ describe('BST operations test recursively', () => {
|
|
|
579
579
|
});
|
|
580
580
|
|
|
581
581
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
582
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
582
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
583
583
|
expect(objBST).toBeInstanceOf(BST);
|
|
584
584
|
objBST.add(11, {key: 11, keyA: 11});
|
|
585
585
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -650,7 +650,7 @@ describe('BST operations test recursively', () => {
|
|
|
650
650
|
objBST.perfectlyBalance();
|
|
651
651
|
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
652
652
|
|
|
653
|
-
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
|
653
|
+
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
654
654
|
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
655
655
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
656
656
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
@@ -775,7 +775,7 @@ describe('BST operations test recursively', () => {
|
|
|
775
775
|
expect(bfsIDs[1]).toBe(12);
|
|
776
776
|
expect(bfsIDs[2]).toBe(16);
|
|
777
777
|
|
|
778
|
-
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
|
778
|
+
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
779
779
|
objBST.bfs(node => bfsNodes.push(node));
|
|
780
780
|
expect(bfsNodes[0].key).toBe(2);
|
|
781
781
|
expect(bfsNodes[1].key).toBe(12);
|
|
@@ -29,7 +29,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
29
29
|
bfsIDs[0] === 11; // true
|
|
30
30
|
expect(bfsIDs[0]).toBe(11);
|
|
31
31
|
|
|
32
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
32
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
33
33
|
objBST.add(11, {key: 11, keyA: 11});
|
|
34
34
|
objBST.add(3, {key: 3, keyA: 3});
|
|
35
35
|
|
|
@@ -207,7 +207,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
207
207
|
});
|
|
208
208
|
|
|
209
209
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
210
|
-
const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
|
|
210
|
+
const objTreeMultiset = new TreeMultiset<{ key: number; keyA: number }>();
|
|
211
211
|
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
|
212
212
|
objTreeMultiset.add(11, {key: 11, keyA: 11});
|
|
213
213
|
objTreeMultiset.add(3, {key: 3, keyA: 3});
|
|
@@ -447,7 +447,7 @@ describe('TreeMultiset operations test recursively', () => {
|
|
|
447
447
|
});
|
|
448
448
|
|
|
449
449
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
450
|
-
const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
|
|
450
|
+
const objTreeMultiset = new TreeMultiset<{ key: number; keyA: number }>();
|
|
451
451
|
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
|
452
452
|
objTreeMultiset.add(11, {key: 11, keyA: 11});
|
|
453
453
|
objTreeMultiset.add(3, {key: 3, keyA: 3});
|
|
@@ -52,3 +52,23 @@ describe('CoordinateMap', () => {
|
|
|
52
52
|
expect(retrievedValue).toBe(value);
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe('CoordinateMap', () => {
|
|
57
|
+
class MyCoordinateMap<V = any> extends CoordinateMap<V> {
|
|
58
|
+
constructor(joint?: string) {
|
|
59
|
+
super(joint);
|
|
60
|
+
this._setJoint(joint += '-')
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const cMap = new MyCoordinateMap<number>('*');
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
cMap.set([0, 0], 0);
|
|
68
|
+
cMap.set([0, 1], 1);
|
|
69
|
+
cMap.set([1, 1], 11);
|
|
70
|
+
})
|
|
71
|
+
it('should joint to be *-', () => {
|
|
72
|
+
expect(cMap.joint).toBe('*-');
|
|
73
|
+
});
|
|
74
|
+
});
|