data-structure-typed 1.33.0 → 1.33.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/.idea/codeStyles/Project.xml +61 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/CHANGELOG.md +1 -1
- package/README.md +197 -257
- package/coverage/coverage-final.json +63 -63
- package/coverage/coverage-summary.json +16 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +107 -2
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +30 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +5 -8
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/docs/index.html +197 -256
- package/docs/modules.html +2 -0
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +8 -18
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +5 -23
- package/lib/data-structures/binary-tree/avl-tree.d.ts +7 -10
- package/lib/data-structures/binary-tree/avl-tree.js +6 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/lib/data-structures/binary-tree/bst.d.ts +2 -2
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.js +1 -1
- package/lib/data-structures/graph/abstract-graph.d.ts +10 -10
- package/lib/data-structures/graph/abstract-graph.js +2 -2
- package/lib/data-structures/graph/directed-graph.d.ts +6 -6
- package/lib/data-structures/graph/directed-graph.js +2 -2
- package/lib/data-structures/graph/map-graph.d.ts +6 -6
- package/lib/data-structures/graph/map-graph.js +2 -2
- package/lib/data-structures/graph/undirected-graph.d.ts +6 -6
- package/lib/data-structures/graph/undirected-graph.js +2 -2
- package/lib/data-structures/hash/hash-table.d.ts +61 -1
- package/lib/data-structures/hash/hash-table.js +136 -0
- package/lib/data-structures/heap/heap.d.ts +31 -31
- package/lib/data-structures/heap/heap.js +11 -11
- package/lib/data-structures/heap/max-heap.d.ts +4 -4
- package/lib/data-structures/heap/max-heap.js +1 -1
- package/lib/data-structures/heap/min-heap.d.ts +4 -4
- package/lib/data-structures/heap/min-heap.js +1 -1
- package/lib/data-structures/linked-list/doubly-linked-list.d.ts +93 -54
- package/lib/data-structures/linked-list/doubly-linked-list.js +79 -22
- package/lib/data-structures/linked-list/singly-linked-list.d.ts +46 -46
- package/lib/data-structures/linked-list/singly-linked-list.js +20 -20
- package/lib/data-structures/matrix/matrix.d.ts +3 -3
- package/lib/data-structures/matrix/matrix2d.d.ts +1 -1
- package/lib/data-structures/matrix/matrix2d.js +3 -2
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/priority-queue.d.ts +26 -26
- package/lib/data-structures/priority-queue/priority-queue.js +8 -8
- package/lib/data-structures/queue/deque.d.ts +30 -30
- package/lib/data-structures/queue/deque.js +7 -7
- package/lib/data-structures/queue/queue.d.ts +27 -27
- package/lib/data-structures/queue/queue.js +8 -8
- package/lib/data-structures/stack/stack.d.ts +15 -15
- package/lib/data-structures/stack/stack.js +6 -6
- package/lib/data-structures/tree/tree.d.ts +7 -7
- package/lib/interfaces/abstract-binary-tree.d.ts +0 -1
- package/lib/interfaces/avl-tree.d.ts +1 -1
- package/lib/types/data-structures/navigator.d.ts +1 -1
- package/package.json +3 -5
- package/test/integration/index.html +6 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +146 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +44 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +54 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +41 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +97 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -3
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +53 -0
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +138 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +80 -0
- package/test/unit/data-structures/queue/deque.test.ts +131 -0
- package/test/unit/data-structures/queue/queue.test.ts +168 -1
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +95 -0
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
<!DOCTYPE html>
|
|
2
|
-
<html lang=
|
|
2
|
+
<html lang='en'>
|
|
3
3
|
<head>
|
|
4
|
-
<meta charset=
|
|
4
|
+
<meta charset='UTF-8'>
|
|
5
5
|
<title>CDN Test</title>
|
|
6
|
-
<script src=
|
|
6
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/umd/bundle.min.js'></script>
|
|
7
7
|
</head>
|
|
8
8
|
<body>
|
|
9
|
-
<div id=
|
|
10
|
-
<ul class=
|
|
9
|
+
<div id='app'>
|
|
10
|
+
<ul class='modules'>
|
|
11
11
|
|
|
12
12
|
</ul>
|
|
13
13
|
</div>
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
last = queue.dequeue();
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
console.log(performance.now() - startTime)
|
|
39
|
+
console.log(performance.now() - startTime);
|
|
40
40
|
</script>
|
|
41
41
|
|
|
42
42
|
</body>
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {BinaryTreeNode} from '../../../../src';
|
|
2
|
+
import {BinaryTree} from 'binary-tree-typed';
|
|
3
|
+
|
|
4
|
+
describe('BinaryTreeNode', () => {
|
|
5
|
+
it('should create an instance of BinaryTreeNode', () => {
|
|
6
|
+
const node = new BinaryTreeNode<number>(1);
|
|
7
|
+
expect(node).toBeInstanceOf(BinaryTreeNode);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should set and get the ID correctly', () => {
|
|
11
|
+
const node = new BinaryTreeNode<number>(1);
|
|
12
|
+
expect(node.id).toBe(1);
|
|
13
|
+
|
|
14
|
+
node.id = 2;
|
|
15
|
+
expect(node.id).toBe(2);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should set and get the value correctly', () => {
|
|
19
|
+
const node = new BinaryTreeNode<number>(1, 42);
|
|
20
|
+
expect(node.val).toBe(42);
|
|
21
|
+
|
|
22
|
+
node.val = 55;
|
|
23
|
+
expect(node.val).toBe(55);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should set and get the left child correctly', () => {
|
|
27
|
+
const node1 = new BinaryTreeNode<number>(1);
|
|
28
|
+
const node2 = new BinaryTreeNode<number>(2);
|
|
29
|
+
|
|
30
|
+
node1.left = node2;
|
|
31
|
+
|
|
32
|
+
expect(node1.left).toBe(node2);
|
|
33
|
+
expect(node2.parent).toBe(node1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should set and get the right child correctly', () => {
|
|
37
|
+
const node1 = new BinaryTreeNode<number>(1);
|
|
38
|
+
const node2 = new BinaryTreeNode<number>(2);
|
|
39
|
+
|
|
40
|
+
node1.right = node2;
|
|
41
|
+
|
|
42
|
+
expect(node1.right).toBe(node2);
|
|
43
|
+
expect(node2.parent).toBe(node1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should set and get the parent correctly', () => {
|
|
47
|
+
const node1 = new BinaryTreeNode<number>(1);
|
|
48
|
+
const node2 = new BinaryTreeNode<number>(2);
|
|
49
|
+
|
|
50
|
+
node1.left = node2;
|
|
51
|
+
|
|
52
|
+
expect(node2.parent).toBe(node1);
|
|
53
|
+
expect(node1.left).toBe(node2);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should set and get the height correctly', () => {
|
|
57
|
+
const node = new BinaryTreeNode<number>(1);
|
|
58
|
+
expect(node.height).toBe(0);
|
|
59
|
+
|
|
60
|
+
node.height = 3;
|
|
61
|
+
expect(node.height).toBe(3);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should determine family position correctly', () => {
|
|
65
|
+
const root = new BinaryTreeNode<number>(1);
|
|
66
|
+
const leftChild = new BinaryTreeNode<number>(2);
|
|
67
|
+
const rightChild = new BinaryTreeNode<number>(3);
|
|
68
|
+
|
|
69
|
+
root.left = leftChild;
|
|
70
|
+
root.right = rightChild;
|
|
71
|
+
|
|
72
|
+
expect(leftChild.familyPosition).toBe('LEFT');
|
|
73
|
+
expect(rightChild.familyPosition).toBe('RIGHT');
|
|
74
|
+
expect(root.familyPosition).toBe('ROOT');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
describe('BinaryTree', () => {
|
|
80
|
+
let binaryTree: BinaryTree;
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
binaryTree = new BinaryTree();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterEach(() => {
|
|
87
|
+
binaryTree.clear();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('should add a node', () => {
|
|
91
|
+
const node = binaryTree.add(1);
|
|
92
|
+
expect(node).not.toBeNull();
|
|
93
|
+
expect(binaryTree.size).toBe(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('should remove a node', () => {
|
|
97
|
+
const node = binaryTree.add(1);
|
|
98
|
+
expect(binaryTree.size).toBe(1);
|
|
99
|
+
|
|
100
|
+
if (node) {
|
|
101
|
+
const result = binaryTree.remove(node);
|
|
102
|
+
expect(result).toHaveLength(1);
|
|
103
|
+
expect(binaryTree.size).toBe(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
test('should add and find nodes', () => {
|
|
110
|
+
binaryTree.add(1);
|
|
111
|
+
binaryTree.add(2);
|
|
112
|
+
binaryTree.add(3);
|
|
113
|
+
|
|
114
|
+
expect(binaryTree.has(1)).toBe(true);
|
|
115
|
+
expect(binaryTree.has(2)).toBe(true);
|
|
116
|
+
expect(binaryTree.has(3)).toBe(true);
|
|
117
|
+
expect(binaryTree.has(4)).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('should traverse in-order', () => {
|
|
121
|
+
binaryTree.add(4);
|
|
122
|
+
binaryTree.add(2);
|
|
123
|
+
binaryTree.add(6);
|
|
124
|
+
binaryTree.add(1);
|
|
125
|
+
binaryTree.add(3);
|
|
126
|
+
binaryTree.add(5);
|
|
127
|
+
binaryTree.add(7);
|
|
128
|
+
|
|
129
|
+
const inOrder = binaryTree.DFS('in');
|
|
130
|
+
|
|
131
|
+
expect(inOrder).toEqual([1, 2, 3, 4, 5, 6, 7]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('should clear the tree', () => {
|
|
135
|
+
binaryTree.add(1);
|
|
136
|
+
binaryTree.add(2);
|
|
137
|
+
|
|
138
|
+
expect(binaryTree.size).toBe(2);
|
|
139
|
+
|
|
140
|
+
binaryTree.clear();
|
|
141
|
+
|
|
142
|
+
expect(binaryTree.size).toBe(0);
|
|
143
|
+
expect(binaryTree.root).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// import {RBTree, RBTreeNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Red-Black Tree Tests', () => {
|
|
4
|
+
// let tree: RBTree<RBTreeNode<number>>;
|
|
5
|
+
//
|
|
6
|
+
// beforeEach(() => {
|
|
7
|
+
// tree = new RBTree<RBTreeNode<number>>();
|
|
8
|
+
// });
|
|
9
|
+
|
|
10
|
+
test('Insertion and In-order Traversal', () => {
|
|
11
|
+
// tree.add(5);
|
|
12
|
+
// tree.add(3);
|
|
13
|
+
// tree.add(7);
|
|
14
|
+
// tree.add(2);
|
|
15
|
+
// tree.add(4);
|
|
16
|
+
// tree.add(6);
|
|
17
|
+
// tree.add(8);
|
|
18
|
+
//
|
|
19
|
+
// const inOrderTraversal: number[] = tree.DFS('in')
|
|
20
|
+
//
|
|
21
|
+
// expect(inOrderTraversal).toEqual([2, 3, 4, 5, 6, 7, 8]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('Deletion', () => {
|
|
25
|
+
// tree.add(5);
|
|
26
|
+
// tree.add(3);
|
|
27
|
+
// tree.add(7);
|
|
28
|
+
// tree.add(2);
|
|
29
|
+
// tree.add(4);
|
|
30
|
+
// tree.add(6);
|
|
31
|
+
// tree.add(8);
|
|
32
|
+
//
|
|
33
|
+
// // Delete a node (e.g., 3) and check if it's gone
|
|
34
|
+
// tree.remove(3);
|
|
35
|
+
// expect(tree.has(3)).toBe(false);
|
|
36
|
+
//
|
|
37
|
+
// // Perform in-order traversal to check if the tree is still balanced
|
|
38
|
+
// const inOrderTraversal: number[] = tree.DFS('in');
|
|
39
|
+
//
|
|
40
|
+
//
|
|
41
|
+
// expect(inOrderTraversal).toEqual([2, 4, 5, 6, 7, 8]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {CoordinateMap} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('CoordinateMap', () => {
|
|
4
|
+
it('should set and get values correctly', () => {
|
|
5
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
6
|
+
const key = [1, 2, 3];
|
|
7
|
+
const value = 'TestValue';
|
|
8
|
+
|
|
9
|
+
coordinateMap.set(key, value);
|
|
10
|
+
const retrievedValue = coordinateMap.get(key);
|
|
11
|
+
|
|
12
|
+
expect(retrievedValue).toBe(value);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should return true when key exists', () => {
|
|
16
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
17
|
+
const key = [1, 2, 3];
|
|
18
|
+
const value = 'TestValue';
|
|
19
|
+
|
|
20
|
+
coordinateMap.set(key, value);
|
|
21
|
+
|
|
22
|
+
expect(coordinateMap.has(key)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return false when key does not exist', () => {
|
|
26
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
27
|
+
const key = [1, 2, 3];
|
|
28
|
+
|
|
29
|
+
expect(coordinateMap.has(key)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should delete key-value pair correctly', () => {
|
|
33
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
34
|
+
const key = [1, 2, 3];
|
|
35
|
+
const value = 'TestValue';
|
|
36
|
+
|
|
37
|
+
coordinateMap.set(key, value);
|
|
38
|
+
coordinateMap.delete(key);
|
|
39
|
+
|
|
40
|
+
expect(coordinateMap.has(key)).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should allow changing the joint character', () => {
|
|
44
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
45
|
+
const key = [1, 2, 3];
|
|
46
|
+
const value = 'TestValue';
|
|
47
|
+
|
|
48
|
+
coordinateMap.set(key, value);
|
|
49
|
+
const newKey = [1, 2, 3];
|
|
50
|
+
const retrievedValue = coordinateMap.get(newKey);
|
|
51
|
+
|
|
52
|
+
expect(retrievedValue).toBe(value);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {CoordinateSet} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('CoordinateSet', () => {
|
|
4
|
+
it('should add and check values correctly', () => {
|
|
5
|
+
const coordinateSet = new CoordinateSet();
|
|
6
|
+
const value = [1, 2, 3];
|
|
7
|
+
|
|
8
|
+
coordinateSet.add(value);
|
|
9
|
+
const hasValue = coordinateSet.has(value);
|
|
10
|
+
|
|
11
|
+
expect(hasValue).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return false when value does not exist', () => {
|
|
15
|
+
const coordinateSet = new CoordinateSet();
|
|
16
|
+
const value = [1, 2, 3];
|
|
17
|
+
|
|
18
|
+
expect(coordinateSet.has(value)).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should delete value correctly', () => {
|
|
22
|
+
const coordinateSet = new CoordinateSet();
|
|
23
|
+
const value = [1, 2, 3];
|
|
24
|
+
|
|
25
|
+
coordinateSet.add(value);
|
|
26
|
+
coordinateSet.delete(value);
|
|
27
|
+
|
|
28
|
+
expect(coordinateSet.has(value)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should allow changing the joint character', () => {
|
|
32
|
+
const coordinateSet = new CoordinateSet();
|
|
33
|
+
const value = [1, 2, 3];
|
|
34
|
+
|
|
35
|
+
coordinateSet.add(value);
|
|
36
|
+
const newValue = [1, 2, 3];
|
|
37
|
+
const hasValue = coordinateSet.has(newValue);
|
|
38
|
+
|
|
39
|
+
expect(hasValue).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {HashNode, HashTable} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('HashNode', () => {
|
|
4
|
+
it('should create a HashNode with key and value', () => {
|
|
5
|
+
const key = 'testKey';
|
|
6
|
+
const value = 'testValue';
|
|
7
|
+
const hashNode = new HashNode(key, value);
|
|
8
|
+
|
|
9
|
+
expect(hashNode.key).toBe(key);
|
|
10
|
+
expect(hashNode.val).toBe(value);
|
|
11
|
+
expect(hashNode.next).toBe(null);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('HashTable', () => {
|
|
16
|
+
it('should initialize with default capacity', () => {
|
|
17
|
+
const hashTable = new HashTable<string, string>();
|
|
18
|
+
|
|
19
|
+
expect(hashTable.capacity).toBe(1000);
|
|
20
|
+
expect(hashTable.size).toBe(0);
|
|
21
|
+
expect(hashTable.buckets.length).toBe(1000);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should initialize with custom capacity', () => {
|
|
25
|
+
const customCapacity = 500;
|
|
26
|
+
const hashTable = new HashTable<string, string>(customCapacity);
|
|
27
|
+
|
|
28
|
+
expect(hashTable.capacity).toBe(customCapacity);
|
|
29
|
+
expect(hashTable.size).toBe(0);
|
|
30
|
+
expect(hashTable.buckets.length).toBe(customCapacity);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should put and get values correctly', () => {
|
|
34
|
+
const hashTable = new HashTable<string, string>();
|
|
35
|
+
const key = 'testKey';
|
|
36
|
+
const value = 'testValue';
|
|
37
|
+
|
|
38
|
+
hashTable.put(key, value);
|
|
39
|
+
const retrievedValue = hashTable.get(key);
|
|
40
|
+
|
|
41
|
+
expect(retrievedValue).toBe(value);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should handle collisions by chaining', () => {
|
|
45
|
+
const hashTable = new HashTable<string, string>();
|
|
46
|
+
const key1 = 'testKey1';
|
|
47
|
+
const value1 = 'testValue1';
|
|
48
|
+
const key2 = 'testKey2';
|
|
49
|
+
const value2 = 'testValue2';
|
|
50
|
+
|
|
51
|
+
hashTable.put(key1, value1);
|
|
52
|
+
hashTable.put(key2, value2);
|
|
53
|
+
|
|
54
|
+
const retrievedValue1 = hashTable.get(key1);
|
|
55
|
+
const retrievedValue2 = hashTable.get(key2);
|
|
56
|
+
|
|
57
|
+
expect(retrievedValue1).toBe(value1);
|
|
58
|
+
expect(retrievedValue2).toBe(value2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should update value for an existing key', () => {
|
|
62
|
+
const hashTable = new HashTable<string, string>();
|
|
63
|
+
const key = 'testKey';
|
|
64
|
+
const initialValue = 'testValue1';
|
|
65
|
+
const updatedValue = 'testValue2';
|
|
66
|
+
|
|
67
|
+
hashTable.put(key, initialValue);
|
|
68
|
+
hashTable.put(key, updatedValue);
|
|
69
|
+
|
|
70
|
+
const retrievedValue = hashTable.get(key);
|
|
71
|
+
|
|
72
|
+
expect(retrievedValue).toBe(updatedValue);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should return undefined for non-existent key', () => {
|
|
76
|
+
const hashTable = new HashTable<string, string>();
|
|
77
|
+
const key = 'nonExistentKey';
|
|
78
|
+
|
|
79
|
+
const retrievedValue = hashTable.get(key);
|
|
80
|
+
|
|
81
|
+
expect(retrievedValue).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should remove key-value pair correctly', () => {
|
|
85
|
+
const hashTable = new HashTable<string, string>();
|
|
86
|
+
const key = 'testKey';
|
|
87
|
+
const value = 'testValue';
|
|
88
|
+
|
|
89
|
+
hashTable.put(key, value);
|
|
90
|
+
hashTable.remove(key);
|
|
91
|
+
|
|
92
|
+
const retrievedValue = hashTable.get(key);
|
|
93
|
+
|
|
94
|
+
expect(retrievedValue).toBeUndefined();
|
|
95
|
+
expect(hashTable.size).toBe(0);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -21,7 +21,6 @@ describe('LinkedList Performance Test', () => {
|
|
|
21
21
|
const singlyList = new SinglyLinkedList<number>();
|
|
22
22
|
let midSinglyNode: SinglyLinkedListNode | null = null;
|
|
23
23
|
|
|
24
|
-
const startSinglyPushTime = performance.now();
|
|
25
24
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
26
25
|
singlyList.push(i);
|
|
27
26
|
if (i === midIndex) {
|
|
@@ -31,7 +30,6 @@ describe('LinkedList Performance Test', () => {
|
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
expect(doublyListPushCost).toBeLessThan(bigO.SQUARED * 2);
|
|
33
|
+
expect(doublyListPushCost).toBeLessThan(bigO.SQUARED * 5);
|
|
36
34
|
});
|
|
37
35
|
});
|
|
@@ -399,3 +399,56 @@ describe('SinglyLinkedList Performance Test', () => {
|
|
|
399
399
|
expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 300);
|
|
400
400
|
});
|
|
401
401
|
});
|
|
402
|
+
describe('SinglyLinkedList', () => {
|
|
403
|
+
let list: SinglyLinkedList<number>;
|
|
404
|
+
|
|
405
|
+
beforeEach(() => {
|
|
406
|
+
list = new SinglyLinkedList<number>();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('should initialize an empty list', () => {
|
|
410
|
+
expect(list.head).toBeNull();
|
|
411
|
+
expect(list.tail).toBeNull();
|
|
412
|
+
expect(list.length).toBe(0);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('should push elements to the end of the list', () => {
|
|
416
|
+
list.push(1);
|
|
417
|
+
list.push(2);
|
|
418
|
+
expect(list.head!.val).toBe(1);
|
|
419
|
+
expect(list.tail!.val).toBe(2);
|
|
420
|
+
expect(list.length).toBe(2);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it('should pop elements from the end of the list', () => {
|
|
424
|
+
list.push(1);
|
|
425
|
+
list.push(2);
|
|
426
|
+
const popped = list.pop();
|
|
427
|
+
expect(popped).toBe(2);
|
|
428
|
+
expect(list.head!.val).toBe(1);
|
|
429
|
+
expect(list.tail!.val).toBe(1);
|
|
430
|
+
expect(list.length).toBe(1);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// Add more test cases for other methods like shift, unshift, getAt, deleteAt, and more.
|
|
434
|
+
|
|
435
|
+
it('should reverse the list', () => {
|
|
436
|
+
list.push(1);
|
|
437
|
+
list.push(2);
|
|
438
|
+
list.push(3);
|
|
439
|
+
list.reverse();
|
|
440
|
+
expect(list.head!.val).toBe(3);
|
|
441
|
+
expect(list.tail!.val).toBe(1);
|
|
442
|
+
// Add more assertions for reversed order.
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
// Add more test cases for other methods like find, indexOf, and more.
|
|
446
|
+
|
|
447
|
+
it('should convert the list to an array', () => {
|
|
448
|
+
list.push(1);
|
|
449
|
+
list.push(2);
|
|
450
|
+
list.push(3);
|
|
451
|
+
const array = list.toArray();
|
|
452
|
+
expect(array).toEqual([1, 2, 3]);
|
|
453
|
+
});
|
|
454
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {MatrixNTI2D} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('MatrixNTI2D', () => {
|
|
4
|
+
it('should initialize a matrix with rows and columns', () => {
|
|
5
|
+
const numRows = 3;
|
|
6
|
+
const numCols = 4;
|
|
7
|
+
const matrix = new MatrixNTI2D({ row: numRows, col: numCols });
|
|
8
|
+
|
|
9
|
+
expect(matrix.toArray().length).toBe(numRows);
|
|
10
|
+
expect(matrix.toArray()[0].length).toBe(numCols);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should initialize all elements with the provided initial value', () => {
|
|
14
|
+
const numRows = 3;
|
|
15
|
+
const numCols = 4;
|
|
16
|
+
const initialValue = 42;
|
|
17
|
+
const matrix = new MatrixNTI2D({ row: numRows, col: numCols, initialVal: initialValue });
|
|
18
|
+
|
|
19
|
+
const matrixArray = matrix.toArray();
|
|
20
|
+
for (let i = 0; i < numRows; i++) {
|
|
21
|
+
for (let j = 0; j < numCols; j++) {
|
|
22
|
+
expect(matrixArray[i][j]).toBe(initialValue);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should initialize all elements with 0 if no initial value is provided', () => {
|
|
28
|
+
const numRows = 3;
|
|
29
|
+
const numCols = 4;
|
|
30
|
+
const matrix = new MatrixNTI2D({ row: numRows, col: numCols });
|
|
31
|
+
|
|
32
|
+
const matrixArray = matrix.toArray();
|
|
33
|
+
for (let i = 0; i < numRows; i++) {
|
|
34
|
+
for (let j = 0; j < numCols; j++) {
|
|
35
|
+
expect(matrixArray[i][j]).toBe(0);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should convert the matrix to a two-dimensional array', () => {
|
|
41
|
+
const numRows = 2;
|
|
42
|
+
const numCols = 3;
|
|
43
|
+
const matrix = new MatrixNTI2D({ row: numRows, col: numCols, initialVal: 1 });
|
|
44
|
+
|
|
45
|
+
const matrixArray = matrix.toArray();
|
|
46
|
+
expect(matrixArray.length).toBe(numRows);
|
|
47
|
+
for (let i = 0; i < numRows; i++) {
|
|
48
|
+
expect(matrixArray[i].length).toBe(numCols);
|
|
49
|
+
for (let j = 0; j < numCols; j++) {
|
|
50
|
+
expect(matrixArray[i][j]).toBe(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {Matrix2D, Vector2D} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Matrix2D', () => {
|
|
4
|
+
it('should initialize with default identity matrix', () => {
|
|
5
|
+
const matrix = new Matrix2D();
|
|
6
|
+
const expectedMatrix = Matrix2D.identity;
|
|
7
|
+
|
|
8
|
+
expect(matrix.m).toEqual(expectedMatrix);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should initialize with provided 2D array', () => {
|
|
12
|
+
const inputMatrix = [
|
|
13
|
+
[2, 0, 0],
|
|
14
|
+
[0, 3, 0],
|
|
15
|
+
[0, 0, 1]
|
|
16
|
+
];
|
|
17
|
+
const matrix = new Matrix2D(inputMatrix);
|
|
18
|
+
|
|
19
|
+
expect(matrix.m).toEqual(inputMatrix);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should initialize with provided Vector2D', () => {
|
|
23
|
+
expect(true).toBeTruthy();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should add two matrices correctly', () => {
|
|
27
|
+
const matrix1 = new Matrix2D([
|
|
28
|
+
[1, 2, 3],
|
|
29
|
+
[4, 5, 6],
|
|
30
|
+
[7, 8, 9]
|
|
31
|
+
]);
|
|
32
|
+
const matrix2 = new Matrix2D([
|
|
33
|
+
[9, 8, 7],
|
|
34
|
+
[6, 5, 4],
|
|
35
|
+
[3, 2, 1]
|
|
36
|
+
]);
|
|
37
|
+
const expectedMatrix = [
|
|
38
|
+
[10, 10, 10],
|
|
39
|
+
[10, 10, 10],
|
|
40
|
+
[10, 10, 10]
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const result = Matrix2D.add(matrix1, matrix2);
|
|
44
|
+
|
|
45
|
+
expect(result.m).toEqual(expectedMatrix);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should subtract two matrices correctly', () => {
|
|
49
|
+
const matrix1 = new Matrix2D([
|
|
50
|
+
[9, 8, 7],
|
|
51
|
+
[6, 5, 4],
|
|
52
|
+
[3, 2, 1]
|
|
53
|
+
]);
|
|
54
|
+
const matrix2 = new Matrix2D([
|
|
55
|
+
[1, 2, 3],
|
|
56
|
+
[4, 5, 6],
|
|
57
|
+
[7, 8, 9]
|
|
58
|
+
]);
|
|
59
|
+
const expectedMatrix = [
|
|
60
|
+
[8, 6, 4],
|
|
61
|
+
[2, 0, -2],
|
|
62
|
+
[-4, -6, -8]
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const result = Matrix2D.subtract(matrix1, matrix2);
|
|
66
|
+
|
|
67
|
+
expect(result.m).toEqual(expectedMatrix);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should multiply two matrices correctly', () => {
|
|
71
|
+
const matrix1 = new Matrix2D([
|
|
72
|
+
[1, 2, 3],
|
|
73
|
+
[4, 5, 6],
|
|
74
|
+
[7, 8, 9]
|
|
75
|
+
]);
|
|
76
|
+
const matrix2 = new Matrix2D([
|
|
77
|
+
[9, 8, 7],
|
|
78
|
+
[6, 5, 4],
|
|
79
|
+
[3, 2, 1]
|
|
80
|
+
]);
|
|
81
|
+
const expectedMatrix = [
|
|
82
|
+
[30, 24, 18],
|
|
83
|
+
[84, 69, 54],
|
|
84
|
+
[138, 114, 90]
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const result = Matrix2D.multiply(matrix1, matrix2);
|
|
88
|
+
|
|
89
|
+
expect(result.m).toEqual(expectedMatrix);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should multiply a matrix by a Vector2D correctly', () => {
|
|
93
|
+
expect(true).toBeTruthy();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should scale a matrix by a value correctly', () => {
|
|
97
|
+
expect(true).toBeTruthy();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should rotate a matrix by radians correctly', () => {
|
|
101
|
+
expect(true).toBeTruthy();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should translate a matrix by a Vector2D correctly', () => {
|
|
105
|
+
const translationVector = new Vector2D(2, 3);
|
|
106
|
+
const expectedMatrix = [
|
|
107
|
+
[1, 0, 2],
|
|
108
|
+
[0, 1, 3],
|
|
109
|
+
[0, 0, 1]
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
const result = Matrix2D.translate(translationVector);
|
|
113
|
+
|
|
114
|
+
expect(result.m).toEqual(expectedMatrix);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should create a view matrix correctly', () => {
|
|
118
|
+
expect(true).toBeTruthy();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should multiply a matrix by a value correctly', () => {
|
|
122
|
+
const matrix = new Matrix2D([
|
|
123
|
+
[1, 2, 3],
|
|
124
|
+
[4, 5, 6],
|
|
125
|
+
[7, 8, 9]
|
|
126
|
+
]);
|
|
127
|
+
const value = 2;
|
|
128
|
+
const expectedMatrix = [
|
|
129
|
+
[2, 4, 6],
|
|
130
|
+
[8, 10, 12],
|
|
131
|
+
[14, 16, 18]
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const result = Matrix2D.multiplyByValue(matrix, value);
|
|
135
|
+
|
|
136
|
+
expect(result.m).toEqual(expectedMatrix);
|
|
137
|
+
});
|
|
138
|
+
});
|