data-structure-typed 0.8.18 → 0.9.16

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