data-structure-typed 0.8.17 → 0.9.16

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 (219) hide show
  1. package/.idea/modules.xml +1 -1
  2. package/README.md +197 -2
  3. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
  5. package/dist/data-structures/binary-tree/avl-tree.js +93 -46
  6. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
  10. package/dist/data-structures/binary-tree/binary-tree.js +480 -370
  11. package/dist/data-structures/binary-tree/bst.d.ts +4 -8
  12. package/dist/data-structures/binary-tree/bst.js +152 -107
  13. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  14. package/dist/data-structures/binary-tree/index.js +7 -0
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +91 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
  23. package/dist/data-structures/graph/abstract-graph.js +546 -311
  24. package/dist/data-structures/graph/directed-graph.d.ts +5 -13
  25. package/dist/data-structures/graph/directed-graph.js +250 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
  27. package/dist/data-structures/graph/undirected-graph.js +166 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
  29. package/dist/data-structures/hash/coordinate-map.js +38 -20
  30. package/dist/data-structures/hash/coordinate-set.js +33 -15
  31. package/dist/data-structures/hash/index.d.ts +5 -0
  32. package/dist/data-structures/hash/index.js +5 -0
  33. package/dist/data-structures/heap/heap.d.ts +2 -8
  34. package/dist/data-structures/heap/heap.js +36 -31
  35. package/dist/data-structures/heap/max-heap.d.ts +3 -2
  36. package/dist/data-structures/heap/max-heap.js +27 -9
  37. package/dist/data-structures/heap/min-heap.d.ts +3 -2
  38. package/dist/data-structures/heap/min-heap.js +27 -9
  39. package/dist/data-structures/index.d.ts +2 -0
  40. package/dist/data-structures/index.js +2 -0
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
  44. package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
  45. package/dist/data-structures/matrix/matrix.js +8 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
  47. package/dist/data-structures/matrix/matrix2d.js +80 -63
  48. package/dist/data-structures/matrix/navigator.d.ts +2 -16
  49. package/dist/data-structures/matrix/navigator.js +37 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +18 -18
  51. package/dist/data-structures/matrix/vector2d.js +117 -94
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +159 -116
  58. package/dist/data-structures/queue/deque.js +82 -56
  59. package/dist/data-structures/queue/index.d.ts +1 -0
  60. package/dist/data-structures/queue/index.js +1 -0
  61. package/dist/data-structures/queue/queue.d.ts +9 -10
  62. package/dist/data-structures/queue/queue.js +34 -34
  63. package/dist/data-structures/stack/stack.d.ts +9 -10
  64. package/dist/data-structures/stack/stack.js +31 -31
  65. package/dist/data-structures/trampoline.d.ts +14 -23
  66. package/dist/data-structures/trampoline.js +103 -25
  67. package/dist/data-structures/trie/trie.d.ts +13 -3
  68. package/dist/data-structures/trie/trie.js +234 -80
  69. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  70. package/dist/data-structures/types/abstract-graph.js +2 -0
  71. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  72. package/dist/data-structures/types/avl-tree.js +2 -0
  73. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  74. package/dist/data-structures/types/binary-tree.js +2 -0
  75. package/dist/data-structures/types/bst.d.ts +7 -0
  76. package/dist/data-structures/types/bst.js +2 -0
  77. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  78. package/dist/data-structures/types/directed-graph.js +2 -0
  79. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  80. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  81. package/dist/data-structures/types/heap.d.ts +7 -0
  82. package/dist/data-structures/types/heap.js +2 -0
  83. package/dist/data-structures/types/index.d.ts +13 -0
  84. package/dist/data-structures/types/index.js +29 -0
  85. package/dist/data-structures/types/navigator.d.ts +14 -0
  86. package/dist/data-structures/types/navigator.js +2 -0
  87. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  88. package/dist/data-structures/types/priority-queue.js +2 -0
  89. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  90. package/dist/data-structures/types/segment-tree.js +2 -0
  91. package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
  92. package/dist/data-structures/types/singly-linked-list.js +2 -0
  93. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  94. package/dist/data-structures/types/tree-multiset.js +2 -0
  95. package/dist/{types → data-structures/types}/utils.d.ts +7 -1
  96. package/dist/{types → data-structures/types}/utils.js +20 -19
  97. package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
  98. package/dist/utils/utils.js +651 -0
  99. package/package.json +20 -42
  100. package/src/data-structures/binary-tree/avl-tree.ts +1 -6
  101. package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
  102. package/src/data-structures/binary-tree/binary-tree.ts +184 -139
  103. package/src/data-structures/binary-tree/bst.ts +15 -24
  104. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  105. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  106. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  107. package/src/data-structures/binary-tree/index.ts +7 -0
  108. package/src/data-structures/binary-tree/segment-tree.ts +20 -12
  109. package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
  110. package/src/data-structures/diagrams/README.md +7 -0
  111. package/src/data-structures/graph/abstract-graph.ts +58 -94
  112. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  113. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  114. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  115. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  116. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  117. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  118. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  119. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  120. package/src/data-structures/graph/diagrams/mst.png +0 -0
  121. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  122. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  123. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  124. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  125. package/src/data-structures/graph/directed-graph.ts +12 -28
  126. package/src/data-structures/graph/undirected-graph.ts +11 -10
  127. package/src/data-structures/hash/coordinate-map.ts +1 -1
  128. package/src/data-structures/hash/index.ts +5 -0
  129. package/src/data-structures/heap/heap.ts +2 -11
  130. package/src/data-structures/heap/max-heap.ts +3 -2
  131. package/src/data-structures/heap/min-heap.ts +3 -2
  132. package/src/data-structures/index.ts +2 -0
  133. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
  134. package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
  135. package/src/data-structures/matrix/matrix2d.ts +11 -11
  136. package/src/data-structures/matrix/navigator.ts +2 -14
  137. package/src/data-structures/matrix/vector2d.ts +52 -52
  138. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  139. package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
  140. package/src/data-structures/priority-queue/priority-queue.ts +70 -78
  141. package/src/data-structures/queue/deque.ts +2 -2
  142. package/src/data-structures/queue/index.ts +1 -0
  143. package/src/data-structures/queue/queue.ts +12 -13
  144. package/src/data-structures/stack/stack.ts +12 -13
  145. package/src/data-structures/trampoline.ts +31 -71
  146. package/src/data-structures/trie/trie.ts +61 -11
  147. package/src/data-structures/types/abstract-graph.ts +51 -0
  148. package/src/data-structures/types/avl-tree.ts +6 -0
  149. package/src/data-structures/types/binary-tree.ts +15 -0
  150. package/src/data-structures/types/bst.ts +5 -0
  151. package/src/data-structures/types/directed-graph.ts +18 -0
  152. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  153. package/src/data-structures/types/heap.ts +8 -0
  154. package/src/data-structures/types/index.ts +13 -0
  155. package/src/data-structures/types/navigator.ts +12 -0
  156. package/src/data-structures/types/priority-queue.ts +9 -0
  157. package/src/data-structures/types/segment-tree.ts +1 -0
  158. package/src/data-structures/types/singly-linked-list.ts +15 -0
  159. package/src/data-structures/types/tree-multiset.ts +3 -0
  160. package/src/{types → data-structures/types}/utils.ts +20 -5
  161. package/src/utils/index.ts +1 -0
  162. package/src/{utils.ts → utils/utils.ts} +32 -132
  163. package/tsconfig.json +9 -6
  164. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  165. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  166. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  167. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  168. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  169. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  170. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  171. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  172. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  173. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  174. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  175. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  176. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  177. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  178. package/dist/types/data-structures/graph/index.d.ts +0 -3
  179. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  180. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  181. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  182. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  185. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  186. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  187. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  188. package/dist/types/data-structures/heap/index.d.ts +0 -3
  189. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  190. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  191. package/dist/types/data-structures/index.d.ts +0 -9
  192. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  193. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  194. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  195. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  196. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  197. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  198. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  199. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  200. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  201. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  202. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  203. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  204. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  205. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  206. package/dist/types/data-structures/queue/index.d.ts +0 -1
  207. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  208. package/dist/types/data-structures/stack/index.d.ts +0 -1
  209. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  210. package/dist/types/data-structures/trampoline.d.ts +0 -25
  211. package/dist/types/data-structures/trie/index.d.ts +0 -1
  212. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  213. package/dist/types/types/index.d.ts +0 -1
  214. package/dist/types/types/utils.d.ts +0 -46
  215. package/dist/utils.js +0 -569
  216. package/src/types/index.ts +0 -1
  217. package/src/types/patches/index.d.ts +0 -0
  218. /package/dist/{types → utils}/index.d.ts +0 -0
  219. /package/dist/{types → utils}/index.js +0 -0
@@ -1,14 +1,5 @@
1
- import {
2
- BinaryTree,
3
- BinaryTreeNode,
4
- BinaryTreeNodeId,
5
- BinaryTreeNodePropertyName,
6
- FamilyPosition,
7
- LoopType,
8
- } from './binary-tree';
9
-
10
- export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
11
- export type BSTDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
1
+ import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types';
2
+ import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree';
12
3
 
13
4
  export enum CP {lt = -1, eq = 0, gt = 1}
14
5
 
@@ -19,15 +10,6 @@ export class BSTNode<T> extends BinaryTreeNode<T> {
19
10
  }
20
11
 
21
12
  export class BST<T> extends BinaryTree<T> {
22
- protected _comparator: BSTComparator = (a, b) => a - b;
23
-
24
- protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
25
- const compared = this._comparator(a, b);
26
- if (compared > 0) return CP.gt;
27
- else if (compared < 0) return CP.lt;
28
- else return CP.eq;
29
- }
30
-
31
13
  constructor(options?: {
32
14
  comparator?: BSTComparator,
33
15
  loopType?: LoopType
@@ -373,8 +355,8 @@ export class BST<T> extends BinaryTree<T> {
373
355
  _height(this.root);
374
356
  } else {
375
357
  const stack: BSTNode<T>[] = [];
376
- let node: BSTNode<T> | null | undefined = this.root, last: BSTNode<T> | null = null,
377
- depths: Map<BSTNode<T>, number> = new Map();
358
+ let node: BSTNode<T> | null | undefined = this.root, last: BSTNode<T> | null = null;
359
+ const depths: Map<BSTNode<T>, number> = new Map();
378
360
 
379
361
  while (stack.length > 0 || node) {
380
362
  if (node) {
@@ -385,8 +367,8 @@ export class BST<T> extends BinaryTree<T> {
385
367
  if (!node.right || last === node.right) {
386
368
  node = stack.pop();
387
369
  if (node) {
388
- let left = node.left ? depths.get(node.left) ?? -1 : -1;
389
- let right = node.right ? depths.get(node.right) ?? -1 : -1;
370
+ const left = node.left ? depths.get(node.left) ?? -1 : -1;
371
+ const right = node.right ? depths.get(node.right) ?? -1 : -1;
390
372
  if (Math.abs(left - right) > 1) return false;
391
373
  depths.set(node, 1 + Math.max(left, right));
392
374
  last = node;
@@ -400,5 +382,14 @@ export class BST<T> extends BinaryTree<T> {
400
382
  return balanced;
401
383
  }
402
384
 
385
+ protected _comparator: BSTComparator = (a, b) => a - b;
386
+
387
+ protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
388
+ const compared = this._comparator(a, b);
389
+ if (compared > 0) return CP.gt;
390
+ else if (compared < 0) return CP.lt;
391
+ else return CP.eq;
392
+ }
393
+
403
394
  // --- end additional functions
404
395
  }
@@ -2,3 +2,10 @@ export * from './binary-tree';
2
2
  export * from './bst';
3
3
  export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
+ export * from './avl-tree';
6
+ export * from './b-tree';
7
+ export * from './rb-tree';
8
+ export * from './splay-tree';
9
+ export * from './aa-tree';
10
+ export * from './tree-multiset';
11
+ export * from './two-three-tree';
@@ -1,7 +1,15 @@
1
- export type SegmentTreeNodeVal = number;
1
+ import type {SegmentTreeNodeVal} from '../types';
2
2
 
3
3
  export class SegmentTreeNode {
4
+ constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
5
+ this._start = start;
6
+ this._end = end;
7
+ this._sum = sum;
8
+ this._val = val || null;
9
+ }
10
+
4
11
  protected _start = 0;
12
+
5
13
  get start(): number {
6
14
  return this._start;
7
15
  }
@@ -11,6 +19,7 @@ export class SegmentTreeNode {
11
19
  }
12
20
 
13
21
  protected _end = 0;
22
+
14
23
  get end(): number {
15
24
  return this._end;
16
25
  }
@@ -20,6 +29,7 @@ export class SegmentTreeNode {
20
29
  }
21
30
 
22
31
  protected _val: SegmentTreeNodeVal | null = null;
32
+
23
33
  get val(): SegmentTreeNodeVal | null {
24
34
  return this._val;
25
35
  }
@@ -29,6 +39,7 @@ export class SegmentTreeNode {
29
39
  }
30
40
 
31
41
  protected _sum = 0;
42
+
32
43
  get sum(): number {
33
44
  return this._sum;
34
45
  }
@@ -38,6 +49,7 @@ export class SegmentTreeNode {
38
49
  }
39
50
 
40
51
  protected _left: SegmentTreeNode | null = null;
52
+
41
53
  get left(): SegmentTreeNode | null {
42
54
  return this._left;
43
55
  }
@@ -47,6 +59,7 @@ export class SegmentTreeNode {
47
59
  }
48
60
 
49
61
  protected _right: SegmentTreeNode | null = null;
62
+
50
63
  get right(): SegmentTreeNode | null {
51
64
  return this._right;
52
65
  }
@@ -54,23 +67,12 @@ export class SegmentTreeNode {
54
67
  set right(v: SegmentTreeNode | null) {
55
68
  this._right = v;
56
69
  }
57
-
58
- constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
59
- this._start = start;
60
- this._end = end;
61
- this._sum = sum;
62
- this._val = val || null;
63
- }
64
70
  }
65
71
 
66
72
  export class SegmentTree {
67
73
  protected _values: number[] = [];
68
74
  protected _start = 0;
69
75
  protected _end: number;
70
- protected _root: SegmentTreeNode | null;
71
- get root(): SegmentTreeNode | null {
72
- return this._root;
73
- }
74
76
 
75
77
  constructor(values: number[], start?: number, end?: number) {
76
78
  start = start || 0;
@@ -81,6 +83,12 @@ export class SegmentTree {
81
83
  this._root = this.build(start, end);
82
84
  }
83
85
 
86
+ protected _root: SegmentTreeNode | null;
87
+
88
+ get root(): SegmentTreeNode | null {
89
+ return this._root;
90
+ }
91
+
84
92
  build(start: number, end: number): SegmentTreeNode {
85
93
  if (start === end) {
86
94
  return new SegmentTreeNode(start, end, this._values[start]);
@@ -1,8 +1,5 @@
1
1
  import {BST, BSTNode} from './bst';
2
- import {BinaryTreeNodeId} from './binary-tree';
3
-
4
- export type TreeMultiSetDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
5
-
2
+ import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types';
6
3
 
7
4
  export class TreeMultiSet<T> extends BST<T> {
8
5
  override createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T> {
@@ -0,0 +1,7 @@
1
+ // 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
2
+ // 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
3
+ // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
4
+ // 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
5
+ // 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
6
+ // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
7
+ // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
@@ -1,60 +1,14 @@
1
1
  import {arrayRemove, uuidV4} from '../../utils';
2
2
  import {PriorityQueue} from '../priority-queue';
3
-
4
- export type VertexId = string | number;
5
- export type DijkstraResult<V> =
6
- { distMap: Map<V, number>, preMap: Map<V, V | null>, seen: Set<V>, paths: V[][], minDist: number, minPath: V[] }
7
- | null;
8
-
9
- export interface I_Graph<V, E> {
10
-
11
- containsVertex(vertexOrId: V | VertexId): boolean;
12
-
13
- getVertex(vertexOrId: VertexId | V): V | null;
14
-
15
- getVertexId(vertexOrId: V | VertexId): VertexId;
16
-
17
- vertexSet(): Map<VertexId, V>;
18
-
19
- addVertex(v: V): boolean;
20
-
21
- removeVertex(vertexOrId: V | VertexId): boolean;
22
-
23
- removeAllVertices(vertices: V[] | VertexId[]): boolean;
24
-
25
- degreeOf(vertexOrId: V | VertexId): number;
26
-
27
- edgesOf(vertexOrId: V | VertexId): E[];
28
-
29
- containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
30
-
31
- // containsEdge(e: E): boolean;
32
-
33
- getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
34
-
35
- // getAllEdges(src: V, dest: V): E[];
36
-
37
- edgeSet(): E[];
38
-
39
- addEdge(edge: E): boolean;
40
-
41
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
42
-
43
- removeEdge(edge: E): E | null;
44
-
45
- // removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[];
46
-
47
- // removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean;
48
-
49
- setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean;
50
-
51
- getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
52
-
53
- getNeighbors(vertexOrId: V | VertexId): V[];
54
- }
3
+ import type {DijkstraResult, IGraph, VertexId} from '../types';
55
4
 
56
5
  export class AbstractVertex {
6
+ constructor(id: VertexId) {
7
+ this._id = id;
8
+ }
9
+
57
10
  private _id: VertexId;
11
+
58
12
  public get id(): VertexId {
59
13
  return this._id;
60
14
  }
@@ -62,15 +16,20 @@ export class AbstractVertex {
62
16
  public set id(v: VertexId) {
63
17
  this._id = v;
64
18
  }
65
-
66
- constructor(id: VertexId) {
67
- this._id = id;
68
- }
69
19
  }
70
20
 
71
21
  export abstract class AbstractEdge {
72
22
 
23
+ static DEFAULT_EDGE_WEIGHT = 1;
24
+
25
+ protected constructor(weight?: number) {
26
+ if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
27
+ this._weight = weight;
28
+ this._hashCode = uuidV4();
29
+ }
30
+
73
31
  private _weight: number;
32
+
74
33
  get weight(): number {
75
34
  return this._weight;
76
35
  }
@@ -88,18 +47,10 @@ export abstract class AbstractEdge {
88
47
  set hashCode(v: string) {
89
48
  this._hashCode = v;
90
49
  }
91
-
92
- protected constructor(weight?: number) {
93
- if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
94
- this._weight = weight;
95
- this._hashCode = uuidV4();
96
- }
97
-
98
- static DEFAULT_EDGE_WEIGHT = 1;
99
50
  }
100
51
 
101
52
  // Connected Component === Largest Connected Sub-Graph
102
- export abstract class AbstractGraph<V extends AbstractVertex, E extends AbstractEdge> implements I_Graph<V, E> {
53
+ export abstract class AbstractGraph<V extends AbstractVertex, E extends AbstractEdge> implements IGraph<V, E> {
103
54
 
104
55
  protected _vertices: Map<VertexId, V> = new Map<VertexId, V>();
105
56
 
@@ -192,7 +143,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
192
143
  if (!visiting.get(neighbor)) {
193
144
  path.push(neighbor);
194
145
  dfs(neighbor, dest, visiting, path);
195
- arrayRemove(path, vertex => vertex === neighbor);
146
+ arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
196
147
  }
197
148
  }
198
149
 
@@ -296,7 +247,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
296
247
  if (!visiting.get(neighbor)) {
297
248
  path.push(neighbor);
298
249
  dfs(neighbor, dest, visiting, path);
299
- arrayRemove(path, vertex => vertex === neighbor);
250
+ arrayRemove(path, (vertex: AbstractVertex) => vertex === neighbor);
300
251
  }
301
252
  }
302
253
 
@@ -338,7 +289,8 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
338
289
  }
339
290
 
340
291
  for (const vertex of vertices) {
341
- distMap.set(vertex[1], Infinity);
292
+ const vertexOrId = vertex[1];
293
+ if (vertexOrId instanceof AbstractVertex) distMap.set(vertexOrId, Infinity);
342
294
  }
343
295
  distMap.set(srcVertex, 0);
344
296
  preMap.set(srcVertex, null);
@@ -359,15 +311,19 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
359
311
 
360
312
  const getPaths = (minV: V | null) => {
361
313
  for (const vertex of vertices) {
362
- const path: V[] = [vertex[1]];
363
- let parent = preMap.get(vertex[1]);
364
- while (parent) {
365
- path.push(parent);
366
- parent = preMap.get(parent);
314
+ const vertexOrId = vertex[1];
315
+
316
+ if (vertexOrId instanceof AbstractVertex) {
317
+ const path: V[] = [vertexOrId];
318
+ let parent = preMap.get(vertexOrId);
319
+ while (parent) {
320
+ path.push(parent);
321
+ parent = preMap.get(parent);
322
+ }
323
+ const reversed = path.reverse();
324
+ if (vertex[1] === minV) minPath = reversed;
325
+ paths.push(reversed);
367
326
  }
368
- const reversed = path.reverse();
369
- if (vertex[1] === minV) minPath = reversed;
370
- paths.push(reversed);
371
327
  }
372
328
  };
373
329
 
@@ -449,7 +405,8 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
449
405
  }
450
406
 
451
407
  for (const vertex of vertices) {
452
- distMap.set(vertex[1], Infinity);
408
+ const vertexOrId = vertex[1];
409
+ if (vertexOrId instanceof AbstractVertex) distMap.set(vertexOrId, Infinity);
453
410
  }
454
411
 
455
412
  const heap = new PriorityQueue<{ id: number, val: V }>({comparator: (a, b) => a.id - b.id});
@@ -460,15 +417,19 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
460
417
 
461
418
  const getPaths = (minV: V | null) => {
462
419
  for (const vertex of vertices) {
463
- const path: V[] = [vertex[1]];
464
- let parent = preMap.get(vertex[1]);
465
- while (parent) {
466
- path.push(parent);
467
- parent = preMap.get(parent);
420
+ const vertexOrId = vertex[1];
421
+ if (vertexOrId instanceof AbstractVertex) {
422
+ const path: V[] = [vertexOrId];
423
+ let parent = preMap.get(vertexOrId);
424
+ while (parent) {
425
+ path.push(parent);
426
+ parent = preMap.get(parent);
427
+ }
428
+ const reversed = path.reverse();
429
+ if (vertex[1] === minV) minPath = reversed;
430
+ paths.push(reversed);
468
431
  }
469
- const reversed = path.reverse();
470
- if (vertex[1] === minV) minPath = reversed;
471
- paths.push(reversed);
432
+
472
433
  }
473
434
  };
474
435
 
@@ -551,7 +512,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
551
512
  let min = Infinity;
552
513
  let minPath: V[] = [];
553
514
  // TODO
554
- let hasNegativeCycle: boolean | undefined = undefined;
515
+ let hasNegativeCycle: boolean | undefined;
555
516
  if (scanNegativeCycle) hasNegativeCycle = false;
556
517
  if (!srcVertex) return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
557
518
 
@@ -598,15 +559,18 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
598
559
 
599
560
  if (genPath) {
600
561
  for (const vertex of vertices) {
601
- const path: V[] = [vertex[1]];
602
- let parent = preMap.get(vertex[1]);
603
- while (parent !== undefined) {
604
- path.push(parent);
605
- parent = preMap.get(parent);
562
+ const vertexOrId = vertex[1];
563
+ if (vertexOrId instanceof AbstractVertex) {
564
+ const path: V[] = [vertexOrId];
565
+ let parent = preMap.get(vertexOrId);
566
+ while (parent !== undefined) {
567
+ path.push(parent);
568
+ parent = preMap.get(parent);
569
+ }
570
+ const reversed = path.reverse();
571
+ if (vertex[1] === minDest) minPath = reversed;
572
+ paths.push(reversed);
606
573
  }
607
- const reversed = path.reverse();
608
- if (vertex[1] === minDest) minPath = reversed;
609
- paths.push(reversed);
610
574
  }
611
575
  }
612
576
 
@@ -1,5 +1,6 @@
1
1
  import {arrayRemove} from '../../utils';
2
- import {AbstractEdge, AbstractGraph, AbstractVertex, VertexId} from './abstract-graph';
2
+ import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
+ import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
3
4
 
4
5
  export class DirectedVertex extends AbstractVertex {
5
6
  constructor(id: VertexId) {
@@ -34,25 +35,8 @@ export class DirectedEdge extends AbstractEdge {
34
35
  }
35
36
  }
36
37
 
37
- export interface I_DirectedGraph<V, E> {
38
- incomingEdgesOf(vertex: V): E[];
39
-
40
- outgoingEdgesOf(vertex: V): E[];
41
-
42
- inDegreeOf(vertexOrId: V | VertexId): number;
43
-
44
- outDegreeOf(vertexOrId: V | VertexId): number;
45
-
46
- getEdgeSrc(e: E): V | null;
47
-
48
- getEdgeDest(e: E): V | null;
49
- }
50
-
51
- // 0 means unknown, 1 means visiting, 2 means visited;
52
- export type TopologicalStatus = 0 | 1 | 2;
53
-
54
38
  // Strongly connected, One direction connected, Weakly connected
55
- export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements I_DirectedGraph<V, E> {
39
+ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
56
40
 
57
41
  protected _outEdgeMap: Map<V, E[]> = new Map<V, E[]>();
58
42
 
@@ -120,12 +104,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
120
104
 
121
105
  const srcOutEdges = this._outEdgeMap.get(src);
122
106
  if (srcOutEdges) {
123
- arrayRemove<E>(srcOutEdges, edge => edge.dest === dest.id);
107
+ arrayRemove<E>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
124
108
  }
125
109
 
126
110
  const destInEdges = this._inEdgeMap.get(dest);
127
111
  if (destInEdges) {
128
- removed = arrayRemove<E>(destInEdges, edge => edge.src === src.id)[0] || null;
112
+ removed = arrayRemove<E>(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
129
113
  }
130
114
  return removed;
131
115
  }
@@ -137,12 +121,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
137
121
  if (src && dest) {
138
122
  const srcOutEdges = this._outEdgeMap.get(src);
139
123
  if (srcOutEdges && srcOutEdges.length > 0) {
140
- arrayRemove(srcOutEdges, edge => edge.src === src.id);
124
+ arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
141
125
  }
142
126
 
143
127
  const destInEdges = this._inEdgeMap.get(dest);
144
128
  if (destInEdges && destInEdges.length > 0) {
145
- removed = arrayRemove(destInEdges, edge => edge.dest === dest.id)[0];
129
+ removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0];
146
130
  }
147
131
 
148
132
  }
@@ -194,7 +178,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
194
178
  return this.getVertex(e.dest);
195
179
  }
196
180
 
197
- getDestinations(vertex: V | null): V[] {
181
+ getDestinations(vertex: V | VertexId | null): V[] {
198
182
  if (vertex === null) {
199
183
  return [];
200
184
  }
@@ -215,7 +199,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
215
199
  * when stored with adjacency list time: O(V+E)
216
200
  * when stored with adjacency matrix time: O(V^2)
217
201
  */
218
- topologicalSort(): V[] | null {
202
+ topologicalSort(): (V | VertexId)[] | null {
219
203
  // vector<vector<int>> g;
220
204
  // vector<int> color;
221
205
  // int last;
@@ -247,14 +231,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
247
231
  // }
248
232
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
249
233
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
250
- const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
234
+ const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V, TopologicalStatus>();
251
235
  for (const entry of this._vertices) {
252
236
  statusMap.set(entry[1], 0);
253
237
  }
254
238
 
255
- const sorted: V[] = [];
239
+ const sorted: (V | VertexId)[] = [];
256
240
  let hasCycle = false;
257
- const dfs = (cur: V) => {
241
+ const dfs = (cur: V | VertexId) => {
258
242
  statusMap.set(cur, 1);
259
243
  const children = this.getDestinations(cur);
260
244
  for (const child of children) {
@@ -1,5 +1,6 @@
1
1
  import {arrayRemove} from '../../utils';
2
- import {AbstractEdge, AbstractGraph, AbstractVertex, VertexId} from './abstract-graph';
2
+ import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
+ import type {VertexId} from '../types';
3
4
 
4
5
  export class UndirectedVertex extends AbstractVertex {
5
6
  constructor(id: VertexId) {
@@ -8,6 +9,11 @@ export class UndirectedVertex extends AbstractVertex {
8
9
  }
9
10
 
10
11
  export class UndirectedEdge extends AbstractEdge {
12
+ constructor(v1: VertexId, v2: VertexId, weight?: number) {
13
+ super(weight);
14
+ this._vertices = [v1, v2];
15
+ }
16
+
11
17
  private _vertices: [VertexId, VertexId];
12
18
 
13
19
  public get vertices() {
@@ -17,20 +23,15 @@ export class UndirectedEdge extends AbstractEdge {
17
23
  public set vertices(v: [VertexId, VertexId]) {
18
24
  this._vertices = v;
19
25
  }
20
-
21
- constructor(v1: VertexId, v2: VertexId, weight?: number) {
22
- super(weight);
23
- this._vertices = [v1, v2];
24
- }
25
26
  }
26
27
 
27
28
  export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
29
+ protected _edges: Map<V, E[]> = new Map();
30
+
28
31
  constructor() {
29
32
  super();
30
33
  }
31
34
 
32
- protected _edges: Map<V, E[]> = new Map();
33
-
34
35
  getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
35
36
  let edges: E[] | undefined = [];
36
37
 
@@ -74,11 +75,11 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
74
75
  const v1Edges = this._edges.get(vertex1);
75
76
  let removed: E | null = null;
76
77
  if (v1Edges) {
77
- removed = arrayRemove<E>(v1Edges, e => e.vertices.includes(vertex2.id))[0] || null;
78
+ removed = arrayRemove<E>(v1Edges, (e: UndirectedEdge) => e.vertices.includes(vertex2.id))[0] || null;
78
79
  }
79
80
  const v2Edges = this._edges.get(vertex2);
80
81
  if (v2Edges) {
81
- arrayRemove<E>(v2Edges, e => e.vertices.includes(vertex1.id));
82
+ arrayRemove<E>(v2Edges, (e: UndirectedEdge) => e.vertices.includes(vertex1.id));
82
83
  }
83
84
  return removed;
84
85
  }
@@ -1,4 +1,4 @@
1
- export class CoordinateSet<V> extends Map<any, V> {
1
+ export class CoordinateMap<V> extends Map<any, V> {
2
2
  private readonly _joint: string = '_';
3
3
 
4
4
  constructor(joint?: string) {
@@ -1 +1,6 @@
1
1
  export * from './hash-table';
2
+ export * from './coordinate-map';
3
+ export * from './coordinate-set';
4
+ export * from './pair';
5
+ export * from './tree-map';
6
+ export * from './tree-set';
@@ -1,17 +1,8 @@
1
1
  import {PriorityQueue} from '../priority-queue';
2
-
3
- export interface HeapOptions<T> {
4
- priority?: (element: T) => number;
5
- }
6
-
7
- export interface HeapItem<T> {
8
- priority: number;
9
- element: T | null;
10
- }
11
-
2
+ import type {HeapItem, HeapOptions} from '../types';
12
3
 
13
4
  /**
14
- * @copyright 2021 Pablo Rios <zrwusa@gmail.com>
5
+ * @copyright 2021 Tyler Zeng <zrwusa@gmail.com>
15
6
  * @license MIT
16
7
  *
17
8
  * @abstract