data-structure-typed 0.9.16 → 1.3.1

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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,1102 +1,40 @@
1
1
  "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
9
- }
10
- };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
29
9
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.BinaryTree = exports.BinaryTreeNode = exports.LoopType = exports.FamilyPosition = void 0;
31
- var trampoline_1 = require("../trampoline");
32
- var FamilyPosition;
33
- (function (FamilyPosition) {
34
- FamilyPosition[FamilyPosition["root"] = 0] = "root";
35
- FamilyPosition[FamilyPosition["left"] = 1] = "left";
36
- FamilyPosition[FamilyPosition["right"] = 2] = "right";
37
- })(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
38
- var LoopType;
39
- (function (LoopType) {
40
- LoopType[LoopType["iterative"] = 1] = "iterative";
41
- LoopType[LoopType["recursive"] = 2] = "recursive";
42
- })(LoopType = exports.LoopType || (exports.LoopType = {}));
43
- var BinaryTreeNode = /** @class */ (function () {
44
- function BinaryTreeNode(id, val, count) {
45
- this._familyPosition = FamilyPosition.root;
46
- this._count = 1;
47
- this._height = 0;
48
- this._id = id;
49
- this._val = val;
50
- this._count = count !== null && count !== void 0 ? count : 1;
10
+ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
+ const abstract_binary_tree_1 = require("./abstract-binary-tree");
12
+ class BinaryTreeNode extends abstract_binary_tree_1.AbstractBinaryTreeNode {
13
+ constructor(id, val) {
14
+ super(id, val);
51
15
  }
52
- Object.defineProperty(BinaryTreeNode.prototype, "id", {
53
- get: function () {
54
- return this._id;
55
- },
56
- set: function (v) {
57
- this._id = v;
58
- },
59
- enumerable: false,
60
- configurable: true
61
- });
62
- Object.defineProperty(BinaryTreeNode.prototype, "val", {
63
- get: function () {
64
- return this._val;
65
- },
66
- set: function (v) {
67
- this._val = v;
68
- },
69
- enumerable: false,
70
- configurable: true
71
- });
72
- Object.defineProperty(BinaryTreeNode.prototype, "left", {
73
- get: function () {
74
- return this._left;
75
- },
76
- set: function (v) {
77
- if (v) {
78
- v.parent = this;
79
- v.familyPosition = FamilyPosition.left;
80
- }
81
- this._left = v;
82
- },
83
- enumerable: false,
84
- configurable: true
85
- });
86
- Object.defineProperty(BinaryTreeNode.prototype, "right", {
87
- get: function () {
88
- return this._right;
89
- },
90
- set: function (v) {
91
- if (v) {
92
- v.parent = this;
93
- v.familyPosition = FamilyPosition.right;
94
- }
95
- this._right = v;
96
- },
97
- enumerable: false,
98
- configurable: true
99
- });
100
- Object.defineProperty(BinaryTreeNode.prototype, "parent", {
101
- get: function () {
102
- return this._parent;
103
- },
104
- set: function (v) {
105
- this._parent = v;
106
- },
107
- enumerable: false,
108
- configurable: true
109
- });
110
- Object.defineProperty(BinaryTreeNode.prototype, "familyPosition", {
111
- get: function () {
112
- return this._familyPosition;
113
- },
114
- set: function (v) {
115
- this._familyPosition = v;
116
- },
117
- enumerable: false,
118
- configurable: true
119
- });
120
- Object.defineProperty(BinaryTreeNode.prototype, "count", {
121
- get: function () {
122
- return this._count;
123
- },
124
- set: function (v) {
125
- this._count = v;
126
- },
127
- enumerable: false,
128
- configurable: true
129
- });
130
- Object.defineProperty(BinaryTreeNode.prototype, "height", {
131
- get: function () {
132
- return this._height;
133
- },
134
- set: function (v) {
135
- this._height = v;
136
- },
137
- enumerable: false,
138
- configurable: true
139
- });
140
- BinaryTreeNode.prototype.swapLocation = function (swapNode) {
141
- var val = swapNode.val, count = swapNode.count, height = swapNode.height;
142
- var tempNode = new BinaryTreeNode(swapNode.id, val);
143
- tempNode.val = val;
144
- tempNode.count = count;
145
- tempNode.height = height;
146
- swapNode.id = this.id;
147
- swapNode.val = this.val;
148
- swapNode.count = this.count;
149
- swapNode.height = this.height;
150
- this.id = tempNode.id;
151
- this.val = tempNode.val;
152
- this.count = tempNode.count;
153
- this.height = tempNode.height;
154
- return swapNode;
155
- };
156
- BinaryTreeNode.prototype.clone = function () {
157
- return new BinaryTreeNode(this.id, this.val, this.count);
158
- };
159
- return BinaryTreeNode;
160
- }());
16
+ }
161
17
  exports.BinaryTreeNode = BinaryTreeNode;
162
- var BinaryTree = /** @class */ (function () {
163
- function BinaryTree(options) {
164
- this._loopType = LoopType.iterative;
165
- this._visitedId = [];
166
- this._visitedVal = [];
167
- this._visitedNode = [];
168
- this._visitedCount = [];
169
- this._visitedLeftSum = [];
170
- this._autoIncrementId = false;
171
- this._maxId = -1;
172
- this._isDuplicatedVal = false;
173
- this._root = null;
174
- this._size = 0;
175
- this._count = 0;
176
- if (options !== undefined) {
177
- var _a = options.loopType, loopType = _a === void 0 ? LoopType.iterative : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.isDuplicatedVal, isDuplicatedVal = _c === void 0 ? false : _c;
178
- this._isDuplicatedVal = isDuplicatedVal;
179
- this._autoIncrementId = autoIncrementId;
180
- this._loopType = loopType;
181
- }
182
- }
183
- Object.defineProperty(BinaryTree.prototype, "root", {
184
- get: function () {
185
- return this._root;
186
- },
187
- set: function (v) {
188
- if (v) {
189
- v.parent = null;
190
- v.familyPosition = FamilyPosition.root;
191
- }
192
- this._root = v;
193
- },
194
- enumerable: false,
195
- configurable: true
196
- });
197
- Object.defineProperty(BinaryTree.prototype, "size", {
198
- get: function () {
199
- return this._size;
200
- },
201
- set: function (v) {
202
- this._size = v;
203
- },
204
- enumerable: false,
205
- configurable: true
206
- });
207
- Object.defineProperty(BinaryTree.prototype, "count", {
208
- get: function () {
209
- return this._count;
210
- },
211
- set: function (v) {
212
- this._count = v;
213
- },
214
- enumerable: false,
215
- configurable: true
216
- });
217
- BinaryTree.prototype.getCount = function () {
218
- return this._count;
219
- };
220
- BinaryTree.prototype.createNode = function (id, val, count) {
221
- return val !== null ? new BinaryTreeNode(id, val, count) : null;
222
- };
223
- BinaryTree.prototype.clear = function () {
224
- this.root = null;
225
- this.size = 0;
226
- this.count = 0;
227
- this._maxId = -1;
228
- };
229
- BinaryTree.prototype.isEmpty = function () {
230
- return this.size === 0;
231
- };
232
- BinaryTree.prototype.insertTo = function (_a) {
233
- var _b, _c;
234
- var newNode = _a.newNode, parent = _a.parent;
235
- if (parent) {
236
- if (parent.left === undefined) {
237
- if (newNode) {
238
- newNode.parent = parent;
239
- newNode.familyPosition = FamilyPosition.left;
240
- }
241
- parent.left = newNode;
242
- if (newNode !== null) {
243
- this.size++;
244
- this.count += (_b = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _b !== void 0 ? _b : 0;
245
- }
246
- return parent.left;
247
- }
248
- else if (parent.right === undefined) {
249
- if (newNode) {
250
- newNode.parent = parent;
251
- newNode.familyPosition = FamilyPosition.right;
252
- }
253
- parent.right = newNode;
254
- if (newNode !== null) {
255
- this.size++;
256
- this.count += (_c = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _c !== void 0 ? _c : 0;
257
- }
258
- return parent.right;
259
- }
260
- else {
261
- return;
262
- }
263
- }
264
- else {
265
- return;
266
- }
267
- };
268
- BinaryTree.prototype.put = function (id, val, count) {
269
- var _this = this;
270
- count = count !== null && count !== void 0 ? count : 1;
271
- var _bfs = function (root, newNode) {
272
- var queue = [root];
273
- while (queue.length > 0) {
274
- var cur = queue.shift();
275
- if (cur) {
276
- var inserted_1 = _this.insertTo({ newNode: newNode, parent: cur });
277
- if (inserted_1 !== undefined)
278
- return inserted_1;
279
- if (cur.left)
280
- queue.push(cur.left);
281
- if (cur.right)
282
- queue.push(cur.right);
283
- }
284
- else
285
- return;
286
- }
287
- return;
288
- };
289
- var inserted;
290
- var needInsert = val !== null ? new BinaryTreeNode(id, val, count) : null;
291
- var existNode = val !== null ? this.get(id, 'id') : null;
292
- if (this.root) {
293
- if (existNode) {
294
- existNode.count += count;
295
- existNode.val = val;
296
- if (needInsert !== null) {
297
- this.count += count;
298
- inserted = existNode;
299
- }
300
- }
301
- else {
302
- inserted = _bfs(this.root, needInsert);
303
- }
304
- }
305
- else {
306
- this.root = val !== null ? new BinaryTreeNode(id, val, count) : null;
307
- if (needInsert !== null) {
308
- this.size = 1;
309
- this.count = count;
310
- }
311
- inserted = this.root;
312
- }
313
- return inserted;
314
- };
315
- BinaryTree.prototype.insertMany = function (data) {
316
- var e_1, _a, e_2, _b;
317
- var _c;
318
- var inserted = [];
319
- var map = new Map();
320
- if (!this._isDuplicatedVal) {
321
- try {
322
- for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
323
- var i = data_1_1.value;
324
- map.set(i, ((_c = map.get(i)) !== null && _c !== void 0 ? _c : 0) + 1);
325
- }
326
- }
327
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
328
- finally {
329
- try {
330
- if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
331
- }
332
- finally { if (e_1) throw e_1.error; }
333
- }
334
- }
335
- try {
336
- for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
337
- var item = data_2_1.value;
338
- var count = this._isDuplicatedVal ? 1 : map.get(item);
339
- if (item instanceof BinaryTreeNode) {
340
- inserted.push(this.put(item.id, item.val, item.count));
341
- }
342
- else if (typeof item === 'number' && !this._autoIncrementId) {
343
- if (!this._isDuplicatedVal) {
344
- if (map.get(item) !== undefined) {
345
- inserted.push(this.put(item, item, count));
346
- map.delete(item);
347
- }
348
- }
349
- else {
350
- inserted.push(this.put(item, item, 1));
351
- }
352
- }
353
- else {
354
- if (item !== null) {
355
- if (!this._isDuplicatedVal) {
356
- if (map.get(item) !== undefined) {
357
- inserted.push(this.put(++this._maxId, item, count));
358
- map.delete(item);
359
- }
360
- }
361
- else {
362
- inserted.push(this.put(++this._maxId, item, 1));
363
- }
364
- }
365
- else {
366
- inserted.push(this.put(Number.MAX_SAFE_INTEGER, item, 0));
367
- }
368
- }
369
- }
370
- }
371
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
372
- finally {
373
- try {
374
- if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
375
- }
376
- finally { if (e_2) throw e_2.error; }
377
- }
378
- return inserted;
379
- };
380
- BinaryTree.prototype.fill = function (data) {
381
- this.clear();
382
- return data.length === this.insertMany(data).length;
383
- };
384
- BinaryTree.prototype.remove = function (id, ignoreCount) {
385
- var nodes = this.getNodes(id, 'id', true);
386
- var node = nodes[0];
387
- if (!node)
388
- node = undefined;
389
- else if (node.count > 1 && !ignoreCount) {
390
- node.count--;
391
- this.count--;
392
- }
393
- else if (node instanceof BinaryTreeNode) {
394
- var _a = __read(this.getSubTreeSizeAndCount(node), 2), subSize = _a[0], subCount = _a[1];
395
- switch (node.familyPosition) {
396
- case 0:
397
- this.size -= subSize;
398
- this.count -= subCount;
399
- node = undefined;
400
- break;
401
- case 1:
402
- if (node.parent) {
403
- this.size -= subSize;
404
- this.count -= subCount;
405
- node.parent.left = null;
406
- }
407
- break;
408
- case 2:
409
- if (node.parent) {
410
- this.size -= subSize;
411
- this.count -= subCount;
412
- node.parent.right = null;
413
- }
414
- break;
415
- }
416
- }
417
- return [{ deleted: node, needBalanced: null }];
418
- };
419
- BinaryTree.prototype.getDepth = function (node) {
420
- var depth = 0;
421
- while (node.parent) {
422
- depth++;
423
- node = node.parent;
424
- }
425
- return depth;
426
- };
427
- BinaryTree.prototype.getHeight = function (beginRoot) {
428
- var _a, _b, _c;
429
- beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
430
- if (!beginRoot)
431
- return -1;
432
- if (this._loopType === LoopType.recursive) {
433
- var _getMaxHeight_1 = function (cur) {
434
- if (!cur)
435
- return -1;
436
- var leftHeight = _getMaxHeight_1(cur.left);
437
- var rightHeight = _getMaxHeight_1(cur.right);
438
- return Math.max(leftHeight, rightHeight) + 1;
439
- };
440
- return _getMaxHeight_1(beginRoot);
441
- }
442
- else {
443
- var stack = [];
444
- var node = beginRoot, last = null;
445
- var depths = new Map();
446
- while (stack.length > 0 || node) {
447
- if (node) {
448
- stack.push(node);
449
- node = node.left;
450
- }
451
- else {
452
- node = stack[stack.length - 1];
453
- if (!node.right || last === node.right) {
454
- node = stack.pop();
455
- if (node) {
456
- var leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
457
- var rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
458
- depths.set(node, 1 + Math.max(leftHeight, rightHeight));
459
- last = node;
460
- node = null;
461
- }
462
- }
463
- else
464
- node = node.right;
465
- }
466
- }
467
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
468
- }
469
- };
470
- BinaryTree.prototype.getMinHeight = function (beginRoot) {
471
- var _a, _b, _c;
472
- beginRoot = beginRoot || this.root;
473
- if (!beginRoot)
474
- return -1;
475
- if (this._loopType === LoopType.recursive) {
476
- var _getMinHeight_1 = function (cur) {
477
- if (!cur)
478
- return 0;
479
- if (!cur.left && !cur.right)
480
- return 0;
481
- var leftMinHeight = _getMinHeight_1(cur.left);
482
- var rightMinHeight = _getMinHeight_1(cur.right);
483
- return Math.min(leftMinHeight, rightMinHeight) + 1;
484
- };
485
- return _getMinHeight_1(beginRoot);
486
- }
487
- else {
488
- var stack = [];
489
- var node = beginRoot, last = null;
490
- var depths = new Map();
491
- while (stack.length > 0 || node) {
492
- if (node) {
493
- stack.push(node);
494
- node = node.left;
495
- }
496
- else {
497
- node = stack[stack.length - 1];
498
- if (!node.right || last === node.right) {
499
- node = stack.pop();
500
- if (node) {
501
- var leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
502
- var rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
503
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
504
- last = node;
505
- node = null;
506
- }
507
- }
508
- else
509
- node = node.right;
510
- }
511
- }
512
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
513
- }
514
- };
515
- BinaryTree.prototype.isBalanced = function (beginRoot) {
516
- return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
517
- };
518
- BinaryTree.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
519
- var _this = this;
520
- if (!this.root)
521
- return [];
522
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
523
- var result = [];
524
- if (this._loopType === LoopType.recursive) {
525
- var _traverse_1 = function (cur) {
526
- if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
527
- return;
528
- if (!cur.left && !cur.right)
529
- return;
530
- cur.left && _traverse_1(cur.left);
531
- cur.right && _traverse_1(cur.right);
532
- };
533
- _traverse_1(this.root);
534
- }
535
- else {
536
- var queue = [this.root];
537
- while (queue.length > 0) {
538
- var cur = queue.shift();
539
- if (cur) {
540
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
541
- return result;
542
- cur.left && queue.push(cur.left);
543
- cur.right && queue.push(cur.right);
544
- }
545
- }
546
- }
547
- return result;
548
- };
549
- BinaryTree.prototype.has = function (nodeProperty, propertyName) {
550
- return this.getNodes(nodeProperty, propertyName).length > 0;
551
- };
552
- BinaryTree.prototype.get = function (nodeProperty, propertyName) {
553
- var _a;
554
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
555
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
556
- };
557
- BinaryTree.prototype.getPathToRoot = function (node) {
558
- var result = [];
559
- while (node.parent) {
560
- result.unshift(node);
561
- node = node.parent;
562
- }
563
- result.unshift(node);
564
- return result;
565
- };
566
- BinaryTree.prototype.getRoot = function () {
567
- return this.root;
568
- };
569
- BinaryTree.prototype.getLeftMost = function (node) {
570
- node = node !== null && node !== void 0 ? node : this.root;
571
- if (!node)
572
- return node;
573
- if (this._loopType === LoopType.recursive) {
574
- var _traverse_2 = function (cur) {
575
- if (!cur.left)
576
- return cur;
577
- return _traverse_2(cur.left);
578
- };
579
- return _traverse_2(node);
580
- }
581
- else {
582
- // Indirect implementation of iteration using tail recursion optimization
583
- var _traverse_3 = (0, trampoline_1.trampoline)(function (cur) {
584
- if (!cur.left)
585
- return cur;
586
- return _traverse_3.cont(cur.left);
587
- });
588
- return _traverse_3(node);
589
- }
590
- };
591
- BinaryTree.prototype.getRightMost = function (node) {
592
- node = node !== null && node !== void 0 ? node : this.root;
593
- if (!node)
594
- return node;
595
- if (this._loopType === LoopType.recursive) {
596
- var _traverse_4 = function (cur) {
597
- if (!cur.right)
598
- return cur;
599
- return _traverse_4(cur.right);
600
- };
601
- return _traverse_4(node);
602
- }
603
- else {
604
- // Indirect implementation of iteration using tail recursion optimization
605
- var _traverse_5 = (0, trampoline_1.trampoline)(function (cur) {
606
- if (!cur.right)
607
- return cur;
608
- return _traverse_5.cont(cur.right);
609
- });
610
- return _traverse_5(node);
611
- }
612
- };
613
- // --- start additional methods ---
614
- BinaryTree.prototype.isBST = function (node) {
615
- node = node !== null && node !== void 0 ? node : this.root;
616
- if (!node)
617
- return true;
618
- if (this._loopType === LoopType.recursive) {
619
- var dfs_1 = function (cur, min, max) {
620
- if (!cur)
621
- return true;
622
- if (cur.id <= min || cur.id >= max)
623
- return false;
624
- return dfs_1(cur.left, min, cur.id) && dfs_1(cur.right, cur.id, max);
625
- };
626
- return dfs_1(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
627
- }
628
- else {
629
- var stack = [];
630
- var prev = Number.MIN_SAFE_INTEGER, curr = node;
631
- while (curr || stack.length > 0) {
632
- while (curr) {
633
- stack.push(curr);
634
- curr = curr.left;
635
- }
636
- curr = stack.pop();
637
- if (!(curr) || prev >= curr.id)
638
- return false;
639
- prev = curr.id;
640
- curr = curr.right;
641
- }
642
- return true;
643
- }
644
- };
645
- BinaryTree.prototype.getSubTreeSizeAndCount = function (subTreeRoot) {
646
- var res = [0, 0];
647
- if (!subTreeRoot)
648
- return res;
649
- if (this._loopType === LoopType.recursive) {
650
- var _traverse_6 = function (cur) {
651
- res[0]++;
652
- res[1] += cur.count;
653
- cur.left && _traverse_6(cur.left);
654
- cur.right && _traverse_6(cur.right);
655
- };
656
- _traverse_6(subTreeRoot);
657
- return res;
658
- }
659
- else {
660
- var stack = [subTreeRoot];
661
- while (stack.length > 0) {
662
- var cur = stack.pop();
663
- res[0]++;
664
- res[1] += cur.count;
665
- cur.right && stack.push(cur.right);
666
- cur.left && stack.push(cur.left);
667
- }
668
- return res;
669
- }
670
- };
671
- BinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
672
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'val';
673
- if (!subTreeRoot)
674
- return 0;
675
- var sum = 0;
676
- var _sumByProperty = function (cur) {
677
- var needSum;
678
- switch (propertyName) {
679
- case 'id':
680
- needSum = cur.id;
681
- break;
682
- case 'count':
683
- needSum = cur.count;
684
- break;
685
- case 'val':
686
- needSum = typeof cur.val === 'number' ? cur.val : 0;
687
- break;
688
- default:
689
- needSum = cur.id;
690
- break;
691
- }
692
- return needSum;
693
- };
694
- if (this._loopType === LoopType.recursive) {
695
- var _traverse_7 = function (cur) {
696
- sum += _sumByProperty(cur);
697
- cur.left && _traverse_7(cur.left);
698
- cur.right && _traverse_7(cur.right);
699
- };
700
- _traverse_7(subTreeRoot);
701
- }
702
- else {
703
- var stack = [subTreeRoot];
704
- while (stack.length > 0) {
705
- var cur = stack.pop();
706
- sum += _sumByProperty(cur);
707
- cur.right && stack.push(cur.right);
708
- cur.left && stack.push(cur.left);
709
- }
710
- }
711
- return sum;
712
- };
713
- BinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
714
- var _this = this;
715
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
716
- if (!subTreeRoot)
717
- return false;
718
- var _addByProperty = function (cur) {
719
- switch (propertyName) {
720
- case 'id':
721
- cur.id += delta;
722
- break;
723
- case 'count':
724
- cur.count += delta;
725
- _this.count += delta;
726
- break;
727
- default:
728
- cur.id += delta;
729
- break;
730
- }
731
- };
732
- if (this._loopType === LoopType.recursive) {
733
- var _traverse_8 = function (cur) {
734
- _addByProperty(cur);
735
- cur.left && _traverse_8(cur.left);
736
- cur.right && _traverse_8(cur.right);
737
- };
738
- _traverse_8(subTreeRoot);
739
- }
740
- else {
741
- var stack = [subTreeRoot];
742
- while (stack.length > 0) {
743
- var cur = stack.pop();
744
- _addByProperty(cur);
745
- cur.right && stack.push(cur.right);
746
- cur.left && stack.push(cur.left);
747
- }
748
- }
749
- return true;
750
- };
751
- BinaryTree.prototype.BFS = function (nodeOrPropertyName) {
752
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
753
- this._resetResults();
754
- var queue = [this.root];
755
- while (queue.length !== 0) {
756
- var cur = queue.shift();
757
- if (cur) {
758
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
759
- if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
760
- queue.push(cur.left);
761
- if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
762
- queue.push(cur.right);
763
- }
764
- }
765
- return this._getResultByPropertyName(nodeOrPropertyName);
766
- };
767
- BinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
768
- var _this = this;
769
- pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
770
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
771
- this._resetResults();
772
- var _traverse = function (node) {
773
- switch (pattern) {
774
- case 'in':
775
- if (node.left)
776
- _traverse(node.left);
777
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
778
- if (node.right)
779
- _traverse(node.right);
780
- break;
781
- case 'pre':
782
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
783
- if (node.left)
784
- _traverse(node.left);
785
- if (node.right)
786
- _traverse(node.right);
787
- break;
788
- case 'post':
789
- if (node.left)
790
- _traverse(node.left);
791
- if (node.right)
792
- _traverse(node.right);
793
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
794
- break;
795
- }
796
- };
797
- this.root && _traverse(this.root);
798
- return this._getResultByPropertyName(nodeOrPropertyName);
799
- };
18
+ class BinaryTree extends abstract_binary_tree_1.AbstractBinaryTree {
800
19
  /**
801
- * Time complexity is O(n)
802
- * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
803
- * @param pattern
804
- * @param nodeOrPropertyName
805
- * @constructor
20
+ * This is a constructor function for a binary tree class that takes an optional options parameter.
21
+ * @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
22
+ * constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
23
+ * different configuration options.
806
24
  */
807
- BinaryTree.prototype.DFSIterative = function (pattern, nodeOrPropertyName) {
808
- pattern = pattern || 'in';
809
- nodeOrPropertyName = nodeOrPropertyName || 'id';
810
- this._resetResults();
811
- if (!this.root)
812
- return this._getResultByPropertyName(nodeOrPropertyName);
813
- // 0: visit, 1: print
814
- var stack = [{ opt: 0, node: this.root }];
815
- while (stack.length > 0) {
816
- var cur = stack.pop();
817
- if (!cur || !cur.node)
818
- continue;
819
- if (cur.opt === 1) {
820
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
821
- }
822
- else {
823
- switch (pattern) {
824
- case 'in':
825
- stack.push({ opt: 0, node: cur.node.right });
826
- stack.push({ opt: 1, node: cur.node });
827
- stack.push({ opt: 0, node: cur.node.left });
828
- break;
829
- case 'pre':
830
- stack.push({ opt: 0, node: cur.node.right });
831
- stack.push({ opt: 0, node: cur.node.left });
832
- stack.push({ opt: 1, node: cur.node });
833
- break;
834
- case 'post':
835
- stack.push({ opt: 1, node: cur.node });
836
- stack.push({ opt: 0, node: cur.node.right });
837
- stack.push({ opt: 0, node: cur.node.left });
838
- break;
839
- default:
840
- stack.push({ opt: 0, node: cur.node.right });
841
- stack.push({ opt: 1, node: cur.node });
842
- stack.push({ opt: 0, node: cur.node.left });
843
- break;
844
- }
845
- }
846
- }
847
- return this._getResultByPropertyName(nodeOrPropertyName);
848
- };
849
- BinaryTree.prototype.levelIterative = function (node, nodeOrPropertyName) {
850
- nodeOrPropertyName = nodeOrPropertyName || 'id';
851
- node = node || this.root;
852
- if (!node)
853
- return [];
854
- this._resetResults();
855
- var queue = [node];
856
- while (queue.length > 0) {
857
- var cur = queue.shift();
858
- if (cur) {
859
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
860
- if (cur.left) {
861
- queue.push(cur.left);
862
- }
863
- if (cur.right) {
864
- queue.push(cur.right);
865
- }
866
- }
867
- }
868
- return this._getResultByPropertyName(nodeOrPropertyName);
869
- };
870
- BinaryTree.prototype.listLevels = function (node, nodeOrPropertyName) {
871
- nodeOrPropertyName = nodeOrPropertyName || 'id';
872
- node = node || this.root;
873
- if (!node)
874
- return [];
875
- var levelsNodes = [];
876
- var collectByProperty = function (node, level) {
877
- switch (nodeOrPropertyName) {
878
- case 'id':
879
- levelsNodes[level].push(node.id);
880
- break;
881
- case 'val':
882
- levelsNodes[level].push(node.val);
883
- break;
884
- case 'node':
885
- levelsNodes[level].push(node);
886
- break;
887
- case 'count':
888
- levelsNodes[level].push(node.count);
889
- break;
890
- default:
891
- levelsNodes[level].push(node.id);
892
- break;
893
- }
894
- };
895
- if (this._loopType === LoopType.recursive) {
896
- var _recursive_1 = function (node, level) {
897
- if (!levelsNodes[level])
898
- levelsNodes[level] = [];
899
- collectByProperty(node, level);
900
- if (node.left)
901
- _recursive_1(node.left, level + 1);
902
- if (node.right)
903
- _recursive_1(node.right, level + 1);
904
- };
905
- _recursive_1(node, 0);
906
- }
907
- else {
908
- var stack = [[node, 0]];
909
- while (stack.length > 0) {
910
- var head = stack.pop();
911
- var _a = __read(head, 2), node_1 = _a[0], level = _a[1];
912
- if (!levelsNodes[level])
913
- levelsNodes[level] = [];
914
- collectByProperty(node_1, level);
915
- if (node_1.right)
916
- stack.push([node_1.right, level + 1]);
917
- if (node_1.left)
918
- stack.push([node_1.left, level + 1]);
919
- }
920
- }
921
- return levelsNodes;
922
- };
923
- BinaryTree.prototype.getPredecessor = function (node) {
924
- if (node.left) {
925
- var predecessor = node.left;
926
- while (!(predecessor) || predecessor.right && predecessor.right !== node) {
927
- if (predecessor) {
928
- predecessor = predecessor.right;
929
- }
930
- }
931
- return predecessor;
932
- }
933
- else {
934
- return node;
935
- }
936
- };
25
+ constructor(options) {
26
+ super(options);
27
+ }
937
28
  /**
938
- * The time complexity of Morris traversal is O(n), it's may slower than others
939
- * The space complexity Morris traversal is O(1) because no using stack
940
- * @param pattern
941
- * @param nodeOrPropertyName
29
+ * The function creates a new binary tree node with an optional value.
30
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
31
+ * `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
32
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
33
+ * stored in the node.
34
+ * @returns a new instance of a BinaryTreeNode with the specified id and value.
942
35
  */
943
- BinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
944
- var _this = this;
945
- if (this.root === null)
946
- return [];
947
- pattern = pattern || 'in';
948
- nodeOrPropertyName = nodeOrPropertyName || 'id';
949
- this._resetResults();
950
- var cur = this.root;
951
- var _reverseEdge = function (node) {
952
- var pre = null;
953
- var next = null;
954
- while (node) {
955
- next = node.right;
956
- node.right = pre;
957
- pre = node;
958
- node = next;
959
- }
960
- return pre;
961
- };
962
- var _printEdge = function (node) {
963
- var tail = _reverseEdge(node);
964
- var cur = tail;
965
- while (cur) {
966
- _this._accumulatedByPropertyName(cur, nodeOrPropertyName);
967
- cur = cur.right;
968
- }
969
- _reverseEdge(tail);
970
- };
971
- switch (pattern) {
972
- case 'in':
973
- while (cur) {
974
- if (cur.left) {
975
- var predecessor = this.getPredecessor(cur);
976
- if (!predecessor.right) {
977
- predecessor.right = cur;
978
- cur = cur.left;
979
- continue;
980
- }
981
- else {
982
- predecessor.right = null;
983
- }
984
- }
985
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
986
- cur = cur.right;
987
- }
988
- break;
989
- case 'pre':
990
- while (cur) {
991
- if (cur.left) {
992
- var predecessor = this.getPredecessor(cur);
993
- if (!predecessor.right) {
994
- predecessor.right = cur;
995
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
996
- cur = cur.left;
997
- continue;
998
- }
999
- else {
1000
- predecessor.right = null;
1001
- }
1002
- }
1003
- else {
1004
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1005
- }
1006
- cur = cur.right;
1007
- }
1008
- break;
1009
- case 'post':
1010
- while (cur) {
1011
- if (cur.left) {
1012
- var predecessor = this.getPredecessor(cur);
1013
- if (predecessor.right === null) {
1014
- predecessor.right = cur;
1015
- cur = cur.left;
1016
- continue;
1017
- }
1018
- else {
1019
- predecessor.right = null;
1020
- _printEdge(cur.left);
1021
- }
1022
- }
1023
- cur = cur.right;
1024
- }
1025
- _printEdge(this.root);
1026
- break;
1027
- }
1028
- return this._getResultByPropertyName(nodeOrPropertyName);
1029
- };
1030
- BinaryTree.prototype._resetResults = function () {
1031
- this._visitedId = [];
1032
- this._visitedVal = [];
1033
- this._visitedNode = [];
1034
- this._visitedCount = [];
1035
- this._visitedLeftSum = [];
1036
- };
1037
- BinaryTree.prototype._pushByPropertyNameStopOrNot = function (cur, result, nodeProperty, propertyName, onlyOne) {
1038
- switch (propertyName) {
1039
- case 'id':
1040
- if (cur.id === nodeProperty) {
1041
- result.push(cur);
1042
- return !!onlyOne;
1043
- }
1044
- break;
1045
- case 'count':
1046
- if (cur.count === nodeProperty) {
1047
- result.push(cur);
1048
- return !!onlyOne;
1049
- }
1050
- break;
1051
- case 'val':
1052
- if (cur.val === nodeProperty) {
1053
- result.push(cur);
1054
- return !!onlyOne;
1055
- }
1056
- break;
1057
- default:
1058
- if (cur.id === nodeProperty) {
1059
- result.push(cur);
1060
- return !!onlyOne;
1061
- }
1062
- break;
1063
- }
1064
- };
1065
- BinaryTree.prototype._accumulatedByPropertyName = function (node, nodeOrPropertyName) {
1066
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1067
- switch (nodeOrPropertyName) {
1068
- case 'id':
1069
- this._visitedId.push(node.id);
1070
- break;
1071
- case 'val':
1072
- this._visitedVal.push(node.val);
1073
- break;
1074
- case 'node':
1075
- this._visitedNode.push(node);
1076
- break;
1077
- case 'count':
1078
- this._visitedCount.push(node.count);
1079
- break;
1080
- default:
1081
- this._visitedId.push(node.id);
1082
- break;
1083
- }
1084
- };
1085
- BinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
1086
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1087
- switch (nodeOrPropertyName) {
1088
- case 'id':
1089
- return this._visitedId;
1090
- case 'val':
1091
- return this._visitedVal;
1092
- case 'node':
1093
- return this._visitedNode;
1094
- case 'count':
1095
- return this._visitedCount;
1096
- default:
1097
- return this._visitedId;
1098
- }
1099
- };
1100
- return BinaryTree;
1101
- }());
36
+ createNode(id, val) {
37
+ return new BinaryTreeNode(id, val);
38
+ }
39
+ }
1102
40
  exports.BinaryTree = BinaryTree;