data-structure-typed 1.42.9 → 1.43.1
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 +27 -18
- package/benchmark/report.html +21 -12
- package/benchmark/report.json +148 -103
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +30 -38
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +30 -38
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +74 -80
- package/test/config.ts +2 -1
- package/test/integration/index.html +30 -11
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +21 -8
- package/test/performance/data-structures/hash/hash-map.test.ts +51 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +12 -1
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +37 -0
- package/test/performance/data-structures/queue/deque.test.ts +11 -1
- package/test/performance/data-structures/queue/queue.test.ts +12 -1
- package/test/performance/data-structures/stack/stack.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +54 -3
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src';
|
|
2
|
+
import {LinkList as CLinkedList} from 'js-sdsl';
|
|
2
3
|
import * as Benchmark from 'benchmark';
|
|
3
4
|
import {magnitude} from '../../../utils';
|
|
5
|
+
import {isCompetitor} from "../../../config";
|
|
4
6
|
|
|
5
7
|
const suite = new Benchmark.Suite();
|
|
6
8
|
const {LINEAR} = magnitude;
|
|
@@ -13,7 +15,16 @@ suite
|
|
|
13
15
|
list.unshift(i);
|
|
14
16
|
}
|
|
15
17
|
})
|
|
16
|
-
|
|
18
|
+
if (isCompetitor) {
|
|
19
|
+
suite.add(`${LINEAR.toLocaleString()} competitor unshift`, () => {
|
|
20
|
+
const list = new CLinkedList<number>();
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
23
|
+
list.pushFront(i);
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
|
|
17
28
|
const list = new DoublyLinkedList<number>();
|
|
18
29
|
|
|
19
30
|
for (let i = 0; i < LINEAR; i++) {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {PriorityQueue as CPriorityQueue} from 'js-sdsl';
|
|
2
|
+
import {PriorityQueue} from '../../../../src';
|
|
3
|
+
import * as Benchmark from 'benchmark';
|
|
4
|
+
import {magnitude} from '../../../utils';
|
|
5
|
+
import {isCompetitor} from "../../../config";
|
|
6
|
+
|
|
7
|
+
const suite = new Benchmark.Suite();
|
|
8
|
+
const {TEN_THOUSAND} = magnitude;
|
|
9
|
+
|
|
10
|
+
suite
|
|
11
|
+
.add(`${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
12
|
+
const pq = new PriorityQueue<number>({comparator: (a, b) => b - a});
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < TEN_THOUSAND; i++) {
|
|
15
|
+
pq.add(i);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < TEN_THOUSAND; i++) {
|
|
19
|
+
pq.pop();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
if (isCompetitor) {
|
|
23
|
+
suite.add(`${TEN_THOUSAND.toLocaleString()} competitor add & pop`, () => {
|
|
24
|
+
const pq = new CPriorityQueue<number>();
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < TEN_THOUSAND; i++) {
|
|
27
|
+
pq.push(i);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < TEN_THOUSAND; i++) {
|
|
31
|
+
pq.pop();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export {suite};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {Deque} from '../../../../src';
|
|
2
|
+
import {Deque as CDeque} from 'js-sdsl';
|
|
2
3
|
import * as Benchmark from 'benchmark';
|
|
3
4
|
import {magnitude} from '../../../utils';
|
|
5
|
+
import {isCompetitor} from "../../../config";
|
|
4
6
|
|
|
5
7
|
export const suite = new Benchmark.Suite();
|
|
6
8
|
const {LINEAR} = magnitude;
|
|
@@ -12,7 +14,15 @@ suite
|
|
|
12
14
|
deque.push(i);
|
|
13
15
|
}
|
|
14
16
|
})
|
|
15
|
-
|
|
17
|
+
if (isCompetitor) {
|
|
18
|
+
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
|
19
|
+
const deque = new CDeque<number>();
|
|
20
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
21
|
+
deque.pushBack(i);
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
suite.add(`${LINEAR.toLocaleString()} shift`, () => {
|
|
16
26
|
const deque = new Deque<number>();
|
|
17
27
|
for (let i = 0; i < LINEAR; i++) {
|
|
18
28
|
deque.push(i);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {Queue} from '../../../../src';
|
|
2
|
+
import {Queue as CQueue} from 'js-sdsl';
|
|
2
3
|
import * as Benchmark from 'benchmark';
|
|
3
4
|
import {magnitude} from '../../../utils';
|
|
5
|
+
import {isCompetitor} from "../../../config";
|
|
4
6
|
|
|
5
7
|
const suite = new Benchmark.Suite();
|
|
6
8
|
const {LINEAR} = magnitude;
|
|
@@ -13,7 +15,16 @@ suite
|
|
|
13
15
|
queue.push(i);
|
|
14
16
|
}
|
|
15
17
|
})
|
|
16
|
-
|
|
18
|
+
if (isCompetitor) {
|
|
19
|
+
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
|
20
|
+
const queue = new CQueue<number>();
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
23
|
+
queue.push(i);
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
|
|
17
28
|
const queue = new Queue<number>();
|
|
18
29
|
|
|
19
30
|
for (let i = 0; i < LINEAR; i++) {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {Stack} from '../../../../src';
|
|
2
|
+
import {Stack as CStack} from 'js-sdsl';
|
|
3
|
+
import * as Benchmark from 'benchmark';
|
|
4
|
+
import {magnitude} from '../../../utils';
|
|
5
|
+
import {isCompetitor} from "../../../config";
|
|
6
|
+
|
|
7
|
+
const suite = new Benchmark.Suite();
|
|
8
|
+
const {LINEAR} = magnitude;
|
|
9
|
+
|
|
10
|
+
suite
|
|
11
|
+
.add(`${LINEAR.toLocaleString()} push`, () => {
|
|
12
|
+
const stack = new Stack<number>();
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
15
|
+
stack.push(i);
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
if (isCompetitor) {
|
|
19
|
+
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
|
20
|
+
const queue = new CStack<number>();
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
23
|
+
queue.push(i);
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
|
|
28
|
+
const queue = new Stack<number>();
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
31
|
+
queue.push(i);
|
|
32
|
+
}
|
|
33
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
34
|
+
queue.pop();
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
if (isCompetitor) {
|
|
38
|
+
suite.add(`${LINEAR.toLocaleString()} competitor push & pop`, () => {
|
|
39
|
+
const queue = new CStack<number>();
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
42
|
+
queue.push(i);
|
|
43
|
+
}
|
|
44
|
+
for (let i = 0; i < LINEAR; i++) {
|
|
45
|
+
queue.pop();
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export {suite};
|
|
@@ -7,6 +7,7 @@ describe('AVL Tree Test', () => {
|
|
|
7
7
|
|
|
8
8
|
for (const i of arr) tree.add(i, i);
|
|
9
9
|
|
|
10
|
+
tree.add(null);
|
|
10
11
|
const node6 = tree.getNode(6);
|
|
11
12
|
|
|
12
13
|
expect(node6 && tree.getHeight(node6)).toBe(3);
|
|
@@ -42,7 +43,7 @@ describe('AVL Tree Test', () => {
|
|
|
42
43
|
expect(bfs[0].key).toBe(8);
|
|
43
44
|
expect(bfs[bfs.length - 1].key).toBe(16);
|
|
44
45
|
|
|
45
|
-
expect(tree.delete(11)[0].deleted?.key).toBe(11);
|
|
46
|
+
expect(tree.delete(tree.getNode(11))[0].deleted?.key).toBe(11);
|
|
46
47
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
47
48
|
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
48
49
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
|
|
2
2
|
import {getRandomIntArray} from "../../../utils";
|
|
3
|
+
import {FamilyPosition} from "binary-tree-typed";
|
|
3
4
|
// import {isDebugTest} from '../../../config';
|
|
4
5
|
|
|
5
6
|
// const isDebug = isDebugTest;
|
|
@@ -65,8 +66,25 @@ describe('BinaryTreeNode', () => {
|
|
|
65
66
|
root.right = rightChild;
|
|
66
67
|
|
|
67
68
|
expect(leftChild.familyPosition).toBe('LEFT');
|
|
69
|
+
leftChild.right = new BinaryTreeNode<number>(4);
|
|
68
70
|
expect(rightChild.familyPosition).toBe('RIGHT');
|
|
69
71
|
expect(root.familyPosition).toBe('ROOT');
|
|
72
|
+
expect(leftChild.familyPosition).toBe('ROOT_LEFT');
|
|
73
|
+
rightChild.left = new BinaryTreeNode<number>(5);
|
|
74
|
+
expect(rightChild.familyPosition).toBe('ROOT_RIGHT');
|
|
75
|
+
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should determine only right child family position correctly', () => {
|
|
79
|
+
const root = new BinaryTreeNode<number>(1);
|
|
80
|
+
const rightChild = new BinaryTreeNode<number>(3);
|
|
81
|
+
const isolated = new BinaryTreeNode<number>(2);
|
|
82
|
+
|
|
83
|
+
root.right = rightChild;
|
|
84
|
+
|
|
85
|
+
expect(rightChild.familyPosition).toBe('RIGHT');
|
|
86
|
+
expect(isolated.familyPosition).toBe(FamilyPosition.ISOLATED);
|
|
87
|
+
expect(root.familyPosition).toBe('ROOT');
|
|
70
88
|
});
|
|
71
89
|
});
|
|
72
90
|
|
|
@@ -87,19 +105,43 @@ describe('BinaryTree', () => {
|
|
|
87
105
|
expect(tree.size).toBe(1);
|
|
88
106
|
});
|
|
89
107
|
|
|
90
|
-
it('should delete
|
|
108
|
+
it('should delete nodes', () => {
|
|
109
|
+
expect(tree.getHeight(tree.root, IterationType.ITERATIVE)).toBe(-1)
|
|
110
|
+
expect(tree.getMinHeight()).toBe(-1)
|
|
91
111
|
const node = tree.add(1);
|
|
92
112
|
expect(tree.size).toBe(1);
|
|
93
113
|
|
|
114
|
+
const leftChild = new BinaryTreeNode<number>(2);
|
|
115
|
+
const rightChild = new BinaryTreeNode<number>(3);
|
|
116
|
+
tree.add(leftChild);
|
|
117
|
+
tree.add(rightChild);
|
|
118
|
+
const root = tree.root;
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
expect(leftChild.familyPosition).toBe('LEFT');
|
|
122
|
+
tree.add(null);
|
|
123
|
+
tree.add(new BinaryTreeNode<number>(4));
|
|
124
|
+
expect(rightChild.familyPosition).toBe('RIGHT');
|
|
125
|
+
expect(root?.familyPosition).toBe('ROOT');
|
|
126
|
+
expect(leftChild.familyPosition).toBe('ROOT_LEFT');
|
|
127
|
+
tree.add(new BinaryTreeNode<number>(5));
|
|
128
|
+
expect(rightChild.familyPosition).toBe('ROOT_RIGHT');
|
|
129
|
+
|
|
130
|
+
tree.delete(new BinaryTreeNode<number>(200));
|
|
131
|
+
tree.delete(rightChild)
|
|
132
|
+
|
|
94
133
|
if (node) {
|
|
95
|
-
const result = tree.delete(node
|
|
134
|
+
const result = tree.delete(node);
|
|
96
135
|
expect(result).toHaveLength(1);
|
|
97
|
-
expect(tree.size).toBe(
|
|
136
|
+
expect(tree.size).toBe(3);
|
|
137
|
+
expect(tree.getMinHeight(tree.root, IterationType.RECURSIVE)).toBe(1)
|
|
98
138
|
}
|
|
139
|
+
|
|
99
140
|
});
|
|
100
141
|
|
|
101
142
|
it('should add and find nodes', () => {
|
|
102
143
|
tree.add(1, 1);
|
|
144
|
+
tree.add(undefined);
|
|
103
145
|
tree.add(2, 2);
|
|
104
146
|
tree.add(3, 3);
|
|
105
147
|
|
|
@@ -191,6 +233,15 @@ describe('BinaryTree', () => {
|
|
|
191
233
|
expect(tree.isSubtreeBST(tree.getNode(4), IterationType.ITERATIVE)).toBe(true);
|
|
192
234
|
});
|
|
193
235
|
|
|
236
|
+
it('should isSubtreeBST', () => {
|
|
237
|
+
tree.addMany([4, 2, 6, 1, 3, 5, 7, 4]);
|
|
238
|
+
|
|
239
|
+
expect(tree.isSubtreeBST(tree.getNode(4), IterationType.RECURSIVE)).toBe(true);
|
|
240
|
+
expect(tree.isSubtreeBST(tree.getNode(4), IterationType.ITERATIVE)).toBe(true);
|
|
241
|
+
expect(tree.getNodes(2, undefined, false, null)).toEqual([])
|
|
242
|
+
expect(tree.getNodes(tree.getNodeByKey(2), undefined, false, tree.root)).toEqual([tree.getNodeByKey(2)])
|
|
243
|
+
});
|
|
244
|
+
|
|
194
245
|
it('should subTreeTraverse', () => {
|
|
195
246
|
tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
|
|
196
247
|
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6, 3, 7]);
|