data-structure-typed 1.34.8 → 1.34.9
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.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +280 -423
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +63 -89
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +11 -30
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +127 -214
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +19 -43
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +147 -218
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +338 -572
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +145 -275
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +46 -87
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +90 -179
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +79 -78
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +61 -141
- package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -0
- package/lib/data-structures/binary-tree/avl-tree.js +22 -0
- package/lib/data-structures/binary-tree/bst.d.ts +3 -3
- package/lib/data-structures/binary-tree/bst.js +12 -15
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +16 -18
- package/lib/data-structures/binary-tree/tree-multiset.js +26 -36
- package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
- package/lib/data-structures/graph/abstract-graph.js +5 -5
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/interfaces/abstract-binary-tree.d.ts +23 -28
- package/lib/interfaces/avl-tree.d.ts +3 -1
- package/lib/interfaces/bst.d.ts +2 -1
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +151 -203
- package/src/data-structures/binary-tree/avl-tree.ts +29 -0
- package/src/data-structures/binary-tree/bst.ts +12 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +27 -39
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/interfaces/abstract-binary-tree.ts +23 -36
- package/src/interfaces/avl-tree.ts +3 -1
- package/src/interfaces/bst.ts +1 -1
- package/src/types/data-structures/abstract-binary-tree.ts +0 -1
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +9 -9
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1,91 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
29
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
30
|
-
if (!m) return o;
|
|
31
|
-
var i = m.call(o), r, ar = [], e;
|
|
32
|
-
try {
|
|
33
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
34
|
-
}
|
|
35
|
-
catch (error) { e = { error: error }; }
|
|
36
|
-
finally {
|
|
37
|
-
try {
|
|
38
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
39
|
-
}
|
|
40
|
-
finally { if (e) throw e.error; }
|
|
41
|
-
}
|
|
42
|
-
return ar;
|
|
43
|
-
};
|
|
44
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
3
|
exports.TreeMultiset = exports.TreeMultisetNode = void 0;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
var _this = _super.call(this, key, val) || this;
|
|
53
|
-
_this._count = count;
|
|
54
|
-
return _this;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const avl_tree_1 = require("./avl-tree");
|
|
6
|
+
class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
7
|
+
constructor(key, val, count = 1) {
|
|
8
|
+
super(key, val);
|
|
9
|
+
this.count = count;
|
|
55
10
|
}
|
|
56
|
-
|
|
57
|
-
get: function () {
|
|
58
|
-
return this._count;
|
|
59
|
-
},
|
|
60
|
-
set: function (v) {
|
|
61
|
-
this._count = v;
|
|
62
|
-
},
|
|
63
|
-
enumerable: false,
|
|
64
|
-
configurable: true
|
|
65
|
-
});
|
|
66
|
-
return TreeMultisetNode;
|
|
67
|
-
}(avl_tree_1.AVLTreeNode));
|
|
11
|
+
}
|
|
68
12
|
exports.TreeMultisetNode = TreeMultisetNode;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
_this._count = 0;
|
|
74
|
-
return _this;
|
|
13
|
+
class TreeMultiset extends avl_tree_1.AVLTree {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super(options);
|
|
16
|
+
this._count = 0;
|
|
75
17
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
enumerable: false,
|
|
81
|
-
configurable: true
|
|
82
|
-
});
|
|
83
|
-
TreeMultiset.prototype.createNode = function (key, val, count) {
|
|
18
|
+
get count() {
|
|
19
|
+
return this._count;
|
|
20
|
+
}
|
|
21
|
+
createNode(key, val, count) {
|
|
84
22
|
return new TreeMultisetNode(key, val, count);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
23
|
+
}
|
|
24
|
+
swapLocation(srcNode, destNode) {
|
|
25
|
+
const { key, val, count, height } = destNode;
|
|
26
|
+
const tempNode = this.createNode(key, val, count);
|
|
89
27
|
if (tempNode) {
|
|
90
28
|
tempNode.height = height;
|
|
91
29
|
destNode.key = srcNode.key;
|
|
@@ -98,10 +36,9 @@ var TreeMultiset = (function (_super) {
|
|
|
98
36
|
srcNode.height = tempNode.height;
|
|
99
37
|
}
|
|
100
38
|
return destNode;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
var inserted = undefined, newNode;
|
|
39
|
+
}
|
|
40
|
+
add(keyOrNode, val, count = 1) {
|
|
41
|
+
let inserted = undefined, newNode;
|
|
105
42
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
106
43
|
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
|
107
44
|
}
|
|
@@ -118,8 +55,8 @@ var TreeMultiset = (function (_super) {
|
|
|
118
55
|
inserted = this.root;
|
|
119
56
|
}
|
|
120
57
|
else {
|
|
121
|
-
|
|
122
|
-
|
|
58
|
+
let cur = this.root;
|
|
59
|
+
let traversing = true;
|
|
123
60
|
while (traversing) {
|
|
124
61
|
if (cur) {
|
|
125
62
|
if (newNode) {
|
|
@@ -168,8 +105,8 @@ var TreeMultiset = (function (_super) {
|
|
|
168
105
|
if (inserted)
|
|
169
106
|
this._balancePath(inserted);
|
|
170
107
|
return inserted;
|
|
171
|
-
}
|
|
172
|
-
|
|
108
|
+
}
|
|
109
|
+
_addTo(newNode, parent) {
|
|
173
110
|
if (parent) {
|
|
174
111
|
if (parent.left === undefined) {
|
|
175
112
|
parent.left = newNode;
|
|
@@ -194,11 +131,11 @@ var TreeMultiset = (function (_super) {
|
|
|
194
131
|
else {
|
|
195
132
|
return;
|
|
196
133
|
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
for (
|
|
201
|
-
|
|
134
|
+
}
|
|
135
|
+
addMany(keysOrNodes, data) {
|
|
136
|
+
const inserted = [];
|
|
137
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
138
|
+
const keyOrNode = keysOrNodes[i];
|
|
202
139
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
203
140
|
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
|
204
141
|
continue;
|
|
@@ -210,35 +147,34 @@ var TreeMultiset = (function (_super) {
|
|
|
210
147
|
inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
|
|
211
148
|
}
|
|
212
149
|
return inserted;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
var sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
150
|
+
}
|
|
151
|
+
perfectlyBalance() {
|
|
152
|
+
const sorted = this.dfs('in', 'node'), n = sorted.length;
|
|
217
153
|
if (sorted.length < 1)
|
|
218
154
|
return false;
|
|
219
155
|
this.clear();
|
|
220
156
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
221
|
-
|
|
157
|
+
const buildBalanceBST = (l, r) => {
|
|
222
158
|
if (l > r)
|
|
223
159
|
return;
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
160
|
+
const m = l + Math.floor((r - l) / 2);
|
|
161
|
+
const midNode = sorted[m];
|
|
162
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
163
|
+
buildBalanceBST(l, m - 1);
|
|
164
|
+
buildBalanceBST(m + 1, r);
|
|
229
165
|
};
|
|
230
|
-
|
|
166
|
+
buildBalanceBST(0, n - 1);
|
|
231
167
|
return true;
|
|
232
168
|
}
|
|
233
169
|
else {
|
|
234
|
-
|
|
170
|
+
const stack = [[0, n - 1]];
|
|
235
171
|
while (stack.length > 0) {
|
|
236
|
-
|
|
172
|
+
const popped = stack.pop();
|
|
237
173
|
if (popped) {
|
|
238
|
-
|
|
174
|
+
const [l, r] = popped;
|
|
239
175
|
if (l <= r) {
|
|
240
|
-
|
|
241
|
-
|
|
176
|
+
const m = l + Math.floor((r - l) / 2);
|
|
177
|
+
const midNode = sorted[m];
|
|
242
178
|
this.add(midNode.key, midNode.val, midNode.count);
|
|
243
179
|
stack.push([m + 1, r]);
|
|
244
180
|
stack.push([l, m - 1]);
|
|
@@ -247,16 +183,16 @@ var TreeMultiset = (function (_super) {
|
|
|
247
183
|
}
|
|
248
184
|
return true;
|
|
249
185
|
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
186
|
+
}
|
|
187
|
+
remove(nodeOrKey, ignoreCount = false) {
|
|
188
|
+
const bstDeletedResult = [];
|
|
253
189
|
if (!this.root)
|
|
254
190
|
return bstDeletedResult;
|
|
255
|
-
|
|
191
|
+
const curr = this.get(nodeOrKey);
|
|
256
192
|
if (!curr)
|
|
257
193
|
return bstDeletedResult;
|
|
258
|
-
|
|
259
|
-
|
|
194
|
+
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
195
|
+
let needBalanced = null, orgCurrent = curr;
|
|
260
196
|
if (curr.count > 1 && !ignoreCount) {
|
|
261
197
|
curr.count--;
|
|
262
198
|
this._setCount(this.count - 1);
|
|
@@ -268,7 +204,7 @@ var TreeMultiset = (function (_super) {
|
|
|
268
204
|
this._setRoot(curr.right);
|
|
269
205
|
}
|
|
270
206
|
else {
|
|
271
|
-
|
|
207
|
+
const { familyPosition: fp } = curr;
|
|
272
208
|
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
|
|
273
209
|
parent.left = curr.right;
|
|
274
210
|
}
|
|
@@ -279,9 +215,9 @@ var TreeMultiset = (function (_super) {
|
|
|
279
215
|
}
|
|
280
216
|
}
|
|
281
217
|
else {
|
|
282
|
-
|
|
218
|
+
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
283
219
|
if (leftSubTreeRightMost) {
|
|
284
|
-
|
|
220
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
285
221
|
orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
|
|
286
222
|
if (parentOfLeftSubTreeMax) {
|
|
287
223
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
@@ -297,30 +233,30 @@ var TreeMultiset = (function (_super) {
|
|
|
297
233
|
this._setSize(this.size - 1);
|
|
298
234
|
this._setCount(this.count - orgCurrent.count);
|
|
299
235
|
}
|
|
300
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced
|
|
236
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
301
237
|
if (needBalanced) {
|
|
302
238
|
this._balancePath(needBalanced);
|
|
303
239
|
}
|
|
304
240
|
return bstDeletedResult;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
241
|
+
}
|
|
242
|
+
getSubTreeCount(subTreeRoot) {
|
|
243
|
+
const res = [0, 0];
|
|
308
244
|
if (!subTreeRoot)
|
|
309
245
|
return res;
|
|
310
246
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
311
|
-
|
|
247
|
+
const _traverse = (cur) => {
|
|
312
248
|
res[0]++;
|
|
313
249
|
res[1] += cur.count;
|
|
314
|
-
cur.left &&
|
|
315
|
-
cur.right &&
|
|
250
|
+
cur.left && _traverse(cur.left);
|
|
251
|
+
cur.right && _traverse(cur.right);
|
|
316
252
|
};
|
|
317
|
-
|
|
253
|
+
_traverse(subTreeRoot);
|
|
318
254
|
return res;
|
|
319
255
|
}
|
|
320
256
|
else {
|
|
321
|
-
|
|
257
|
+
const stack = [subTreeRoot];
|
|
322
258
|
while (stack.length > 0) {
|
|
323
|
-
|
|
259
|
+
const cur = stack.pop();
|
|
324
260
|
res[0]++;
|
|
325
261
|
res[1] += cur.count;
|
|
326
262
|
cur.right && stack.push(cur.right);
|
|
@@ -328,67 +264,66 @@ var TreeMultiset = (function (_super) {
|
|
|
328
264
|
}
|
|
329
265
|
return res;
|
|
330
266
|
}
|
|
331
|
-
}
|
|
332
|
-
|
|
267
|
+
}
|
|
268
|
+
subTreeSumCount(subTreeRoot) {
|
|
333
269
|
if (typeof subTreeRoot === 'number')
|
|
334
270
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
335
271
|
if (!subTreeRoot)
|
|
336
272
|
return 0;
|
|
337
|
-
|
|
273
|
+
let sum = 0;
|
|
338
274
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
339
|
-
|
|
275
|
+
const _traverse = (cur) => {
|
|
340
276
|
sum += cur.count;
|
|
341
|
-
cur.left &&
|
|
342
|
-
cur.right &&
|
|
277
|
+
cur.left && _traverse(cur.left);
|
|
278
|
+
cur.right && _traverse(cur.right);
|
|
343
279
|
};
|
|
344
|
-
|
|
280
|
+
_traverse(subTreeRoot);
|
|
345
281
|
}
|
|
346
282
|
else {
|
|
347
|
-
|
|
283
|
+
const stack = [subTreeRoot];
|
|
348
284
|
while (stack.length > 0) {
|
|
349
|
-
|
|
285
|
+
const cur = stack.pop();
|
|
350
286
|
sum += cur.count;
|
|
351
287
|
cur.right && stack.push(cur.right);
|
|
352
288
|
cur.left && stack.push(cur.left);
|
|
353
289
|
}
|
|
354
290
|
}
|
|
355
291
|
return sum;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
var _this = this;
|
|
292
|
+
}
|
|
293
|
+
subTreeAddCount(subTreeRoot, delta) {
|
|
359
294
|
if (typeof subTreeRoot === 'number')
|
|
360
295
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
361
296
|
if (!subTreeRoot)
|
|
362
297
|
return false;
|
|
363
|
-
|
|
298
|
+
const _addByProperty = (cur) => {
|
|
364
299
|
cur.count += delta;
|
|
365
|
-
|
|
300
|
+
this._setCount(this.count + delta);
|
|
366
301
|
};
|
|
367
302
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
368
|
-
|
|
303
|
+
const _traverse = (cur) => {
|
|
369
304
|
_addByProperty(cur);
|
|
370
|
-
cur.left &&
|
|
371
|
-
cur.right &&
|
|
305
|
+
cur.left && _traverse(cur.left);
|
|
306
|
+
cur.right && _traverse(cur.right);
|
|
372
307
|
};
|
|
373
|
-
|
|
308
|
+
_traverse(subTreeRoot);
|
|
374
309
|
}
|
|
375
310
|
else {
|
|
376
|
-
|
|
311
|
+
const stack = [subTreeRoot];
|
|
377
312
|
while (stack.length > 0) {
|
|
378
|
-
|
|
313
|
+
const cur = stack.pop();
|
|
379
314
|
_addByProperty(cur);
|
|
380
315
|
cur.right && stack.push(cur.right);
|
|
381
316
|
cur.left && stack.push(cur.left);
|
|
382
317
|
}
|
|
383
318
|
}
|
|
384
319
|
return true;
|
|
385
|
-
}
|
|
386
|
-
|
|
320
|
+
}
|
|
321
|
+
getNodesByCount(nodeProperty, onlyOne = false) {
|
|
387
322
|
if (!this.root)
|
|
388
323
|
return [];
|
|
389
|
-
|
|
324
|
+
const result = [];
|
|
390
325
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
391
|
-
|
|
326
|
+
const _traverse = (cur) => {
|
|
392
327
|
if (cur.count === nodeProperty) {
|
|
393
328
|
result.push(cur);
|
|
394
329
|
if (onlyOne)
|
|
@@ -396,15 +331,15 @@ var TreeMultiset = (function (_super) {
|
|
|
396
331
|
}
|
|
397
332
|
if (!cur.left && !cur.right)
|
|
398
333
|
return;
|
|
399
|
-
cur.left &&
|
|
400
|
-
cur.right &&
|
|
334
|
+
cur.left && _traverse(cur.left);
|
|
335
|
+
cur.right && _traverse(cur.right);
|
|
401
336
|
};
|
|
402
|
-
|
|
337
|
+
_traverse(this.root);
|
|
403
338
|
}
|
|
404
339
|
else {
|
|
405
|
-
|
|
340
|
+
const queue = [this.root];
|
|
406
341
|
while (queue.length > 0) {
|
|
407
|
-
|
|
342
|
+
const cur = queue.shift();
|
|
408
343
|
if (cur) {
|
|
409
344
|
if (cur.count === nodeProperty) {
|
|
410
345
|
result.push(cur);
|
|
@@ -417,72 +352,68 @@ var TreeMultiset = (function (_super) {
|
|
|
417
352
|
}
|
|
418
353
|
}
|
|
419
354
|
return result;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
return nodes.map(
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
return levels.map(
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
return nodes.map(function (node) { return node.count; });
|
|
443
|
-
};
|
|
444
|
-
TreeMultiset.prototype.lesserSumCount = function (beginNode) {
|
|
445
|
-
var _this = this;
|
|
355
|
+
}
|
|
356
|
+
BFSCount() {
|
|
357
|
+
const nodes = super.bfs('node');
|
|
358
|
+
return nodes.map(node => node.count);
|
|
359
|
+
}
|
|
360
|
+
listLevelsCount(node) {
|
|
361
|
+
const levels = super.listLevels(node, 'node');
|
|
362
|
+
return levels.map(level => level.map(node => node.count));
|
|
363
|
+
}
|
|
364
|
+
morrisCount(pattern = 'in') {
|
|
365
|
+
const nodes = super.morris(pattern, 'node');
|
|
366
|
+
return nodes.map(node => node.count);
|
|
367
|
+
}
|
|
368
|
+
dfsCountIterative(pattern = 'in') {
|
|
369
|
+
const nodes = super.dfsIterative(pattern, 'node');
|
|
370
|
+
return nodes.map(node => node.count);
|
|
371
|
+
}
|
|
372
|
+
dfsCount(pattern = 'in') {
|
|
373
|
+
const nodes = super.dfs(pattern, 'node');
|
|
374
|
+
return nodes.map(node => node.count);
|
|
375
|
+
}
|
|
376
|
+
lesserSumCount(beginNode) {
|
|
446
377
|
if (typeof beginNode === 'number')
|
|
447
378
|
beginNode = this.get(beginNode, 'key');
|
|
448
379
|
if (!beginNode)
|
|
449
380
|
return 0;
|
|
450
381
|
if (!this.root)
|
|
451
382
|
return 0;
|
|
452
|
-
|
|
453
|
-
|
|
383
|
+
const key = beginNode.key;
|
|
384
|
+
let sum = 0;
|
|
454
385
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
455
|
-
|
|
456
|
-
|
|
386
|
+
const _traverse = (cur) => {
|
|
387
|
+
const compared = this._compare(cur.key, key);
|
|
457
388
|
if (compared === types_1.CP.eq) {
|
|
458
389
|
if (cur.right)
|
|
459
|
-
sum +=
|
|
390
|
+
sum += this.subTreeSumCount(cur.right);
|
|
460
391
|
return;
|
|
461
392
|
}
|
|
462
393
|
else if (compared === types_1.CP.lt) {
|
|
463
394
|
if (cur.left)
|
|
464
|
-
sum +=
|
|
395
|
+
sum += this.subTreeSumCount(cur.left);
|
|
465
396
|
sum += cur.count;
|
|
466
397
|
if (cur.right)
|
|
467
|
-
|
|
398
|
+
_traverse(cur.right);
|
|
468
399
|
else
|
|
469
400
|
return;
|
|
470
401
|
}
|
|
471
402
|
else {
|
|
472
403
|
if (cur.left)
|
|
473
|
-
|
|
404
|
+
_traverse(cur.left);
|
|
474
405
|
else
|
|
475
406
|
return;
|
|
476
407
|
}
|
|
477
408
|
};
|
|
478
|
-
|
|
409
|
+
_traverse(this.root);
|
|
479
410
|
}
|
|
480
411
|
else {
|
|
481
|
-
|
|
412
|
+
const queue = [this.root];
|
|
482
413
|
while (queue.length > 0) {
|
|
483
|
-
|
|
414
|
+
const cur = queue.shift();
|
|
484
415
|
if (cur) {
|
|
485
|
-
|
|
416
|
+
const compared = this._compare(cur.key, key);
|
|
486
417
|
if (compared === types_1.CP.eq) {
|
|
487
418
|
if (cur.right)
|
|
488
419
|
sum += this.subTreeSumCount(cur.right);
|
|
@@ -507,37 +438,36 @@ var TreeMultiset = (function (_super) {
|
|
|
507
438
|
}
|
|
508
439
|
}
|
|
509
440
|
return sum;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
var _this = this;
|
|
441
|
+
}
|
|
442
|
+
allGreaterNodesAddCount(node, delta) {
|
|
513
443
|
if (typeof node === 'number')
|
|
514
444
|
node = this.get(node, 'key');
|
|
515
445
|
if (!node)
|
|
516
446
|
return false;
|
|
517
|
-
|
|
447
|
+
const key = node.key;
|
|
518
448
|
if (!this.root)
|
|
519
449
|
return false;
|
|
520
450
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
521
|
-
|
|
522
|
-
|
|
451
|
+
const _traverse = (cur) => {
|
|
452
|
+
const compared = this._compare(cur.key, key);
|
|
523
453
|
if (compared === types_1.CP.gt)
|
|
524
454
|
cur.count += delta;
|
|
525
455
|
if (!cur.left && !cur.right)
|
|
526
456
|
return;
|
|
527
|
-
if (cur.left &&
|
|
528
|
-
|
|
529
|
-
if (cur.right &&
|
|
530
|
-
|
|
457
|
+
if (cur.left && this._compare(cur.left.key, key) === types_1.CP.gt)
|
|
458
|
+
_traverse(cur.left);
|
|
459
|
+
if (cur.right && this._compare(cur.right.key, key) === types_1.CP.gt)
|
|
460
|
+
_traverse(cur.right);
|
|
531
461
|
};
|
|
532
|
-
|
|
462
|
+
_traverse(this.root);
|
|
533
463
|
return true;
|
|
534
464
|
}
|
|
535
465
|
else {
|
|
536
|
-
|
|
466
|
+
const queue = [this.root];
|
|
537
467
|
while (queue.length > 0) {
|
|
538
|
-
|
|
468
|
+
const cur = queue.shift();
|
|
539
469
|
if (cur) {
|
|
540
|
-
|
|
470
|
+
const compared = this._compare(cur.key, key);
|
|
541
471
|
if (compared === types_1.CP.gt)
|
|
542
472
|
cur.count += delta;
|
|
543
473
|
if (cur.left && this._compare(cur.left.key, key) === types_1.CP.gt)
|
|
@@ -548,15 +478,14 @@ var TreeMultiset = (function (_super) {
|
|
|
548
478
|
}
|
|
549
479
|
return true;
|
|
550
480
|
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
|
|
481
|
+
}
|
|
482
|
+
clear() {
|
|
483
|
+
super.clear();
|
|
554
484
|
this._setCount(0);
|
|
555
|
-
}
|
|
556
|
-
|
|
485
|
+
}
|
|
486
|
+
_setCount(v) {
|
|
557
487
|
this._count = v;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
}(avl_tree_1.AVLTree));
|
|
488
|
+
}
|
|
489
|
+
}
|
|
561
490
|
exports.TreeMultiset = TreeMultiset;
|
|
562
491
|
//# sourceMappingURL=tree-multiset.js.map
|