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