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
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "0.8.17",
3
+ "version": "0.9.16",
4
4
  "description": "Hash (CoordinateSet, CoordinateMap) Heap (MaxHeap, MinHeap) Binary Tree (AVL Tree, Binary Indexed Tree, Binary Search Tree, Segment Tree, Tree Multiset) Graph (Directed Graph, Undirected Graph) Linked List (Singly Linked List, Doubly Linked List) Matrix Priority Queue (Max Priority Queue, Min Priority Queue) Queue (Queue, Dequeue) Stack Trie",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
+ "build": "rm -rf dist && npx tsc",
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
9
10
  "repository": {
@@ -11,51 +12,27 @@
11
12
  "url": "git+https://github.com/zrwusa/data-structure-typed.git"
12
13
  },
13
14
  "keywords": [
15
+ "Binary Tree",
16
+ "AVL Tree",
17
+ "Binary Search Tree (BST)",
18
+ "Tree Multiset",
19
+ "Segment Tree",
20
+ "Binary Indexed Tree",
21
+ "Graph",
22
+ "Directed Graph",
23
+ "Undirected Graph",
24
+ "Singly Linked List",
14
25
  "Hash",
15
26
  "CoordinateSet",
16
27
  "CoordinateMap",
17
28
  "Heap",
18
- "MaxHeap",
19
- "MinHeap",
20
- "Binary",
21
- "Tree",
22
- "AVL",
23
- "Tree",
24
- "Binary",
25
- "Indexed",
26
- "Tree",
27
- "Binary",
28
- "Search",
29
- "Tree",
30
- "Segment",
31
- "Tree",
32
- "Tree",
33
- "Multiset",
34
- "Graph",
35
- "Directed",
36
- "Graph",
37
- "Undirected",
38
- "Graph",
39
- "Linked",
40
- "List",
41
- "Singly",
42
- "Linked",
43
- "List",
44
- "Doubly",
45
- "Linked",
46
- "List",
47
- "Matrix",
48
- "Priority",
49
- "Queue",
50
- "Max",
51
- "Priority",
52
- "Queue",
53
- "Min",
54
- "Priority",
55
- "Queue",
56
- "Queue",
29
+ "Doubly Linked List",
30
+ "Priority Queue",
31
+ "Max Priority Queue",
32
+ "Min Priority Queue",
57
33
  "Queue",
58
- "Dequeue",
34
+ "ObjectDeque",
35
+ "ArrayDeque",
59
36
  "Stack",
60
37
  "Trie"
61
38
  ],
@@ -67,7 +44,8 @@
67
44
  "homepage": "https://github.com/zrwusa/data-structure-typed#readme",
68
45
  "types": "dist/index.d.ts",
69
46
  "devDependencies": {
70
- "@types/lodash": "^4.14.178",
47
+ "@types/lodash": "^4.14.197",
48
+ "@types/node": "^20.4.9",
71
49
  "typescript": "^4.6.2"
72
50
  },
73
51
  "dependencies": {
@@ -1,10 +1,5 @@
1
1
  import {BST, BSTNode} from './bst';
2
- import {BinaryTreeNodeId} from './binary-tree';
3
-
4
- export interface AVLTreeDeleted<T> {
5
- deleted: AVLTreeNode<T> | null;
6
- needBalanced: AVLTreeNode<T> | null;
7
- }
2
+ import type {AVLTreeDeleted, BinaryTreeNodeId} from '../types';
8
3
 
9
4
  export class AVLTreeNode<T> extends BSTNode<T> {
10
5
  override clone(): AVLTreeNode<T> {
@@ -5,6 +5,10 @@ export class BinaryIndexedTree {
5
5
  this._sumTree = new Array<number>(n + 1).fill(0);
6
6
  }
7
7
 
8
+ static lowBit(x: number) {
9
+ return x & (-x);
10
+ }
11
+
8
12
  update(i: number, delta: number) {
9
13
  while (i < this._sumTree.length) {
10
14
  this._sumTree[i] += delta;
@@ -26,8 +30,4 @@ export class BinaryIndexedTree {
26
30
  throw 'Index out of bounds';
27
31
  return this.getPrefixSum(end) - this.getPrefixSum(start);
28
32
  }
29
-
30
- static lowBit(x: number) {
31
- return x & (-x);
32
- }
33
33
  }
@@ -1,25 +1,25 @@
1
- import {ThunkOrValue, trampoline} from '../trampoline';
2
-
3
- export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
4
- export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
5
- export type DFSOrderPattern = 'in' | 'pre' | 'post';
6
- export type BinaryTreeNodeId = number;
7
- export type BinaryTreeDeleted<T> = { deleted: BinaryTreeNode<T> | null | undefined, needBalanced: BinaryTreeNode<T> | null };
8
- export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
9
- export type ResultsByProperty<T> = ResultByProperty<T>[];
10
-
11
- export interface BinaryTreeNodeObj<T> {
12
- id: BinaryTreeNodeId;
13
- val: T;
14
- count?: number;
15
- }
1
+ import {trampoline} from '../trampoline';
2
+ import type {
3
+ BinaryTreeDeleted,
4
+ BinaryTreeNodeId,
5
+ BinaryTreeNodePropertyName, DFSOrderPattern,
6
+ NodeOrPropertyName, ResultByProperty,
7
+ ResultsByProperty
8
+ } from '../types';
16
9
 
17
10
  export enum FamilyPosition {root, left, right}
18
11
 
19
12
  export enum LoopType { iterative = 1, recursive = 2}
20
13
 
21
14
  export class BinaryTreeNode<T> {
15
+ constructor(id: BinaryTreeNodeId, val: T, count?: number) {
16
+ this._id = id;
17
+ this._val = val;
18
+ this._count = count ?? 1;
19
+ }
20
+
22
21
  protected _id: BinaryTreeNodeId;
22
+
23
23
  get id(): BinaryTreeNodeId {
24
24
  return this._id;
25
25
  }
@@ -29,6 +29,7 @@ export class BinaryTreeNode<T> {
29
29
  }
30
30
 
31
31
  protected _val: T;
32
+
32
33
  get val(): T {
33
34
  return this._val;
34
35
  }
@@ -38,6 +39,7 @@ export class BinaryTreeNode<T> {
38
39
  }
39
40
 
40
41
  protected _left?: BinaryTreeNode<T> | null;
42
+
41
43
  get left(): BinaryTreeNode<T> | null | undefined {
42
44
  return this._left;
43
45
  }
@@ -51,6 +53,7 @@ export class BinaryTreeNode<T> {
51
53
  }
52
54
 
53
55
  protected _right?: BinaryTreeNode<T> | null;
56
+
54
57
  get right(): BinaryTreeNode<T> | null | undefined {
55
58
  return this._right;
56
59
  }
@@ -63,7 +66,8 @@ export class BinaryTreeNode<T> {
63
66
  this._right = v;
64
67
  }
65
68
 
66
- protected _parent: BinaryTreeNode<T> | null | undefined = undefined;
69
+ protected _parent: BinaryTreeNode<T> | null | undefined;
70
+
67
71
  get parent(): BinaryTreeNode<T> | null | undefined {
68
72
  return this._parent;
69
73
  }
@@ -73,6 +77,7 @@ export class BinaryTreeNode<T> {
73
77
  }
74
78
 
75
79
  protected _familyPosition: FamilyPosition = FamilyPosition.root;
80
+
76
81
  get familyPosition(): FamilyPosition {
77
82
  return this._familyPosition;
78
83
  }
@@ -82,6 +87,7 @@ export class BinaryTreeNode<T> {
82
87
  }
83
88
 
84
89
  protected _count = 1;
90
+
85
91
  get count(): number {
86
92
  return this._count;
87
93
  }
@@ -100,12 +106,6 @@ export class BinaryTreeNode<T> {
100
106
  this._height = v;
101
107
  }
102
108
 
103
- constructor(id: BinaryTreeNodeId, val: T, count?: number) {
104
- this._id = id;
105
- this._val = val;
106
- this._count = count ?? 1;
107
- }
108
-
109
109
  swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T> {
110
110
  const {val, count, height} = swapNode;
111
111
  const tempNode = new BinaryTreeNode<T>(swapNode.id, val);
@@ -131,8 +131,36 @@ export class BinaryTreeNode<T> {
131
131
  }
132
132
 
133
133
  export class BinaryTree<T> {
134
+ protected _loopType: LoopType = LoopType.iterative;
135
+ protected _visitedId: BinaryTreeNodeId[] = [];
136
+ protected _visitedVal: Array<T> = [];
137
+ protected _visitedNode: BinaryTreeNode<T>[] = [];
138
+ protected _visitedCount: number[] = [];
139
+ protected _visitedLeftSum: number[] = [];
140
+ private readonly _autoIncrementId: boolean = false;
141
+ private _maxId: number = -1;
142
+ private readonly _isDuplicatedVal: boolean = false;
143
+
144
+ constructor(options?: {
145
+ loopType?: LoopType,
146
+ autoIncrementId?: boolean,
147
+ isDuplicatedVal?: boolean
148
+ }) {
149
+ if (options !== undefined) {
150
+ const {
151
+ loopType = LoopType.iterative,
152
+ autoIncrementId = false,
153
+ isDuplicatedVal = false
154
+ } = options;
155
+ this._isDuplicatedVal = isDuplicatedVal;
156
+ this._autoIncrementId = autoIncrementId;
157
+ this._loopType = loopType;
158
+ }
159
+ }
160
+
134
161
  protected _root: BinaryTreeNode<T> | null = null;
135
- public get root(): BinaryTreeNode<T> | null {
162
+
163
+ protected get root(): BinaryTreeNode<T> | null {
136
164
  return this._root;
137
165
  }
138
166
 
@@ -145,7 +173,8 @@ export class BinaryTree<T> {
145
173
  }
146
174
 
147
175
  protected _size = 0;
148
- get size(): number {
176
+
177
+ protected get size(): number {
149
178
  return this._size;
150
179
  }
151
180
 
@@ -154,7 +183,8 @@ export class BinaryTree<T> {
154
183
  }
155
184
 
156
185
  protected _count = 0;
157
- get count(): number {
186
+
187
+ protected get count(): number {
158
188
  return this._count;
159
189
  }
160
190
 
@@ -162,40 +192,8 @@ export class BinaryTree<T> {
162
192
  this._count = v;
163
193
  }
164
194
 
165
- private readonly _autoIncrementId: boolean = false;
166
- private _maxId: number = -1;
167
- private readonly _isDuplicatedVal: boolean = false;
168
-
169
- protected _loopType: LoopType = LoopType.iterative;
170
- protected _visitedId: BinaryTreeNodeId[] = [];
171
- protected _visitedVal: Array<T> = [];
172
- protected _visitedNode: BinaryTreeNode<T>[] = [];
173
- protected _visitedCount: number[] = [];
174
- protected _visitedLeftSum: number[] = [];
175
-
176
- protected _resetResults() {
177
- this._visitedId = [];
178
- this._visitedVal = [];
179
- this._visitedNode = [];
180
- this._visitedCount = [];
181
- this._visitedLeftSum = [];
182
- }
183
-
184
- constructor(options?: {
185
- loopType?: LoopType,
186
- autoIncrementId?: boolean,
187
- isDuplicatedVal?: boolean
188
- }) {
189
- if (options !== undefined) {
190
- const {
191
- loopType = LoopType.iterative,
192
- autoIncrementId = false,
193
- isDuplicatedVal = false
194
- } = options;
195
- this._isDuplicatedVal = isDuplicatedVal;
196
- this._autoIncrementId = autoIncrementId;
197
- this._loopType = loopType;
198
- }
195
+ getCount(): number {
196
+ return this._count;
199
197
  }
200
198
 
201
199
  createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode<T> | null {
@@ -393,8 +391,8 @@ export class BinaryTree<T> {
393
391
  return _getMaxHeight(beginRoot);
394
392
  } else {
395
393
  const stack: BinaryTreeNode<T>[] = [];
396
- let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null,
397
- depths: Map<BinaryTreeNode<T>, number> = new Map();
394
+ let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null;
395
+ const depths: Map<BinaryTreeNode<T>, number> = new Map();
398
396
 
399
397
  while (stack.length > 0 || node) {
400
398
  if (node) {
@@ -405,8 +403,8 @@ export class BinaryTree<T> {
405
403
  if (!node.right || last === node.right) {
406
404
  node = stack.pop();
407
405
  if (node) {
408
- let leftHeight = node.left ? depths.get(node.left) ?? -1 : -1;
409
- let rightHeight = node.right ? depths.get(node.right) ?? -1 : -1;
406
+ const leftHeight = node.left ? depths.get(node.left) ?? -1 : -1;
407
+ const rightHeight = node.right ? depths.get(node.right) ?? -1 : -1;
410
408
  depths.set(node, 1 + Math.max(leftHeight, rightHeight));
411
409
  last = node;
412
410
  node = null;
@@ -435,8 +433,8 @@ export class BinaryTree<T> {
435
433
  return _getMinHeight(beginRoot);
436
434
  } else {
437
435
  const stack: BinaryTreeNode<T>[] = [];
438
- let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null,
439
- depths: Map<BinaryTreeNode<T>, number> = new Map();
436
+ let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null;
437
+ const depths: Map<BinaryTreeNode<T>, number> = new Map();
440
438
 
441
439
  while (stack.length > 0 || node) {
442
440
  if (node) {
@@ -447,8 +445,8 @@ export class BinaryTree<T> {
447
445
  if (!node.right || last === node.right) {
448
446
  node = stack.pop();
449
447
  if (node) {
450
- let leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
451
- let rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
448
+ const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
449
+ const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
452
450
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
453
451
  last = node;
454
452
  node = null;
@@ -514,76 +512,14 @@ export class BinaryTree<T> {
514
512
  return result;
515
513
  }
516
514
 
517
- protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
518
- switch (propertyName) {
519
- case 'id':
520
- if (cur.id === nodeProperty) {
521
- result.push(cur);
522
- return !!onlyOne;
523
- }
524
- break;
525
- case 'count':
526
- if (cur.count === nodeProperty) {
527
- result.push(cur);
528
- return !!onlyOne;
529
- }
530
- break;
531
- case 'val':
532
- if (cur.val === nodeProperty) {
533
- result.push(cur);
534
- return !!onlyOne;
535
- }
536
- break;
537
- default:
538
- if (cur.id === nodeProperty) {
539
- result.push(cur);
540
- return !!onlyOne;
541
- }
542
- break;
543
- }
544
- }
545
-
546
- protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName ?: NodeOrPropertyName) {
547
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
548
-
549
- switch (nodeOrPropertyName) {
550
- case 'id':
551
- this._visitedId.push(node.id);
552
- break;
553
- case 'val':
554
- this._visitedVal.push(node.val);
555
- break;
556
- case 'node':
557
- this._visitedNode.push(node);
558
- break;
559
- case 'count':
560
- this._visitedCount.push(node.count);
561
- break;
562
- default:
563
- this._visitedId.push(node.id);
564
- break;
565
- }
566
- }
567
-
568
- protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
569
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
570
-
571
- switch (nodeOrPropertyName) {
572
- case 'id':
573
- return this._visitedId;
574
- case 'val':
575
- return this._visitedVal;
576
- case 'node':
577
- return this._visitedNode;
578
- case 'count':
579
- return this._visitedCount;
580
- default:
581
- return this._visitedId;
582
- }
515
+ getRoot(): BinaryTreeNode<T> | null {
516
+ return this.root;
583
517
  }
584
518
 
585
519
  getLeftMost(): BinaryTreeNode<T> | null;
520
+
586
521
  getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
522
+
587
523
  getLeftMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
588
524
  node = node ?? this.root;
589
525
  if (!node) return node;
@@ -598,7 +534,7 @@ export class BinaryTree<T> {
598
534
  return _traverse(node);
599
535
  } else {
600
536
  // Indirect implementation of iteration using tail recursion optimization
601
- const _traverse = trampoline((cur: BinaryTreeNode<T>): ThunkOrValue<BinaryTreeNode<T> | null> => {
537
+ const _traverse = trampoline((cur: BinaryTreeNode<T>) => {
602
538
  if (!cur.left) return cur;
603
539
  return _traverse.cont(cur.left);
604
540
  });
@@ -608,7 +544,9 @@ export class BinaryTree<T> {
608
544
  }
609
545
 
610
546
  getRightMost(): BinaryTreeNode<T> | null;
547
+
611
548
  getRightMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
549
+
612
550
  getRightMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
613
551
  node = node ?? this.root;
614
552
  if (!node) return node;
@@ -622,7 +560,7 @@ export class BinaryTree<T> {
622
560
  return _traverse(node);
623
561
  } else {
624
562
  // Indirect implementation of iteration using tail recursion optimization
625
- const _traverse = trampoline((cur: BinaryTreeNode<T>): ThunkOrValue<BinaryTreeNode<T> | null> => {
563
+ const _traverse = trampoline((cur: BinaryTreeNode<T>) => {
626
564
  if (!cur.right) return cur;
627
565
  return _traverse.cont(cur.right);
628
566
  });
@@ -653,7 +591,7 @@ export class BinaryTree<T> {
653
591
  curr = curr.left;
654
592
  }
655
593
  curr = stack.pop()!;
656
- if (prev >= curr.id) return false;
594
+ if (!(curr) || prev >= curr.id) return false;
657
595
  prev = curr.id;
658
596
  curr = curr.right;
659
597
  }
@@ -779,10 +717,15 @@ export class BinaryTree<T> {
779
717
  }
780
718
 
781
719
  BFS(): BinaryTreeNodeId[];
720
+
782
721
  BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
722
+
783
723
  BFS(nodeOrPropertyName: 'val'): T[];
724
+
784
725
  BFS(nodeOrPropertyName: 'node'): BinaryTreeNode<T>[];
726
+
785
727
  BFS(nodeOrPropertyName: 'count'): number[];
728
+
786
729
  BFS(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
787
730
  nodeOrPropertyName = nodeOrPropertyName ?? 'id';
788
731
  this._resetResults();
@@ -801,10 +744,15 @@ export class BinaryTree<T> {
801
744
  }
802
745
 
803
746
  DFS(): BinaryTreeNodeId[];
747
+
804
748
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
749
+
805
750
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
751
+
806
752
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
753
+
807
754
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
755
+
808
756
  DFS(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
809
757
  pattern = pattern ?? 'in';
810
758
  nodeOrPropertyName = nodeOrPropertyName ?? 'id';
@@ -834,9 +782,13 @@ export class BinaryTree<T> {
834
782
  }
835
783
 
836
784
  DFSIterative(): BinaryTreeNodeId[];
785
+
837
786
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
787
+
838
788
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
789
+
839
790
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
791
+
840
792
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
841
793
 
842
794
  /**
@@ -889,10 +841,15 @@ export class BinaryTree<T> {
889
841
  }
890
842
 
891
843
  levelIterative(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[];
844
+
892
845
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
846
+
893
847
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[];
848
+
894
849
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
850
+
895
851
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[];
852
+
896
853
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
897
854
  nodeOrPropertyName = nodeOrPropertyName || 'id';
898
855
  node = node || this.root;
@@ -918,10 +875,15 @@ export class BinaryTree<T> {
918
875
  }
919
876
 
920
877
  listLevels(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[][];
878
+
921
879
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
880
+
922
881
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[][];
882
+
923
883
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[][];
884
+
924
885
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[][];
886
+
925
887
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: NodeOrPropertyName): ResultByProperty<T>[][] {
926
888
  nodeOrPropertyName = nodeOrPropertyName || 'id';
927
889
  node = node || this.root;
@@ -977,9 +939,11 @@ export class BinaryTree<T> {
977
939
 
978
940
  getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T> {
979
941
  if (node.left) {
980
- let predecessor: BinaryTreeNode<T> | null = node.left;
981
- while (predecessor.right && predecessor.right !== node) {
982
- predecessor = predecessor.right;
942
+ let predecessor: BinaryTreeNode<T> | null | undefined = node.left;
943
+ while (!(predecessor) || predecessor.right && predecessor.right !== node) {
944
+ if (predecessor) {
945
+ predecessor = predecessor.right;
946
+ }
983
947
  }
984
948
  return predecessor;
985
949
  } else {
@@ -988,10 +952,15 @@ export class BinaryTree<T> {
988
952
  }
989
953
 
990
954
  morris(): BinaryTreeNodeId[];
955
+
991
956
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
957
+
992
958
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
959
+
993
960
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
961
+
994
962
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
963
+
995
964
  /**
996
965
  * The time complexity of Morris traversal is O(n), it's may slower than others
997
966
  * The space complexity Morris traversal is O(1) because no using stack
@@ -1084,5 +1053,81 @@ export class BinaryTree<T> {
1084
1053
  return this._getResultByPropertyName(nodeOrPropertyName);
1085
1054
  }
1086
1055
 
1056
+ protected _resetResults() {
1057
+ this._visitedId = [];
1058
+ this._visitedVal = [];
1059
+ this._visitedNode = [];
1060
+ this._visitedCount = [];
1061
+ this._visitedLeftSum = [];
1062
+ }
1063
+
1064
+ protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
1065
+ switch (propertyName) {
1066
+ case 'id':
1067
+ if (cur.id === nodeProperty) {
1068
+ result.push(cur);
1069
+ return !!onlyOne;
1070
+ }
1071
+ break;
1072
+ case 'count':
1073
+ if (cur.count === nodeProperty) {
1074
+ result.push(cur);
1075
+ return !!onlyOne;
1076
+ }
1077
+ break;
1078
+ case 'val':
1079
+ if (cur.val === nodeProperty) {
1080
+ result.push(cur);
1081
+ return !!onlyOne;
1082
+ }
1083
+ break;
1084
+ default:
1085
+ if (cur.id === nodeProperty) {
1086
+ result.push(cur);
1087
+ return !!onlyOne;
1088
+ }
1089
+ break;
1090
+ }
1091
+ }
1092
+
1093
+ protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName ?: NodeOrPropertyName) {
1094
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1095
+
1096
+ switch (nodeOrPropertyName) {
1097
+ case 'id':
1098
+ this._visitedId.push(node.id);
1099
+ break;
1100
+ case 'val':
1101
+ this._visitedVal.push(node.val);
1102
+ break;
1103
+ case 'node':
1104
+ this._visitedNode.push(node);
1105
+ break;
1106
+ case 'count':
1107
+ this._visitedCount.push(node.count);
1108
+ break;
1109
+ default:
1110
+ this._visitedId.push(node.id);
1111
+ break;
1112
+ }
1113
+ }
1114
+
1115
+ protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
1116
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1117
+
1118
+ switch (nodeOrPropertyName) {
1119
+ case 'id':
1120
+ return this._visitedId;
1121
+ case 'val':
1122
+ return this._visitedVal;
1123
+ case 'node':
1124
+ return this._visitedNode;
1125
+ case 'count':
1126
+ return this._visitedCount;
1127
+ default:
1128
+ return this._visitedId;
1129
+ }
1130
+ }
1131
+
1087
1132
  // --- end additional methods ---
1088
1133
  }