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,431 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BST = exports.BSTNode = exports.CP = void 0;
4
+ const binary_tree_1 = require("./binary-tree");
5
+ var CP;
6
+ (function (CP) {
7
+ CP[CP["lt"] = -1] = "lt";
8
+ CP[CP["eq"] = 0] = "eq";
9
+ CP[CP["gt"] = 1] = "gt";
10
+ })(CP = exports.CP || (exports.CP = {}));
11
+ class BSTNode extends binary_tree_1.BinaryTreeNode {
12
+ clone() {
13
+ return new BSTNode(this.id, this.val, this.count);
14
+ }
15
+ }
16
+ exports.BSTNode = BSTNode;
17
+ class BST extends binary_tree_1.BinaryTree {
18
+ _compare(a, b) {
19
+ const compared = this._comparator(a, b);
20
+ if (compared > 0)
21
+ return CP.gt;
22
+ else if (compared < 0)
23
+ return CP.lt;
24
+ else
25
+ return CP.eq;
26
+ }
27
+ constructor(options) {
28
+ super(options);
29
+ this._comparator = (a, b) => a - b;
30
+ if (options !== undefined) {
31
+ const { comparator } = options;
32
+ if (comparator !== undefined) {
33
+ this._comparator = comparator;
34
+ }
35
+ }
36
+ }
37
+ createNode(id, val, count) {
38
+ return val !== null ? new BSTNode(id, val, count) : null;
39
+ }
40
+ put(id, val, count = 1) {
41
+ var _a;
42
+ let inserted = null;
43
+ const newNode = this.createNode(id, val, count);
44
+ if (this.root === null) {
45
+ this.root = newNode;
46
+ this.size++;
47
+ this.count += (_a = newNode === null || newNode === void 0 ? void 0 : newNode.count) !== null && _a !== void 0 ? _a : 1;
48
+ inserted = (this.root);
49
+ }
50
+ else {
51
+ let cur = this.root;
52
+ let traversing = true;
53
+ while (traversing) {
54
+ if (cur !== null && newNode !== null) {
55
+ if (this._compare(cur.id, id) === CP.eq) {
56
+ if (newNode) {
57
+ cur.count += newNode.count;
58
+ this.count += newNode.count;
59
+ cur.val = newNode.val;
60
+ }
61
+ //Duplicates are not accepted.
62
+ traversing = false;
63
+ inserted = cur;
64
+ }
65
+ else if (this._compare(cur.id, id) === CP.gt) {
66
+ // Traverse left of the node
67
+ if (cur.left === undefined) {
68
+ if (newNode) {
69
+ newNode.parent = cur;
70
+ newNode.familyPosition = binary_tree_1.FamilyPosition.left;
71
+ }
72
+ //Add to the left of the current node
73
+ cur.left = newNode;
74
+ this.size++;
75
+ this.count += newNode.count;
76
+ traversing = false;
77
+ inserted = cur.left;
78
+ }
79
+ else {
80
+ //Traverse the left of the current node
81
+ if (cur.left)
82
+ cur = cur.left;
83
+ }
84
+ }
85
+ else if (this._compare(cur.id, id) === CP.lt) {
86
+ // Traverse right of the node
87
+ if (cur.right === undefined) {
88
+ if (newNode) {
89
+ newNode.parent = cur;
90
+ newNode.familyPosition = binary_tree_1.FamilyPosition.right;
91
+ }
92
+ //Add to the right of the current node
93
+ cur.right = newNode;
94
+ this.size++;
95
+ this.count += newNode.count;
96
+ traversing = false;
97
+ inserted = (cur.right);
98
+ }
99
+ else {
100
+ //Traverse the left of the current node
101
+ if (cur.right)
102
+ cur = cur.right;
103
+ }
104
+ }
105
+ }
106
+ else {
107
+ traversing = false;
108
+ }
109
+ }
110
+ }
111
+ return inserted;
112
+ }
113
+ get(nodeProperty, propertyName) {
114
+ var _a;
115
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
116
+ return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
117
+ }
118
+ lastKey() {
119
+ var _a, _b, _c, _d, _e, _f;
120
+ if (this._compare(0, 1) === CP.lt)
121
+ return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
122
+ else if (this._compare(0, 1) === CP.gt)
123
+ return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
124
+ else
125
+ return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
126
+ }
127
+ remove(id, ignoreCount) {
128
+ const bstDeletedResult = [];
129
+ if (!this.root)
130
+ return bstDeletedResult;
131
+ const curr = this.get(id);
132
+ if (!curr)
133
+ return bstDeletedResult;
134
+ const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
135
+ let needBalanced = null, orgCurrent = curr;
136
+ if (curr.count > 1 && !ignoreCount) {
137
+ curr.count--;
138
+ this.count--;
139
+ }
140
+ else {
141
+ if (!curr.left) {
142
+ if (!parent) {
143
+ if (curr.right !== undefined)
144
+ this.root = curr.right;
145
+ }
146
+ else {
147
+ switch (curr.familyPosition) {
148
+ case binary_tree_1.FamilyPosition.left:
149
+ parent.left = curr.right;
150
+ break;
151
+ case binary_tree_1.FamilyPosition.right:
152
+ parent.right = curr.right;
153
+ break;
154
+ }
155
+ needBalanced = parent;
156
+ }
157
+ }
158
+ else {
159
+ const leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
160
+ if (leftSubTreeMax) {
161
+ const parentOfLeftSubTreeMax = leftSubTreeMax.parent;
162
+ orgCurrent = curr.swapLocation(leftSubTreeMax);
163
+ if (parentOfLeftSubTreeMax) {
164
+ if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
165
+ parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
166
+ else
167
+ parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
168
+ needBalanced = parentOfLeftSubTreeMax;
169
+ }
170
+ }
171
+ }
172
+ this.size--;
173
+ this.count -= curr.count;
174
+ }
175
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
176
+ return bstDeletedResult;
177
+ }
178
+ getNodes(nodeProperty, propertyName, onlyOne) {
179
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
180
+ if (!this.root)
181
+ return [];
182
+ const result = [];
183
+ if (this._loopType === binary_tree_1.LoopType.recursive) {
184
+ const _traverse = (cur) => {
185
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
186
+ return;
187
+ if (!cur.left && !cur.right)
188
+ return;
189
+ if (propertyName === 'id') {
190
+ if (this._compare(cur.id, nodeProperty) === CP.gt)
191
+ cur.left && _traverse(cur.left);
192
+ if (this._compare(cur.id, nodeProperty) === CP.lt)
193
+ cur.right && _traverse(cur.right);
194
+ }
195
+ else {
196
+ cur.left && _traverse(cur.left);
197
+ cur.right && _traverse(cur.right);
198
+ }
199
+ };
200
+ _traverse(this.root);
201
+ }
202
+ else {
203
+ const queue = [this.root];
204
+ while (queue.length > 0) {
205
+ const cur = queue.shift();
206
+ if (cur) {
207
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
208
+ return result;
209
+ if (propertyName === 'id') {
210
+ if (this._compare(cur.id, nodeProperty) === CP.gt)
211
+ cur.left && queue.push(cur.left);
212
+ if (this._compare(cur.id, nodeProperty) === CP.lt)
213
+ cur.right && queue.push(cur.right);
214
+ }
215
+ else {
216
+ cur.left && queue.push(cur.left);
217
+ cur.right && queue.push(cur.right);
218
+ }
219
+ }
220
+ }
221
+ }
222
+ return result;
223
+ }
224
+ // --- start additional functions
225
+ lesserSum(id, propertyName) {
226
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
227
+ if (!this.root)
228
+ return 0;
229
+ const getSumByPropertyName = (cur) => {
230
+ let needSum;
231
+ switch (propertyName) {
232
+ case 'id':
233
+ needSum = cur.id;
234
+ break;
235
+ case 'count':
236
+ needSum = cur.count;
237
+ break;
238
+ default:
239
+ needSum = cur.id;
240
+ break;
241
+ }
242
+ return needSum;
243
+ };
244
+ let sum = 0;
245
+ if (this._loopType === binary_tree_1.LoopType.recursive) {
246
+ const _traverse = (cur) => {
247
+ const compared = this._compare(cur.id, id);
248
+ if (compared === CP.eq) {
249
+ if (cur.right)
250
+ sum += this.subTreeSum(cur.right, propertyName);
251
+ return;
252
+ }
253
+ else if (compared === CP.lt) {
254
+ if (cur.left)
255
+ sum += this.subTreeSum(cur.left, propertyName);
256
+ sum += getSumByPropertyName(cur);
257
+ if (cur.right)
258
+ _traverse(cur.right);
259
+ else
260
+ return;
261
+ }
262
+ else {
263
+ if (cur.left)
264
+ _traverse(cur.left);
265
+ else
266
+ return;
267
+ }
268
+ };
269
+ _traverse(this.root);
270
+ }
271
+ else {
272
+ const queue = [this.root];
273
+ while (queue.length > 0) {
274
+ const cur = queue.shift();
275
+ if (cur) {
276
+ const compared = this._compare(cur.id, id);
277
+ if (compared === CP.eq) {
278
+ if (cur.right)
279
+ sum += this.subTreeSum(cur.right, propertyName);
280
+ return sum;
281
+ }
282
+ else if (compared === CP.lt) { // todo maybe a bug
283
+ if (cur.left)
284
+ sum += this.subTreeSum(cur.left, propertyName);
285
+ sum += getSumByPropertyName(cur);
286
+ if (cur.right)
287
+ queue.push(cur.right);
288
+ else
289
+ return sum;
290
+ }
291
+ else {
292
+ if (cur.left)
293
+ queue.push(cur.left);
294
+ else
295
+ return sum;
296
+ }
297
+ }
298
+ }
299
+ }
300
+ return sum;
301
+ }
302
+ allGreaterNodesAdd(node, delta, propertyName) {
303
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
304
+ if (!this.root)
305
+ return false;
306
+ const _sumByPropertyName = (cur) => {
307
+ switch (propertyName) {
308
+ case 'id':
309
+ cur.id += delta;
310
+ break;
311
+ case 'count':
312
+ cur.count += delta;
313
+ break;
314
+ default:
315
+ cur.id += delta;
316
+ break;
317
+ }
318
+ };
319
+ if (this._loopType === binary_tree_1.LoopType.recursive) {
320
+ const _traverse = (cur) => {
321
+ const compared = this._compare(cur.id, node.id);
322
+ _sumByPropertyName(cur);
323
+ if (!cur.left && !cur.right)
324
+ return;
325
+ if (cur.left && compared === CP.gt)
326
+ _traverse(cur.left);
327
+ else if (cur.right && compared === CP.gt)
328
+ _traverse(cur.right);
329
+ };
330
+ _traverse(this.root);
331
+ return true;
332
+ }
333
+ else {
334
+ const queue = [this.root];
335
+ while (queue.length > 0) {
336
+ const cur = queue.shift();
337
+ if (cur) {
338
+ const compared = this._compare(cur.id, node.id);
339
+ _sumByPropertyName(cur);
340
+ if (cur.left && compared === CP.gt)
341
+ queue.push(cur.left);
342
+ else if (cur.right && compared === CP.gt)
343
+ queue.push(cur.right);
344
+ }
345
+ }
346
+ return true;
347
+ }
348
+ }
349
+ balance() {
350
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
351
+ this.clear();
352
+ if (sorted.length < 1)
353
+ return false;
354
+ if (this._loopType === binary_tree_1.LoopType.recursive) {
355
+ const buildBalanceBST = (l, r) => {
356
+ if (l > r)
357
+ return;
358
+ const m = l + Math.floor((r - l) / 2);
359
+ const midNode = sorted[m];
360
+ this.put(midNode.id, midNode.val, midNode.count);
361
+ buildBalanceBST(l, m - 1);
362
+ buildBalanceBST(m + 1, r);
363
+ };
364
+ buildBalanceBST(0, n - 1);
365
+ return true;
366
+ }
367
+ else {
368
+ const stack = [[0, n - 1]];
369
+ while (stack.length > 0) {
370
+ const popped = stack.pop();
371
+ if (popped) {
372
+ const [l, r] = popped;
373
+ if (l <= r) {
374
+ const m = l + Math.floor((r - l) / 2);
375
+ const midNode = sorted[m];
376
+ this.put(midNode.id, midNode.val, midNode.count);
377
+ stack.push([m + 1, r]);
378
+ stack.push([l, m - 1]);
379
+ }
380
+ }
381
+ }
382
+ return true;
383
+ }
384
+ }
385
+ isAVLBalanced() {
386
+ var _a, _b;
387
+ if (!this.root)
388
+ return true;
389
+ let balanced = true;
390
+ if (this._loopType === binary_tree_1.LoopType.recursive) {
391
+ const _height = (cur) => {
392
+ if (!cur)
393
+ return 0;
394
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
395
+ if (Math.abs(leftHeight - rightHeight) > 1)
396
+ balanced = false;
397
+ return Math.max(leftHeight, rightHeight) + 1;
398
+ };
399
+ _height(this.root);
400
+ }
401
+ else {
402
+ const stack = [];
403
+ let node = this.root, last = null, depths = new Map();
404
+ while (stack.length > 0 || node) {
405
+ if (node) {
406
+ stack.push(node);
407
+ node = node.left;
408
+ }
409
+ else {
410
+ node = stack[stack.length - 1];
411
+ if (!node.right || last === node.right) {
412
+ node = stack.pop();
413
+ if (node) {
414
+ let left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
415
+ let right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
416
+ if (Math.abs(left - right) > 1)
417
+ return false;
418
+ depths.set(node, 1 + Math.max(left, right));
419
+ last = node;
420
+ node = null;
421
+ }
422
+ }
423
+ else
424
+ node = node.right;
425
+ }
426
+ }
427
+ }
428
+ return balanced;
429
+ }
430
+ }
431
+ exports.BST = BST;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./binary-tree"), exports);
18
+ __exportStar(require("./bst"), exports);
19
+ __exportStar(require("./binary-indexed-tree"), exports);
20
+ __exportStar(require("./segment-tree"), exports);
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RBTree = void 0;
4
+ class RBTree {
5
+ }
6
+ exports.RBTree = RBTree;
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SegmentTree = exports.SegmentTreeNode = void 0;
4
+ class SegmentTreeNode {
5
+ get start() {
6
+ return this._start;
7
+ }
8
+ set start(v) {
9
+ this._start = v;
10
+ }
11
+ get end() {
12
+ return this._end;
13
+ }
14
+ set end(v) {
15
+ this._end = v;
16
+ }
17
+ get val() {
18
+ return this._val;
19
+ }
20
+ set val(v) {
21
+ this._val = v;
22
+ }
23
+ get sum() {
24
+ return this._sum;
25
+ }
26
+ set sum(v) {
27
+ this._sum = v;
28
+ }
29
+ get left() {
30
+ return this._left;
31
+ }
32
+ set left(v) {
33
+ this._left = v;
34
+ }
35
+ get right() {
36
+ return this._right;
37
+ }
38
+ set right(v) {
39
+ this._right = v;
40
+ }
41
+ constructor(start, end, sum, val) {
42
+ this._start = 0;
43
+ this._end = 0;
44
+ this._val = null;
45
+ this._sum = 0;
46
+ this._left = null;
47
+ this._right = null;
48
+ this._start = start;
49
+ this._end = end;
50
+ this._sum = sum;
51
+ this._val = val || null;
52
+ }
53
+ }
54
+ exports.SegmentTreeNode = SegmentTreeNode;
55
+ class SegmentTree {
56
+ get root() {
57
+ return this._root;
58
+ }
59
+ constructor(values, start, end) {
60
+ this._values = [];
61
+ this._start = 0;
62
+ start = start || 0;
63
+ end = end || values.length - 1;
64
+ this._values = values;
65
+ this._start = start;
66
+ this._end = end;
67
+ this._root = this.build(start, end);
68
+ }
69
+ build(start, end) {
70
+ if (start === end) {
71
+ return new SegmentTreeNode(start, end, this._values[start]);
72
+ }
73
+ const mid = start + Math.floor((end - start) / 2);
74
+ const left = this.build(start, mid);
75
+ const right = this.build(mid + 1, end);
76
+ const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
77
+ cur.left = left;
78
+ cur.right = right;
79
+ return cur;
80
+ }
81
+ updateNode(index, sum, val) {
82
+ const root = this.root || null;
83
+ if (!root) {
84
+ return;
85
+ }
86
+ const dfs = (cur, index, sum, val) => {
87
+ if (cur.start === cur.end && cur.start === index) {
88
+ cur.sum = sum;
89
+ // cur.val = val;
90
+ return;
91
+ }
92
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
93
+ if (index <= mid) {
94
+ if (cur.left) {
95
+ dfs(cur.left, index, sum, val);
96
+ }
97
+ }
98
+ else {
99
+ if (cur.right) {
100
+ dfs(cur.right, index, sum, val);
101
+ }
102
+ }
103
+ if (cur.left && cur.right) {
104
+ cur.sum = cur.left.sum + cur.right.sum;
105
+ }
106
+ };
107
+ dfs(root, index, sum);
108
+ }
109
+ querySumByRange(indexA, indexB) {
110
+ const root = this.root || null;
111
+ if (!root) {
112
+ return 0;
113
+ }
114
+ const dfs = (cur, i, j) => {
115
+ if (cur.start === i && cur.end === j) {
116
+ return cur.sum;
117
+ }
118
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
119
+ if (j <= mid) {
120
+ // TODO after no-non-null-assertion not ensure the logic
121
+ if (cur.left) {
122
+ return dfs(cur.left, i, j);
123
+ }
124
+ else {
125
+ return NaN;
126
+ }
127
+ }
128
+ else if (i > mid) {
129
+ // TODO after no-non-null-assertion not ensure the logic
130
+ if (cur.right) {
131
+ // TODO after no-non-null-assertion not ensure the logic
132
+ return dfs(cur.right, i, j);
133
+ }
134
+ else {
135
+ return NaN;
136
+ }
137
+ }
138
+ else {
139
+ // TODO after no-non-null-assertion not ensure the logic
140
+ if (cur.left && cur.right) {
141
+ return dfs(cur.left, i, mid) + dfs(cur.right, mid + 1, j);
142
+ }
143
+ else {
144
+ return NaN;
145
+ }
146
+ }
147
+ };
148
+ return dfs(root, indexA, indexB);
149
+ }
150
+ }
151
+ exports.SegmentTree = SegmentTree;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SplayTree = void 0;
4
+ class SplayTree {
5
+ }
6
+ exports.SplayTree = SplayTree;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TreeMultiSet = void 0;
4
+ const bst_1 = require("./bst");
5
+ class TreeMultiSet extends bst_1.BST {
6
+ createNode(id, val, count) {
7
+ return new bst_1.BSTNode(id, val, count);
8
+ }
9
+ put(id, val, count) {
10
+ return super.put(id, val, count);
11
+ }
12
+ remove(id, isUpdateAllLeftSum) {
13
+ return super.remove(id, isUpdateAllLeftSum);
14
+ }
15
+ }
16
+ exports.TreeMultiSet = TreeMultiSet;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TwoThreeTree = void 0;
4
+ class TwoThreeTree {
5
+ }
6
+ exports.TwoThreeTree = TwoThreeTree;