data-structure-typed 1.33.7 → 1.33.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 +25 -14
- package/package.json +22 -22
- package/.prettierrc.js +0 -16
- package/coverage/coverage-final.json +0 -68
- package/coverage/coverage-summary.json +0 -69
- package/docs/.nojekyll +0 -1
- package/docs/index.html +0 -875
- package/docs/modules.html +0 -336
- package/jest.config.js +0 -8
- package/src/data-structures/binary-tree/aa-tree.ts +0 -1
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
- package/src/data-structures/binary-tree/avl-tree.ts +0 -307
- package/src/data-structures/binary-tree/b-tree.ts +0 -1
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
- package/src/data-structures/binary-tree/binary-tree.ts +0 -47
- package/src/data-structures/binary-tree/bst.ts +0 -537
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -366
- package/src/data-structures/binary-tree/segment-tree.ts +0 -260
- package/src/data-structures/binary-tree/splay-tree.ts +0 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
- package/src/data-structures/graph/abstract-graph.ts +0 -1040
- package/src/data-structures/graph/directed-graph.ts +0 -470
- package/src/data-structures/graph/index.ts +0 -4
- package/src/data-structures/graph/map-graph.ts +0 -129
- package/src/data-structures/graph/undirected-graph.ts +0 -274
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-map.ts +0 -203
- package/src/data-structures/hash/hash-table.ts +0 -277
- package/src/data-structures/hash/index.ts +0 -7
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -212
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -32
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -213
- package/src/data-structures/matrix/navigator.ts +0 -121
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
- package/src/data-structures/priority-queue/priority-queue.ts +0 -359
- package/src/data-structures/queue/deque.ts +0 -297
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -191
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/index.ts +0 -4
- package/src/interfaces/abstract-binary-tree.ts +0 -189
- package/src/interfaces/abstract-graph.ts +0 -31
- package/src/interfaces/avl-tree.ts +0 -25
- package/src/interfaces/binary-tree.ts +0 -6
- package/src/interfaces/bst.ts +0 -31
- package/src/interfaces/directed-graph.ts +0 -20
- package/src/interfaces/doubly-linked-list.ts +0 -1
- package/src/interfaces/heap.ts +0 -1
- package/src/interfaces/index.ts +0 -15
- package/src/interfaces/navigator.ts +0 -1
- package/src/interfaces/priority-queue.ts +0 -1
- package/src/interfaces/rb-tree.ts +0 -9
- package/src/interfaces/segment-tree.ts +0 -1
- package/src/interfaces/singly-linked-list.ts +0 -1
- package/src/interfaces/tree-multiset.ts +0 -7
- package/src/interfaces/undirected-graph.ts +0 -6
- package/src/types/data-structures/abstract-binary-tree.ts +0 -50
- package/src/types/data-structures/abstract-graph.ts +0 -11
- package/src/types/data-structures/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree.ts +0 -5
- package/src/types/data-structures/bst.ts +0 -13
- package/src/types/data-structures/directed-graph.ts +0 -8
- package/src/types/data-structures/doubly-linked-list.ts +0 -1
- package/src/types/data-structures/hash.ts +0 -1
- package/src/types/data-structures/heap.ts +0 -5
- package/src/types/data-structures/index.ts +0 -16
- package/src/types/data-structures/map-graph.ts +0 -1
- package/src/types/data-structures/navigator.ts +0 -13
- package/src/types/data-structures/priority-queue.ts +0 -9
- package/src/types/data-structures/rb-tree.ts +0 -8
- package/src/types/data-structures/segment-tree.ts +0 -1
- package/src/types/data-structures/singly-linked-list.ts +0 -1
- package/src/types/data-structures/tree-multiset.ts +0 -6
- package/src/types/helpers.ts +0 -1
- package/src/types/index.ts +0 -3
- package/src/types/utils/index.ts +0 -2
- package/src/types/utils/utils.ts +0 -6
- package/src/types/utils/validate-type.ts +0 -35
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -79
- package/test/integration/avl-tree.test.ts +0 -108
- package/test/integration/bst.test.ts +0 -380
- package/test/integration/heap.test.js +0 -16
- package/test/integration/index.html +0 -44
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
- package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
- package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
- package/test/unit/data-structures/graph/overall.test.ts +0 -49
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
- package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
- package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
- package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
- package/test/unit/data-structures/heap/heap.test.ts +0 -55
- package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
- package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
- package/test/unit/data-structures/linked-list/index.ts +0 -4
- package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
- package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
- package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
- package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
- package/test/unit/data-structures/queue/deque.test.ts +0 -130
- package/test/unit/data-structures/queue/queue.test.ts +0 -199
- package/test/unit/data-structures/stack/stack.test.ts +0 -67
- package/test/unit/data-structures/tree/tree.test.ts +0 -39
- package/test/unit/data-structures/trie/trie.test.ts +0 -95
- package/test/utils/index.ts +0 -2
- package/test/utils/magnitude.ts +0 -21
- package/test/utils/number.ts +0 -3
- package/tsconfig.json +0 -29
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {Character, NavigatorParams, Turning, Navigator} from '../../../../src';
|
|
2
|
-
|
|
3
|
-
const exampleMatrix: number[][] = [
|
|
4
|
-
[0, 0, 0, 0],
|
|
5
|
-
[0, 1, 1, 0],
|
|
6
|
-
[0, 0, 0, 0]
|
|
7
|
-
];
|
|
8
|
-
|
|
9
|
-
// Create a sample redirect object
|
|
10
|
-
const exampleTurning: Turning = {
|
|
11
|
-
up: 'right',
|
|
12
|
-
right: 'down',
|
|
13
|
-
down: 'left',
|
|
14
|
-
left: 'up'
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// Create a sample move callback function
|
|
18
|
-
const exampleOnMove = () => {
|
|
19
|
-
expect(true).toBeTruthy();
|
|
20
|
-
// console.log(`Moved to position (${cur[0]}, ${cur[1]})`);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
// Create an initial parameter object for the example
|
|
24
|
-
const exampleInit: NavigatorParams<number>['init'] = {
|
|
25
|
-
cur: [0, 0],
|
|
26
|
-
charDir: 'right',
|
|
27
|
-
VISITED: -1
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// Create a Navigator Params object
|
|
31
|
-
const exampleNavigatorParams: NavigatorParams<number> = {
|
|
32
|
-
matrix: exampleMatrix,
|
|
33
|
-
turning: exampleTurning,
|
|
34
|
-
onMove: exampleOnMove,
|
|
35
|
-
init: exampleInit
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
describe('Character class', () => {
|
|
39
|
-
it('should create a character with the correct direction', () => {
|
|
40
|
-
const character = new Character('up', exampleTurning);
|
|
41
|
-
expect(character.direction).toBe('up');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('should turn the character in the correct direction', () => {
|
|
45
|
-
const character = new Character('up', exampleTurning);
|
|
46
|
-
const turnedCharacter = character.turn();
|
|
47
|
-
expect(turnedCharacter.direction).toBe('right');
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('Navigator class', () => {
|
|
52
|
-
let navigator: Navigator<number>;
|
|
53
|
-
|
|
54
|
-
beforeEach(() => {
|
|
55
|
-
navigator = new Navigator(exampleNavigatorParams);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should initialize with the correct matrix and current position', () => {
|
|
59
|
-
expect(navigator['_matrix']).toEqual(exampleMatrix);
|
|
60
|
-
expect(navigator['_cur']).toEqual(exampleInit.cur);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should move the character correctly', () => {
|
|
64
|
-
navigator.move('right');
|
|
65
|
-
expect(navigator['_cur']).toEqual([0, 1]);
|
|
66
|
-
expect(navigator['_matrix'][0][1]).toBe(exampleInit.VISITED);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('should turn the character correctly', () => {
|
|
70
|
-
expect(navigator['_character'].direction).toBe('right');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should check for valid moves correctly', () => {
|
|
74
|
-
expect(navigator.check('up')).toBe(false); // Blocked by wall
|
|
75
|
-
expect(navigator.check('right')).toBe(true); // Open path
|
|
76
|
-
expect(navigator.check('down')).toBe(true); // Blocked by wall
|
|
77
|
-
expect(navigator.check('left')).toBe(false); // Open path
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import {MaxPriorityQueue} from '../../../../src';
|
|
2
|
-
import {bigO, magnitude} from '../../../utils';
|
|
3
|
-
|
|
4
|
-
describe('MaxPriorityQueue Operation Test', () => {
|
|
5
|
-
it('should add elements and maintain heap property', () => {
|
|
6
|
-
const priorityQueue = new MaxPriorityQueue<number>();
|
|
7
|
-
|
|
8
|
-
priorityQueue.add(5);
|
|
9
|
-
priorityQueue.add(3);
|
|
10
|
-
priorityQueue.add(7);
|
|
11
|
-
priorityQueue.add(1);
|
|
12
|
-
|
|
13
|
-
expect(priorityQueue.poll()).toBe(7);
|
|
14
|
-
expect(priorityQueue.poll()).toBe(5);
|
|
15
|
-
expect(priorityQueue.poll()).toBe(3);
|
|
16
|
-
expect(priorityQueue.poll()).toBe(1);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
20
|
-
const priorityQueue = new MaxPriorityQueue<{keyA: number}>({
|
|
21
|
-
nodes: [{keyA: 5}, {keyA: 3}, {keyA: 1}],
|
|
22
|
-
comparator: (a, b) => b.keyA - a.keyA
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
priorityQueue.add({keyA: 7});
|
|
26
|
-
|
|
27
|
-
expect(priorityQueue.poll()?.keyA).toBe(7);
|
|
28
|
-
expect(priorityQueue.poll()?.keyA).toBe(5);
|
|
29
|
-
expect(priorityQueue.poll()?.keyA).toBe(3);
|
|
30
|
-
expect(priorityQueue.poll()?.keyA).toBe(1);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should return and remove the smallest element', () => {
|
|
34
|
-
const priorityQueue = new MaxPriorityQueue<number>();
|
|
35
|
-
priorityQueue.add(5);
|
|
36
|
-
priorityQueue.add(3);
|
|
37
|
-
priorityQueue.add(7);
|
|
38
|
-
|
|
39
|
-
expect(priorityQueue.poll()).toBe(7);
|
|
40
|
-
expect(priorityQueue.poll()).toBe(5);
|
|
41
|
-
expect(priorityQueue.size).toBe(1);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('should create a clone of the priority queue', () => {
|
|
45
|
-
const priorityQueue = new MaxPriorityQueue<number>();
|
|
46
|
-
priorityQueue.add(5);
|
|
47
|
-
priorityQueue.add(3);
|
|
48
|
-
priorityQueue.add(7);
|
|
49
|
-
|
|
50
|
-
const clone = priorityQueue.clone();
|
|
51
|
-
expect(clone.poll()).toBe(7);
|
|
52
|
-
expect(clone.poll()).toBe(5);
|
|
53
|
-
expect(clone.poll()).toBe(3);
|
|
54
|
-
expect(clone.isEmpty()).toBe(true);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should correctly heapify an array', () => {
|
|
58
|
-
const array = [5, 3, 7, 1];
|
|
59
|
-
const heap = MaxPriorityQueue.heapify<number>({nodes: array});
|
|
60
|
-
|
|
61
|
-
expect(heap.poll()).toBe(7);
|
|
62
|
-
expect(heap.poll()).toBe(5);
|
|
63
|
-
expect(heap.poll()).toBe(3);
|
|
64
|
-
expect(heap.poll()).toBe(1);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should correctly heapify an object array', () => {
|
|
68
|
-
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
|
69
|
-
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>({nodes, comparator: (a, b) => b.keyA - a.keyA});
|
|
70
|
-
|
|
71
|
-
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
72
|
-
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
73
|
-
expect(maxPQ.poll()?.keyA).toBe(3);
|
|
74
|
-
expect(maxPQ.poll()?.keyA).toBe(1);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('MaxPriorityQueue Performance Test', () => {
|
|
79
|
-
it('should the poll method adheres to a time complexity of O(log n) and executed correctly under large scale distinct data', () => {
|
|
80
|
-
const nodes = Array.from(
|
|
81
|
-
new Set<number>(Array.from(new Array(magnitude.LINEAR), () => Math.floor(Math.random() * magnitude.LINEAR * 100)))
|
|
82
|
-
);
|
|
83
|
-
expect(nodes.length).toBeGreaterThan(magnitude.LINEAR / 2);
|
|
84
|
-
const maxPQ = new MaxPriorityQueue<number>({nodes});
|
|
85
|
-
|
|
86
|
-
let prev = Number.MAX_SAFE_INTEGER;
|
|
87
|
-
const startTime = performance.now();
|
|
88
|
-
while (maxPQ.size > 0) {
|
|
89
|
-
const polled = maxPQ.poll();
|
|
90
|
-
if (polled) {
|
|
91
|
-
prev = polled;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 50);
|
|
95
|
-
expect(prev).toBeGreaterThan(0);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should sorted.length to be the same as original data', () => {
|
|
99
|
-
// const magnitude = 1000;
|
|
100
|
-
// const maxPriorityQueue = new MaxPriorityQueue<number>({nodes: Array.from(new Array<number>(magnitude), () => Math.floor(Math.random() * magnitude))});
|
|
101
|
-
// const nodeCount = maxPriorityQueue.getNodes().length;
|
|
102
|
-
// const sorted = maxPriorityQueue.sort();
|
|
103
|
-
//
|
|
104
|
-
// expect(sorted.length).toBe(nodeCount); // TODO Plan to support sorting of duplicate elements.
|
|
105
|
-
});
|
|
106
|
-
});
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import {MinPriorityQueue, PriorityQueue} from '../../../../src';
|
|
2
|
-
|
|
3
|
-
describe('MinPriorityQueue Operation Test', () => {
|
|
4
|
-
it('should check if a node exists in the queue', () => {
|
|
5
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
6
|
-
priorityQueue.add(5);
|
|
7
|
-
|
|
8
|
-
expect(priorityQueue.has(5)).toBe(true);
|
|
9
|
-
expect(priorityQueue.has(3)).toBe(false);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should return the smallest element without removing it', () => {
|
|
13
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
14
|
-
priorityQueue.add(5);
|
|
15
|
-
priorityQueue.add(3);
|
|
16
|
-
priorityQueue.add(7);
|
|
17
|
-
|
|
18
|
-
expect(priorityQueue.peek()).toBe(3);
|
|
19
|
-
expect(priorityQueue.size).toBe(3);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should return the last element', () => {
|
|
23
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
24
|
-
priorityQueue.add(5);
|
|
25
|
-
priorityQueue.add(3);
|
|
26
|
-
priorityQueue.add(7);
|
|
27
|
-
|
|
28
|
-
expect(priorityQueue.leaf()).toBe(7);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should check if the queue is empty', () => {
|
|
32
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
33
|
-
|
|
34
|
-
expect(priorityQueue.isEmpty()).toBe(true);
|
|
35
|
-
|
|
36
|
-
priorityQueue.add(5);
|
|
37
|
-
|
|
38
|
-
expect(priorityQueue.isEmpty()).toBe(false);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should clear the queue', () => {
|
|
42
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
43
|
-
priorityQueue.add(5);
|
|
44
|
-
priorityQueue.add(3);
|
|
45
|
-
priorityQueue.add(7);
|
|
46
|
-
|
|
47
|
-
priorityQueue.clear();
|
|
48
|
-
|
|
49
|
-
expect(priorityQueue.size).toBe(0);
|
|
50
|
-
expect(priorityQueue.isEmpty()).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should sort the elements', () => {
|
|
54
|
-
const priorityQueue = new MinPriorityQueue<number>();
|
|
55
|
-
priorityQueue.add(5);
|
|
56
|
-
priorityQueue.add(3);
|
|
57
|
-
priorityQueue.add(7);
|
|
58
|
-
priorityQueue.add(1);
|
|
59
|
-
|
|
60
|
-
const sortedArray = priorityQueue.sort();
|
|
61
|
-
expect(sortedArray).toEqual([1, 3, 5, 7]);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should PriorityQueue poll, pee, heapify, toArray work well', function () {
|
|
65
|
-
const minPQ = new PriorityQueue<number>({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => a - b});
|
|
66
|
-
expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]);
|
|
67
|
-
minPQ.poll();
|
|
68
|
-
minPQ.poll();
|
|
69
|
-
minPQ.poll();
|
|
70
|
-
expect(minPQ.toArray()).toEqual([4, 5, 6]);
|
|
71
|
-
expect(minPQ.peek()).toBe(4);
|
|
72
|
-
expect(
|
|
73
|
-
PriorityQueue.heapify({
|
|
74
|
-
nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10],
|
|
75
|
-
comparator: (a, b) => a - b
|
|
76
|
-
}).toArray()
|
|
77
|
-
).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () {
|
|
81
|
-
const maxPriorityQueue = new PriorityQueue<number>({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => b - a});
|
|
82
|
-
expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]);
|
|
83
|
-
maxPriorityQueue.poll();
|
|
84
|
-
maxPriorityQueue.poll();
|
|
85
|
-
maxPriorityQueue.poll();
|
|
86
|
-
expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]);
|
|
87
|
-
expect(maxPriorityQueue.peek()).toBe(3);
|
|
88
|
-
expect(
|
|
89
|
-
PriorityQueue.heapify({
|
|
90
|
-
nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10],
|
|
91
|
-
comparator: (a, b) => a - b
|
|
92
|
-
}).toArray()
|
|
93
|
-
).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('should PriorityQueue clone, sort, getNodes, DFS work well', function () {
|
|
97
|
-
const minPQ1 = new PriorityQueue<number>({nodes: [2, 5, 8, 3, 1, 6, 7, 4], comparator: (a, b) => a - b});
|
|
98
|
-
const clonedPriorityQueue = minPQ1.clone();
|
|
99
|
-
expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes());
|
|
100
|
-
expect(clonedPriorityQueue.sort()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
101
|
-
expect(minPQ1.DFS('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]);
|
|
102
|
-
expect(minPQ1.DFS('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]);
|
|
103
|
-
expect(minPQ1.DFS('pre')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import {PriorityQueue} from '../../../../src';
|
|
2
|
-
import {getRandomInt} from '../../../utils';
|
|
3
|
-
|
|
4
|
-
describe('PriorityQueue Operation Test', () => {
|
|
5
|
-
it('should validate a priority queue', () => {
|
|
6
|
-
const minPQ = new PriorityQueue<number>({nodes: [1, 5, 7, 9, 3, 6, 2], comparator: (a, b) => a - b});
|
|
7
|
-
|
|
8
|
-
expect(minPQ.isValid()).toBe(true);
|
|
9
|
-
expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => a - b})).toBe(true);
|
|
10
|
-
expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => b - a})).toBe(false);
|
|
11
|
-
expect(
|
|
12
|
-
PriorityQueue.isPriorityQueueified({
|
|
13
|
-
nodes: [1, 5, 7, 9, 3, 6, 2],
|
|
14
|
-
comparator: (a, b) => b - a
|
|
15
|
-
})
|
|
16
|
-
).toBe(false);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('Priority Queue Performance Test', () => {
|
|
21
|
-
it('should numeric heap work well', function () {
|
|
22
|
-
const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000));
|
|
23
|
-
const minPriorityQueue = new PriorityQueue<number>({nodes: values, comparator: (a, b) => a - b});
|
|
24
|
-
const sorted = minPriorityQueue.sort();
|
|
25
|
-
expect(sorted).toEqual(values.sort((a, b) => a - b));
|
|
26
|
-
});
|
|
27
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import {Deque, ArrayDeque, ObjectDeque} from '../../../../src';
|
|
2
|
-
|
|
3
|
-
describe('Deque Tests', () => {
|
|
4
|
-
// Test cases for the Deque class (DoublyLinkedList-based)
|
|
5
|
-
describe('Deque (DoublyLinkedList-based)', () => {
|
|
6
|
-
let deque: Deque<number>;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
deque = new Deque<number>();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should add elements at the beginning and end', () => {
|
|
13
|
-
deque.addFirst(1);
|
|
14
|
-
deque.addLast(2);
|
|
15
|
-
expect(deque.peekFirst()).toBe(1);
|
|
16
|
-
expect(deque.peekLast()).toBe(2);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should remove elements from the beginning and end', () => {
|
|
20
|
-
deque.addFirst(1);
|
|
21
|
-
deque.addLast(2);
|
|
22
|
-
deque.pollFirst();
|
|
23
|
-
deque.pollLast();
|
|
24
|
-
expect(deque.isEmpty()).toBe(true);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should handle edge case when removing from an empty deque', () => {
|
|
28
|
-
const result = deque.pollFirst();
|
|
29
|
-
expect(result).toBeUndefined();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should correctly report its size', () => {
|
|
33
|
-
deque.addFirst(1);
|
|
34
|
-
deque.addLast(2);
|
|
35
|
-
expect(deque.size).toBe(2);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should handle adding and removing elements alternately', () => {
|
|
39
|
-
deque.addFirst(1);
|
|
40
|
-
expect(deque.pollFirst()).toBe(1);
|
|
41
|
-
deque.addLast(2);
|
|
42
|
-
expect(deque.pollLast()).toBe(2);
|
|
43
|
-
expect(deque.isEmpty()).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should handle adding and removing elements in a cyclic manner', () => {
|
|
47
|
-
deque.addFirst(1);
|
|
48
|
-
deque.addLast(2);
|
|
49
|
-
expect(deque.pollFirst()).toBe(1);
|
|
50
|
-
deque.addFirst(3);
|
|
51
|
-
expect(deque.pollLast()).toBe(2);
|
|
52
|
-
expect(deque.size).toBe(1);
|
|
53
|
-
});
|
|
54
|
-
// Add more test cases as needed
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// Test cases for the ObjectDeque class
|
|
58
|
-
describe('ObjectDeque', () => {
|
|
59
|
-
let objectDeque: ObjectDeque<string>;
|
|
60
|
-
|
|
61
|
-
beforeEach(() => {
|
|
62
|
-
objectDeque = new ObjectDeque<string>();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should add elements at the beginning and end', () => {
|
|
66
|
-
objectDeque.addFirst('one');
|
|
67
|
-
objectDeque.addLast('two');
|
|
68
|
-
expect(objectDeque.peekFirst()).toBe('one');
|
|
69
|
-
expect(objectDeque.peekLast()).toBe('two');
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should remove elements from the beginning and end', () => {
|
|
73
|
-
objectDeque.addFirst('one');
|
|
74
|
-
objectDeque.addLast('two');
|
|
75
|
-
objectDeque.pollFirst();
|
|
76
|
-
objectDeque.pollLast();
|
|
77
|
-
expect(objectDeque.isEmpty()).toBe(true);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('should handle edge case when removing from an empty deque', () => {
|
|
81
|
-
const result = objectDeque.pollFirst();
|
|
82
|
-
expect(result).toBeUndefined();
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('should correctly report its size', () => {
|
|
86
|
-
objectDeque.addFirst('one');
|
|
87
|
-
objectDeque.addLast('two');
|
|
88
|
-
expect(objectDeque.size).toBe(2);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// Add more test cases as needed
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// Test cases for the ArrayDeque class
|
|
95
|
-
describe('ArrayDeque', () => {
|
|
96
|
-
let arrayDeque: ArrayDeque<number>;
|
|
97
|
-
|
|
98
|
-
beforeEach(() => {
|
|
99
|
-
arrayDeque = new ArrayDeque<number>();
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should add elements at the beginning and end', () => {
|
|
103
|
-
arrayDeque.addFirst(1);
|
|
104
|
-
arrayDeque.addLast(2);
|
|
105
|
-
expect(arrayDeque.peekFirst()).toBe(1);
|
|
106
|
-
expect(arrayDeque.peekLast()).toBe(2);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should remove elements from the beginning and end', () => {
|
|
110
|
-
arrayDeque.addFirst(1);
|
|
111
|
-
arrayDeque.addLast(2);
|
|
112
|
-
arrayDeque.pollFirst();
|
|
113
|
-
arrayDeque.pollLast();
|
|
114
|
-
expect(arrayDeque.isEmpty()).toBe(true);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should handle edge case when removing from an empty deque', () => {
|
|
118
|
-
const result = arrayDeque.pollFirst();
|
|
119
|
-
expect(result).toBeNull();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should correctly report its size', () => {
|
|
123
|
-
arrayDeque.addFirst(1);
|
|
124
|
-
arrayDeque.addLast(2);
|
|
125
|
-
expect(arrayDeque.size).toBe(2);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// Add more test cases as needed
|
|
129
|
-
});
|
|
130
|
-
});
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import {Queue, LinkedListQueue} from '../../../../src';
|
|
2
|
-
import {bigO, magnitude} from '../../../utils';
|
|
3
|
-
|
|
4
|
-
describe('Queue Operation Test', () => {
|
|
5
|
-
it('should validate a queue', () => {
|
|
6
|
-
const queue = new Queue<number>();
|
|
7
|
-
for (let i = 0; i < 1000; i++) {
|
|
8
|
-
queue.enqueue(i);
|
|
9
|
-
}
|
|
10
|
-
let last: number | undefined = 0;
|
|
11
|
-
for (let i = 0; i < 1000; i++) {
|
|
12
|
-
last = queue.dequeue();
|
|
13
|
-
}
|
|
14
|
-
expect(last).toBe(999);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('Queue Performance Test', () => {
|
|
19
|
-
it('should numeric queue performance well', function () {
|
|
20
|
-
const queue = new Queue<number>();
|
|
21
|
-
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
22
|
-
queue.enqueue(i);
|
|
23
|
-
}
|
|
24
|
-
let last: number | undefined = 0;
|
|
25
|
-
|
|
26
|
-
const startTime = performance.now();
|
|
27
|
-
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
28
|
-
last = queue.dequeue();
|
|
29
|
-
}
|
|
30
|
-
expect(last).toBe(magnitude.LINEAR - 1);
|
|
31
|
-
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('Queue', () => {
|
|
36
|
-
let queue: Queue<number>;
|
|
37
|
-
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
queue = new Queue<number>();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should initialize an empty queue', () => {
|
|
43
|
-
expect(queue.size).toBe(0);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should push elements to the end of the queue', () => {
|
|
47
|
-
queue.push(1);
|
|
48
|
-
queue.push(2);
|
|
49
|
-
expect(queue.peek()).toBe(1);
|
|
50
|
-
expect(queue.size).toBe(2);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// it('should shift elements from the front of the queue', () => {
|
|
54
|
-
// queue.push(1);
|
|
55
|
-
// queue.push(2);
|
|
56
|
-
// const shifted = queue.shift();
|
|
57
|
-
// expect(shifted).toBe(1);
|
|
58
|
-
// expect(queue.peek()).toBe(2);
|
|
59
|
-
// expect(queue.size).toBe(1);
|
|
60
|
-
// });
|
|
61
|
-
//
|
|
62
|
-
// it('should peek at the front of the queue', () => {
|
|
63
|
-
// queue.push(1);
|
|
64
|
-
// queue.push(2);
|
|
65
|
-
// expect(queue.peek()).toBe(1);
|
|
66
|
-
// });
|
|
67
|
-
|
|
68
|
-
// Add more test cases for other methods of Queue.
|
|
69
|
-
});
|
|
70
|
-
describe('Queue', () => {
|
|
71
|
-
let queue: Queue<number>;
|
|
72
|
-
|
|
73
|
-
beforeEach(() => {
|
|
74
|
-
queue = new Queue<number>();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should initialize an empty queue', () => {
|
|
78
|
-
expect(queue.size).toBe(0);
|
|
79
|
-
expect(queue.isEmpty()).toBe(true);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should push elements to the end of the queue', () => {
|
|
83
|
-
queue.push(1);
|
|
84
|
-
queue.push(2);
|
|
85
|
-
expect(queue.size).toBe(2);
|
|
86
|
-
expect(queue.peek()).toBe(1);
|
|
87
|
-
expect(queue.peekLast()).toBe(2);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should shift elements from the front of the queue', () => {
|
|
91
|
-
queue.push(1);
|
|
92
|
-
queue.push(2);
|
|
93
|
-
const shifted = queue.shift();
|
|
94
|
-
expect(shifted).toBe(1);
|
|
95
|
-
expect(queue.size).toBe(1);
|
|
96
|
-
expect(queue.peek()).toBe(2);
|
|
97
|
-
expect(queue.peekLast()).toBe(2);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should handle shifting when queue reaches half size', () => {
|
|
101
|
-
for (let i = 1; i <= 5; i++) {
|
|
102
|
-
queue.push(i);
|
|
103
|
-
}
|
|
104
|
-
for (let i = 1; i <= 3; i++) {
|
|
105
|
-
queue.shift();
|
|
106
|
-
}
|
|
107
|
-
// Queue size should be 2, but internal array size is still 5.
|
|
108
|
-
// Test that shifting optimizes the internal array.
|
|
109
|
-
expect(queue.size).toBe(2);
|
|
110
|
-
expect(queue.nodes.length).toBe(2);
|
|
111
|
-
expect(queue.peek()).toBe(4);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should peek at the front and end of the queue', () => {
|
|
115
|
-
queue.push(1);
|
|
116
|
-
queue.push(2);
|
|
117
|
-
expect(queue.peek()).toBe(1);
|
|
118
|
-
expect(queue.peekLast()).toBe(2);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should handle shifting when the queue is empty', () => {
|
|
122
|
-
const shifted = queue.shift();
|
|
123
|
-
expect(shifted).toBeUndefined();
|
|
124
|
-
expect(queue.size).toBe(0);
|
|
125
|
-
expect(queue.peek()).toBeUndefined();
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should handle peeking when the queue is empty', () => {
|
|
129
|
-
expect(queue.peek()).toBeUndefined();
|
|
130
|
-
expect(queue.peekLast()).toBeUndefined();
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should handle clearing the queue', () => {
|
|
134
|
-
for (let i = 1; i <= 3; i++) {
|
|
135
|
-
queue.push(i);
|
|
136
|
-
}
|
|
137
|
-
queue.clear();
|
|
138
|
-
expect(queue.size).toBe(0);
|
|
139
|
-
expect(queue.peek()).toBeUndefined();
|
|
140
|
-
expect(queue.peekLast()).toBeUndefined();
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('should clone the queue', () => {
|
|
144
|
-
for (let i = 1; i <= 3; i++) {
|
|
145
|
-
queue.push(i);
|
|
146
|
-
}
|
|
147
|
-
const clonedQueue = queue.clone();
|
|
148
|
-
expect(clonedQueue.size).toBe(3);
|
|
149
|
-
expect(clonedQueue.peek()).toBe(1);
|
|
150
|
-
expect(clonedQueue.peekLast()).toBe(3);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('should handle creating a queue from an array', () => {
|
|
154
|
-
const elements = [1, 2, 3, 4, 5];
|
|
155
|
-
const newQueue = Queue.fromArray(elements);
|
|
156
|
-
expect(newQueue.size).toBe(5);
|
|
157
|
-
expect(newQueue.peek()).toBe(1);
|
|
158
|
-
expect(newQueue.peekLast()).toBe(5);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should iterate through the queue', () => {
|
|
162
|
-
for (let i = 1; i <= 3; i++) {
|
|
163
|
-
queue.push(i);
|
|
164
|
-
}
|
|
165
|
-
const values = Array.from(queue);
|
|
166
|
-
expect(values).toEqual([1, 2, 3]);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
describe('LinkedListQueue', () => {
|
|
170
|
-
let queue: LinkedListQueue<string>;
|
|
171
|
-
|
|
172
|
-
beforeEach(() => {
|
|
173
|
-
queue = new LinkedListQueue<string>();
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should enqueue elements to the end of the queue', () => {
|
|
177
|
-
queue.enqueue('A');
|
|
178
|
-
queue.enqueue('B');
|
|
179
|
-
expect(queue.peek()).toBe('A');
|
|
180
|
-
expect(queue.length).toBe(2);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('should dequeue elements from the front of the queue', () => {
|
|
184
|
-
queue.enqueue('A');
|
|
185
|
-
queue.enqueue('B');
|
|
186
|
-
const dequeued = queue.dequeue();
|
|
187
|
-
expect(dequeued).toBe('A');
|
|
188
|
-
expect(queue.peek()).toBe('B');
|
|
189
|
-
expect(queue.length).toBe(1);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it('should peek at the front of the queue', () => {
|
|
193
|
-
queue.enqueue('A');
|
|
194
|
-
queue.enqueue('B');
|
|
195
|
-
expect(queue.peek()).toBe('A');
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// Add more test cases for other methods of LinkedListQueue.
|
|
199
|
-
});
|