data-structure-typed 0.8.18 → 1.3.0

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