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
|
@@ -39,3 +39,28 @@ describe('CoordinateSet', () => {
|
|
|
39
39
|
expect(hasValue).toBe(true);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
+
|
|
43
|
+
describe('MyCoordinateSet', () => {
|
|
44
|
+
class MyCoordinateSet extends CoordinateSet {
|
|
45
|
+
constructor(joint?: string) {
|
|
46
|
+
super(joint);
|
|
47
|
+
this._setJoint(joint += '-')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const mySet = new MyCoordinateSet('*');
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
mySet.add([0, 0]);
|
|
55
|
+
mySet.add([0, 1]);
|
|
56
|
+
mySet.add([1, 1]);
|
|
57
|
+
})
|
|
58
|
+
it('should joint to be *-', () => {
|
|
59
|
+
expect(mySet.joint).toBe('*-');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should has, delete', () => {
|
|
63
|
+
mySet.delete([0, 1]);
|
|
64
|
+
expect(mySet.has([0, 1])).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -15,7 +15,9 @@ describe('HashNode', () => {
|
|
|
15
15
|
describe('HashTable', () => {
|
|
16
16
|
it('should initialize with default capacity', () => {
|
|
17
17
|
const hashTable = new HashTable<string, string>();
|
|
18
|
-
|
|
18
|
+
hashTable.capacity = hashTable.capacity;
|
|
19
|
+
hashTable.buckets = hashTable.buckets;
|
|
20
|
+
hashTable.hashFn = hashTable.hashFn;
|
|
19
21
|
expect(hashTable.capacity).toBe(16);
|
|
20
22
|
expect(hashTable.size).toBe(0);
|
|
21
23
|
expect(hashTable.buckets.length).toBe(16);
|
|
@@ -22,7 +22,7 @@ describe('Heap Operation Test', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('should object heap work well', function () {
|
|
25
|
-
const minHeap = new MinHeap<{a: string; key: number}>({comparator: (a, b) => a.key - b.key});
|
|
25
|
+
const minHeap = new MinHeap<{ a: string; key: number }>({comparator: (a, b) => a.key - b.key});
|
|
26
26
|
minHeap.add({key: 1, a: 'a1'});
|
|
27
27
|
minHeap.add({key: 6, a: 'a6'});
|
|
28
28
|
minHeap.add({key: 2, a: 'a2'});
|
|
@@ -37,7 +37,7 @@ describe('Heap Operation Test', () => {
|
|
|
37
37
|
i++;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const maxHeap = new MaxHeap<{key: number; a: string}>({comparator: (a, b) => b.key - a.key});
|
|
40
|
+
const maxHeap = new MaxHeap<{ key: number; a: string }>({comparator: (a, b) => b.key - a.key});
|
|
41
41
|
maxHeap.add({key: 1, a: 'a1'});
|
|
42
42
|
maxHeap.add({key: 6, a: 'a6'});
|
|
43
43
|
maxHeap.add({key: 5, a: 'a5'});
|
|
@@ -1,9 +1,72 @@
|
|
|
1
|
-
import {DoublyLinkedList} from '../../../../src';
|
|
1
|
+
import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
|
+
describe('DoublyLinkedListNode', () => {
|
|
5
|
+
it('should DoublyLinkedListNode', () => {
|
|
6
|
+
const node1 = new DoublyLinkedListNode<number>(2);
|
|
7
|
+
expect(node1.val).toBe(2);
|
|
8
|
+
node1.val = 1;
|
|
9
|
+
expect(node1.val).toBe(1);
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('DoublyLinkedList Operation Test', () => {
|
|
14
|
+
let list: DoublyLinkedList<number>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
list = DoublyLinkedList.fromArray([1, 2, 3, 4, 5]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should out of bound index', () => {
|
|
21
|
+
expect(list.getNodeAt(-1)).toBe(null);
|
|
22
|
+
expect(list.getNodeAt(5)).toBe(null);
|
|
23
|
+
expect(list.insertAt(5, 6)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should insertBefore', () => {
|
|
27
|
+
expect(list.insertBefore(1, 0)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should deleteAt', () => {
|
|
31
|
+
expect(list.deleteAt(1)).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should delete tail', () => {
|
|
35
|
+
expect(list.delete(list.tail)).toBe(true);
|
|
36
|
+
expect(list.tail?.val).toBe(4);
|
|
37
|
+
expect(list.delete(6)).toBe(false);
|
|
38
|
+
expect(list.tail?.val).toBe(4);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
it('should find null', () => {
|
|
43
|
+
expect(list.find(val => val === 6)).toBe(null);
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should indexOf -1', () => {
|
|
48
|
+
expect(list.indexOf(6)).toBe(-1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should findBackward null', () => {
|
|
52
|
+
expect(list.findBackward(val => val === 0)).toBe(null);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should insertAfter tail', () => {
|
|
56
|
+
expect(list.insertAfter(list.tail!, 6)).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should insertAfter tail', () => {
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
expect([...list]).toEqual([1, 2, 3, 4, 5]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
|
|
4
67
|
describe('DoublyLinkedList Operation Test', () => {
|
|
5
68
|
let list: DoublyLinkedList<number>;
|
|
6
|
-
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
69
|
+
let objectList: DoublyLinkedList<{ keyA: number }>;
|
|
7
70
|
|
|
8
71
|
beforeEach(() => {
|
|
9
72
|
list = new DoublyLinkedList();
|
|
@@ -109,7 +172,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
109
172
|
list.reverse();
|
|
110
173
|
|
|
111
174
|
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
112
|
-
expect(list.
|
|
175
|
+
expect(list.toArrayBackward()).toEqual([1, 2, 3]);
|
|
113
176
|
});
|
|
114
177
|
|
|
115
178
|
it('should map elements using a callback function', () => {
|
|
@@ -189,7 +252,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
189
252
|
list.push(3);
|
|
190
253
|
list.push(4);
|
|
191
254
|
|
|
192
|
-
const lastEven = list.
|
|
255
|
+
const lastEven = list.findBackward(val => val % 2 === 0);
|
|
193
256
|
|
|
194
257
|
expect(lastEven).toBe(4);
|
|
195
258
|
});
|
|
@@ -211,7 +274,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
211
274
|
list.push(2);
|
|
212
275
|
list.push(3);
|
|
213
276
|
|
|
214
|
-
const reversedArray = list.
|
|
277
|
+
const reversedArray = list.toArrayBackward();
|
|
215
278
|
|
|
216
279
|
expect(reversedArray).toEqual([3, 2, 1]);
|
|
217
280
|
});
|
|
@@ -327,8 +390,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
327
390
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
328
391
|
expect(insertSuccess).toBe(true);
|
|
329
392
|
|
|
330
|
-
const
|
|
331
|
-
expect(
|
|
393
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
394
|
+
expect(getNode?.val).toEqual(newObj);
|
|
332
395
|
|
|
333
396
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
334
397
|
expect(deleted).toBe(true);
|
|
@@ -11,7 +11,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
11
11
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
12
12
|
doublyList.push(i);
|
|
13
13
|
if (i === midIndex) {
|
|
14
|
-
midNode = doublyList.
|
|
14
|
+
midNode = doublyList.getNode(i);
|
|
15
15
|
} else if (i > midIndex && midNode) {
|
|
16
16
|
doublyList.insertBefore(midNode, i);
|
|
17
17
|
}
|
|
@@ -24,7 +24,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
24
24
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
25
25
|
singlyList.push(i);
|
|
26
26
|
if (i === midIndex) {
|
|
27
|
-
midSinglyNode = singlyList.
|
|
27
|
+
midSinglyNode = singlyList.getNode(i);
|
|
28
28
|
} else if (i > midIndex && midSinglyNode) {
|
|
29
29
|
singlyList.insertBefore(midSinglyNode.val, i);
|
|
30
30
|
}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import {SinglyLinkedList} from '../../../../src';
|
|
1
|
+
import {SinglyLinkedList, SinglyLinkedListNode} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
|
+
describe('SinglyLinkedListNode', () => {
|
|
5
|
+
it('should SinglyLinkedList', () => {
|
|
6
|
+
const node1 = new SinglyLinkedListNode<number>(2);
|
|
7
|
+
expect(node1.val).toBe(2);
|
|
8
|
+
node1.val = 1;
|
|
9
|
+
expect(node1.val).toBe(1);
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
|
|
4
13
|
describe('SinglyLinkedList Operation Test', () => {
|
|
5
14
|
let list: SinglyLinkedList<number>;
|
|
6
|
-
let objectList: SinglyLinkedList<{keyA: number}>;
|
|
15
|
+
let objectList: SinglyLinkedList<{ keyA: number }>;
|
|
7
16
|
beforeEach(() => {
|
|
8
17
|
list = new SinglyLinkedList<number>();
|
|
9
|
-
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
18
|
+
objectList = new SinglyLinkedList<{ keyA: number }>();
|
|
10
19
|
});
|
|
11
20
|
|
|
12
21
|
describe('push', () => {
|
|
@@ -21,8 +30,10 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
21
30
|
it('should delete and return the last element of the list', () => {
|
|
22
31
|
list.push(1);
|
|
23
32
|
list.push(2);
|
|
33
|
+
list.push(3);
|
|
24
34
|
const popped = list.pop();
|
|
25
|
-
expect(popped).toBe(
|
|
35
|
+
expect(popped).toBe(3);
|
|
36
|
+
expect(list.popLast()).toBe(2);
|
|
26
37
|
expect(list.toArray()).toEqual([1]);
|
|
27
38
|
});
|
|
28
39
|
|
|
@@ -36,9 +47,11 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
36
47
|
it('should delete and return the first element of the list', () => {
|
|
37
48
|
list.push(1);
|
|
38
49
|
list.push(2);
|
|
50
|
+
list.push(3);
|
|
39
51
|
const shifted = list.shift();
|
|
40
52
|
expect(shifted).toBe(1);
|
|
41
|
-
expect(list.
|
|
53
|
+
expect(list.popFirst()).toBe(2);
|
|
54
|
+
expect(list.toArray()).toEqual([3]);
|
|
42
55
|
});
|
|
43
56
|
|
|
44
57
|
it('should return undefined if the list is empty', () => {
|
|
@@ -50,7 +63,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
50
63
|
describe('unshift', () => {
|
|
51
64
|
it('should add elements to the beginning of the list', () => {
|
|
52
65
|
list.unshift(1);
|
|
53
|
-
list.
|
|
66
|
+
list.addFirst(2);
|
|
54
67
|
expect(list.toArray()).toEqual([2, 1]);
|
|
55
68
|
});
|
|
56
69
|
});
|
|
@@ -62,6 +75,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
62
75
|
list.push(3);
|
|
63
76
|
const element = list.getAt(1);
|
|
64
77
|
expect(element).toBe(2);
|
|
78
|
+
expect(list.getNodeAt(2)?.val).toBe(3);
|
|
65
79
|
});
|
|
66
80
|
|
|
67
81
|
it('should return undefined for an out-of-bounds index', () => {
|
|
@@ -113,9 +127,14 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
113
127
|
list.push(1);
|
|
114
128
|
list.push(2);
|
|
115
129
|
list.push(3);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
expect(list.
|
|
130
|
+
list.push(4);
|
|
131
|
+
list.push(5);
|
|
132
|
+
expect(list.delete(2)).toBe(true);
|
|
133
|
+
expect(list.toArray()).toEqual([1, 3, 4, 5]);
|
|
134
|
+
expect(list.delete(1)).toBe(true);
|
|
135
|
+
expect(list.toArray()).toEqual([3, 4, 5]);
|
|
136
|
+
expect(list.delete(5)).toBe(true);
|
|
137
|
+
expect(list.toArray()).toEqual([3, 4]);
|
|
119
138
|
});
|
|
120
139
|
|
|
121
140
|
it('should return false if the value is not found', () => {
|
|
@@ -362,8 +381,8 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
362
381
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
363
382
|
expect(insertSuccess).toBe(true);
|
|
364
383
|
|
|
365
|
-
const
|
|
366
|
-
expect(
|
|
384
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
385
|
+
expect(getNode?.val).toEqual(newObj);
|
|
367
386
|
|
|
368
387
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
369
388
|
expect(deleted).toBe(true);
|
|
@@ -427,7 +446,6 @@ describe('SinglyLinkedList', () => {
|
|
|
427
446
|
expect(list.length).toBe(1);
|
|
428
447
|
});
|
|
429
448
|
|
|
430
|
-
// Add more test cases for other methods like shift, unshift, getAt, deleteAt, and more.
|
|
431
449
|
|
|
432
450
|
it('should reverse the list', () => {
|
|
433
451
|
list.push(1);
|
|
@@ -439,7 +457,6 @@ describe('SinglyLinkedList', () => {
|
|
|
439
457
|
// Add more assertions for reversed order.
|
|
440
458
|
});
|
|
441
459
|
|
|
442
|
-
// Add more test cases for other methods like find, indexOf, and more.
|
|
443
460
|
|
|
444
461
|
it('should convert the list to an array', () => {
|
|
445
462
|
list.push(1);
|
|
@@ -447,5 +464,35 @@ describe('SinglyLinkedList', () => {
|
|
|
447
464
|
list.push(3);
|
|
448
465
|
const array = list.toArray();
|
|
449
466
|
expect(array).toEqual([1, 2, 3]);
|
|
467
|
+
// @ts-ignore
|
|
468
|
+
expect([...list]).toEqual([1, 2, 3]);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('should filter the list', () => {
|
|
472
|
+
list.push(1);
|
|
473
|
+
list.push(2);
|
|
474
|
+
list.push(3);
|
|
475
|
+
expect(list.filter(val => val !== 2).toArray()).toEqual([1, 3]);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
it('should forEach the list', () => {
|
|
480
|
+
list.push(1);
|
|
481
|
+
list.push(2);
|
|
482
|
+
list.push(3);
|
|
483
|
+
list.forEach(val => val++);
|
|
484
|
+
expect(list.toArray()).toEqual([1, 2, 3])
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('should map the list', () => {
|
|
488
|
+
list.addLast(1);
|
|
489
|
+
list.push(2);
|
|
490
|
+
list.push(3);
|
|
491
|
+
expect(list.map(val => val * 2).toArray()).toEqual([2, 4, 6]);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('should reduce the list', () => {
|
|
495
|
+
const list1 = SinglyLinkedList.fromArray([1, 2, 3]);
|
|
496
|
+
expect(list1.reduce((acc, val) => acc + val, 0)).toEqual(6);
|
|
450
497
|
});
|
|
451
498
|
});
|
|
@@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
20
|
-
const priorityQueue = new MaxPriorityQueue<{keyA: number}>({comparator: (a, b) => b.keyA - a.keyA});
|
|
20
|
+
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({comparator: (a, b) => b.keyA - a.keyA});
|
|
21
21
|
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
|
22
22
|
priorityQueue.add({keyA: 7});
|
|
23
23
|
|
|
@@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
64
64
|
|
|
65
65
|
it('should correctly heapify an object array', () => {
|
|
66
66
|
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
|
67
|
-
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
|
67
|
+
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
|
68
68
|
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
70
70
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
@@ -15,20 +15,20 @@ describe('Deque Tests', () => {
|
|
|
15
15
|
it('should add elements at the beginning and end', () => {
|
|
16
16
|
deque.addFirst(1);
|
|
17
17
|
deque.addLast(2);
|
|
18
|
-
expect(deque.
|
|
19
|
-
expect(deque.
|
|
18
|
+
expect(deque.getFirst()).toBe(1);
|
|
19
|
+
expect(deque.getLast()).toBe(2);
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
it('should delete elements from the beginning and end', () => {
|
|
23
23
|
deque.addFirst(1);
|
|
24
24
|
deque.addLast(2);
|
|
25
|
-
deque.
|
|
26
|
-
deque.
|
|
25
|
+
deque.popFirst();
|
|
26
|
+
deque.popLast();
|
|
27
27
|
expect(deque.isEmpty()).toBe(true);
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
it('should handle edge case when removing from an empty deque', () => {
|
|
31
|
-
const result = deque.
|
|
31
|
+
const result = deque.popFirst();
|
|
32
32
|
expect(result).toBeUndefined();
|
|
33
33
|
});
|
|
34
34
|
|
|
@@ -40,18 +40,18 @@ describe('Deque Tests', () => {
|
|
|
40
40
|
|
|
41
41
|
it('should handle adding and removing elements alternately', () => {
|
|
42
42
|
deque.addFirst(1);
|
|
43
|
-
expect(deque.
|
|
43
|
+
expect(deque.popFirst()).toBe(1);
|
|
44
44
|
deque.addLast(2);
|
|
45
|
-
expect(deque.
|
|
45
|
+
expect(deque.popLast()).toBe(2);
|
|
46
46
|
expect(deque.isEmpty()).toBe(true);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
it('should handle adding and removing elements in a cyclic manner', () => {
|
|
50
50
|
deque.addFirst(1);
|
|
51
51
|
deque.addLast(2);
|
|
52
|
-
expect(deque.
|
|
52
|
+
expect(deque.popFirst()).toBe(1);
|
|
53
53
|
deque.addFirst(3);
|
|
54
|
-
expect(deque.
|
|
54
|
+
expect(deque.popLast()).toBe(2);
|
|
55
55
|
expect(deque.size).toBe(1);
|
|
56
56
|
});
|
|
57
57
|
// Add more test cases as needed
|
|
@@ -68,20 +68,20 @@ describe('Deque Tests', () => {
|
|
|
68
68
|
it('should add elements at the beginning and end', () => {
|
|
69
69
|
objectDeque.addFirst('one');
|
|
70
70
|
objectDeque.addLast('two');
|
|
71
|
-
expect(objectDeque.
|
|
72
|
-
expect(objectDeque.
|
|
71
|
+
expect(objectDeque.getFirst()).toBe('one');
|
|
72
|
+
expect(objectDeque.getLast()).toBe('two');
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
it('should delete elements from the beginning and end', () => {
|
|
76
76
|
objectDeque.addFirst('one');
|
|
77
77
|
objectDeque.addLast('two');
|
|
78
|
-
objectDeque.
|
|
79
|
-
objectDeque.
|
|
78
|
+
objectDeque.popFirst();
|
|
79
|
+
objectDeque.popLast();
|
|
80
80
|
expect(objectDeque.isEmpty()).toBe(true);
|
|
81
81
|
});
|
|
82
82
|
|
|
83
83
|
it('should handle edge case when removing from an empty deque', () => {
|
|
84
|
-
const result = objectDeque.
|
|
84
|
+
const result = objectDeque.popFirst();
|
|
85
85
|
expect(result).toBeUndefined();
|
|
86
86
|
});
|
|
87
87
|
|
|
@@ -105,20 +105,20 @@ describe('Deque Tests', () => {
|
|
|
105
105
|
it('should add elements at the beginning and end', () => {
|
|
106
106
|
arrayDeque.addFirst(1);
|
|
107
107
|
arrayDeque.addLast(2);
|
|
108
|
-
expect(arrayDeque.
|
|
109
|
-
expect(arrayDeque.
|
|
108
|
+
expect(arrayDeque.getFirst()).toBe(1);
|
|
109
|
+
expect(arrayDeque.getLast()).toBe(2);
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
it('should delete elements from the beginning and end', () => {
|
|
113
113
|
arrayDeque.addFirst(1);
|
|
114
114
|
arrayDeque.addLast(2);
|
|
115
|
-
arrayDeque.
|
|
116
|
-
arrayDeque.
|
|
115
|
+
arrayDeque.popFirst();
|
|
116
|
+
arrayDeque.popLast();
|
|
117
117
|
expect(arrayDeque.isEmpty()).toBe(true);
|
|
118
118
|
});
|
|
119
119
|
|
|
120
120
|
it('should handle edge case when removing from an empty deque', () => {
|
|
121
|
-
const result = arrayDeque.
|
|
121
|
+
const result = arrayDeque.popFirst();
|
|
122
122
|
expect(result).toBeNull();
|
|
123
123
|
});
|
|
124
124
|
|
|
@@ -165,16 +165,16 @@ describe('Deque', () => {
|
|
|
165
165
|
deque.addLast(2);
|
|
166
166
|
|
|
167
167
|
expect(deque.size).toBe(2);
|
|
168
|
-
expect(deque.
|
|
169
|
-
expect(deque.
|
|
168
|
+
expect(deque.getFirst()).toBe(1);
|
|
169
|
+
expect(deque.getLast()).toBe(2);
|
|
170
170
|
});
|
|
171
171
|
|
|
172
172
|
test('should remove elements from the front and back', () => {
|
|
173
173
|
deque.addFirst(1);
|
|
174
174
|
deque.addLast(2);
|
|
175
175
|
|
|
176
|
-
const firstElement = deque.
|
|
177
|
-
const lastElement = deque.
|
|
176
|
+
const firstElement = deque.popFirst();
|
|
177
|
+
const lastElement = deque.popLast();
|
|
178
178
|
|
|
179
179
|
expect(deque.size).toBe(0);
|
|
180
180
|
expect(firstElement).toBe(1);
|
|
@@ -203,7 +203,7 @@ describe('Deque', () => {
|
|
|
203
203
|
deque.addLast(1);
|
|
204
204
|
expect(deque.isEmpty()).toBe(false);
|
|
205
205
|
|
|
206
|
-
deque.
|
|
206
|
+
deque.popFirst();
|
|
207
207
|
expect(deque.isEmpty()).toBe(true);
|
|
208
208
|
});
|
|
209
209
|
});
|
|
@@ -225,16 +225,16 @@ describe('ArrayDeque', () => {
|
|
|
225
225
|
deque.addLast(2);
|
|
226
226
|
|
|
227
227
|
expect(deque.size).toBe(2);
|
|
228
|
-
expect(deque.
|
|
229
|
-
expect(deque.
|
|
228
|
+
expect(deque.getFirst()).toBe(1);
|
|
229
|
+
expect(deque.getLast()).toBe(2);
|
|
230
230
|
});
|
|
231
231
|
|
|
232
232
|
test('should remove elements from the front and back', () => {
|
|
233
233
|
deque.addFirst(1);
|
|
234
234
|
deque.addLast(2);
|
|
235
235
|
|
|
236
|
-
const firstElement = deque.
|
|
237
|
-
const lastElement = deque.
|
|
236
|
+
const firstElement = deque.popFirst();
|
|
237
|
+
const lastElement = deque.popLast();
|
|
238
238
|
|
|
239
239
|
expect(deque.size).toBe(0);
|
|
240
240
|
expect(firstElement).toBe(1);
|
|
@@ -263,7 +263,7 @@ describe('ArrayDeque', () => {
|
|
|
263
263
|
deque.addLast(1);
|
|
264
264
|
expect(deque.isEmpty()).toBe(false);
|
|
265
265
|
|
|
266
|
-
deque.
|
|
266
|
+
deque.popFirst();
|
|
267
267
|
expect(deque.isEmpty()).toBe(true);
|
|
268
268
|
});
|
|
269
269
|
|
|
@@ -319,8 +319,8 @@ describe('ObjectDeque', () => {
|
|
|
319
319
|
deque.addFirst(2);
|
|
320
320
|
|
|
321
321
|
expect(deque.size).toBe(2);
|
|
322
|
-
expect(deque.
|
|
323
|
-
expect(deque.
|
|
322
|
+
expect(deque.getFirst()).toBe(2);
|
|
323
|
+
expect(deque.getLast()).toBe(1);
|
|
324
324
|
});
|
|
325
325
|
|
|
326
326
|
test('should add elements to the end of the deque', () => {
|
|
@@ -328,37 +328,37 @@ describe('ObjectDeque', () => {
|
|
|
328
328
|
deque.addLast(2);
|
|
329
329
|
|
|
330
330
|
expect(deque.size).toBe(2);
|
|
331
|
-
expect(deque.
|
|
332
|
-
expect(deque.
|
|
331
|
+
expect(deque.getFirst()).toBe(1);
|
|
332
|
+
expect(deque.getLast()).toBe(2);
|
|
333
333
|
});
|
|
334
334
|
|
|
335
335
|
test('should remove elements from the front of the deque', () => {
|
|
336
336
|
deque.addLast(1);
|
|
337
337
|
deque.addLast(2);
|
|
338
338
|
|
|
339
|
-
const removedElement = deque.
|
|
339
|
+
const removedElement = deque.popFirst();
|
|
340
340
|
|
|
341
341
|
expect(deque.size).toBe(1);
|
|
342
342
|
expect(removedElement).toBe(1);
|
|
343
|
-
expect(deque.
|
|
343
|
+
expect(deque.getFirst()).toBe(2);
|
|
344
344
|
});
|
|
345
345
|
|
|
346
346
|
test('should remove elements from the end of the deque', () => {
|
|
347
347
|
deque.addLast(1);
|
|
348
348
|
deque.addLast(2);
|
|
349
349
|
|
|
350
|
-
const removedElement = deque.
|
|
350
|
+
const removedElement = deque.popFirst();
|
|
351
351
|
|
|
352
352
|
expect(deque.size).toBe(1);
|
|
353
353
|
expect(removedElement).toBe(1);
|
|
354
|
-
expect(deque.
|
|
354
|
+
expect(deque.getLast()).toBe(2);
|
|
355
355
|
});
|
|
356
356
|
|
|
357
357
|
test('should return the element at the front of the deque without removing it', () => {
|
|
358
358
|
deque.addFirst(1);
|
|
359
359
|
deque.addFirst(2);
|
|
360
360
|
|
|
361
|
-
expect(deque.
|
|
361
|
+
expect(deque.getFirst()).toBe(2);
|
|
362
362
|
expect(deque.size).toBe(2);
|
|
363
363
|
});
|
|
364
364
|
|
|
@@ -366,7 +366,7 @@ describe('ObjectDeque', () => {
|
|
|
366
366
|
deque.addLast(1);
|
|
367
367
|
deque.addLast(2);
|
|
368
368
|
|
|
369
|
-
expect(deque.
|
|
369
|
+
expect(deque.getLast()).toBe(2);
|
|
370
370
|
expect(deque.size).toBe(2);
|
|
371
371
|
});
|
|
372
372
|
|
|
@@ -391,9 +391,9 @@ describe('ObjectDeque', () => {
|
|
|
391
391
|
deque.addLast(2);
|
|
392
392
|
deque.addLast(3);
|
|
393
393
|
|
|
394
|
-
expect(deque.
|
|
394
|
+
expect(deque.getFirst()).toBe(1);
|
|
395
395
|
expect(deque.get(1)).toBe(2);
|
|
396
|
-
expect(deque.
|
|
396
|
+
expect(deque.getLast()).toBe(3);
|
|
397
397
|
});
|
|
398
398
|
|
|
399
399
|
test('should insert elements at a specific index', () => {
|
|
@@ -402,9 +402,9 @@ describe('ObjectDeque', () => {
|
|
|
402
402
|
deque.addLast(3);
|
|
403
403
|
|
|
404
404
|
expect(deque.size).toBe(3);
|
|
405
|
-
expect(deque.
|
|
405
|
+
expect(deque.getFirst()).toBe(1);
|
|
406
406
|
expect(deque.get(1)).toBe(2);
|
|
407
407
|
expect(deque.get(2)).toBe(3);
|
|
408
|
-
expect(deque.
|
|
408
|
+
expect(deque.getLast()).toBe(3);
|
|
409
409
|
});
|
|
410
410
|
});
|
|
@@ -86,7 +86,7 @@ describe('Queue', () => {
|
|
|
86
86
|
queue.push(2);
|
|
87
87
|
expect(queue.size).toBe(2);
|
|
88
88
|
expect(queue.peek()).toBe(1);
|
|
89
|
-
expect(queue.
|
|
89
|
+
expect(queue.getLast()).toBe(2);
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
it('should shift elements from the front of the queue', () => {
|
|
@@ -96,7 +96,7 @@ describe('Queue', () => {
|
|
|
96
96
|
expect(shifted).toBe(1);
|
|
97
97
|
expect(queue.size).toBe(1);
|
|
98
98
|
expect(queue.peek()).toBe(2);
|
|
99
|
-
expect(queue.
|
|
99
|
+
expect(queue.getLast()).toBe(2);
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
it('should handle shifting when queue reaches half size', () => {
|
|
@@ -117,7 +117,7 @@ describe('Queue', () => {
|
|
|
117
117
|
queue.push(1);
|
|
118
118
|
queue.push(2);
|
|
119
119
|
expect(queue.peek()).toBe(1);
|
|
120
|
-
expect(queue.
|
|
120
|
+
expect(queue.getLast()).toBe(2);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
it('should handle shifting when the queue is empty', () => {
|
|
@@ -129,7 +129,7 @@ describe('Queue', () => {
|
|
|
129
129
|
|
|
130
130
|
it('should handle peeking when the queue is empty', () => {
|
|
131
131
|
expect(queue.peek()).toBeUndefined();
|
|
132
|
-
expect(queue.
|
|
132
|
+
expect(queue.getLast()).toBeUndefined();
|
|
133
133
|
});
|
|
134
134
|
|
|
135
135
|
it('should handle clearing the queue', () => {
|
|
@@ -139,7 +139,7 @@ describe('Queue', () => {
|
|
|
139
139
|
queue.clear();
|
|
140
140
|
expect(queue.size).toBe(0);
|
|
141
141
|
expect(queue.peek()).toBeUndefined();
|
|
142
|
-
expect(queue.
|
|
142
|
+
expect(queue.getLast()).toBeUndefined();
|
|
143
143
|
});
|
|
144
144
|
|
|
145
145
|
it('should clone the queue', () => {
|
|
@@ -149,7 +149,7 @@ describe('Queue', () => {
|
|
|
149
149
|
const clonedQueue = queue.clone();
|
|
150
150
|
expect(clonedQueue.size).toBe(3);
|
|
151
151
|
expect(clonedQueue.peek()).toBe(1);
|
|
152
|
-
expect(clonedQueue.
|
|
152
|
+
expect(clonedQueue.getLast()).toBe(3);
|
|
153
153
|
});
|
|
154
154
|
|
|
155
155
|
it('should handle creating a queue from an array', () => {
|
|
@@ -157,7 +157,7 @@ describe('Queue', () => {
|
|
|
157
157
|
const newQueue = Queue.fromArray(elements);
|
|
158
158
|
expect(newQueue.size).toBe(5);
|
|
159
159
|
expect(newQueue.peek()).toBe(1);
|
|
160
|
-
expect(newQueue.
|
|
160
|
+
expect(newQueue.getLast()).toBe(5);
|
|
161
161
|
});
|
|
162
162
|
|
|
163
163
|
it('should iterate through the queue', () => {
|
package/test/utils/big-o.ts
CHANGED
|
@@ -26,7 +26,7 @@ export const bigO = {
|
|
|
26
26
|
|
|
27
27
|
function findPotentialN(input: any): number {
|
|
28
28
|
let longestArray: any[] = [];
|
|
29
|
-
let mostProperties: {[key: string]: any} = {};
|
|
29
|
+
let mostProperties: { [key: string]: any } = {};
|
|
30
30
|
|
|
31
31
|
function recurse(obj: any) {
|
|
32
32
|
if (Array.isArray(obj)) {
|