data-structure-typed 1.33.9 → 1.33.10
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/package.json +1 -1
- package/src/data-structures/binary-tree/aa-tree.ts +1 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
- package/src/data-structures/binary-tree/avl-tree.ts +307 -0
- package/src/data-structures/binary-tree/b-tree.ts +1 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -0
- package/src/data-structures/binary-tree/bst.ts +537 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +260 -0
- package/src/data-structures/binary-tree/splay-tree.ts +1 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
- package/src/data-structures/graph/abstract-graph.ts +1040 -0
- package/src/data-structures/graph/directed-graph.ts +470 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +129 -0
- package/src/data-structures/graph/undirected-graph.ts +274 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-map.ts +203 -0
- package/src/data-structures/hash/hash-table.ts +277 -0
- package/src/data-structures/hash/index.ts +7 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +212 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +32 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +166 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/priority-queue.ts +359 -0
- package/src/data-structures/queue/deque.ts +297 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +225 -0
- package/src/index.ts +4 -0
- package/src/interfaces/abstract-binary-tree.ts +189 -0
- package/src/interfaces/abstract-graph.ts +31 -0
- package/src/interfaces/avl-tree.ts +25 -0
- package/src/interfaces/binary-tree.ts +6 -0
- package/src/interfaces/bst.ts +31 -0
- package/src/interfaces/directed-graph.ts +20 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +15 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/rb-tree.ts +9 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/interfaces/tree-multiset.ts +7 -0
- package/src/interfaces/undirected-graph.ts +6 -0
- package/src/types/data-structures/abstract-binary-tree.ts +50 -0
- package/src/types/data-structures/abstract-graph.ts +11 -0
- package/src/types/data-structures/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree.ts +5 -0
- package/src/types/data-structures/bst.ts +13 -0
- package/src/types/data-structures/directed-graph.ts +8 -0
- package/src/types/data-structures/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/hash.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +16 -0
- package/src/types/data-structures/map-graph.ts +1 -0
- package/src/types/data-structures/navigator.ts +13 -0
- package/src/types/data-structures/priority-queue.ts +9 -0
- package/src/types/data-structures/rb-tree.ts +8 -0
- package/src/types/data-structures/segment-tree.ts +1 -0
- package/src/types/data-structures/singly-linked-list.ts +1 -0
- package/src/types/data-structures/tree-multiset.ts +6 -0
- package/src/types/helpers.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +79 -0
- package/test/integration/avl-tree.test.ts +108 -0
- package/test/integration/bst.test.ts +380 -0
- package/test/integration/heap.test.js +16 -0
- package/test/integration/index.html +52 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +380 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +461 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +519 -0
- package/test/unit/data-structures/graph/index.ts +2 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +45 -0
- package/test/unit/data-structures/graph/overall.test.ts +49 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +59 -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-map.test.ts +104 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +184 -0
- package/test/unit/data-structures/heap/heap.test.ts +55 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +82 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +364 -0
- package/test/unit/data-structures/linked-list/index.ts +4 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +35 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +451 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/linked-list/skip-list.test.ts +55 -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 +79 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +106 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
- package/test/unit/data-structures/queue/deque.test.ts +130 -0
- package/test/unit/data-structures/queue/queue.test.ts +199 -0
- 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/test/utils/index.ts +2 -0
- package/test/utils/magnitude.ts +21 -0
- package/test/utils/number.ts +3 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {Stack} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Stack', () => {
|
|
4
|
+
let stack: Stack<number>;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
stack = new Stack<number>();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should be empty when initialized', () => {
|
|
11
|
+
expect(stack.isEmpty()).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should push elements onto the stack', () => {
|
|
15
|
+
stack.push(1);
|
|
16
|
+
stack.push(2);
|
|
17
|
+
stack.push(3);
|
|
18
|
+
expect(stack.size()).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should peek at the top element without removing it', () => {
|
|
22
|
+
stack.push(1);
|
|
23
|
+
stack.push(2);
|
|
24
|
+
stack.push(3);
|
|
25
|
+
expect(stack.peek()).toBe(3);
|
|
26
|
+
expect(stack.size()).toBe(3);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should pop elements from the stack', () => {
|
|
30
|
+
stack.push(1);
|
|
31
|
+
stack.push(2);
|
|
32
|
+
stack.push(3);
|
|
33
|
+
const poppedElement = stack.pop();
|
|
34
|
+
expect(poppedElement).toBe(3);
|
|
35
|
+
expect(stack.size()).toBe(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should return null when popping from an empty stack', () => {
|
|
39
|
+
const poppedElement = stack.pop();
|
|
40
|
+
expect(poppedElement).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should convert the stack to an array', () => {
|
|
44
|
+
stack.push(1);
|
|
45
|
+
stack.push(2);
|
|
46
|
+
stack.push(3);
|
|
47
|
+
const stackArray = stack.toArray();
|
|
48
|
+
expect(stackArray).toEqual([1, 2, 3]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should clear all elements from the stack', () => {
|
|
52
|
+
stack.push(1);
|
|
53
|
+
stack.push(2);
|
|
54
|
+
stack.push(3);
|
|
55
|
+
stack.clear();
|
|
56
|
+
expect(stack.size()).toBe(0);
|
|
57
|
+
expect(stack.isEmpty()).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should clone the stack', () => {
|
|
61
|
+
stack.push(1);
|
|
62
|
+
stack.push(2);
|
|
63
|
+
const clonedStack = stack.clone();
|
|
64
|
+
expect(clonedStack.size()).toBe(2);
|
|
65
|
+
expect(clonedStack.pop()).toBe(2);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {TreeNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('TreeNode', () => {
|
|
4
|
+
it('should create a TreeNode with the given id and value', () => {
|
|
5
|
+
const node = new TreeNode<string>('1', 'Node 1');
|
|
6
|
+
expect(node.id).toBe('1');
|
|
7
|
+
expect(node.value).toBe('Node 1');
|
|
8
|
+
expect(node.children).toEqual([]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should add children to the TreeNode', () => {
|
|
12
|
+
const parentNode = new TreeNode<string>('1', 'Parent Node');
|
|
13
|
+
const child1 = new TreeNode<string>('2', 'Child 1');
|
|
14
|
+
const child2 = new TreeNode<string>('3', 'Child 2');
|
|
15
|
+
|
|
16
|
+
parentNode.addChildren([child1, child2]);
|
|
17
|
+
|
|
18
|
+
expect(parentNode.children).toEqual([child1, child2]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should calculate the height of the tree correctly', () => {
|
|
22
|
+
const rootNode = new TreeNode<string>('1', 'Root Node');
|
|
23
|
+
const child1 = new TreeNode<string>('2', 'Child 1');
|
|
24
|
+
const child2 = new TreeNode<string>('3', 'Child 2');
|
|
25
|
+
const grandchild1 = new TreeNode<string>('4', 'Grandchild 1');
|
|
26
|
+
const grandchild2 = new TreeNode<string>('5', 'Grandchild 2');
|
|
27
|
+
|
|
28
|
+
rootNode.addChildren([child1, child2]);
|
|
29
|
+
child1.addChildren([grandchild1]);
|
|
30
|
+
child2.addChildren([grandchild2]);
|
|
31
|
+
|
|
32
|
+
expect(rootNode.getHeight()).toBe(3); // Height of the tree should be 3
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should handle nodes without children when calculating height', () => {
|
|
36
|
+
const rootNode = new TreeNode<string>('1', 'Root Node');
|
|
37
|
+
expect(rootNode.getHeight()).toBe(1); // Height of a single node should be 1
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {Trie, TrieNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('TrieNode', () => {
|
|
4
|
+
it('should create a TrieNode with the given value', () => {
|
|
5
|
+
const node = new TrieNode('a');
|
|
6
|
+
expect(node.val).toBe('a');
|
|
7
|
+
expect(node.isEnd).toBe(false);
|
|
8
|
+
expect(node.children.size).toBe(0);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should add a child to TrieNode', () => {
|
|
12
|
+
const parentNode = new TrieNode('a');
|
|
13
|
+
const childNode = new TrieNode('b');
|
|
14
|
+
parentNode.children.set('b', childNode);
|
|
15
|
+
|
|
16
|
+
expect(parentNode.children.size).toBe(1);
|
|
17
|
+
expect(parentNode.children.get('b')).toBe(childNode);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should set isEnd property correctly', () => {
|
|
21
|
+
const node = new TrieNode('a');
|
|
22
|
+
node.isEnd = true;
|
|
23
|
+
expect(node.isEnd).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('Trie', () => {
|
|
28
|
+
it('should create an empty Trie', () => {
|
|
29
|
+
const trie = new Trie();
|
|
30
|
+
expect(trie.root.val).toBe('');
|
|
31
|
+
expect(trie.root.children.size).toBe(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should add words to Trie', () => {
|
|
35
|
+
const trie = new Trie();
|
|
36
|
+
trie.add('apple');
|
|
37
|
+
trie.add('app');
|
|
38
|
+
expect(trie.has('apple')).toBe(true);
|
|
39
|
+
expect(trie.has('app')).toBe(true);
|
|
40
|
+
expect(trie.has('banana')).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should check if a string is an absolute prefix', () => {
|
|
44
|
+
const trie = new Trie();
|
|
45
|
+
trie.add('apple');
|
|
46
|
+
trie.add('app');
|
|
47
|
+
expect(trie.isAbsPrefix('appl')).toBe(true);
|
|
48
|
+
expect(trie.isAbsPrefix('apples')).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should check if a string is a prefix', () => {
|
|
52
|
+
const trie = new Trie();
|
|
53
|
+
trie.add('apple');
|
|
54
|
+
trie.add('app');
|
|
55
|
+
expect(trie.isPrefix('app')).toBe(true);
|
|
56
|
+
expect(trie.isPrefix('banana')).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should check if a string is a common prefix', () => {
|
|
60
|
+
const trie = new Trie();
|
|
61
|
+
trie.add('apple');
|
|
62
|
+
trie.add('app');
|
|
63
|
+
expect(trie.isCommonPrefix('ap')).toBe(true);
|
|
64
|
+
expect(trie.isCommonPrefix('app')).toBe(true);
|
|
65
|
+
expect(trie.isCommonPrefix('b')).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should get the longest common prefix', () => {
|
|
69
|
+
const trie = new Trie();
|
|
70
|
+
trie.add('apple');
|
|
71
|
+
trie.add('app');
|
|
72
|
+
expect(trie.getLongestCommonPrefix()).toBe('app');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should get all words with a given prefix', () => {
|
|
76
|
+
const trie = new Trie();
|
|
77
|
+
trie.add('apple');
|
|
78
|
+
trie.add('app');
|
|
79
|
+
trie.add('application');
|
|
80
|
+
const words = trie.getAll('app');
|
|
81
|
+
expect(words).toEqual(['apple', 'application', 'app']);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should remove words from Trie', () => {
|
|
85
|
+
const trie = new Trie();
|
|
86
|
+
trie.add('apple');
|
|
87
|
+
trie.add('app');
|
|
88
|
+
expect(trie.has('apple')).toBe(true);
|
|
89
|
+
trie.remove('apple');
|
|
90
|
+
expect(trie.has('apple')).toBe(false);
|
|
91
|
+
expect(trie.has('app')).toBe(true);
|
|
92
|
+
trie.remove('app');
|
|
93
|
+
expect(trie.has('app')).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const orderReducedBy = 2; // reduction of magnitude's order compared to the baseline magnitude
|
|
2
|
+
|
|
3
|
+
export const magnitude = {
|
|
4
|
+
CONSTANT: Math.floor(Number.MAX_SAFE_INTEGER / Math.pow(10, orderReducedBy)),
|
|
5
|
+
LOG_N: Math.pow(10, 9 - orderReducedBy),
|
|
6
|
+
LINEAR: Math.pow(10, 6 - orderReducedBy),
|
|
7
|
+
N_LOG_N: Math.pow(10, 5 - orderReducedBy),
|
|
8
|
+
SQUARED: Math.pow(10, 4 - orderReducedBy),
|
|
9
|
+
CUBED: Math.pow(10, 3 - orderReducedBy),
|
|
10
|
+
FACTORIAL: 20 - orderReducedBy
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const bigO = {
|
|
14
|
+
CONSTANT: magnitude.CONSTANT / 100000,
|
|
15
|
+
LOG_N: Math.log2(magnitude.LOG_N) / 1000,
|
|
16
|
+
LINEAR: magnitude.LINEAR / 1000,
|
|
17
|
+
N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
|
|
18
|
+
SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
|
|
19
|
+
CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
|
|
20
|
+
FACTORIAL: 10000
|
|
21
|
+
};
|