data-structure-typed 1.34.7 → 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 +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- 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 +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- 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 +168 -239
- 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 +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- 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 +33 -46
- 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 +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- 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/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- 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 +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1,225 +1,118 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.AbstractBinaryTree = exports.AbstractBinaryTreeNode = void 0;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
},
|
|
52
|
-
set: function (v) {
|
|
53
|
-
if (v) {
|
|
54
|
-
v.parent = this;
|
|
55
|
-
}
|
|
56
|
-
this._left = v;
|
|
57
|
-
},
|
|
58
|
-
enumerable: false,
|
|
59
|
-
configurable: true
|
|
60
|
-
});
|
|
61
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "right", {
|
|
62
|
-
get: function () {
|
|
63
|
-
return this._right;
|
|
64
|
-
},
|
|
65
|
-
set: function (v) {
|
|
66
|
-
if (v) {
|
|
67
|
-
v.parent = this;
|
|
68
|
-
}
|
|
69
|
-
this._right = v;
|
|
70
|
-
},
|
|
71
|
-
enumerable: false,
|
|
72
|
-
configurable: true
|
|
73
|
-
});
|
|
74
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "parent", {
|
|
75
|
-
get: function () {
|
|
76
|
-
return this._parent;
|
|
77
|
-
},
|
|
78
|
-
set: function (v) {
|
|
79
|
-
this._parent = v;
|
|
80
|
-
},
|
|
81
|
-
enumerable: false,
|
|
82
|
-
configurable: true
|
|
83
|
-
});
|
|
84
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "height", {
|
|
85
|
-
get: function () {
|
|
86
|
-
return this._height;
|
|
87
|
-
},
|
|
88
|
-
set: function (v) {
|
|
89
|
-
this._height = v;
|
|
90
|
-
},
|
|
91
|
-
enumerable: false,
|
|
92
|
-
configurable: true
|
|
93
|
-
});
|
|
94
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "familyPosition", {
|
|
95
|
-
get: function () {
|
|
96
|
-
var that = this;
|
|
97
|
-
if (that.parent) {
|
|
98
|
-
if (that.parent.left === that) {
|
|
99
|
-
if (that.left || that.right) {
|
|
100
|
-
return types_1.FamilyPosition.ROOT_LEFT;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
return types_1.FamilyPosition.LEFT;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else if (that.parent.right === that) {
|
|
107
|
-
if (that.left || that.right) {
|
|
108
|
-
return types_1.FamilyPosition.ROOT_RIGHT;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
return types_1.FamilyPosition.RIGHT;
|
|
112
|
-
}
|
|
4
|
+
const utils_1 = require("../../utils");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
class AbstractBinaryTreeNode {
|
|
7
|
+
constructor(key, val) {
|
|
8
|
+
this.key = key;
|
|
9
|
+
this.val = val;
|
|
10
|
+
}
|
|
11
|
+
get left() {
|
|
12
|
+
return this._left;
|
|
13
|
+
}
|
|
14
|
+
set left(v) {
|
|
15
|
+
if (v) {
|
|
16
|
+
v.parent = this;
|
|
17
|
+
}
|
|
18
|
+
this._left = v;
|
|
19
|
+
}
|
|
20
|
+
get right() {
|
|
21
|
+
return this._right;
|
|
22
|
+
}
|
|
23
|
+
set right(v) {
|
|
24
|
+
if (v) {
|
|
25
|
+
v.parent = this;
|
|
26
|
+
}
|
|
27
|
+
this._right = v;
|
|
28
|
+
}
|
|
29
|
+
get familyPosition() {
|
|
30
|
+
const that = this;
|
|
31
|
+
if (that.parent) {
|
|
32
|
+
if (that.parent.left === that) {
|
|
33
|
+
if (that.left || that.right) {
|
|
34
|
+
return types_1.FamilyPosition.ROOT_LEFT;
|
|
113
35
|
}
|
|
114
36
|
else {
|
|
115
|
-
return types_1.FamilyPosition.
|
|
37
|
+
return types_1.FamilyPosition.LEFT;
|
|
116
38
|
}
|
|
117
39
|
}
|
|
118
|
-
else {
|
|
40
|
+
else if (that.parent.right === that) {
|
|
119
41
|
if (that.left || that.right) {
|
|
120
|
-
return types_1.FamilyPosition.
|
|
42
|
+
return types_1.FamilyPosition.ROOT_RIGHT;
|
|
121
43
|
}
|
|
122
44
|
else {
|
|
123
|
-
return types_1.FamilyPosition.
|
|
45
|
+
return types_1.FamilyPosition.RIGHT;
|
|
124
46
|
}
|
|
125
47
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
48
|
+
else {
|
|
49
|
+
return types_1.FamilyPosition.MAL_NODE;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
if (that.left || that.right) {
|
|
54
|
+
return types_1.FamilyPosition.ROOT;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return types_1.FamilyPosition.ISOLATED;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
132
62
|
exports.AbstractBinaryTreeNode = AbstractBinaryTreeNode;
|
|
133
|
-
|
|
134
|
-
|
|
63
|
+
class AbstractBinaryTree {
|
|
64
|
+
constructor(options) {
|
|
135
65
|
this._root = null;
|
|
136
66
|
this._size = 0;
|
|
137
67
|
this._loopType = types_1.LoopType.ITERATIVE;
|
|
138
|
-
this.
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
68
|
+
this.visitedKey = [];
|
|
69
|
+
this.visitedVal = [];
|
|
70
|
+
this.visitedNode = [];
|
|
141
71
|
if (options !== undefined) {
|
|
142
|
-
|
|
72
|
+
const { loopType = types_1.LoopType.ITERATIVE } = options;
|
|
143
73
|
this._loopType = loopType;
|
|
144
74
|
}
|
|
145
75
|
this.clear();
|
|
146
76
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
configurable: true
|
|
160
|
-
});
|
|
161
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "loopType", {
|
|
162
|
-
get: function () {
|
|
163
|
-
return this._loopType;
|
|
164
|
-
},
|
|
165
|
-
enumerable: false,
|
|
166
|
-
configurable: true
|
|
167
|
-
});
|
|
168
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "visitedId", {
|
|
169
|
-
get: function () {
|
|
170
|
-
return this._visitedId;
|
|
171
|
-
},
|
|
172
|
-
enumerable: false,
|
|
173
|
-
configurable: true
|
|
174
|
-
});
|
|
175
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "visitedVal", {
|
|
176
|
-
get: function () {
|
|
177
|
-
return this._visitedVal;
|
|
178
|
-
},
|
|
179
|
-
enumerable: false,
|
|
180
|
-
configurable: true
|
|
181
|
-
});
|
|
182
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "visitedNode", {
|
|
183
|
-
get: function () {
|
|
184
|
-
return this._visitedNode;
|
|
185
|
-
},
|
|
186
|
-
enumerable: false,
|
|
187
|
-
configurable: true
|
|
188
|
-
});
|
|
189
|
-
AbstractBinaryTree.prototype.swapLocation = function (srcNode, destNode) {
|
|
190
|
-
var id = destNode.id, val = destNode.val, height = destNode.height;
|
|
191
|
-
var tempNode = this.createNode(id, val);
|
|
77
|
+
get root() {
|
|
78
|
+
return this._root;
|
|
79
|
+
}
|
|
80
|
+
get size() {
|
|
81
|
+
return this._size;
|
|
82
|
+
}
|
|
83
|
+
get loopType() {
|
|
84
|
+
return this._loopType;
|
|
85
|
+
}
|
|
86
|
+
swapLocation(srcNode, destNode) {
|
|
87
|
+
const { key, val } = destNode;
|
|
88
|
+
const tempNode = this.createNode(key, val);
|
|
192
89
|
if (tempNode) {
|
|
193
|
-
|
|
194
|
-
destNode.id = srcNode.id;
|
|
90
|
+
destNode.key = srcNode.key;
|
|
195
91
|
destNode.val = srcNode.val;
|
|
196
|
-
|
|
197
|
-
srcNode.id = tempNode.id;
|
|
92
|
+
srcNode.key = tempNode.key;
|
|
198
93
|
srcNode.val = tempNode.val;
|
|
199
|
-
srcNode.height = tempNode.height;
|
|
200
94
|
}
|
|
201
95
|
return destNode;
|
|
202
|
-
}
|
|
203
|
-
|
|
96
|
+
}
|
|
97
|
+
clear() {
|
|
204
98
|
this._root = null;
|
|
205
99
|
this._size = 0;
|
|
206
100
|
this._clearResults();
|
|
207
|
-
}
|
|
208
|
-
|
|
101
|
+
}
|
|
102
|
+
isEmpty() {
|
|
209
103
|
return this.size === 0;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
var queue = [root];
|
|
104
|
+
}
|
|
105
|
+
add(keyOrNode, val) {
|
|
106
|
+
const _bfs = (root, newNode) => {
|
|
107
|
+
const queue = [root];
|
|
215
108
|
while (queue.length > 0) {
|
|
216
|
-
|
|
109
|
+
const cur = queue.shift();
|
|
217
110
|
if (cur) {
|
|
218
|
-
if (newNode && cur.
|
|
111
|
+
if (newNode && cur.key === newNode.key)
|
|
219
112
|
return;
|
|
220
|
-
|
|
221
|
-
if (
|
|
222
|
-
return
|
|
113
|
+
const inserted = this._addTo(newNode, cur);
|
|
114
|
+
if (inserted !== undefined)
|
|
115
|
+
return inserted;
|
|
223
116
|
if (cur.left)
|
|
224
117
|
queue.push(cur.left);
|
|
225
118
|
if (cur.right)
|
|
@@ -230,20 +123,20 @@ var AbstractBinaryTree = (function () {
|
|
|
230
123
|
}
|
|
231
124
|
return;
|
|
232
125
|
};
|
|
233
|
-
|
|
234
|
-
if (
|
|
126
|
+
let inserted, needInsert;
|
|
127
|
+
if (keyOrNode === null) {
|
|
235
128
|
needInsert = null;
|
|
236
129
|
}
|
|
237
|
-
else if (typeof
|
|
238
|
-
needInsert = this.createNode(
|
|
130
|
+
else if (typeof keyOrNode === 'number') {
|
|
131
|
+
needInsert = this.createNode(keyOrNode, val);
|
|
239
132
|
}
|
|
240
|
-
else if (
|
|
241
|
-
needInsert =
|
|
133
|
+
else if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
134
|
+
needInsert = keyOrNode;
|
|
242
135
|
}
|
|
243
136
|
else {
|
|
244
137
|
return;
|
|
245
138
|
}
|
|
246
|
-
|
|
139
|
+
const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
|
|
247
140
|
if (this.root) {
|
|
248
141
|
if (existNode) {
|
|
249
142
|
existNode.val = val;
|
|
@@ -264,44 +157,44 @@ var AbstractBinaryTree = (function () {
|
|
|
264
157
|
inserted = this.root;
|
|
265
158
|
}
|
|
266
159
|
return inserted;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
for (
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
-
inserted.push(this.add(
|
|
160
|
+
}
|
|
161
|
+
addMany(keysOrNodes, data) {
|
|
162
|
+
const inserted = [];
|
|
163
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
164
|
+
const keyOrNode = keysOrNodes[i];
|
|
165
|
+
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
166
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
|
274
167
|
continue;
|
|
275
168
|
}
|
|
276
|
-
if (
|
|
169
|
+
if (keyOrNode === null) {
|
|
277
170
|
inserted.push(this.add(null));
|
|
278
171
|
continue;
|
|
279
172
|
}
|
|
280
|
-
|
|
281
|
-
inserted.push(this.add(
|
|
173
|
+
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
174
|
+
inserted.push(this.add(keyOrNode, val));
|
|
282
175
|
}
|
|
283
176
|
return inserted;
|
|
284
|
-
}
|
|
285
|
-
|
|
177
|
+
}
|
|
178
|
+
refill(keysOrNodes, data) {
|
|
286
179
|
this.clear();
|
|
287
|
-
return
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
180
|
+
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
181
|
+
}
|
|
182
|
+
remove(nodeOrKey) {
|
|
183
|
+
const bstDeletedResult = [];
|
|
291
184
|
if (!this.root)
|
|
292
185
|
return bstDeletedResult;
|
|
293
|
-
|
|
186
|
+
const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
|
|
294
187
|
if (!curr)
|
|
295
188
|
return bstDeletedResult;
|
|
296
|
-
|
|
297
|
-
|
|
189
|
+
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
190
|
+
let needBalanced = null, orgCurrent = curr;
|
|
298
191
|
if (!curr.left) {
|
|
299
192
|
if (!parent) {
|
|
300
193
|
if (curr.right !== undefined)
|
|
301
194
|
this._setRoot(curr.right);
|
|
302
195
|
}
|
|
303
196
|
else {
|
|
304
|
-
|
|
197
|
+
const { familyPosition: fp } = curr;
|
|
305
198
|
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
|
|
306
199
|
parent.left = curr.right;
|
|
307
200
|
}
|
|
@@ -312,9 +205,9 @@ var AbstractBinaryTree = (function () {
|
|
|
312
205
|
}
|
|
313
206
|
}
|
|
314
207
|
else {
|
|
315
|
-
|
|
208
|
+
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
316
209
|
if (leftSubTreeRightMost) {
|
|
317
|
-
|
|
210
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
318
211
|
orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
|
|
319
212
|
if (parentOfLeftSubTreeMax) {
|
|
320
213
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
@@ -326,43 +219,42 @@ var AbstractBinaryTree = (function () {
|
|
|
326
219
|
}
|
|
327
220
|
}
|
|
328
221
|
this._setSize(this.size - 1);
|
|
329
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced
|
|
222
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
330
223
|
return bstDeletedResult;
|
|
331
|
-
}
|
|
332
|
-
|
|
224
|
+
}
|
|
225
|
+
getDepth(beginRoot = this.root) {
|
|
333
226
|
if (typeof beginRoot === 'number')
|
|
334
|
-
beginRoot = this.get(beginRoot, '
|
|
335
|
-
|
|
227
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
228
|
+
let depth = 0;
|
|
336
229
|
while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
|
|
337
230
|
depth++;
|
|
338
231
|
beginRoot = beginRoot.parent;
|
|
339
232
|
}
|
|
340
233
|
return depth;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
234
|
+
}
|
|
235
|
+
getHeight(beginRoot = this.root) {
|
|
344
236
|
if (typeof beginRoot === 'number')
|
|
345
|
-
beginRoot = this.get(beginRoot, '
|
|
237
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
346
238
|
if (!beginRoot)
|
|
347
239
|
return -1;
|
|
348
240
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
349
|
-
|
|
241
|
+
const _getMaxHeight = (cur) => {
|
|
350
242
|
if (!cur)
|
|
351
243
|
return -1;
|
|
352
|
-
|
|
353
|
-
|
|
244
|
+
const leftHeight = _getMaxHeight(cur.left);
|
|
245
|
+
const rightHeight = _getMaxHeight(cur.right);
|
|
354
246
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
355
247
|
};
|
|
356
|
-
return
|
|
248
|
+
return _getMaxHeight(beginRoot);
|
|
357
249
|
}
|
|
358
250
|
else {
|
|
359
251
|
if (!beginRoot) {
|
|
360
252
|
return -1;
|
|
361
253
|
}
|
|
362
|
-
|
|
363
|
-
|
|
254
|
+
const stack = [{ node: beginRoot, depth: 0 }];
|
|
255
|
+
let maxHeight = 0;
|
|
364
256
|
while (stack.length > 0) {
|
|
365
|
-
|
|
257
|
+
const { node, depth } = stack.pop();
|
|
366
258
|
if (node.left) {
|
|
367
259
|
stack.push({ node: node.left, depth: depth + 1 });
|
|
368
260
|
}
|
|
@@ -373,28 +265,27 @@ var AbstractBinaryTree = (function () {
|
|
|
373
265
|
}
|
|
374
266
|
return maxHeight;
|
|
375
267
|
}
|
|
376
|
-
}
|
|
377
|
-
|
|
268
|
+
}
|
|
269
|
+
getMinHeight(beginRoot = this.root) {
|
|
378
270
|
var _a, _b, _c;
|
|
379
|
-
beginRoot = beginRoot || this.root;
|
|
380
271
|
if (!beginRoot)
|
|
381
272
|
return -1;
|
|
382
273
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
383
|
-
|
|
274
|
+
const _getMinHeight = (cur) => {
|
|
384
275
|
if (!cur)
|
|
385
276
|
return 0;
|
|
386
277
|
if (!cur.left && !cur.right)
|
|
387
278
|
return 0;
|
|
388
|
-
|
|
389
|
-
|
|
279
|
+
const leftMinHeight = _getMinHeight(cur.left);
|
|
280
|
+
const rightMinHeight = _getMinHeight(cur.right);
|
|
390
281
|
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
391
282
|
};
|
|
392
|
-
return
|
|
283
|
+
return _getMinHeight(beginRoot);
|
|
393
284
|
}
|
|
394
285
|
else {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
286
|
+
const stack = [];
|
|
287
|
+
let node = beginRoot, last = null;
|
|
288
|
+
const depths = new Map();
|
|
398
289
|
while (stack.length > 0 || node) {
|
|
399
290
|
if (node) {
|
|
400
291
|
stack.push(node);
|
|
@@ -405,8 +296,8 @@ var AbstractBinaryTree = (function () {
|
|
|
405
296
|
if (!node.right || last === node.right) {
|
|
406
297
|
node = stack.pop();
|
|
407
298
|
if (node) {
|
|
408
|
-
|
|
409
|
-
|
|
299
|
+
const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
300
|
+
const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
410
301
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
411
302
|
last = node;
|
|
412
303
|
node = null;
|
|
@@ -418,31 +309,29 @@ var AbstractBinaryTree = (function () {
|
|
|
418
309
|
}
|
|
419
310
|
return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
|
|
420
311
|
}
|
|
421
|
-
}
|
|
422
|
-
|
|
312
|
+
}
|
|
313
|
+
isPerfectlyBalanced(beginRoot = this.root) {
|
|
423
314
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
var _this = this;
|
|
315
|
+
}
|
|
316
|
+
getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
427
317
|
if (!this.root)
|
|
428
318
|
return [];
|
|
429
|
-
|
|
430
|
-
var result = [];
|
|
319
|
+
const result = [];
|
|
431
320
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
432
|
-
|
|
433
|
-
if (
|
|
321
|
+
const _traverse = (cur) => {
|
|
322
|
+
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
434
323
|
return;
|
|
435
324
|
if (!cur.left && !cur.right)
|
|
436
325
|
return;
|
|
437
|
-
cur.left &&
|
|
438
|
-
cur.right &&
|
|
326
|
+
cur.left && _traverse(cur.left);
|
|
327
|
+
cur.right && _traverse(cur.right);
|
|
439
328
|
};
|
|
440
|
-
|
|
329
|
+
_traverse(this.root);
|
|
441
330
|
}
|
|
442
331
|
else {
|
|
443
|
-
|
|
332
|
+
const queue = [this.root];
|
|
444
333
|
while (queue.length > 0) {
|
|
445
|
-
|
|
334
|
+
const cur = queue.shift();
|
|
446
335
|
if (cur) {
|
|
447
336
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
448
337
|
return result;
|
|
@@ -452,209 +341,201 @@ var AbstractBinaryTree = (function () {
|
|
|
452
341
|
}
|
|
453
342
|
}
|
|
454
343
|
return result;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
344
|
+
}
|
|
345
|
+
has(nodeProperty, propertyName = 'key') {
|
|
458
346
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
459
|
-
}
|
|
460
|
-
|
|
347
|
+
}
|
|
348
|
+
get(nodeProperty, propertyName = 'key') {
|
|
461
349
|
var _a;
|
|
462
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
463
350
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
var result = [];
|
|
351
|
+
}
|
|
352
|
+
getPathToRoot(node, isReverse = true) {
|
|
353
|
+
const result = [];
|
|
468
354
|
while (node.parent) {
|
|
469
355
|
result.push(node);
|
|
470
356
|
node = node.parent;
|
|
471
357
|
}
|
|
472
358
|
result.push(node);
|
|
473
359
|
return isReverse ? result.reverse() : result;
|
|
474
|
-
}
|
|
475
|
-
|
|
360
|
+
}
|
|
361
|
+
getLeftMost(beginRoot = this.root) {
|
|
476
362
|
if (typeof beginRoot === 'number')
|
|
477
|
-
beginRoot = this.get(beginRoot, '
|
|
478
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
363
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
479
364
|
if (!beginRoot)
|
|
480
365
|
return beginRoot;
|
|
481
366
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
482
|
-
|
|
367
|
+
const _traverse = (cur) => {
|
|
483
368
|
if (!cur.left)
|
|
484
369
|
return cur;
|
|
485
|
-
return
|
|
370
|
+
return _traverse(cur.left);
|
|
486
371
|
};
|
|
487
|
-
return
|
|
372
|
+
return _traverse(beginRoot);
|
|
488
373
|
}
|
|
489
374
|
else {
|
|
490
|
-
|
|
375
|
+
const _traverse = (0, utils_1.trampoline)((cur) => {
|
|
491
376
|
if (!cur.left)
|
|
492
377
|
return cur;
|
|
493
|
-
return
|
|
378
|
+
return _traverse.cont(cur.left);
|
|
494
379
|
});
|
|
495
|
-
return
|
|
380
|
+
return _traverse(beginRoot);
|
|
496
381
|
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
return node;
|
|
382
|
+
}
|
|
383
|
+
getRightMost(beginRoot = this.root) {
|
|
384
|
+
if (!beginRoot)
|
|
385
|
+
return beginRoot;
|
|
502
386
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
503
|
-
|
|
387
|
+
const _traverse = (cur) => {
|
|
504
388
|
if (!cur.right)
|
|
505
389
|
return cur;
|
|
506
|
-
return
|
|
390
|
+
return _traverse(cur.right);
|
|
507
391
|
};
|
|
508
|
-
return
|
|
392
|
+
return _traverse(beginRoot);
|
|
509
393
|
}
|
|
510
394
|
else {
|
|
511
|
-
|
|
395
|
+
const _traverse = (0, utils_1.trampoline)((cur) => {
|
|
512
396
|
if (!cur.right)
|
|
513
397
|
return cur;
|
|
514
|
-
return
|
|
398
|
+
return _traverse.cont(cur.right);
|
|
515
399
|
});
|
|
516
|
-
return
|
|
400
|
+
return _traverse(beginRoot);
|
|
517
401
|
}
|
|
518
|
-
}
|
|
519
|
-
|
|
402
|
+
}
|
|
403
|
+
isSubtreeBST(node) {
|
|
520
404
|
if (!node)
|
|
521
405
|
return true;
|
|
522
406
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
523
|
-
|
|
407
|
+
const dfs = (cur, min, max) => {
|
|
524
408
|
if (!cur)
|
|
525
409
|
return true;
|
|
526
|
-
if (cur.
|
|
410
|
+
if (cur.key <= min || cur.key >= max)
|
|
527
411
|
return false;
|
|
528
|
-
return
|
|
412
|
+
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
529
413
|
};
|
|
530
|
-
return
|
|
414
|
+
return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
531
415
|
}
|
|
532
416
|
else {
|
|
533
|
-
|
|
534
|
-
|
|
417
|
+
const stack = [];
|
|
418
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = node;
|
|
535
419
|
while (curr || stack.length > 0) {
|
|
536
420
|
while (curr) {
|
|
537
421
|
stack.push(curr);
|
|
538
422
|
curr = curr.left;
|
|
539
423
|
}
|
|
540
424
|
curr = stack.pop();
|
|
541
|
-
if (!curr || prev >= curr.
|
|
425
|
+
if (!curr || prev >= curr.key)
|
|
542
426
|
return false;
|
|
543
|
-
prev = curr.
|
|
427
|
+
prev = curr.key;
|
|
544
428
|
curr = curr.right;
|
|
545
429
|
}
|
|
546
430
|
return true;
|
|
547
431
|
}
|
|
548
|
-
}
|
|
549
|
-
|
|
432
|
+
}
|
|
433
|
+
isBST() {
|
|
550
434
|
return this.isSubtreeBST(this.root);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
|
|
435
|
+
}
|
|
436
|
+
getSubTreeSize(subTreeRoot) {
|
|
437
|
+
let size = 0;
|
|
554
438
|
if (!subTreeRoot)
|
|
555
439
|
return size;
|
|
556
440
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
557
|
-
|
|
441
|
+
const _traverse = (cur) => {
|
|
558
442
|
size++;
|
|
559
|
-
cur.left &&
|
|
560
|
-
cur.right &&
|
|
443
|
+
cur.left && _traverse(cur.left);
|
|
444
|
+
cur.right && _traverse(cur.right);
|
|
561
445
|
};
|
|
562
|
-
|
|
446
|
+
_traverse(subTreeRoot);
|
|
563
447
|
return size;
|
|
564
448
|
}
|
|
565
449
|
else {
|
|
566
|
-
|
|
450
|
+
const stack = [subTreeRoot];
|
|
567
451
|
while (stack.length > 0) {
|
|
568
|
-
|
|
452
|
+
const cur = stack.pop();
|
|
569
453
|
size++;
|
|
570
454
|
cur.right && stack.push(cur.right);
|
|
571
455
|
cur.left && stack.push(cur.left);
|
|
572
456
|
}
|
|
573
457
|
return size;
|
|
574
458
|
}
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
459
|
+
}
|
|
460
|
+
subTreeSum(subTreeRoot, propertyName = 'key') {
|
|
578
461
|
if (typeof subTreeRoot === 'number')
|
|
579
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
462
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
580
463
|
if (!subTreeRoot)
|
|
581
464
|
return 0;
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
465
|
+
let sum = 0;
|
|
466
|
+
const _sumByProperty = (cur) => {
|
|
467
|
+
let needSum;
|
|
585
468
|
switch (propertyName) {
|
|
586
|
-
case '
|
|
587
|
-
needSum = cur.
|
|
469
|
+
case 'key':
|
|
470
|
+
needSum = cur.key;
|
|
588
471
|
break;
|
|
589
472
|
case 'val':
|
|
590
473
|
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
591
474
|
break;
|
|
592
475
|
default:
|
|
593
|
-
needSum = cur.
|
|
476
|
+
needSum = cur.key;
|
|
594
477
|
break;
|
|
595
478
|
}
|
|
596
479
|
return needSum;
|
|
597
480
|
};
|
|
598
481
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
599
|
-
|
|
482
|
+
const _traverse = (cur) => {
|
|
600
483
|
sum += _sumByProperty(cur);
|
|
601
|
-
cur.left &&
|
|
602
|
-
cur.right &&
|
|
484
|
+
cur.left && _traverse(cur.left);
|
|
485
|
+
cur.right && _traverse(cur.right);
|
|
603
486
|
};
|
|
604
|
-
|
|
487
|
+
_traverse(subTreeRoot);
|
|
605
488
|
}
|
|
606
489
|
else {
|
|
607
|
-
|
|
490
|
+
const stack = [subTreeRoot];
|
|
608
491
|
while (stack.length > 0) {
|
|
609
|
-
|
|
492
|
+
const cur = stack.pop();
|
|
610
493
|
sum += _sumByProperty(cur);
|
|
611
494
|
cur.right && stack.push(cur.right);
|
|
612
495
|
cur.left && stack.push(cur.left);
|
|
613
496
|
}
|
|
614
497
|
}
|
|
615
498
|
return sum;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
499
|
+
}
|
|
500
|
+
subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
|
|
619
501
|
if (typeof subTreeRoot === 'number')
|
|
620
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
502
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
621
503
|
if (!subTreeRoot)
|
|
622
504
|
return false;
|
|
623
|
-
|
|
505
|
+
const _addByProperty = (cur) => {
|
|
624
506
|
switch (propertyName) {
|
|
625
|
-
case '
|
|
626
|
-
cur.
|
|
507
|
+
case 'key':
|
|
508
|
+
cur.key += delta;
|
|
627
509
|
break;
|
|
628
510
|
default:
|
|
629
|
-
cur.
|
|
511
|
+
cur.key += delta;
|
|
630
512
|
break;
|
|
631
513
|
}
|
|
632
514
|
};
|
|
633
515
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
634
|
-
|
|
516
|
+
const _traverse = (cur) => {
|
|
635
517
|
_addByProperty(cur);
|
|
636
|
-
cur.left &&
|
|
637
|
-
cur.right &&
|
|
518
|
+
cur.left && _traverse(cur.left);
|
|
519
|
+
cur.right && _traverse(cur.right);
|
|
638
520
|
};
|
|
639
|
-
|
|
521
|
+
_traverse(subTreeRoot);
|
|
640
522
|
}
|
|
641
523
|
else {
|
|
642
|
-
|
|
524
|
+
const stack = [subTreeRoot];
|
|
643
525
|
while (stack.length > 0) {
|
|
644
|
-
|
|
526
|
+
const cur = stack.pop();
|
|
645
527
|
_addByProperty(cur);
|
|
646
528
|
cur.right && stack.push(cur.right);
|
|
647
529
|
cur.left && stack.push(cur.left);
|
|
648
530
|
}
|
|
649
531
|
}
|
|
650
532
|
return true;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
533
|
+
}
|
|
534
|
+
bfs(nodeOrPropertyName = 'key') {
|
|
654
535
|
this._clearResults();
|
|
655
|
-
|
|
536
|
+
const queue = [this.root];
|
|
656
537
|
while (queue.length !== 0) {
|
|
657
|
-
|
|
538
|
+
const cur = queue.shift();
|
|
658
539
|
if (cur) {
|
|
659
540
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
660
541
|
if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
|
|
@@ -664,23 +545,20 @@ var AbstractBinaryTree = (function () {
|
|
|
664
545
|
}
|
|
665
546
|
}
|
|
666
547
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
var _this = this;
|
|
670
|
-
pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
|
|
671
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
548
|
+
}
|
|
549
|
+
dfs(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
672
550
|
this._clearResults();
|
|
673
|
-
|
|
551
|
+
const _traverse = (node) => {
|
|
674
552
|
switch (pattern) {
|
|
675
553
|
case 'in':
|
|
676
554
|
if (node.left)
|
|
677
555
|
_traverse(node.left);
|
|
678
|
-
|
|
556
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
679
557
|
if (node.right)
|
|
680
558
|
_traverse(node.right);
|
|
681
559
|
break;
|
|
682
560
|
case 'pre':
|
|
683
|
-
|
|
561
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
684
562
|
if (node.left)
|
|
685
563
|
_traverse(node.left);
|
|
686
564
|
if (node.right)
|
|
@@ -691,22 +569,20 @@ var AbstractBinaryTree = (function () {
|
|
|
691
569
|
_traverse(node.left);
|
|
692
570
|
if (node.right)
|
|
693
571
|
_traverse(node.right);
|
|
694
|
-
|
|
572
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
695
573
|
break;
|
|
696
574
|
}
|
|
697
575
|
};
|
|
698
576
|
this.root && _traverse(this.root);
|
|
699
577
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
pattern = pattern || 'in';
|
|
703
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
578
|
+
}
|
|
579
|
+
dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
704
580
|
this._clearResults();
|
|
705
581
|
if (!this.root)
|
|
706
582
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
707
|
-
|
|
583
|
+
const stack = [{ opt: 0, node: this.root }];
|
|
708
584
|
while (stack.length > 0) {
|
|
709
|
-
|
|
585
|
+
const cur = stack.pop();
|
|
710
586
|
if (!cur || !cur.node)
|
|
711
587
|
continue;
|
|
712
588
|
if (cur.opt === 1) {
|
|
@@ -738,16 +614,14 @@ var AbstractBinaryTree = (function () {
|
|
|
738
614
|
}
|
|
739
615
|
}
|
|
740
616
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
744
|
-
node = node || this.root;
|
|
617
|
+
}
|
|
618
|
+
levelIterative(node = this.root, nodeOrPropertyName = 'key') {
|
|
745
619
|
if (!node)
|
|
746
620
|
return [];
|
|
747
621
|
this._clearResults();
|
|
748
|
-
|
|
622
|
+
const queue = [node];
|
|
749
623
|
while (queue.length > 0) {
|
|
750
|
-
|
|
624
|
+
const cur = queue.shift();
|
|
751
625
|
if (cur) {
|
|
752
626
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
753
627
|
if (cur.left) {
|
|
@@ -759,17 +633,15 @@ var AbstractBinaryTree = (function () {
|
|
|
759
633
|
}
|
|
760
634
|
}
|
|
761
635
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
765
|
-
node = node || this.root;
|
|
636
|
+
}
|
|
637
|
+
listLevels(node = this.root, nodeOrPropertyName = 'key') {
|
|
766
638
|
if (!node)
|
|
767
639
|
return [];
|
|
768
|
-
|
|
769
|
-
|
|
640
|
+
const levelsNodes = [];
|
|
641
|
+
const collectByProperty = (node, level) => {
|
|
770
642
|
switch (nodeOrPropertyName) {
|
|
771
|
-
case '
|
|
772
|
-
levelsNodes[level].push(node.
|
|
643
|
+
case 'key':
|
|
644
|
+
levelsNodes[level].push(node.key);
|
|
773
645
|
break;
|
|
774
646
|
case 'val':
|
|
775
647
|
levelsNodes[level].push(node.val);
|
|
@@ -778,41 +650,41 @@ var AbstractBinaryTree = (function () {
|
|
|
778
650
|
levelsNodes[level].push(node);
|
|
779
651
|
break;
|
|
780
652
|
default:
|
|
781
|
-
levelsNodes[level].push(node.
|
|
653
|
+
levelsNodes[level].push(node.key);
|
|
782
654
|
break;
|
|
783
655
|
}
|
|
784
656
|
};
|
|
785
657
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
786
|
-
|
|
658
|
+
const _recursive = (node, level) => {
|
|
787
659
|
if (!levelsNodes[level])
|
|
788
660
|
levelsNodes[level] = [];
|
|
789
661
|
collectByProperty(node, level);
|
|
790
662
|
if (node.left)
|
|
791
|
-
|
|
663
|
+
_recursive(node.left, level + 1);
|
|
792
664
|
if (node.right)
|
|
793
|
-
|
|
665
|
+
_recursive(node.right, level + 1);
|
|
794
666
|
};
|
|
795
|
-
|
|
667
|
+
_recursive(node, 0);
|
|
796
668
|
}
|
|
797
669
|
else {
|
|
798
|
-
|
|
670
|
+
const stack = [[node, 0]];
|
|
799
671
|
while (stack.length > 0) {
|
|
800
|
-
|
|
801
|
-
|
|
672
|
+
const head = stack.pop();
|
|
673
|
+
const [node, level] = head;
|
|
802
674
|
if (!levelsNodes[level])
|
|
803
675
|
levelsNodes[level] = [];
|
|
804
|
-
collectByProperty(
|
|
805
|
-
if (
|
|
806
|
-
stack.push([
|
|
807
|
-
if (
|
|
808
|
-
stack.push([
|
|
676
|
+
collectByProperty(node, level);
|
|
677
|
+
if (node.right)
|
|
678
|
+
stack.push([node.right, level + 1]);
|
|
679
|
+
if (node.left)
|
|
680
|
+
stack.push([node.left, level + 1]);
|
|
809
681
|
}
|
|
810
682
|
}
|
|
811
683
|
return levelsNodes;
|
|
812
|
-
}
|
|
813
|
-
|
|
684
|
+
}
|
|
685
|
+
getPredecessor(node) {
|
|
814
686
|
if (node.left) {
|
|
815
|
-
|
|
687
|
+
let predecessor = node.left;
|
|
816
688
|
while (!predecessor || (predecessor.right && predecessor.right !== node)) {
|
|
817
689
|
if (predecessor) {
|
|
818
690
|
predecessor = predecessor.right;
|
|
@@ -823,18 +695,15 @@ var AbstractBinaryTree = (function () {
|
|
|
823
695
|
else {
|
|
824
696
|
return node;
|
|
825
697
|
}
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
var _this = this;
|
|
698
|
+
}
|
|
699
|
+
morris(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
829
700
|
if (this.root === null)
|
|
830
701
|
return [];
|
|
831
|
-
pattern = pattern || 'in';
|
|
832
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
833
702
|
this._clearResults();
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
703
|
+
let cur = this.root;
|
|
704
|
+
const _reverseEdge = (node) => {
|
|
705
|
+
let pre = null;
|
|
706
|
+
let next = null;
|
|
838
707
|
while (node) {
|
|
839
708
|
next = node.right;
|
|
840
709
|
node.right = pre;
|
|
@@ -843,11 +712,11 @@ var AbstractBinaryTree = (function () {
|
|
|
843
712
|
}
|
|
844
713
|
return pre;
|
|
845
714
|
};
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
715
|
+
const _printEdge = (node) => {
|
|
716
|
+
const tail = _reverseEdge(node);
|
|
717
|
+
let cur = tail;
|
|
849
718
|
while (cur) {
|
|
850
|
-
|
|
719
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
851
720
|
cur = cur.right;
|
|
852
721
|
}
|
|
853
722
|
_reverseEdge(tail);
|
|
@@ -856,7 +725,7 @@ var AbstractBinaryTree = (function () {
|
|
|
856
725
|
case 'in':
|
|
857
726
|
while (cur) {
|
|
858
727
|
if (cur.left) {
|
|
859
|
-
|
|
728
|
+
const predecessor = this.getPredecessor(cur);
|
|
860
729
|
if (!predecessor.right) {
|
|
861
730
|
predecessor.right = cur;
|
|
862
731
|
cur = cur.left;
|
|
@@ -873,7 +742,7 @@ var AbstractBinaryTree = (function () {
|
|
|
873
742
|
case 'pre':
|
|
874
743
|
while (cur) {
|
|
875
744
|
if (cur.left) {
|
|
876
|
-
|
|
745
|
+
const predecessor = this.getPredecessor(cur);
|
|
877
746
|
if (!predecessor.right) {
|
|
878
747
|
predecessor.right = cur;
|
|
879
748
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
@@ -893,7 +762,7 @@ var AbstractBinaryTree = (function () {
|
|
|
893
762
|
case 'post':
|
|
894
763
|
while (cur) {
|
|
895
764
|
if (cur.left) {
|
|
896
|
-
|
|
765
|
+
const predecessor = this.getPredecessor(cur);
|
|
897
766
|
if (predecessor.right === null) {
|
|
898
767
|
predecessor.right = cur;
|
|
899
768
|
cur = cur.left;
|
|
@@ -910,8 +779,8 @@ var AbstractBinaryTree = (function () {
|
|
|
910
779
|
break;
|
|
911
780
|
}
|
|
912
781
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
913
|
-
}
|
|
914
|
-
|
|
782
|
+
}
|
|
783
|
+
_addTo(newNode, parent) {
|
|
915
784
|
if (parent) {
|
|
916
785
|
if (parent.left === undefined) {
|
|
917
786
|
parent.left = newNode;
|
|
@@ -934,86 +803,74 @@ var AbstractBinaryTree = (function () {
|
|
|
934
803
|
else {
|
|
935
804
|
return;
|
|
936
805
|
}
|
|
937
|
-
}
|
|
938
|
-
|
|
806
|
+
}
|
|
807
|
+
_setLoopType(value) {
|
|
939
808
|
this._loopType = value;
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
this._visitedId = value;
|
|
943
|
-
};
|
|
944
|
-
AbstractBinaryTree.prototype._setVisitedVal = function (value) {
|
|
945
|
-
this._visitedVal = value;
|
|
946
|
-
};
|
|
947
|
-
AbstractBinaryTree.prototype._setVisitedNode = function (value) {
|
|
948
|
-
this._visitedNode = value;
|
|
949
|
-
};
|
|
950
|
-
AbstractBinaryTree.prototype._setRoot = function (v) {
|
|
809
|
+
}
|
|
810
|
+
_setRoot(v) {
|
|
951
811
|
if (v) {
|
|
952
812
|
v.parent = undefined;
|
|
953
813
|
}
|
|
954
814
|
this._root = v;
|
|
955
|
-
}
|
|
956
|
-
|
|
815
|
+
}
|
|
816
|
+
_setSize(v) {
|
|
957
817
|
this._size = v;
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
this.
|
|
961
|
-
this.
|
|
962
|
-
this.
|
|
963
|
-
}
|
|
964
|
-
|
|
818
|
+
}
|
|
819
|
+
_clearResults() {
|
|
820
|
+
this.visitedKey = [];
|
|
821
|
+
this.visitedVal = [];
|
|
822
|
+
this.visitedNode = [];
|
|
823
|
+
}
|
|
824
|
+
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
965
825
|
switch (propertyName) {
|
|
966
|
-
case '
|
|
967
|
-
if (cur.
|
|
826
|
+
case 'key':
|
|
827
|
+
if (cur.key === nodeProperty) {
|
|
968
828
|
result.push(cur);
|
|
969
|
-
return
|
|
829
|
+
return onlyOne;
|
|
970
830
|
}
|
|
971
831
|
break;
|
|
972
832
|
case 'val':
|
|
973
833
|
if (cur.val === nodeProperty) {
|
|
974
834
|
result.push(cur);
|
|
975
|
-
return
|
|
835
|
+
return onlyOne;
|
|
976
836
|
}
|
|
977
837
|
break;
|
|
978
838
|
default:
|
|
979
|
-
if (cur.
|
|
839
|
+
if (cur.key === nodeProperty) {
|
|
980
840
|
result.push(cur);
|
|
981
|
-
return
|
|
841
|
+
return onlyOne;
|
|
982
842
|
}
|
|
983
843
|
break;
|
|
984
844
|
}
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
845
|
+
}
|
|
846
|
+
_accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
|
|
988
847
|
switch (nodeOrPropertyName) {
|
|
989
|
-
case '
|
|
990
|
-
this.
|
|
848
|
+
case 'key':
|
|
849
|
+
this.visitedKey.push(node.key);
|
|
991
850
|
break;
|
|
992
851
|
case 'val':
|
|
993
|
-
this.
|
|
852
|
+
this.visitedVal.push(node.val);
|
|
994
853
|
break;
|
|
995
854
|
case 'node':
|
|
996
|
-
this.
|
|
855
|
+
this.visitedNode.push(node);
|
|
997
856
|
break;
|
|
998
857
|
default:
|
|
999
|
-
this.
|
|
858
|
+
this.visitedKey.push(node.key);
|
|
1000
859
|
break;
|
|
1001
860
|
}
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
861
|
+
}
|
|
862
|
+
_getResultByPropertyName(nodeOrPropertyName = 'key') {
|
|
1005
863
|
switch (nodeOrPropertyName) {
|
|
1006
|
-
case '
|
|
1007
|
-
return this.
|
|
864
|
+
case 'key':
|
|
865
|
+
return this.visitedKey;
|
|
1008
866
|
case 'val':
|
|
1009
|
-
return this.
|
|
867
|
+
return this.visitedVal;
|
|
1010
868
|
case 'node':
|
|
1011
|
-
return this.
|
|
869
|
+
return this.visitedNode;
|
|
1012
870
|
default:
|
|
1013
|
-
return this.
|
|
871
|
+
return this.visitedKey;
|
|
1014
872
|
}
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
}());
|
|
873
|
+
}
|
|
874
|
+
}
|
|
1018
875
|
exports.AbstractBinaryTree = AbstractBinaryTree;
|
|
1019
876
|
//# sourceMappingURL=abstract-binary-tree.js.map
|