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.
Files changed (139) hide show
  1. package/package.json +1 -1
  2. package/src/data-structures/binary-tree/aa-tree.ts +1 -0
  3. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
  4. package/src/data-structures/binary-tree/avl-tree.ts +307 -0
  5. package/src/data-structures/binary-tree/b-tree.ts +1 -0
  6. package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
  7. package/src/data-structures/binary-tree/binary-tree.ts +47 -0
  8. package/src/data-structures/binary-tree/bst.ts +537 -0
  9. package/src/data-structures/binary-tree/index.ts +12 -0
  10. package/src/data-structures/binary-tree/rb-tree.ts +366 -0
  11. package/src/data-structures/binary-tree/segment-tree.ts +260 -0
  12. package/src/data-structures/binary-tree/splay-tree.ts +1 -0
  13. package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
  14. package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
  15. package/src/data-structures/graph/abstract-graph.ts +1040 -0
  16. package/src/data-structures/graph/directed-graph.ts +470 -0
  17. package/src/data-structures/graph/index.ts +4 -0
  18. package/src/data-structures/graph/map-graph.ts +129 -0
  19. package/src/data-structures/graph/undirected-graph.ts +274 -0
  20. package/src/data-structures/hash/coordinate-map.ts +67 -0
  21. package/src/data-structures/hash/coordinate-set.ts +56 -0
  22. package/src/data-structures/hash/hash-map.ts +203 -0
  23. package/src/data-structures/hash/hash-table.ts +277 -0
  24. package/src/data-structures/hash/index.ts +7 -0
  25. package/src/data-structures/hash/pair.ts +1 -0
  26. package/src/data-structures/hash/tree-map.ts +1 -0
  27. package/src/data-structures/hash/tree-set.ts +1 -0
  28. package/src/data-structures/heap/heap.ts +212 -0
  29. package/src/data-structures/heap/index.ts +3 -0
  30. package/src/data-structures/heap/max-heap.ts +31 -0
  31. package/src/data-structures/heap/min-heap.ts +32 -0
  32. package/src/data-structures/index.ts +11 -0
  33. package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
  34. package/src/data-structures/linked-list/index.ts +3 -0
  35. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  36. package/src/data-structures/linked-list/skip-linked-list.ts +166 -0
  37. package/src/data-structures/matrix/index.ts +4 -0
  38. package/src/data-structures/matrix/matrix.ts +27 -0
  39. package/src/data-structures/matrix/matrix2d.ts +213 -0
  40. package/src/data-structures/matrix/navigator.ts +121 -0
  41. package/src/data-structures/matrix/vector2d.ts +316 -0
  42. package/src/data-structures/priority-queue/index.ts +3 -0
  43. package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
  44. package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
  45. package/src/data-structures/priority-queue/priority-queue.ts +359 -0
  46. package/src/data-structures/queue/deque.ts +297 -0
  47. package/src/data-structures/queue/index.ts +2 -0
  48. package/src/data-structures/queue/queue.ts +191 -0
  49. package/src/data-structures/stack/index.ts +1 -0
  50. package/src/data-structures/stack/stack.ts +98 -0
  51. package/src/data-structures/tree/index.ts +1 -0
  52. package/src/data-structures/tree/tree.ts +69 -0
  53. package/src/data-structures/trie/index.ts +1 -0
  54. package/src/data-structures/trie/trie.ts +225 -0
  55. package/src/index.ts +4 -0
  56. package/src/interfaces/abstract-binary-tree.ts +189 -0
  57. package/src/interfaces/abstract-graph.ts +31 -0
  58. package/src/interfaces/avl-tree.ts +25 -0
  59. package/src/interfaces/binary-tree.ts +6 -0
  60. package/src/interfaces/bst.ts +31 -0
  61. package/src/interfaces/directed-graph.ts +20 -0
  62. package/src/interfaces/doubly-linked-list.ts +1 -0
  63. package/src/interfaces/heap.ts +1 -0
  64. package/src/interfaces/index.ts +15 -0
  65. package/src/interfaces/navigator.ts +1 -0
  66. package/src/interfaces/priority-queue.ts +1 -0
  67. package/src/interfaces/rb-tree.ts +9 -0
  68. package/src/interfaces/segment-tree.ts +1 -0
  69. package/src/interfaces/singly-linked-list.ts +1 -0
  70. package/src/interfaces/tree-multiset.ts +7 -0
  71. package/src/interfaces/undirected-graph.ts +6 -0
  72. package/src/types/data-structures/abstract-binary-tree.ts +50 -0
  73. package/src/types/data-structures/abstract-graph.ts +11 -0
  74. package/src/types/data-structures/avl-tree.ts +5 -0
  75. package/src/types/data-structures/binary-tree.ts +5 -0
  76. package/src/types/data-structures/bst.ts +13 -0
  77. package/src/types/data-structures/directed-graph.ts +8 -0
  78. package/src/types/data-structures/doubly-linked-list.ts +1 -0
  79. package/src/types/data-structures/hash.ts +1 -0
  80. package/src/types/data-structures/heap.ts +5 -0
  81. package/src/types/data-structures/index.ts +16 -0
  82. package/src/types/data-structures/map-graph.ts +1 -0
  83. package/src/types/data-structures/navigator.ts +13 -0
  84. package/src/types/data-structures/priority-queue.ts +9 -0
  85. package/src/types/data-structures/rb-tree.ts +8 -0
  86. package/src/types/data-structures/segment-tree.ts +1 -0
  87. package/src/types/data-structures/singly-linked-list.ts +1 -0
  88. package/src/types/data-structures/tree-multiset.ts +6 -0
  89. package/src/types/helpers.ts +1 -0
  90. package/src/types/index.ts +3 -0
  91. package/src/types/utils/index.ts +2 -0
  92. package/src/types/utils/utils.ts +6 -0
  93. package/src/types/utils/validate-type.ts +35 -0
  94. package/src/utils/index.ts +1 -0
  95. package/src/utils/utils.ts +79 -0
  96. package/test/integration/avl-tree.test.ts +108 -0
  97. package/test/integration/bst.test.ts +380 -0
  98. package/test/integration/heap.test.js +16 -0
  99. package/test/integration/index.html +52 -0
  100. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +108 -0
  101. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
  102. package/test/unit/data-structures/binary-tree/bst.test.ts +380 -0
  103. package/test/unit/data-structures/binary-tree/overall.test.ts +65 -0
  104. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
  105. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
  106. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +461 -0
  107. package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
  108. package/test/unit/data-structures/graph/directed-graph.test.ts +519 -0
  109. package/test/unit/data-structures/graph/index.ts +2 -0
  110. package/test/unit/data-structures/graph/map-graph.test.ts +45 -0
  111. package/test/unit/data-structures/graph/overall.test.ts +49 -0
  112. package/test/unit/data-structures/graph/undirected-graph.test.ts +59 -0
  113. package/test/unit/data-structures/hash/coordinate-map.test.ts +54 -0
  114. package/test/unit/data-structures/hash/coordinate-set.test.ts +41 -0
  115. package/test/unit/data-structures/hash/hash-map.test.ts +104 -0
  116. package/test/unit/data-structures/hash/hash-table.test.ts +184 -0
  117. package/test/unit/data-structures/heap/heap.test.ts +55 -0
  118. package/test/unit/data-structures/heap/max-heap.test.ts +44 -0
  119. package/test/unit/data-structures/heap/min-heap.test.ts +82 -0
  120. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +364 -0
  121. package/test/unit/data-structures/linked-list/index.ts +4 -0
  122. package/test/unit/data-structures/linked-list/linked-list.test.ts +35 -0
  123. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +451 -0
  124. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
  125. package/test/unit/data-structures/linked-list/skip-list.test.ts +55 -0
  126. package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
  127. package/test/unit/data-structures/matrix/matrix2d.test.ts +138 -0
  128. package/test/unit/data-structures/matrix/navigator.test.ts +79 -0
  129. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +106 -0
  130. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
  131. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
  132. package/test/unit/data-structures/queue/deque.test.ts +130 -0
  133. package/test/unit/data-structures/queue/queue.test.ts +199 -0
  134. package/test/unit/data-structures/stack/stack.test.ts +67 -0
  135. package/test/unit/data-structures/tree/tree.test.ts +39 -0
  136. package/test/unit/data-structures/trie/trie.test.ts +95 -0
  137. package/test/utils/index.ts +2 -0
  138. package/test/utils/magnitude.ts +21 -0
  139. package/test/utils/number.ts +3 -0
@@ -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,104 @@
1
+ import {HashMap} from '../../../../src';
2
+
3
+ describe('HashMap', () => {
4
+ let hashMap: HashMap<string, number>;
5
+
6
+ beforeEach(() => {
7
+ hashMap = new HashMap<string, number>();
8
+ });
9
+
10
+ it('should initialize correctly', () => {
11
+ expect(hashMap.size).toBe(0);
12
+ expect(hashMap.table.length).toBe(16);
13
+ expect(hashMap.loadFactor).toBe(0.75);
14
+ expect(hashMap.capacityMultiplier).toBe(2);
15
+ expect(hashMap.initialCapacity).toBe(16);
16
+ expect(hashMap.isEmpty()).toBe(true);
17
+ });
18
+
19
+ it('should put and get values', () => {
20
+ hashMap.set('one', 1);
21
+ hashMap.set('two', 2);
22
+ hashMap.set('three', 3);
23
+
24
+ expect(hashMap.get('one')).toBe(1);
25
+ expect(hashMap.get('two')).toBe(2);
26
+ expect(hashMap.get('three')).toBe(3);
27
+ });
28
+
29
+ it('should handle key collisions', () => {
30
+ // Force a collision by setting two different keys to the same bucket
31
+ hashMap.hashFn = () => 0; // Override hash function to return the same index
32
+ hashMap.set('key1', 1);
33
+ hashMap.set('key2', 2);
34
+
35
+ expect(hashMap.get('key1')).toBe(1);
36
+ expect(hashMap.get('key2')).toBe(2);
37
+ });
38
+
39
+ it('should remove values', () => {
40
+ hashMap.set('one', 1);
41
+ hashMap.set('two', 2);
42
+
43
+ hashMap.remove('one');
44
+ expect(hashMap.get('one')).toBeUndefined();
45
+ expect(hashMap.size).toBe(1);
46
+ });
47
+
48
+ it('should clear the HashMap', () => {
49
+ hashMap.set('one', 1);
50
+ hashMap.set('two', 2);
51
+
52
+ hashMap.clear();
53
+ expect(hashMap.size).toBe(0);
54
+ expect(hashMap.isEmpty()).toBe(true);
55
+ });
56
+
57
+ it('should iterate over entries', () => {
58
+ hashMap.set('one', 1);
59
+ hashMap.set('two', 2);
60
+ hashMap.set('three', 3);
61
+
62
+ const entries = Array.from(hashMap.entries());
63
+ expect(entries).toEqual(
64
+ expect.arrayContaining([
65
+ ['one', 1],
66
+ ['two', 2],
67
+ ['three', 3]
68
+ ])
69
+ );
70
+ });
71
+
72
+ it('should resize the table when load factor is exceeded', () => {
73
+ // Set a small initial capacity for testing resizing
74
+ hashMap = new HashMap<string, number>(4, 0.5);
75
+
76
+ hashMap.set('one', 1);
77
+ hashMap.set('two', 2);
78
+ hashMap.set('three', 3);
79
+ hashMap.set('four', 4); // This should trigger a resize
80
+
81
+ expect(hashMap.table.length).toBe(8);
82
+ expect(hashMap.get('one')).toBe(1);
83
+ expect(hashMap.get('two')).toBe(2);
84
+ expect(hashMap.get('three')).toBe(3);
85
+ expect(hashMap.get('four')).toBe(4);
86
+ });
87
+
88
+ it('should allow using a custom hash function', () => {
89
+ const customHashFn = () => {
90
+ // A simple custom hash function that always returns 0
91
+ return 0;
92
+ };
93
+ hashMap = new HashMap<string, number>(16, 0.75, customHashFn);
94
+
95
+ hashMap.set('one', 1);
96
+ hashMap.set('two', 2);
97
+
98
+ expect(hashMap.get('one')).toBe(1);
99
+ expect(hashMap.get('two')).toBe(2);
100
+ // Since the custom hash function always returns 0, these keys will collide.
101
+ // Make sure they are stored separately.
102
+ expect(hashMap.table[0].length).toBe(2);
103
+ });
104
+ });
@@ -0,0 +1,184 @@
1
+ import {HashTableNode, 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 HashTableNode(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(16);
20
+ expect(hashTable.size).toBe(0);
21
+ expect(hashTable.buckets.length).toBe(16);
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.set(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.set(key1, value1);
52
+ hashTable.set(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.set(key, initialValue);
68
+ hashTable.set(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.set(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
+ });
98
+
99
+ describe('HashTable', () => {
100
+ let hashTable: HashTable<string, number>;
101
+
102
+ beforeEach(() => {
103
+ hashTable = new HashTable<string, number>();
104
+ });
105
+
106
+ it('should insert and retrieve values correctly', () => {
107
+ hashTable.set('one', 1);
108
+ hashTable.set('two', 2);
109
+
110
+ expect(hashTable.get('one')).toBe(1);
111
+ expect(hashTable.get('two')).toBe(2);
112
+ });
113
+
114
+ it('should update values correctly', () => {
115
+ hashTable.set('one', 1);
116
+ expect(hashTable.get('one')).toBe(1);
117
+
118
+ hashTable.set('one', 100); // Update the value
119
+ expect(hashTable.get('one')).toBe(100);
120
+ });
121
+
122
+ it('should handle collisions correctly', () => {
123
+ hashTable = new HashTable<string, number>(1); // Set a small capacity to force collisions
124
+ hashTable.set('one', 1);
125
+ hashTable.set('two', 2);
126
+
127
+ expect(hashTable.get('one')).toBe(1);
128
+ expect(hashTable.get('two')).toBe(2);
129
+ });
130
+
131
+ it('should remove values correctly', () => {
132
+ hashTable.set('one', 1);
133
+ hashTable.set('two', 2);
134
+ hashTable.remove('one');
135
+
136
+ expect(hashTable.get('one')).toBeUndefined();
137
+ expect(hashTable.get('two')).toBe(2);
138
+ });
139
+
140
+ it('should handle non-existent keys correctly', () => {
141
+ expect(hashTable.get('non-existent')).toBeUndefined();
142
+ hashTable.remove('non-existent'); // Removing a non-existent key should not cause errors
143
+ });
144
+
145
+ it('should handle custom hash function correctly', () => {
146
+ // const customHashFn = () => {
147
+ // // Custom hash function that returns a fixed value for all keys
148
+ // return 42;
149
+ // };
150
+
151
+ hashTable = new HashTable<string, number>(16);
152
+ hashTable.set('one', 1);
153
+ expect(hashTable.get('one')).toBe(1);
154
+ expect(hashTable.get('two')).toBeUndefined();
155
+ });
156
+
157
+ it('should expand when load factor exceeds threshold', () => {
158
+ hashTable = new HashTable<string, number>(2); // Set a small capacity to trigger expansion
159
+ hashTable.set('one', 1);
160
+ hashTable.set('two', 2);
161
+ hashTable.set('three', 3); // This should trigger an expansion
162
+
163
+ expect(hashTable.capacity).toBe(16);
164
+ expect(hashTable.get('one')).toBe(1);
165
+ expect(hashTable.get('two')).toBe(2);
166
+ expect(hashTable.get('three')).toBe(3);
167
+ });
168
+ });
169
+
170
+ describe('HashTable performance', function () {
171
+ it('Items set performance', function () {
172
+ const mag = 100000;
173
+ const ht = new HashTable();
174
+ // const s = performance.now();
175
+ for (let i = 0; i < mag; i++) {
176
+ ht.set(i, i);
177
+ }
178
+ // const s1 = performance.now();
179
+ const map = new Map();
180
+ for (let i = 0; i < mag; i++) {
181
+ map.set(i, i);
182
+ }
183
+ });
184
+ });
@@ -0,0 +1,55 @@
1
+ import {MaxHeap, MinHeap} from '../../../../src';
2
+
3
+ describe('Heap Operation Test', () => {
4
+ it('should numeric heap work well', function () {
5
+ const minNumHeap = new MinHeap<number>();
6
+ minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
7
+ expect(minNumHeap.has(1)).toBe(true);
8
+ expect(minNumHeap.has(2)).toBe(true);
9
+ expect(minNumHeap.poll()).toBe(0);
10
+ expect(minNumHeap.poll()).toBe(1);
11
+ expect(minNumHeap.peek()).toBe(2);
12
+ expect(!minNumHeap.has(1));
13
+ expect(minNumHeap.has(2));
14
+ const arrFromHeap = minNumHeap.toArray();
15
+ expect(arrFromHeap.length).toBe(4);
16
+ expect(arrFromHeap[0]).toBe(2);
17
+ expect(arrFromHeap[1]).toBe(5);
18
+ expect(arrFromHeap[2]).toBe(9);
19
+ expect(arrFromHeap[3]).toBe(6);
20
+ expect(minNumHeap.sort()).toEqual([2, 5, 6, 9]);
21
+ });
22
+
23
+ it('should object heap work well', function () {
24
+ const minHeap = new MinHeap<{a: string}>();
25
+ minHeap.add(1, {a: 'a1'});
26
+ minHeap.add(6, {a: 'a6'});
27
+ minHeap.add(2, {a: 'a2'});
28
+ minHeap.add(0, {a: 'a0'});
29
+
30
+ expect(minHeap.peek()).toEqual({a: 'a0'});
31
+ expect(minHeap.toArray()).toEqual([{a: 'a0'}, {a: 'a1'}, {a: 'a2'}, {a: 'a6'}]);
32
+ let i = 0;
33
+ const expectPolled = [{a: 'a0'}, {a: 'a1'}, {a: 'a2'}, {a: 'a6'}];
34
+ while (minHeap.size > 0) {
35
+ expect(minHeap.poll()).toEqual(expectPolled[i]);
36
+ i++;
37
+ }
38
+
39
+ const maxHeap = new MaxHeap<{a: string}>();
40
+ maxHeap.add(1, {a: 'a1'});
41
+ maxHeap.add(6, {a: 'a6'});
42
+ maxHeap.add(5, {a: 'a5'});
43
+ maxHeap.add(2, {a: 'a2'});
44
+ maxHeap.add(0, {a: 'a0'});
45
+ maxHeap.add(9, {a: 'a9'});
46
+ expect(maxHeap.peek()).toEqual({a: 'a9'});
47
+ expect(maxHeap.toArray()).toEqual([{a: 'a9'}, {a: 'a2'}, {a: 'a6'}, {a: 'a1'}, {a: 'a0'}, {a: 'a5'}]);
48
+ const maxExpectPolled = [{a: 'a9'}, {a: 'a6'}, {a: 'a5'}, {a: 'a2'}, {a: 'a1'}, {a: 'a0'}];
49
+ let maxI = 0;
50
+ while (maxHeap.size > 0) {
51
+ expect(maxHeap.poll()).toEqual(maxExpectPolled[maxI]);
52
+ maxI++;
53
+ }
54
+ });
55
+ });
@@ -0,0 +1,44 @@
1
+ import {HeapItem, MaxHeap} from '../../../../src';
2
+
3
+ describe('MaxHeap Operation Test', () => {
4
+ it('should object Max Heap operations be proper', function () {
5
+ const maxHeap = new MaxHeap<{keyA: string}>();
6
+ const myObj1 = {keyA: 'a1'},
7
+ myObj6 = {keyA: 'a6'},
8
+ myObj5 = {keyA: 'a5'},
9
+ myObj2 = {keyA: 'a2'},
10
+ myObj0 = {keyA: 'a0'},
11
+ myObj9 = {keyA: 'a9'};
12
+ maxHeap.add(1, myObj1);
13
+ expect(maxHeap.has(myObj1)).toBe(true);
14
+ expect(maxHeap.has(myObj9)).toBe(false);
15
+ maxHeap.add(6, myObj6);
16
+ expect(maxHeap.has(myObj6)).toBe(true);
17
+ maxHeap.add(5, myObj5);
18
+ expect(maxHeap.has(myObj5)).toBe(true);
19
+ maxHeap.add(2, myObj2);
20
+ expect(maxHeap.has(myObj2)).toBe(true);
21
+ expect(maxHeap.has(myObj6)).toBe(true);
22
+ maxHeap.add(0, myObj0);
23
+ expect(maxHeap.has(myObj0)).toBe(true);
24
+ expect(maxHeap.has(myObj9)).toBe(false);
25
+ maxHeap.add(9, myObj9);
26
+ expect(maxHeap.has(myObj9)).toBe(true);
27
+
28
+ const peek9 = maxHeap.peek(true);
29
+ peek9 && peek9.val && expect(peek9.val.keyA).toBe('a9');
30
+
31
+ const heapToArr = maxHeap.toArray(true);
32
+ expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a9', 'a2', 'a6', 'a1', 'a0', 'a5']);
33
+
34
+ const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0'];
35
+ let i = 0;
36
+ while (maxHeap.size > 0) {
37
+ const polled = maxHeap.poll(true);
38
+ expect(polled).toBeInstanceOf(HeapItem);
39
+ polled && expect(polled.val).toHaveProperty('keyA');
40
+ polled && polled.val && expect(polled.val.keyA).toBe(values[i]);
41
+ i++;
42
+ }
43
+ });
44
+ });
@@ -0,0 +1,82 @@
1
+ import {HeapItem, MinHeap} from '../../../../src';
2
+
3
+ describe('MinHeap Operation Test', () => {
4
+ it('should numeric Min Heap operations be proper', function () {
5
+ const minNumHeap = new MinHeap<number>();
6
+ expect(minNumHeap).toBeInstanceOf(MinHeap);
7
+
8
+ minNumHeap.add(1);
9
+ expect(minNumHeap.has(1)).toBe(true);
10
+ minNumHeap.add(6);
11
+ expect(minNumHeap.has(2)).toBe(false);
12
+ expect(minNumHeap.has(6)).toBe(true);
13
+ minNumHeap.add(2);
14
+ expect(minNumHeap.has(2)).toBe(true);
15
+ minNumHeap.add(0);
16
+ expect(minNumHeap.has(0)).toBe(true);
17
+ minNumHeap.add(5);
18
+ expect(minNumHeap.has(5)).toBe(true);
19
+ minNumHeap.add(9);
20
+ expect(minNumHeap.has(9)).toBe(true);
21
+ expect(minNumHeap.size).toBe(6);
22
+
23
+ const poll1 = minNumHeap.poll(true);
24
+ expect(poll1).toBeInstanceOf(HeapItem);
25
+ poll1 instanceof HeapItem && expect(poll1.val).toBe(0);
26
+
27
+ const poll2 = minNumHeap.poll(true);
28
+ expect(poll2).toBeInstanceOf(HeapItem);
29
+ poll2 instanceof HeapItem && expect(poll2.val).toBe(1);
30
+
31
+ const peek1 = minNumHeap.peek(true);
32
+ expect(peek1).toBeInstanceOf(HeapItem);
33
+ peek1 instanceof HeapItem && expect(peek1.val).toBe(2);
34
+
35
+ const heapArray = minNumHeap.toArray(true);
36
+ expect(heapArray).toBeInstanceOf(Array);
37
+ expect(heapArray.map(item => item?.priority)).toEqual([2, 5, 9, 6]);
38
+ expect(minNumHeap.size).toBe(4);
39
+ });
40
+
41
+ it('should object Min Heap operations be proper', function () {
42
+ class MyObject {
43
+ keyA: string;
44
+
45
+ constructor(keyA: string) {
46
+ this.keyA = keyA;
47
+ }
48
+ }
49
+
50
+ const minObjHeap = new MinHeap<MyObject>();
51
+
52
+ const obj1 = new MyObject('a1'),
53
+ obj6 = new MyObject('a6'),
54
+ obj2 = new MyObject('a2'),
55
+ obj0 = new MyObject('a0');
56
+ minObjHeap.add(1, obj1);
57
+ expect(minObjHeap.has(obj1)).toBe(true);
58
+ expect(minObjHeap.has(obj6)).toBe(false);
59
+ minObjHeap.add(6, obj6);
60
+ expect(minObjHeap.has(obj6)).toBe(true);
61
+ minObjHeap.add(2, obj2);
62
+ expect(minObjHeap.has(obj2)).toBe(true);
63
+ minObjHeap.add(0, obj0);
64
+ expect(minObjHeap.has(obj0)).toBe(true);
65
+
66
+ const peek = minObjHeap.peek(true);
67
+ peek && peek.val && expect(peek.val.keyA).toBe('a0');
68
+
69
+ const heapToArr = minObjHeap.toArray(true);
70
+ expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a0', 'a1', 'a2', 'a6']);
71
+
72
+ const values = ['a0', 'a1', 'a2', 'a6'];
73
+ let i = 0;
74
+ while (minObjHeap.size > 0) {
75
+ const polled = minObjHeap.poll(true);
76
+ expect(polled).toBeInstanceOf(HeapItem);
77
+ polled && expect(polled.val).toBeInstanceOf(MyObject);
78
+ polled && polled.val && expect(polled.val.keyA).toBe(values[i]);
79
+ i++;
80
+ }
81
+ });
82
+ });