linked-list-typed 1.45.0 → 1.45.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.
Files changed (47) hide show
  1. package/dist/data-structures/hash/hash-map.d.ts +58 -58
  2. package/dist/data-structures/hash/hash-map.js +73 -73
  3. package/dist/data-structures/heap/heap.js +21 -12
  4. package/package.json +2 -2
  5. package/src/data-structures/binary-tree/avl-tree.ts +7 -7
  6. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
  7. package/src/data-structures/binary-tree/binary-tree.ts +39 -31
  8. package/src/data-structures/binary-tree/bst.ts +12 -8
  9. package/src/data-structures/binary-tree/rb-tree.ts +17 -6
  10. package/src/data-structures/binary-tree/segment-tree.ts +1 -1
  11. package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
  12. package/src/data-structures/graph/abstract-graph.ts +46 -31
  13. package/src/data-structures/graph/directed-graph.ts +10 -5
  14. package/src/data-structures/graph/map-graph.ts +8 -8
  15. package/src/data-structures/graph/undirected-graph.ts +9 -9
  16. package/src/data-structures/hash/hash-map.ts +103 -103
  17. package/src/data-structures/hash/hash-table.ts +1 -1
  18. package/src/data-structures/hash/tree-map.ts +2 -1
  19. package/src/data-structures/hash/tree-set.ts +2 -1
  20. package/src/data-structures/heap/heap.ts +30 -17
  21. package/src/data-structures/heap/max-heap.ts +3 -3
  22. package/src/data-structures/heap/min-heap.ts +3 -3
  23. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  24. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  25. package/src/data-structures/matrix/matrix.ts +2 -2
  26. package/src/data-structures/matrix/matrix2d.ts +1 -1
  27. package/src/data-structures/matrix/navigator.ts +3 -3
  28. package/src/data-structures/matrix/vector2d.ts +2 -1
  29. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
  30. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
  31. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  32. package/src/data-structures/queue/deque.ts +5 -4
  33. package/src/data-structures/queue/queue.ts +2 -2
  34. package/src/data-structures/tree/tree.ts +1 -1
  35. package/src/data-structures/trie/trie.ts +1 -1
  36. package/src/interfaces/binary-tree.ts +2 -2
  37. package/src/interfaces/graph.ts +1 -1
  38. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  39. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  40. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  41. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  42. package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
  43. package/src/types/data-structures/hash/hash-map.ts +6 -6
  44. package/src/types/data-structures/matrix/navigator.ts +1 -1
  45. package/src/types/utils/utils.ts +1 -1
  46. package/src/types/utils/validate-type.ts +18 -4
  47. package/src/utils/utils.ts +6 -6
@@ -6,12 +6,23 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import {BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNodeNested} from '../../types';
10
- import {BST, BSTNode} from './bst';
11
- import {IBinaryTree} from '../../interfaces';
12
- import {BinaryTreeNode} from './binary-tree';
13
-
14
- export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
9
+ import {
10
+ BiTreeDeleteResult,
11
+ BTNCallback,
12
+ BTNKey,
13
+ IterationType,
14
+ RBTNColor,
15
+ RBTreeOptions,
16
+ RedBlackTreeNodeNested
17
+ } from '../../types';
18
+ import { BST, BSTNode } from './bst';
19
+ import { IBinaryTree } from '../../interfaces';
20
+ import { BinaryTreeNode } from './binary-tree';
21
+
22
+ export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<
23
+ V,
24
+ N
25
+ > {
15
26
  color: RBTNColor;
16
27
 
17
28
  constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type {SegmentTreeNodeVal} from '../../types';
9
+ import type { SegmentTreeNodeVal } from '../../types';
10
10
 
11
11
  export class SegmentTreeNode {
12
12
  start = 0;
@@ -5,12 +5,15 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {BTNKey, TreeMultimapNodeNested, TreeMultimapOptions} from '../../types';
9
- import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
10
- import {IBinaryTree} from '../../interfaces';
11
- import {AVLTree, AVLTreeNode} from './avl-tree';
12
-
13
- export class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
8
+ import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
+ import { BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType } from '../../types';
10
+ import { IBinaryTree } from '../../interfaces';
11
+ import { AVLTree, AVLTreeNode } from './avl-tree';
12
+
13
+ export class TreeMultimapNode<
14
+ V = any,
15
+ N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>
16
+ > extends AVLTreeNode<V, N> {
14
17
  count: number;
15
18
 
16
19
  /**
@@ -292,7 +295,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
292
295
  if (!parent) {
293
296
  if (curr.right !== undefined) this._setRoot(curr.right);
294
297
  } else {
295
- const {familyPosition: fp} = curr;
298
+ const { familyPosition: fp } = curr;
296
299
  if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
297
300
  parent.left = curr.right;
298
301
  } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
@@ -320,7 +323,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
320
323
  if (orgCurrent) this._count -= orgCurrent.count;
321
324
  }
322
325
 
323
- deletedResult.push({deleted: orgCurrent, needBalanced});
326
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
324
327
 
325
328
  if (needBalanced) {
326
329
  this._balancePath(needBalanced);
@@ -396,7 +399,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
396
399
  srcNode = this.ensureNotKey(srcNode);
397
400
  destNode = this.ensureNotKey(destNode);
398
401
  if (srcNode && destNode) {
399
- const {key, value, count, height} = destNode;
402
+ const { key, value, count, height } = destNode;
400
403
  const tempNode = this.createNode(key, value, count);
401
404
  if (tempNode) {
402
405
  tempNode.height = height;
@@ -5,11 +5,11 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import {uuidV4} from '../../utils';
9
- import {PriorityQueue} from '../priority-queue';
10
- import type {DijkstraResult, VertexKey} from '../../types';
11
- import {IGraph} from '../../interfaces';
12
- import {Queue} from '../queue';
8
+ import { uuidV4 } from '../../utils';
9
+ import { PriorityQueue } from '../priority-queue';
10
+ import type { DijkstraResult, VertexKey } from '../../types';
11
+ import { IGraph } from '../../interfaces';
12
+ import { Queue } from '../queue';
13
13
 
14
14
  export abstract class AbstractVertex<V = any> {
15
15
  key: VertexKey;
@@ -300,11 +300,11 @@ export abstract class AbstractGraph<
300
300
  return [];
301
301
  }
302
302
 
303
- const stack: {vertex: VO; path: VO[]}[] = [];
304
- stack.push({vertex: vertex1, path: [vertex1]});
303
+ const stack: { vertex: VO; path: VO[] }[] = [];
304
+ stack.push({ vertex: vertex1, path: [vertex1] });
305
305
 
306
306
  while (stack.length > 0) {
307
- const {vertex, path} = stack.pop()!;
307
+ const { vertex, path } = stack.pop()!;
308
308
 
309
309
  if (vertex === vertex2) {
310
310
  paths.push(path);
@@ -315,7 +315,7 @@ export abstract class AbstractGraph<
315
315
  for (const neighbor of neighbors) {
316
316
  if (!path.includes(neighbor)) {
317
317
  const newPath = [...path, neighbor];
318
- stack.push({vertex: neighbor, path: newPath});
318
+ stack.push({ vertex: neighbor, path: newPath });
319
319
  }
320
320
  }
321
321
  }
@@ -514,7 +514,12 @@ export abstract class AbstractGraph<
514
514
  * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
515
515
  * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
516
516
  */
517
- dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> {
517
+ dijkstraWithoutHeap(
518
+ src: VO | VertexKey,
519
+ dest?: VO | VertexKey | null,
520
+ getMinDist?: boolean,
521
+ genPaths?: boolean
522
+ ): DijkstraResult<VO> {
518
523
  if (getMinDist === undefined) getMinDist = false;
519
524
  if (genPaths === undefined) genPaths = false;
520
525
 
@@ -586,7 +591,7 @@ export abstract class AbstractGraph<
586
591
  if (genPaths) {
587
592
  getPaths(destVertex);
588
593
  }
589
- return {distMap, preMap, seen, paths, minDist, minPath};
594
+ return { distMap, preMap, seen, paths, minDist, minPath };
590
595
  }
591
596
  const neighbors = this.getNeighbors(cur);
592
597
  for (const neighbor of neighbors) {
@@ -609,18 +614,18 @@ export abstract class AbstractGraph<
609
614
  }
610
615
 
611
616
  getMinDist &&
612
- distMap.forEach((d, v) => {
613
- if (v !== srcVertex) {
614
- if (d < minDist) {
615
- minDist = d;
616
- if (genPaths) minDest = v;
617
- }
617
+ distMap.forEach((d, v) => {
618
+ if (v !== srcVertex) {
619
+ if (d < minDist) {
620
+ minDist = d;
621
+ if (genPaths) minDest = v;
618
622
  }
619
- });
623
+ }
624
+ });
620
625
 
621
626
  genPaths && getPaths(minDest);
622
627
 
623
- return {distMap, preMap, seen, paths, minDist, minPath};
628
+ return { distMap, preMap, seen, paths, minDist, minPath };
624
629
  }
625
630
 
626
631
  /**
@@ -657,7 +662,12 @@ export abstract class AbstractGraph<
657
662
  * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
658
663
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
659
664
  */
660
- dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> {
665
+ dijkstra(
666
+ src: VO | VertexKey,
667
+ dest?: VO | VertexKey | null,
668
+ getMinDist?: boolean,
669
+ genPaths?: boolean
670
+ ): DijkstraResult<VO> {
661
671
  if (getMinDist === undefined) getMinDist = false;
662
672
  if (genPaths === undefined) genPaths = false;
663
673
 
@@ -681,8 +691,8 @@ export abstract class AbstractGraph<
681
691
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
682
692
  }
683
693
 
684
- const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
685
- heap.add({key: 0, value: srcVertex});
694
+ const heap = new PriorityQueue<{ key: number; value: VO }>({ comparator: (a, b) => a.key - b.key });
695
+ heap.add({ key: 0, value: srcVertex });
686
696
 
687
697
  distMap.set(srcVertex, 0);
688
698
  preMap.set(srcVertex, null);
@@ -723,7 +733,7 @@ export abstract class AbstractGraph<
723
733
  if (genPaths) {
724
734
  getPaths(destVertex);
725
735
  }
726
- return {distMap, preMap, seen, paths, minDist, minPath};
736
+ return { distMap, preMap, seen, paths, minDist, minPath };
727
737
  }
728
738
  const neighbors = this.getNeighbors(cur);
729
739
  for (const neighbor of neighbors) {
@@ -733,7 +743,7 @@ export abstract class AbstractGraph<
733
743
  const distSrcToNeighbor = distMap.get(neighbor);
734
744
  if (distSrcToNeighbor) {
735
745
  if (dist + weight < distSrcToNeighbor) {
736
- heap.add({key: dist + weight, value: neighbor});
746
+ heap.add({ key: dist + weight, value: neighbor });
737
747
  preMap.set(neighbor, cur);
738
748
  distMap.set(neighbor, dist + weight);
739
749
  }
@@ -760,7 +770,7 @@ export abstract class AbstractGraph<
760
770
  getPaths(minDest);
761
771
  }
762
772
 
763
- return {distMap, preMap, seen, paths, minDist, minPath};
773
+ return { distMap, preMap, seen, paths, minDist, minPath };
764
774
  }
765
775
 
766
776
  /**
@@ -800,7 +810,7 @@ export abstract class AbstractGraph<
800
810
  // TODO
801
811
  let hasNegativeCycle: boolean | undefined;
802
812
  if (scanNegativeCycle) hasNegativeCycle = false;
803
- if (!srcVertex) return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
813
+ if (!srcVertex) return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
804
814
 
805
815
  const vertices = this._vertices;
806
816
  const numOfVertices = vertices.size;
@@ -872,7 +882,7 @@ export abstract class AbstractGraph<
872
882
  }
873
883
  }
874
884
 
875
- return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
885
+ return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
876
886
  }
877
887
 
878
888
  /**
@@ -913,7 +923,7 @@ export abstract class AbstractGraph<
913
923
  * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
914
924
  * path between vertices in the
915
925
  */
916
- floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
926
+ floydWarshall(): { costs: number[][]; predecessor: (VO | null)[][] } {
917
927
  const idAndVertices = [...this._vertices];
918
928
  const n = idAndVertices.length;
919
929
 
@@ -945,7 +955,7 @@ export abstract class AbstractGraph<
945
955
  }
946
956
  }
947
957
  }
948
- return {costs, predecessor};
958
+ return { costs, predecessor };
949
959
  }
950
960
 
951
961
  /**
@@ -982,7 +992,12 @@ export abstract class AbstractGraph<
982
992
  * are arrays of vertices that form cycles within the SCCs.
983
993
  * @returns The function `tarjan` returns an object with the following properties:
984
994
  */
985
- tarjan(needCutVertexes: boolean = false, needBridges: boolean = false, needSCCs: boolean = true, needCycles: boolean = false) {
995
+ tarjan(
996
+ needCutVertexes: boolean = false,
997
+ needBridges: boolean = false,
998
+ needSCCs: boolean = true,
999
+ needCycles: boolean = false
1000
+ ) {
986
1001
  // !! in undirected graph we will not let child visit parent when dfs
987
1002
  // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
988
1003
  // !! bridge: low(child) > dfn(cur)
@@ -1081,7 +1096,7 @@ export abstract class AbstractGraph<
1081
1096
  });
1082
1097
  }
1083
1098
 
1084
- return {dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles};
1099
+ return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
1085
1100
  }
1086
1101
 
1087
1102
  /**
@@ -5,10 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import {arrayRemove} from '../../utils';
9
- import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
- import type {TopologicalStatus, VertexKey} from '../../types';
11
- import {IGraph} from '../../interfaces';
8
+ import { arrayRemove } from '../../utils';
9
+ import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
10
+ import type { TopologicalStatus, VertexKey } from '../../types';
11
+ import { IGraph } from '../../interfaces';
12
12
 
13
13
  export class DirectedVertex<V = any> extends AbstractVertex<V> {
14
14
  /**
@@ -45,7 +45,12 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
45
45
  }
46
46
  }
47
47
 
48
- export class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>>
48
+ export class DirectedGraph<
49
+ V = any,
50
+ E = any,
51
+ VO extends DirectedVertex<V> = DirectedVertex<V>,
52
+ EO extends DirectedEdge<E> = DirectedEdge<E>
53
+ >
49
54
  extends AbstractGraph<V, E, VO, EO>
50
55
  implements IGraph<V, E, VO, EO> {
51
56
  /**
@@ -1,5 +1,5 @@
1
- import {MapGraphCoordinate, VertexKey} from '../../types';
2
- import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
1
+ import { MapGraphCoordinate, VertexKey } from '../../types';
2
+ import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
3
3
 
4
4
  export class MapVertex<V = any> extends DirectedVertex<V> {
5
5
  lat: number;
@@ -40,12 +40,12 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
40
40
  }
41
41
  }
42
42
 
43
- export class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<
44
- V,
45
- E,
46
- VO,
47
- EO
48
- > {
43
+ export class MapGraph<
44
+ V = any,
45
+ E = any,
46
+ VO extends MapVertex<V> = MapVertex<V>,
47
+ EO extends MapEdge<E> = MapEdge<E>
48
+ > extends DirectedGraph<V, E, VO, EO> {
49
49
  /**
50
50
  * The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
51
51
  * @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
@@ -5,10 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import {arrayRemove} from '../../utils';
9
- import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
- import type {VertexKey} from '../../types';
11
- import {IGraph} from '../../interfaces';
8
+ import { arrayRemove } from '../../utils';
9
+ import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
10
+ import type { VertexKey } from '../../types';
11
+ import { IGraph } from '../../interfaces';
12
12
 
13
13
  export class UndirectedVertex<V = any> extends AbstractVertex<V> {
14
14
  /**
@@ -43,11 +43,11 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
43
43
  }
44
44
 
45
45
  export class UndirectedGraph<
46
- V = any,
47
- E = any,
48
- VO extends UndirectedVertex<V> = UndirectedVertex<V>,
49
- EO extends UndirectedEdge<E> = UndirectedEdge<E>
50
- >
46
+ V = any,
47
+ E = any,
48
+ VO extends UndirectedVertex<V> = UndirectedVertex<V>,
49
+ EO extends UndirectedEdge<E> = UndirectedEdge<E>
50
+ >
51
51
  extends AbstractGraph<V, E, VO, EO>
52
52
  implements IGraph<V, E, VO, EO> {
53
53
  /**