data-structure-typed 1.41.8 → 1.42.0

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.
@@ -174,8 +174,11 @@ describe('BinaryTree', () => {
174
174
  });
175
175
 
176
176
  it('should subTreeTraverse', () => {
177
- tree.addMany([4, 2, 6, 1, 3, 5, 7]);
178
- expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6, 5, 7]);
177
+ tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
178
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6,3, 7]);
179
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6,3, 7]);
180
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.ITERATIVE, true)).toEqual([6, 3, 7, null]);
181
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.RECURSIVE, true)).toEqual([6, 3, 7, null]);
179
182
  });
180
183
 
181
184
  it('should clear the tree', () => {
@@ -266,10 +269,18 @@ describe('BinaryTree traversals', () => {
266
269
 
267
270
  const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
268
271
  tree.refill(arr);
272
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
273
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
274
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
275
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
276
+
269
277
  expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
270
278
  expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([
271
279
  35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55
272
280
  ]);
281
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
282
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
283
+
273
284
  expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
274
285
  expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
275
286
  expect(tree.dfs(node => node.key, 'post', tree.root, IterationType.RECURSIVE)).toEqual([
@@ -282,9 +293,7 @@ describe('BinaryTree traversals', () => {
282
293
  35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
283
294
  ]);
284
295
 
285
- const levels = tree.listLevels(node => node.key);
286
- expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
287
- isDebug && console.log(levels);
296
+ expect(tree.listLevels(node => node.key)).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
288
297
 
289
298
  expect(tree.listLevels(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
290
299
  [35],
@@ -292,9 +301,21 @@ describe('BinaryTree traversals', () => {
292
301
  [15, 29, 50],
293
302
  [16, 28, 30, 45, 55]
294
303
  ]);
295
- isDebug && console.log(levels);
304
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.ITERATIVE, true)).toEqual([
305
+ [35],
306
+ [20, 40],
307
+ [15, 29, null,50],
308
+ [null, 16, 28, 30, 45, 55]
309
+ ]);
310
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.RECURSIVE, true)).toEqual([
311
+ [35],
312
+ [20, 40],
313
+ [15, 29, null,50],
314
+ [null, 16, 28, 30, 45, 55]
315
+ ]);
296
316
  });
297
317
 
318
+
298
319
  describe('BinaryTree', () => {
299
320
  let tree: BinaryTree<string>;
300
321
 
@@ -149,8 +149,7 @@ describe('UndirectedGraph', () => {
149
149
  expect(degreeOfC).toBe(1);
150
150
  });
151
151
 
152
- it('xxx', () => {
153
- // const start = performance.now();
152
+ it('should getAllPathsBetween work well in 66 vertexes 97 edges graph', () => {
154
153
  const graph = new UndirectedGraph<{name: string}, number>();
155
154
  for (const v of saltyVertexes) {
156
155
  graph.addVertex(v.name, v);
@@ -159,9 +158,19 @@ describe('UndirectedGraph', () => {
159
158
  const [s, d] = e;
160
159
  graph.addEdge(s.name, d.name, d.weight);
161
160
  }
162
- // const result = graph.getAllPathsBetween('Intersection_1','Intersection_5');
163
- // console.log('---xxx', performance.now() - start, result)
164
- // const result = graph.dijkstra('Intersection_1','Intersection_5', true, true);
165
- // console.log('---xxx', performance.now() - start, result)
161
+ const allPaths = graph.getAllPathsBetween('Intersection_1','Intersection_5');
162
+ expect(allPaths.length).toBe(1000);
163
+ const minWeightedPathDFS = graph.getMinPathBetween('Intersection_1','Intersection_5', true, true);
164
+ expect(minWeightedPathDFS?.[0]?.key).toBe('Intersection_1');
165
+ expect(minWeightedPathDFS?.[5]?.key).toBe('Intersection_42');
166
+ expect(minWeightedPathDFS?.[8]?.key).toBe('Intersection_18');
167
+ expect(minWeightedPathDFS?.[27]?.key).toBe('Intersection_6');
168
+ const minWeightedPath = graph.dijkstra('Intersection_1','Intersection_5', true, true);
169
+
170
+ expect(minWeightedPath?.minPath?.[0]?.key).toBe('Intersection_1')
171
+ expect(minWeightedPath?.minPath?.[1]?.key).toBe('Intersection_2')
172
+ expect(minWeightedPath?.minPath?.[2]?.key).toBe('Intersection_3')
173
+ expect(minWeightedPath?.minPath?.[3]?.key).toBe('Intersection_4')
174
+ expect(minWeightedPath?.minPath?.[4]?.key).toBe('Intersection_5')
166
175
  });
167
176
  });