data-structure-typed 0.8.6

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 (166) hide show
  1. package/.idea/data-structure-typed.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +2 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +6 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +231 -0
  7. package/dist/data-structures/binary-tree/b-tree.js +6 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +992 -0
  10. package/dist/data-structures/binary-tree/bst.js +431 -0
  11. package/dist/data-structures/binary-tree/index.js +20 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +6 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +151 -0
  14. package/dist/data-structures/binary-tree/splay-tree.js +6 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
  16. package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
  17. package/dist/data-structures/graph/abstract-graph.js +648 -0
  18. package/dist/data-structures/graph/directed-graph.js +268 -0
  19. package/dist/data-structures/graph/index.js +19 -0
  20. package/dist/data-structures/graph/undirected-graph.js +142 -0
  21. package/dist/data-structures/hash/coordinate-map.js +24 -0
  22. package/dist/data-structures/hash/coordinate-set.js +21 -0
  23. package/dist/data-structures/hash/hash-table.js +2 -0
  24. package/dist/data-structures/hash/index.js +17 -0
  25. package/dist/data-structures/hash/pair.js +2 -0
  26. package/dist/data-structures/hash/tree-map.js +2 -0
  27. package/dist/data-structures/hash/tree-set.js +2 -0
  28. package/dist/data-structures/heap/heap.js +114 -0
  29. package/dist/data-structures/heap/index.js +19 -0
  30. package/dist/data-structures/heap/max-heap.js +22 -0
  31. package/dist/data-structures/heap/min-heap.js +22 -0
  32. package/dist/data-structures/index.js +25 -0
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
  34. package/dist/data-structures/linked-list/index.js +18 -0
  35. package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
  37. package/dist/data-structures/matrix/index.js +19 -0
  38. package/dist/data-structures/matrix/matrix.js +14 -0
  39. package/dist/data-structures/matrix/matrix2d.js +119 -0
  40. package/dist/data-structures/matrix/navigator.js +78 -0
  41. package/dist/data-structures/matrix/vector2d.js +161 -0
  42. package/dist/data-structures/priority-queue/index.js +19 -0
  43. package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
  45. package/dist/data-structures/priority-queue/priority-queue.js +174 -0
  46. package/dist/data-structures/queue/deque.js +132 -0
  47. package/dist/data-structures/queue/index.js +17 -0
  48. package/dist/data-structures/queue/queue.js +113 -0
  49. package/dist/data-structures/stack/index.js +17 -0
  50. package/dist/data-structures/stack/stack.js +97 -0
  51. package/dist/data-structures/trampoline.js +52 -0
  52. package/dist/data-structures/trie/index.js +17 -0
  53. package/dist/data-structures/trie/trie.js +141 -0
  54. package/dist/index.js +17 -0
  55. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
  57. package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
  61. package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
  62. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
  64. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
  65. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
  66. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
  69. package/dist/types/data-structures/graph/index.d.ts +3 -0
  70. package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
  71. package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
  72. package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
  73. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  74. package/dist/types/data-structures/hash/index.d.ts +1 -0
  75. package/dist/types/data-structures/hash/pair.d.ts +1 -0
  76. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  77. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  78. package/dist/types/data-structures/heap/heap.d.ts +72 -0
  79. package/dist/types/data-structures/heap/index.d.ts +3 -0
  80. package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
  81. package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
  82. package/dist/types/data-structures/index.d.ts +9 -0
  83. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
  84. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  85. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
  86. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  87. package/dist/types/data-structures/matrix/index.d.ts +3 -0
  88. package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
  89. package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
  90. package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
  91. package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
  92. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  93. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
  94. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
  95. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
  96. package/dist/types/data-structures/queue/deque.d.ts +37 -0
  97. package/dist/types/data-structures/queue/index.d.ts +1 -0
  98. package/dist/types/data-structures/queue/queue.d.ts +76 -0
  99. package/dist/types/data-structures/stack/index.d.ts +1 -0
  100. package/dist/types/data-structures/stack/stack.d.ts +69 -0
  101. package/dist/types/data-structures/trampoline.d.ts +25 -0
  102. package/dist/types/data-structures/trie/index.d.ts +1 -0
  103. package/dist/types/data-structures/trie/trie.d.ts +28 -0
  104. package/dist/types/index.d.ts +1 -0
  105. package/dist/types/index.js +17 -0
  106. package/dist/types/types/index.d.ts +1 -0
  107. package/dist/types/types/utils.d.ts +46 -0
  108. package/dist/types/utils.d.ts +122 -0
  109. package/dist/types/utils.js +53 -0
  110. package/dist/utils.js +569 -0
  111. package/package.json +75 -0
  112. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  113. package/src/data-structures/binary-tree/avl-tree.ts +232 -0
  114. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  115. package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
  117. package/src/data-structures/binary-tree/bst.ts +404 -0
  118. package/src/data-structures/binary-tree/index.ts +4 -0
  119. package/src/data-structures/binary-tree/rb-tree.ts +3 -0
  120. package/src/data-structures/binary-tree/segment-tree.ts +164 -0
  121. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  122. package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
  123. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  124. package/src/data-structures/graph/abstract-graph.ts +789 -0
  125. package/src/data-structures/graph/directed-graph.ts +322 -0
  126. package/src/data-structures/graph/index.ts +3 -0
  127. package/src/data-structures/graph/undirected-graph.ts +154 -0
  128. package/src/data-structures/hash/coordinate-map.ts +24 -0
  129. package/src/data-structures/hash/coordinate-set.ts +20 -0
  130. package/src/data-structures/hash/hash-table.ts +1 -0
  131. package/src/data-structures/hash/index.ts +1 -0
  132. package/src/data-structures/hash/pair.ts +1 -0
  133. package/src/data-structures/hash/tree-map.ts +1 -0
  134. package/src/data-structures/hash/tree-set.ts +1 -0
  135. package/src/data-structures/heap/heap.ts +136 -0
  136. package/src/data-structures/heap/index.ts +3 -0
  137. package/src/data-structures/heap/max-heap.ts +22 -0
  138. package/src/data-structures/heap/min-heap.ts +24 -0
  139. package/src/data-structures/index.ts +10 -0
  140. package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
  141. package/src/data-structures/linked-list/index.ts +2 -0
  142. package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
  143. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  144. package/src/data-structures/matrix/index.ts +3 -0
  145. package/src/data-structures/matrix/matrix.ts +13 -0
  146. package/src/data-structures/matrix/matrix2d.ts +125 -0
  147. package/src/data-structures/matrix/navigator.ts +99 -0
  148. package/src/data-structures/matrix/vector2d.ts +189 -0
  149. package/src/data-structures/priority-queue/index.ts +3 -0
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +208 -0
  153. package/src/data-structures/queue/deque.ts +139 -0
  154. package/src/data-structures/queue/index.ts +1 -0
  155. package/src/data-structures/queue/queue.ts +123 -0
  156. package/src/data-structures/stack/index.ts +1 -0
  157. package/src/data-structures/stack/stack.ts +104 -0
  158. package/src/data-structures/trampoline.ts +91 -0
  159. package/src/data-structures/trie/index.ts +1 -0
  160. package/src/data-structures/trie/trie.ts +153 -0
  161. package/src/index.ts +1 -0
  162. package/src/types/index.ts +1 -0
  163. package/src/types/patches/index.d.ts +0 -0
  164. package/src/types/utils.ts +158 -0
  165. package/src/utils.ts +605 -0
  166. package/tsconfig.json +52 -0
@@ -0,0 +1,992 @@
1
+ "use strict";
2
+ 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);
100
+ }
101
+ }
102
+ 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
+ }
764
+ /**
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
770
+ */
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
+ }
898
+ }
899
+ /**
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
904
+ */
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);
990
+ }
991
+ }
992
+ exports.BinaryTree = BinaryTree;