data-structure-typed 1.18.5 → 1.18.7
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/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +357 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1480 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -6
- package/dist/data-structures/binary-tree/avl-tree.js +7 -5
- package/dist/data-structures/binary-tree/binary-tree.d.ts +19 -337
- package/dist/data-structures/binary-tree/binary-tree.js +40 -1441
- package/dist/data-structures/binary-tree/bst.d.ts +31 -17
- package/dist/data-structures/binary-tree/bst.js +57 -40
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +30 -1
- package/dist/data-structures/binary-tree/rb-tree.js +29 -20
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +21 -25
- package/dist/data-structures/binary-tree/tree-multiset.js +40 -31
- package/dist/data-structures/graph/abstract-graph.d.ts +20 -21
- package/dist/data-structures/graph/abstract-graph.js +24 -19
- package/dist/data-structures/graph/directed-graph.d.ts +12 -13
- package/dist/data-structures/graph/directed-graph.js +21 -25
- package/dist/data-structures/graph/undirected-graph.d.ts +15 -19
- package/dist/data-structures/graph/undirected-graph.js +16 -29
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +101 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.js +2 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +1 -4
- package/dist/data-structures/interfaces/avl-tree.d.ts +16 -1
- package/dist/data-structures/interfaces/binary-tree.d.ts +4 -25
- package/dist/data-structures/interfaces/bst.d.ts +18 -1
- package/dist/data-structures/interfaces/directed-graph.d.ts +4 -1
- package/dist/data-structures/interfaces/index.d.ts +1 -1
- package/dist/data-structures/interfaces/index.js +1 -1
- package/dist/data-structures/interfaces/rb-tree.d.ts +9 -0
- package/dist/data-structures/interfaces/rb-tree.js +2 -0
- package/dist/data-structures/interfaces/undirected-graph.d.ts +4 -1
- package/dist/data-structures/types/abstract-binary-tree.d.ts +32 -0
- package/dist/data-structures/types/abstract-binary-tree.js +21 -0
- package/dist/data-structures/types/avl-tree.d.ts +2 -4
- package/dist/data-structures/types/binary-tree.d.ts +2 -10
- package/dist/data-structures/types/bst.d.ts +10 -5
- package/dist/data-structures/types/bst.js +7 -0
- package/dist/data-structures/types/helpers.d.ts +8 -0
- package/dist/data-structures/types/helpers.js +2 -0
- package/dist/data-structures/types/index.d.ts +3 -0
- package/dist/data-structures/types/index.js +3 -0
- package/dist/data-structures/types/rb-tree.d.ts +6 -0
- package/dist/data-structures/types/rb-tree.js +8 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -3
- package/dist/utils/utils.js +62 -0
- package/docs/assets/search.js +1 -1
- package/docs/classes/AVLTree.html +316 -204
- package/docs/classes/AVLTreeNode.html +141 -81
- package/docs/classes/AaTree.html +27 -10
- package/docs/classes/AbstractBinaryTree.html +2118 -0
- package/docs/classes/AbstractBinaryTreeNode.html +524 -0
- package/docs/classes/AbstractEdge.html +37 -20
- package/docs/classes/AbstractGraph.html +152 -144
- package/docs/classes/AbstractVertex.html +34 -17
- package/docs/classes/ArrayDeque.html +40 -23
- package/docs/classes/BST.html +294 -187
- package/docs/classes/BSTNode.html +144 -82
- package/docs/classes/BTree.html +27 -10
- package/docs/classes/BinaryIndexedTree.html +35 -18
- package/docs/classes/BinaryTree.html +597 -500
- package/docs/classes/BinaryTreeNode.html +189 -168
- package/docs/classes/Character.html +30 -13
- package/docs/classes/CoordinateMap.html +35 -18
- package/docs/classes/CoordinateSet.html +34 -17
- package/docs/classes/Deque.html +60 -43
- package/docs/classes/DirectedEdge.html +41 -24
- package/docs/classes/DirectedGraph.html +208 -247
- package/docs/classes/DirectedVertex.html +32 -15
- package/docs/classes/DoublyLinkedList.html +63 -46
- package/docs/classes/DoublyLinkedListNode.html +37 -20
- package/docs/classes/HashTable.html +27 -10
- package/docs/classes/Heap.html +41 -24
- package/docs/classes/HeapItem.html +34 -17
- package/docs/classes/Matrix2D.html +42 -25
- package/docs/classes/MatrixNTI2D.html +30 -13
- package/docs/classes/MaxHeap.html +41 -24
- package/docs/classes/MaxPriorityQueue.html +60 -43
- package/docs/classes/MinHeap.html +41 -24
- package/docs/classes/MinPriorityQueue.html +60 -43
- package/docs/classes/Navigator.html +36 -19
- package/docs/classes/ObjectDeque.html +51 -34
- package/docs/classes/Pair.html +27 -10
- package/docs/classes/PriorityQueue.html +58 -41
- package/docs/classes/Queue.html +40 -23
- package/docs/classes/RBTree.html +2388 -0
- package/docs/classes/RBTreeNode.html +516 -0
- package/docs/classes/SegmentTree.html +43 -26
- package/docs/classes/SegmentTreeNode.html +46 -29
- package/docs/classes/SinglyLinkedList.html +60 -43
- package/docs/classes/SinglyLinkedListNode.html +34 -17
- package/docs/classes/SkipLinkedList.html +27 -10
- package/docs/classes/SplayTree.html +27 -10
- package/docs/classes/Stack.html +38 -21
- package/docs/classes/TreeMap.html +27 -10
- package/docs/classes/TreeMultiSet.html +332 -224
- package/docs/classes/TreeMultiSetNode.html +499 -0
- package/docs/classes/TreeNode.html +42 -25
- package/docs/classes/TreeSet.html +27 -10
- package/docs/classes/Trie.html +39 -22
- package/docs/classes/TrieNode.html +37 -20
- package/docs/classes/TwoThreeTree.html +27 -10
- package/docs/classes/UndirectedEdge.html +38 -21
- package/docs/classes/UndirectedGraph.html +179 -227
- package/docs/classes/UndirectedVertex.html +32 -15
- package/docs/classes/Vector2D.html +54 -37
- package/docs/enums/CP.html +33 -16
- package/docs/enums/FamilyPosition.html +45 -28
- package/docs/enums/LoopType.html +39 -22
- package/docs/{interfaces/IBinaryTree.html → enums/RBColor.html} +51 -54
- package/docs/enums/TopologicalProperty.html +30 -13
- package/docs/index.html +26 -9
- package/docs/interfaces/IAVLTree.html +1378 -0
- package/docs/interfaces/IAVLTreeNode.html +405 -0
- package/docs/interfaces/IAbstractBinaryTree.html +1124 -0
- package/docs/interfaces/{IBinaryTreeNode.html → IAbstractBinaryTreeNode.html} +76 -75
- package/docs/interfaces/{IGraph.html → IAbstractGraph.html} +67 -94
- package/docs/interfaces/IBST.html +1271 -0
- package/docs/interfaces/IBSTNode.html +408 -0
- package/docs/interfaces/IDirectedGraph.html +344 -20
- package/docs/interfaces/IUNDirectedGraph.html +337 -12
- package/docs/modules.html +52 -18
- package/docs/types/{AVLTreeDeleted.html → AVLTreeOptions.html} +32 -24
- package/docs/types/{ResultsByProperty.html → AbstractBinaryTreeNodeProperties.html} +32 -15
- package/docs/types/AbstractBinaryTreeNodeProperty.html +152 -0
- package/docs/types/AbstractBinaryTreeOptions.html +156 -0
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +152 -0
- package/docs/types/BSTComparator.html +27 -10
- package/docs/types/{TreeMultiSetDeletedResult.html → BSTOptions.html} +32 -22
- package/docs/types/BinaryTreeDeletedResult.html +159 -0
- package/docs/types/BinaryTreeNodeId.html +27 -10
- package/docs/types/BinaryTreeNodePropertyName.html +27 -10
- package/docs/types/{BinaryTreeDeleted.html → BinaryTreeOptions.html} +32 -24
- package/docs/types/DFSOrderPattern.html +27 -10
- package/docs/types/DijkstraResult.html +27 -10
- package/docs/types/Direction.html +27 -10
- package/docs/types/EdgeId.html +27 -10
- package/docs/types/HeapOptions.html +27 -10
- package/docs/types/IdObject.html +157 -0
- package/docs/types/{ResultByProperty.html → KeyValObject.html} +36 -19
- package/docs/types/NavigatorParams.html +27 -10
- package/docs/types/NodeOrPropertyName.html +27 -10
- package/docs/types/PriorityQueueComparator.html +27 -10
- package/docs/types/PriorityQueueDFSOrderPattern.html +27 -10
- package/docs/types/PriorityQueueOptions.html +27 -10
- package/docs/types/{BSTDeletedResult.html → RBTreeOptions.html} +32 -24
- package/docs/types/RecursiveAVLTreeNode.html +27 -10
- package/docs/types/RecursiveBSTNode.html +27 -10
- package/docs/types/RecursiveBinaryTreeNode.html +27 -10
- package/docs/types/RecursiveTreeMultiSetNode.html +152 -0
- package/docs/types/SegmentTreeNodeVal.html +27 -10
- package/docs/types/TopologicalStatus.html +27 -10
- package/docs/types/TreeMultiSetOptions.html +152 -0
- package/docs/types/Turning.html +27 -10
- package/docs/types/VertexId.html +27 -10
- package/package.json +2 -2
|
@@ -0,0 +1,1480 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
9
|
+
var __values = (this && this.__values) || function(o) {
|
|
10
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
11
|
+
if (m) return m.call(o);
|
|
12
|
+
if (o && typeof o.length === "number") return {
|
|
13
|
+
next: function () {
|
|
14
|
+
if (o && i >= o.length) o = void 0;
|
|
15
|
+
return { value: o && o[i++], done: !o };
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
19
|
+
};
|
|
20
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
21
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
22
|
+
if (!m) return o;
|
|
23
|
+
var i = m.call(o), r, ar = [], e;
|
|
24
|
+
try {
|
|
25
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
26
|
+
}
|
|
27
|
+
catch (error) { e = { error: error }; }
|
|
28
|
+
finally {
|
|
29
|
+
try {
|
|
30
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
31
|
+
}
|
|
32
|
+
finally { if (e) throw e.error; }
|
|
33
|
+
}
|
|
34
|
+
return ar;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.AbstractBinaryTree = exports.AbstractBinaryTreeNode = void 0;
|
|
38
|
+
var utils_1 = require("../../utils");
|
|
39
|
+
var types_1 = require("../types");
|
|
40
|
+
var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
41
|
+
/**
|
|
42
|
+
* The constructor function initializes a BinaryTreeNode object with an id, value, and count.
|
|
43
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
|
|
44
|
+
* for the binary tree node.
|
|
45
|
+
* @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
|
|
46
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
47
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that represents the number of times the
|
|
48
|
+
* value `val` appears in the binary tree node. If the `count` parameter is not provided, it defaults to 1.
|
|
49
|
+
*/
|
|
50
|
+
function AbstractBinaryTreeNode(id, val, count) {
|
|
51
|
+
this._familyPosition = types_1.FamilyPosition.ROOT;
|
|
52
|
+
this._count = 1;
|
|
53
|
+
this._height = 0;
|
|
54
|
+
this._id = id;
|
|
55
|
+
this._val = val;
|
|
56
|
+
this._count = count !== null && count !== void 0 ? count : 1;
|
|
57
|
+
}
|
|
58
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "id", {
|
|
59
|
+
get: function () {
|
|
60
|
+
return this._id;
|
|
61
|
+
},
|
|
62
|
+
set: function (v) {
|
|
63
|
+
this._id = v;
|
|
64
|
+
},
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: true
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "val", {
|
|
69
|
+
get: function () {
|
|
70
|
+
return this._val;
|
|
71
|
+
},
|
|
72
|
+
set: function (value) {
|
|
73
|
+
this._val = value;
|
|
74
|
+
},
|
|
75
|
+
enumerable: false,
|
|
76
|
+
configurable: true
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "left", {
|
|
79
|
+
get: function () {
|
|
80
|
+
return this._left;
|
|
81
|
+
},
|
|
82
|
+
set: function (v) {
|
|
83
|
+
if (v) {
|
|
84
|
+
v.parent = this;
|
|
85
|
+
v.familyPosition = types_1.FamilyPosition.LEFT;
|
|
86
|
+
}
|
|
87
|
+
this._left = v;
|
|
88
|
+
},
|
|
89
|
+
enumerable: false,
|
|
90
|
+
configurable: true
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "right", {
|
|
93
|
+
get: function () {
|
|
94
|
+
return this._right;
|
|
95
|
+
},
|
|
96
|
+
set: function (v) {
|
|
97
|
+
if (v) {
|
|
98
|
+
v.parent = this;
|
|
99
|
+
v.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
100
|
+
}
|
|
101
|
+
this._right = v;
|
|
102
|
+
},
|
|
103
|
+
enumerable: false,
|
|
104
|
+
configurable: true
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "parent", {
|
|
107
|
+
get: function () {
|
|
108
|
+
return this._parent;
|
|
109
|
+
},
|
|
110
|
+
set: function (v) {
|
|
111
|
+
this._parent = v;
|
|
112
|
+
},
|
|
113
|
+
enumerable: false,
|
|
114
|
+
configurable: true
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "familyPosition", {
|
|
117
|
+
get: function () {
|
|
118
|
+
return this._familyPosition;
|
|
119
|
+
},
|
|
120
|
+
set: function (v) {
|
|
121
|
+
this._familyPosition = v;
|
|
122
|
+
},
|
|
123
|
+
enumerable: false,
|
|
124
|
+
configurable: true
|
|
125
|
+
});
|
|
126
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "count", {
|
|
127
|
+
get: function () {
|
|
128
|
+
return this._count;
|
|
129
|
+
},
|
|
130
|
+
set: function (v) {
|
|
131
|
+
this._count = v;
|
|
132
|
+
},
|
|
133
|
+
enumerable: false,
|
|
134
|
+
configurable: true
|
|
135
|
+
});
|
|
136
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "height", {
|
|
137
|
+
get: function () {
|
|
138
|
+
return this._height;
|
|
139
|
+
},
|
|
140
|
+
set: function (v) {
|
|
141
|
+
this._height = v;
|
|
142
|
+
},
|
|
143
|
+
enumerable: false,
|
|
144
|
+
configurable: true
|
|
145
|
+
});
|
|
146
|
+
/**
|
|
147
|
+
* The function swaps the location of two nodes in a binary tree.
|
|
148
|
+
* @param {FAMILY} swapNode - The `swapNode` parameter is of type `FAMILY`, which represents a node in a family tree.
|
|
149
|
+
* @returns the `swapNode` object after swapping its properties with the properties of `this` object.
|
|
150
|
+
*/
|
|
151
|
+
AbstractBinaryTreeNode.prototype.swapLocation = function (swapNode) {
|
|
152
|
+
var val = swapNode.val, count = swapNode.count, height = swapNode.height;
|
|
153
|
+
var tempNode = this.createNode(swapNode.id, val);
|
|
154
|
+
if (tempNode instanceof AbstractBinaryTreeNode) {
|
|
155
|
+
tempNode.val = val;
|
|
156
|
+
tempNode.count = count;
|
|
157
|
+
tempNode.height = height;
|
|
158
|
+
swapNode.id = this.id;
|
|
159
|
+
swapNode.val = this.val;
|
|
160
|
+
swapNode.count = this.count;
|
|
161
|
+
swapNode.height = this.height;
|
|
162
|
+
this.id = tempNode.id;
|
|
163
|
+
this.val = tempNode.val;
|
|
164
|
+
this.count = tempNode.count;
|
|
165
|
+
this.height = tempNode.height;
|
|
166
|
+
}
|
|
167
|
+
return swapNode;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* The `clone` function returns a new instance of the `FAMILY` class with the same `id`, `val`, and `count` properties.
|
|
171
|
+
* @returns The `clone()` method is returning a new instance of the `FAMILY` class with the same `id`, `val`, and
|
|
172
|
+
* `count` values as the current instance.
|
|
173
|
+
*/
|
|
174
|
+
AbstractBinaryTreeNode.prototype.clone = function () {
|
|
175
|
+
return this.createNode(this.id, this.val, this.count);
|
|
176
|
+
};
|
|
177
|
+
return AbstractBinaryTreeNode;
|
|
178
|
+
}());
|
|
179
|
+
exports.AbstractBinaryTreeNode = AbstractBinaryTreeNode;
|
|
180
|
+
var AbstractBinaryTree = /** @class */ (function () {
|
|
181
|
+
/**
|
|
182
|
+
* The protected constructor initializes the options for an abstract binary tree.
|
|
183
|
+
* @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
|
|
184
|
+
* tree.
|
|
185
|
+
*/
|
|
186
|
+
function AbstractBinaryTree(options) {
|
|
187
|
+
this._loopType = types_1.LoopType.ITERATIVE;
|
|
188
|
+
this._visitedId = [];
|
|
189
|
+
this._visitedVal = [];
|
|
190
|
+
this._visitedNode = [];
|
|
191
|
+
this._visitedCount = [];
|
|
192
|
+
this._visitedLeftSum = [];
|
|
193
|
+
this._autoIncrementId = false;
|
|
194
|
+
this._maxId = -1;
|
|
195
|
+
this._isDuplicatedVal = false;
|
|
196
|
+
this._root = null;
|
|
197
|
+
this._size = 0;
|
|
198
|
+
this._count = 0;
|
|
199
|
+
if (options !== undefined) {
|
|
200
|
+
var _a = options.loopType, loopType = _a === void 0 ? types_1.LoopType.ITERATIVE : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.isDuplicatedVal, isDuplicatedVal = _c === void 0 ? false : _c;
|
|
201
|
+
this._isDuplicatedVal = isDuplicatedVal;
|
|
202
|
+
this._autoIncrementId = autoIncrementId;
|
|
203
|
+
this._loopType = loopType;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "loopType", {
|
|
207
|
+
get: function () {
|
|
208
|
+
return this._loopType;
|
|
209
|
+
},
|
|
210
|
+
enumerable: false,
|
|
211
|
+
configurable: true
|
|
212
|
+
});
|
|
213
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedId", {
|
|
214
|
+
get: function () {
|
|
215
|
+
return this._visitedId;
|
|
216
|
+
},
|
|
217
|
+
enumerable: false,
|
|
218
|
+
configurable: true
|
|
219
|
+
});
|
|
220
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedVal", {
|
|
221
|
+
get: function () {
|
|
222
|
+
return this._visitedVal;
|
|
223
|
+
},
|
|
224
|
+
enumerable: false,
|
|
225
|
+
configurable: true
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedNode", {
|
|
228
|
+
get: function () {
|
|
229
|
+
return this._visitedNode;
|
|
230
|
+
},
|
|
231
|
+
enumerable: false,
|
|
232
|
+
configurable: true
|
|
233
|
+
});
|
|
234
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedCount", {
|
|
235
|
+
get: function () {
|
|
236
|
+
return this._visitedCount;
|
|
237
|
+
},
|
|
238
|
+
enumerable: false,
|
|
239
|
+
configurable: true
|
|
240
|
+
});
|
|
241
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "visitedLeftSum", {
|
|
242
|
+
get: function () {
|
|
243
|
+
return this._visitedLeftSum;
|
|
244
|
+
},
|
|
245
|
+
enumerable: false,
|
|
246
|
+
configurable: true
|
|
247
|
+
});
|
|
248
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "autoIncrementId", {
|
|
249
|
+
get: function () {
|
|
250
|
+
return this._autoIncrementId;
|
|
251
|
+
},
|
|
252
|
+
enumerable: false,
|
|
253
|
+
configurable: true
|
|
254
|
+
});
|
|
255
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "maxId", {
|
|
256
|
+
get: function () {
|
|
257
|
+
return this._maxId;
|
|
258
|
+
},
|
|
259
|
+
enumerable: false,
|
|
260
|
+
configurable: true
|
|
261
|
+
});
|
|
262
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "isDuplicatedVal", {
|
|
263
|
+
get: function () {
|
|
264
|
+
return this._isDuplicatedVal;
|
|
265
|
+
},
|
|
266
|
+
enumerable: false,
|
|
267
|
+
configurable: true
|
|
268
|
+
});
|
|
269
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "root", {
|
|
270
|
+
get: function () {
|
|
271
|
+
return this._root;
|
|
272
|
+
},
|
|
273
|
+
enumerable: false,
|
|
274
|
+
configurable: true
|
|
275
|
+
});
|
|
276
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "size", {
|
|
277
|
+
get: function () {
|
|
278
|
+
return this._size;
|
|
279
|
+
},
|
|
280
|
+
enumerable: false,
|
|
281
|
+
configurable: true
|
|
282
|
+
});
|
|
283
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "count", {
|
|
284
|
+
get: function () {
|
|
285
|
+
return this._count;
|
|
286
|
+
},
|
|
287
|
+
enumerable: false,
|
|
288
|
+
configurable: true
|
|
289
|
+
});
|
|
290
|
+
/**
|
|
291
|
+
* The clear function resets the state of an object by setting its properties to their initial values.
|
|
292
|
+
*/
|
|
293
|
+
AbstractBinaryTree.prototype.clear = function () {
|
|
294
|
+
this._setRoot(null);
|
|
295
|
+
this._setSize(0);
|
|
296
|
+
this._setCount(0);
|
|
297
|
+
this._setMaxId(-1);
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
301
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
302
|
+
*/
|
|
303
|
+
AbstractBinaryTree.prototype.isEmpty = function () {
|
|
304
|
+
return this.size === 0;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* The `add` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
|
|
308
|
+
* already exists.
|
|
309
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
|
|
310
|
+
* identify each node in the binary tree.
|
|
311
|
+
* @param {N} val - The value to be inserted into the binary tree.
|
|
312
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
313
|
+
* value should be inserted into the binary tree. If not provided, it defaults to 1.
|
|
314
|
+
* @returns The function `add` returns a `N` object if a new node is inserted, or `null` if no new node
|
|
315
|
+
* is inserted, or `undefined` if the insertion fails.
|
|
316
|
+
*/
|
|
317
|
+
AbstractBinaryTree.prototype.add = function (id, val, count) {
|
|
318
|
+
var _this = this;
|
|
319
|
+
count = count !== null && count !== void 0 ? count : 1;
|
|
320
|
+
var _bfs = function (root, newNode) {
|
|
321
|
+
var queue = [root];
|
|
322
|
+
while (queue.length > 0) {
|
|
323
|
+
var cur = queue.shift();
|
|
324
|
+
if (cur) {
|
|
325
|
+
var inserted_1 = _this.addTo(newNode, cur);
|
|
326
|
+
if (inserted_1 !== undefined)
|
|
327
|
+
return inserted_1;
|
|
328
|
+
if (cur.left)
|
|
329
|
+
queue.push(cur.left);
|
|
330
|
+
if (cur.right)
|
|
331
|
+
queue.push(cur.right);
|
|
332
|
+
}
|
|
333
|
+
else
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
return;
|
|
337
|
+
};
|
|
338
|
+
var inserted;
|
|
339
|
+
var needInsert = val !== null ? this.createNode(id, val !== null && val !== void 0 ? val : id, count) : null;
|
|
340
|
+
var existNode = val !== null ? this.get(id, 'id') : null;
|
|
341
|
+
if (this.root) {
|
|
342
|
+
if (existNode) {
|
|
343
|
+
existNode.count += count;
|
|
344
|
+
existNode.val = val !== null && val !== void 0 ? val : id;
|
|
345
|
+
if (needInsert !== null) {
|
|
346
|
+
this._setCount(this.count + count);
|
|
347
|
+
inserted = existNode;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
inserted = _bfs(this.root, needInsert);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
this._setRoot(val !== null ? this.createNode(id, val !== null && val !== void 0 ? val : id, count) : null);
|
|
356
|
+
if (needInsert !== null) {
|
|
357
|
+
this._setSize(1);
|
|
358
|
+
this._setCount(count);
|
|
359
|
+
}
|
|
360
|
+
inserted = this.root;
|
|
361
|
+
}
|
|
362
|
+
return inserted;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* The function adds a new node to a binary tree as the left or right child of a given parent node.
|
|
366
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
367
|
+
* either a node object (`N`) or `null`.
|
|
368
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
369
|
+
* child.
|
|
370
|
+
* @returns either the left or right child node that was added to the parent node. It can also return `null` or
|
|
371
|
+
* `undefined` in certain cases.
|
|
372
|
+
*/
|
|
373
|
+
AbstractBinaryTree.prototype.addTo = function (newNode, parent) {
|
|
374
|
+
var _a, _b;
|
|
375
|
+
if (parent) {
|
|
376
|
+
if (parent.left === undefined) {
|
|
377
|
+
if (newNode) {
|
|
378
|
+
newNode.parent = parent;
|
|
379
|
+
newNode.familyPosition = types_1.FamilyPosition.LEFT;
|
|
380
|
+
}
|
|
381
|
+
parent.left = newNode;
|
|
382
|
+
if (newNode !== null) {
|
|
383
|
+
this._setSize(this.size + 1);
|
|
384
|
+
this._setCount((_a = this.count + newNode.count) !== null && _a !== void 0 ? _a : 0);
|
|
385
|
+
}
|
|
386
|
+
return parent.left;
|
|
387
|
+
}
|
|
388
|
+
else if (parent.right === undefined) {
|
|
389
|
+
if (newNode) {
|
|
390
|
+
newNode.parent = parent;
|
|
391
|
+
newNode.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
392
|
+
}
|
|
393
|
+
parent.right = newNode;
|
|
394
|
+
if (newNode !== null) {
|
|
395
|
+
this._setSize(this.size + 1);
|
|
396
|
+
this._setCount((_b = this.count + newNode.count) !== null && _b !== void 0 ? _b : 0);
|
|
397
|
+
}
|
|
398
|
+
return parent.right;
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
|
|
410
|
+
* null/undefined values.
|
|
411
|
+
* @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
|
|
412
|
+
* array of `N` objects.
|
|
413
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
414
|
+
*/
|
|
415
|
+
AbstractBinaryTree.prototype.addMany = function (data) {
|
|
416
|
+
var e_1, _a, e_2, _b;
|
|
417
|
+
var _c;
|
|
418
|
+
var inserted = [];
|
|
419
|
+
var map = new Map();
|
|
420
|
+
if (!this._isDuplicatedVal) {
|
|
421
|
+
try {
|
|
422
|
+
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
423
|
+
var i = data_1_1.value;
|
|
424
|
+
map.set(i, ((_c = map.get(i)) !== null && _c !== void 0 ? _c : 0) + 1);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
428
|
+
finally {
|
|
429
|
+
try {
|
|
430
|
+
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
431
|
+
}
|
|
432
|
+
finally { if (e_1) throw e_1.error; }
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
|
|
437
|
+
var item = data_2_1.value;
|
|
438
|
+
// TODO will this cause an issue?
|
|
439
|
+
var count = this._isDuplicatedVal ? 1 : map.get(item);
|
|
440
|
+
if (item instanceof AbstractBinaryTreeNode) {
|
|
441
|
+
inserted.push(this.add(item.id, item.val, item.count));
|
|
442
|
+
}
|
|
443
|
+
else if (typeof item === 'number') {
|
|
444
|
+
if (!this._autoIncrementId) {
|
|
445
|
+
if (!this._isDuplicatedVal) {
|
|
446
|
+
if (map.get(item) !== undefined) {
|
|
447
|
+
inserted.push(this.add(item, item, count));
|
|
448
|
+
map.delete(item);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
inserted.push(this.add(item, item, 1));
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
else if (item instanceof Object) {
|
|
457
|
+
if (!this._isDuplicatedVal) {
|
|
458
|
+
if (map.has(item)) {
|
|
459
|
+
var newId = void 0;
|
|
460
|
+
if (!this._autoIncrementId) {
|
|
461
|
+
if (Object.keys(item).includes('id')) {
|
|
462
|
+
newId = item.id;
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
console.warn('Object value must has an id property when the autoIncrementId is false');
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
newId = this.maxId + 1;
|
|
471
|
+
this._setMaxId(newId);
|
|
472
|
+
}
|
|
473
|
+
inserted.push(this.add(newId, item, count));
|
|
474
|
+
map.delete(item);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
inserted.push(this.add(++this._maxId, item, 1));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
else if (item === null) {
|
|
482
|
+
inserted.push(this.add(Number.MAX_SAFE_INTEGER, item, 0));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
487
|
+
finally {
|
|
488
|
+
try {
|
|
489
|
+
if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
|
|
490
|
+
}
|
|
491
|
+
finally { if (e_2) throw e_2.error; }
|
|
492
|
+
}
|
|
493
|
+
return inserted;
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
|
|
497
|
+
* was successful.
|
|
498
|
+
* @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
|
|
499
|
+
* array of `N` objects.
|
|
500
|
+
* @returns The method is returning a boolean value.
|
|
501
|
+
*/
|
|
502
|
+
AbstractBinaryTree.prototype.fill = function (data) {
|
|
503
|
+
this.clear();
|
|
504
|
+
return data.length === this.addMany(data).length;
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* The function removes a node from a binary tree and returns information about the deleted node.
|
|
508
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
|
|
509
|
+
* It is of type `BinaryTreeNodeId`.
|
|
510
|
+
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
511
|
+
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
512
|
+
* not be decremented and the overall count of the binary tree will not be updated. If `
|
|
513
|
+
* @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
|
|
514
|
+
* "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
|
|
515
|
+
* "needBalanced" property is always null.
|
|
516
|
+
*/
|
|
517
|
+
AbstractBinaryTree.prototype.remove = function (id, ignoreCount) {
|
|
518
|
+
var nodes = this.getNodes(id, 'id', true);
|
|
519
|
+
var node = nodes[0];
|
|
520
|
+
if (!node)
|
|
521
|
+
node = undefined;
|
|
522
|
+
else if (node.count > 1 && !ignoreCount) {
|
|
523
|
+
node.count--;
|
|
524
|
+
this._setCount(this.count - 1);
|
|
525
|
+
}
|
|
526
|
+
else if (node instanceof AbstractBinaryTreeNode) {
|
|
527
|
+
var _a = __read(this.getSubTreeSizeAndCount(node), 2), subSize = _a[0], subCount = _a[1];
|
|
528
|
+
switch (node.familyPosition) {
|
|
529
|
+
case 0:
|
|
530
|
+
this._setSize(this.size - subSize);
|
|
531
|
+
this._setCount(this.count - subCount);
|
|
532
|
+
node = undefined;
|
|
533
|
+
break;
|
|
534
|
+
case 1:
|
|
535
|
+
if (node.parent) {
|
|
536
|
+
this._setSize(this.size - subSize);
|
|
537
|
+
this._setCount(this.count - subCount);
|
|
538
|
+
node.parent.left = null;
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
case 2:
|
|
542
|
+
if (node.parent) {
|
|
543
|
+
this._setSize(this.size - subSize);
|
|
544
|
+
this._setCount(this.count - subCount);
|
|
545
|
+
node.parent.right = null;
|
|
546
|
+
}
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return [{ deleted: node, needBalanced: null }];
|
|
551
|
+
};
|
|
552
|
+
/**
|
|
553
|
+
* The function calculates the depth of a binary tree node by traversing its parent nodes.
|
|
554
|
+
* @param node - N - This is the node for which we want to calculate the depth. It is a generic type,
|
|
555
|
+
* meaning it can represent any type of data that we want to store in the node.
|
|
556
|
+
* @returns The depth of the given binary tree node.
|
|
557
|
+
*/
|
|
558
|
+
AbstractBinaryTree.prototype.getDepth = function (node) {
|
|
559
|
+
var depth = 0;
|
|
560
|
+
while (node.parent) {
|
|
561
|
+
depth++;
|
|
562
|
+
node = node.parent;
|
|
563
|
+
}
|
|
564
|
+
return depth;
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
|
|
568
|
+
* approach.
|
|
569
|
+
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
|
|
570
|
+
* `N | null`. It represents the starting node from which to calculate the height of the binary tree.
|
|
571
|
+
* If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
|
|
572
|
+
* @returns the height of the binary tree.
|
|
573
|
+
*/
|
|
574
|
+
AbstractBinaryTree.prototype.getHeight = function (beginRoot) {
|
|
575
|
+
var _a, _b, _c;
|
|
576
|
+
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
577
|
+
if (!beginRoot)
|
|
578
|
+
return -1;
|
|
579
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
580
|
+
var _getMaxHeight_1 = function (cur) {
|
|
581
|
+
if (!cur)
|
|
582
|
+
return -1;
|
|
583
|
+
var leftHeight = _getMaxHeight_1(cur.left);
|
|
584
|
+
var rightHeight = _getMaxHeight_1(cur.right);
|
|
585
|
+
return Math.max(leftHeight, rightHeight) + 1;
|
|
586
|
+
};
|
|
587
|
+
return _getMaxHeight_1(beginRoot);
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
var stack = [];
|
|
591
|
+
var node = beginRoot, last = null;
|
|
592
|
+
var depths = new Map();
|
|
593
|
+
while (stack.length > 0 || node) {
|
|
594
|
+
if (node) {
|
|
595
|
+
stack.push(node);
|
|
596
|
+
node = node.left;
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
node = stack[stack.length - 1];
|
|
600
|
+
if (!node.right || last === node.right) {
|
|
601
|
+
node = stack.pop();
|
|
602
|
+
if (node) {
|
|
603
|
+
var leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
604
|
+
var rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
605
|
+
depths.set(node, 1 + Math.max(leftHeight, rightHeight));
|
|
606
|
+
last = node;
|
|
607
|
+
node = null;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
else
|
|
611
|
+
node = node.right;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
/**
|
|
618
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
619
|
+
* approach.
|
|
620
|
+
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
|
|
621
|
+
* `N | null`. It represents the starting node from which to calculate the minimum height of the binary
|
|
622
|
+
* tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
|
|
623
|
+
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
624
|
+
*/
|
|
625
|
+
AbstractBinaryTree.prototype.getMinHeight = function (beginRoot) {
|
|
626
|
+
var _a, _b, _c;
|
|
627
|
+
beginRoot = beginRoot || this.root;
|
|
628
|
+
if (!beginRoot)
|
|
629
|
+
return -1;
|
|
630
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
631
|
+
var _getMinHeight_1 = function (cur) {
|
|
632
|
+
if (!cur)
|
|
633
|
+
return 0;
|
|
634
|
+
if (!cur.left && !cur.right)
|
|
635
|
+
return 0;
|
|
636
|
+
var leftMinHeight = _getMinHeight_1(cur.left);
|
|
637
|
+
var rightMinHeight = _getMinHeight_1(cur.right);
|
|
638
|
+
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
639
|
+
};
|
|
640
|
+
return _getMinHeight_1(beginRoot);
|
|
641
|
+
}
|
|
642
|
+
else {
|
|
643
|
+
var stack = [];
|
|
644
|
+
var node = beginRoot, last = null;
|
|
645
|
+
var depths = new Map();
|
|
646
|
+
while (stack.length > 0 || node) {
|
|
647
|
+
if (node) {
|
|
648
|
+
stack.push(node);
|
|
649
|
+
node = node.left;
|
|
650
|
+
}
|
|
651
|
+
else {
|
|
652
|
+
node = stack[stack.length - 1];
|
|
653
|
+
if (!node.right || last === node.right) {
|
|
654
|
+
node = stack.pop();
|
|
655
|
+
if (node) {
|
|
656
|
+
var leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
657
|
+
var rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
658
|
+
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
659
|
+
last = node;
|
|
660
|
+
node = null;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
else
|
|
664
|
+
node = node.right;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
/**
|
|
671
|
+
* The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
|
|
672
|
+
* @param {N | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
|
|
673
|
+
* of type `N | null`, which means it can either be a `BinaryTreeNode` object or `null`.
|
|
674
|
+
* @returns The method is returning a boolean value.
|
|
675
|
+
*/
|
|
676
|
+
AbstractBinaryTree.prototype.isBalanced = function (beginRoot) {
|
|
677
|
+
return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
|
|
678
|
+
};
|
|
679
|
+
/**
|
|
680
|
+
* The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
|
|
681
|
+
* searching recursively or iteratively.
|
|
682
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
683
|
+
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
684
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
685
|
+
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
|
|
686
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
687
|
+
* return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
|
|
688
|
+
* function will stop traversing the tree and return the first matching node. If `
|
|
689
|
+
* @returns The function `getNodes` returns an array of `N | null | undefined` objects.
|
|
690
|
+
*/
|
|
691
|
+
AbstractBinaryTree.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
692
|
+
var _this = this;
|
|
693
|
+
if (!this.root)
|
|
694
|
+
return [];
|
|
695
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
696
|
+
var result = [];
|
|
697
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
698
|
+
var _traverse_1 = function (cur) {
|
|
699
|
+
if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
700
|
+
return;
|
|
701
|
+
if (!cur.left && !cur.right)
|
|
702
|
+
return;
|
|
703
|
+
cur.left && _traverse_1(cur.left);
|
|
704
|
+
cur.right && _traverse_1(cur.right);
|
|
705
|
+
};
|
|
706
|
+
_traverse_1(this.root);
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
var queue = [this.root];
|
|
710
|
+
while (queue.length > 0) {
|
|
711
|
+
var cur = queue.shift();
|
|
712
|
+
if (cur) {
|
|
713
|
+
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
714
|
+
return result;
|
|
715
|
+
cur.left && queue.push(cur.left);
|
|
716
|
+
cur.right && queue.push(cur.right);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
return result;
|
|
721
|
+
};
|
|
722
|
+
/**
|
|
723
|
+
* The function checks if a binary tree node has a specific property or if any node in the tree has a specific
|
|
724
|
+
* property.
|
|
725
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
726
|
+
* generic type `N`. It represents the property of a binary tree node that you want to check.
|
|
727
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
728
|
+
* specifies the name of the property to check for in the nodes.
|
|
729
|
+
* @returns a boolean value.
|
|
730
|
+
*/
|
|
731
|
+
AbstractBinaryTree.prototype.has = function (nodeProperty, propertyName) {
|
|
732
|
+
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
733
|
+
};
|
|
734
|
+
/**
|
|
735
|
+
* The function returns the first binary tree node that matches the given property name and value, or null if no match
|
|
736
|
+
* is found.
|
|
737
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
738
|
+
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
739
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
740
|
+
* specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
|
|
741
|
+
* @returns a BinaryTreeNode object or null.
|
|
742
|
+
*/
|
|
743
|
+
AbstractBinaryTree.prototype.get = function (nodeProperty, propertyName) {
|
|
744
|
+
var _a;
|
|
745
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
746
|
+
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
747
|
+
};
|
|
748
|
+
/**
|
|
749
|
+
* The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
|
|
750
|
+
* root of a binary tree.
|
|
751
|
+
* @param node - The `node` parameter is a BinaryTreeNode object.
|
|
752
|
+
* @returns The function `getPathToRoot` returns an array of `N` objects, representing the path from
|
|
753
|
+
* the given `node` to the root of the binary tree.
|
|
754
|
+
*/
|
|
755
|
+
AbstractBinaryTree.prototype.getPathToRoot = function (node) {
|
|
756
|
+
var result = [];
|
|
757
|
+
while (node.parent) {
|
|
758
|
+
result.unshift(node);
|
|
759
|
+
node = node.parent;
|
|
760
|
+
}
|
|
761
|
+
result.unshift(node);
|
|
762
|
+
return result;
|
|
763
|
+
};
|
|
764
|
+
/**
|
|
765
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either recursively or iteratively using tail
|
|
766
|
+
* recursion optimization.
|
|
767
|
+
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N
|
|
768
|
+
* | null`. It represents the starting node from which to find the leftmost node in a binary tree. If no node is
|
|
769
|
+
* provided, the function will use the root node of the binary tree.
|
|
770
|
+
* @returns The `getLeftMost` function returns the leftmost node in a binary tree.
|
|
771
|
+
*/
|
|
772
|
+
AbstractBinaryTree.prototype.getLeftMost = function (node) {
|
|
773
|
+
node = node !== null && node !== void 0 ? node : this.root;
|
|
774
|
+
if (!node)
|
|
775
|
+
return node;
|
|
776
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
777
|
+
var _traverse_2 = function (cur) {
|
|
778
|
+
if (!cur.left)
|
|
779
|
+
return cur;
|
|
780
|
+
return _traverse_2(cur.left);
|
|
781
|
+
};
|
|
782
|
+
return _traverse_2(node);
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
// Indirect implementation of iteration using tail recursion optimization
|
|
786
|
+
var _traverse_3 = (0, utils_1.trampoline)(function (cur) {
|
|
787
|
+
if (!cur.left)
|
|
788
|
+
return cur;
|
|
789
|
+
return _traverse_3.cont(cur.left);
|
|
790
|
+
});
|
|
791
|
+
return _traverse_3(node);
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
/**
|
|
795
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
|
|
796
|
+
* tail recursion optimization.
|
|
797
|
+
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N
|
|
798
|
+
* | null`. It represents the starting node from which to find the rightmost node in a binary tree. If no node is
|
|
799
|
+
* provided, the function will use the root node of the binary tree.
|
|
800
|
+
* @returns The `getRightMost` function returns the rightmost node in a binary tree.
|
|
801
|
+
*/
|
|
802
|
+
AbstractBinaryTree.prototype.getRightMost = function (node) {
|
|
803
|
+
node = node !== null && node !== void 0 ? node : this.root;
|
|
804
|
+
if (!node)
|
|
805
|
+
return node;
|
|
806
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
807
|
+
var _traverse_4 = function (cur) {
|
|
808
|
+
if (!cur.right)
|
|
809
|
+
return cur;
|
|
810
|
+
return _traverse_4(cur.right);
|
|
811
|
+
};
|
|
812
|
+
return _traverse_4(node);
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
// Indirect implementation of iteration using tail recursion optimization
|
|
816
|
+
var _traverse_5 = (0, utils_1.trampoline)(function (cur) {
|
|
817
|
+
if (!cur.right)
|
|
818
|
+
return cur;
|
|
819
|
+
return _traverse_5.cont(cur.right);
|
|
820
|
+
});
|
|
821
|
+
return _traverse_5(node);
|
|
822
|
+
}
|
|
823
|
+
};
|
|
824
|
+
/**
|
|
825
|
+
* The function `isBSTByRooted` checks if a binary tree is a binary search tree (BST) by rooted traversal.
|
|
826
|
+
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
827
|
+
* @returns a boolean value.
|
|
828
|
+
*/
|
|
829
|
+
AbstractBinaryTree.prototype.isBSTByRooted = function (node) {
|
|
830
|
+
if (!node)
|
|
831
|
+
return true;
|
|
832
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
833
|
+
var dfs_1 = function (cur, min, max) {
|
|
834
|
+
if (!cur)
|
|
835
|
+
return true;
|
|
836
|
+
if (cur.id <= min || cur.id >= max)
|
|
837
|
+
return false;
|
|
838
|
+
return dfs_1(cur.left, min, cur.id) && dfs_1(cur.right, cur.id, max);
|
|
839
|
+
};
|
|
840
|
+
return dfs_1(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
841
|
+
}
|
|
842
|
+
else {
|
|
843
|
+
var stack = [];
|
|
844
|
+
var prev = Number.MIN_SAFE_INTEGER, curr = node;
|
|
845
|
+
while (curr || stack.length > 0) {
|
|
846
|
+
while (curr) {
|
|
847
|
+
stack.push(curr);
|
|
848
|
+
curr = curr.left;
|
|
849
|
+
}
|
|
850
|
+
curr = stack.pop();
|
|
851
|
+
if (!(curr) || prev >= curr.id)
|
|
852
|
+
return false;
|
|
853
|
+
prev = curr.id;
|
|
854
|
+
curr = curr.right;
|
|
855
|
+
}
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
/**
|
|
860
|
+
* The function checks if a binary tree is a binary search tree.
|
|
861
|
+
* @param {N | null} [node] - The `node` parameter is of type `N` or `null`. It represents the root node of a binary
|
|
862
|
+
* search tree (BST).
|
|
863
|
+
* @returns a boolean value.
|
|
864
|
+
*/
|
|
865
|
+
AbstractBinaryTree.prototype.isBST = function (node) {
|
|
866
|
+
return this.isBSTByRooted(this.root);
|
|
867
|
+
};
|
|
868
|
+
/**
|
|
869
|
+
* The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
|
|
870
|
+
* traversal.
|
|
871
|
+
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
|
|
872
|
+
* tree.
|
|
873
|
+
* @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
|
|
874
|
+
* represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
|
|
875
|
+
*/
|
|
876
|
+
AbstractBinaryTree.prototype.getSubTreeSizeAndCount = function (subTreeRoot) {
|
|
877
|
+
var res = [0, 0];
|
|
878
|
+
if (!subTreeRoot)
|
|
879
|
+
return res;
|
|
880
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
881
|
+
var _traverse_6 = function (cur) {
|
|
882
|
+
res[0]++;
|
|
883
|
+
res[1] += cur.count;
|
|
884
|
+
cur.left && _traverse_6(cur.left);
|
|
885
|
+
cur.right && _traverse_6(cur.right);
|
|
886
|
+
};
|
|
887
|
+
_traverse_6(subTreeRoot);
|
|
888
|
+
return res;
|
|
889
|
+
}
|
|
890
|
+
else {
|
|
891
|
+
var stack = [subTreeRoot];
|
|
892
|
+
while (stack.length > 0) {
|
|
893
|
+
var cur = stack.pop();
|
|
894
|
+
res[0]++;
|
|
895
|
+
res[1] += cur.count;
|
|
896
|
+
cur.right && stack.push(cur.right);
|
|
897
|
+
cur.left && stack.push(cur.left);
|
|
898
|
+
}
|
|
899
|
+
return res;
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
// --- start additional methods ---
|
|
903
|
+
/**
|
|
904
|
+
* The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
|
|
905
|
+
* iteratively.
|
|
906
|
+
* @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
|
|
907
|
+
* sum.
|
|
908
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
909
|
+
* specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
|
|
910
|
+
* provided, it defaults to `'val'`.
|
|
911
|
+
* @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
|
|
912
|
+
*/
|
|
913
|
+
AbstractBinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
|
|
914
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
915
|
+
if (!subTreeRoot)
|
|
916
|
+
return 0;
|
|
917
|
+
var sum = 0;
|
|
918
|
+
var _sumByProperty = function (cur) {
|
|
919
|
+
var needSum;
|
|
920
|
+
switch (propertyName) {
|
|
921
|
+
case 'id':
|
|
922
|
+
needSum = cur.id;
|
|
923
|
+
break;
|
|
924
|
+
case 'count':
|
|
925
|
+
needSum = cur.count;
|
|
926
|
+
break;
|
|
927
|
+
case 'val':
|
|
928
|
+
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
929
|
+
break;
|
|
930
|
+
default:
|
|
931
|
+
needSum = cur.id;
|
|
932
|
+
break;
|
|
933
|
+
}
|
|
934
|
+
return needSum;
|
|
935
|
+
};
|
|
936
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
937
|
+
var _traverse_7 = function (cur) {
|
|
938
|
+
sum += _sumByProperty(cur);
|
|
939
|
+
cur.left && _traverse_7(cur.left);
|
|
940
|
+
cur.right && _traverse_7(cur.right);
|
|
941
|
+
};
|
|
942
|
+
_traverse_7(subTreeRoot);
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
var stack = [subTreeRoot];
|
|
946
|
+
while (stack.length > 0) {
|
|
947
|
+
var cur = stack.pop();
|
|
948
|
+
sum += _sumByProperty(cur);
|
|
949
|
+
cur.right && stack.push(cur.right);
|
|
950
|
+
cur.left && stack.push(cur.left);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
return sum;
|
|
954
|
+
};
|
|
955
|
+
/**
|
|
956
|
+
* The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
|
|
957
|
+
* @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
|
|
958
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
959
|
+
* each node in the subtree should be increased or decreased.
|
|
960
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
961
|
+
* specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
|
|
962
|
+
* @returns a boolean value, which is `true`.
|
|
963
|
+
*/
|
|
964
|
+
AbstractBinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
|
|
965
|
+
var _this = this;
|
|
966
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
967
|
+
if (!subTreeRoot)
|
|
968
|
+
return false;
|
|
969
|
+
var _addByProperty = function (cur) {
|
|
970
|
+
switch (propertyName) {
|
|
971
|
+
case 'id':
|
|
972
|
+
cur.id += delta;
|
|
973
|
+
break;
|
|
974
|
+
case 'count':
|
|
975
|
+
cur.count += delta;
|
|
976
|
+
_this._setCount(_this.count + delta);
|
|
977
|
+
break;
|
|
978
|
+
default:
|
|
979
|
+
cur.id += delta;
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
984
|
+
var _traverse_8 = function (cur) {
|
|
985
|
+
_addByProperty(cur);
|
|
986
|
+
cur.left && _traverse_8(cur.left);
|
|
987
|
+
cur.right && _traverse_8(cur.right);
|
|
988
|
+
};
|
|
989
|
+
_traverse_8(subTreeRoot);
|
|
990
|
+
}
|
|
991
|
+
else {
|
|
992
|
+
var stack = [subTreeRoot];
|
|
993
|
+
while (stack.length > 0) {
|
|
994
|
+
var cur = stack.pop();
|
|
995
|
+
_addByProperty(cur);
|
|
996
|
+
cur.right && stack.push(cur.right);
|
|
997
|
+
cur.left && stack.push(cur.left);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return true;
|
|
1001
|
+
};
|
|
1002
|
+
/**
|
|
1003
|
+
* The BFS function performs a breadth-first search on a binary tree and returns the results based on a specified node
|
|
1004
|
+
* or property name.
|
|
1005
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1006
|
+
* represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
|
|
1007
|
+
* performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
|
|
1008
|
+
* performed starting from the root node
|
|
1009
|
+
* @returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1010
|
+
*/
|
|
1011
|
+
AbstractBinaryTree.prototype.BFS = function (nodeOrPropertyName) {
|
|
1012
|
+
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1013
|
+
this._resetResults();
|
|
1014
|
+
var queue = [this.root];
|
|
1015
|
+
while (queue.length !== 0) {
|
|
1016
|
+
var cur = queue.shift();
|
|
1017
|
+
if (cur) {
|
|
1018
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1019
|
+
if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
|
|
1020
|
+
queue.push(cur.left);
|
|
1021
|
+
if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
|
|
1022
|
+
queue.push(cur.right);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1026
|
+
};
|
|
1027
|
+
/**
|
|
1028
|
+
* The DFS function performs a depth-first search traversal on a binary tree and returns the results based on the
|
|
1029
|
+
* specified pattern and node or property name.
|
|
1030
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order in which the nodes
|
|
1031
|
+
* of a binary tree are traversed during the Depth-First Search (DFS) algorithm. It can take one of three values: 'in',
|
|
1032
|
+
* 'pre', or 'post'.
|
|
1033
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
|
|
1034
|
+
* either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
|
|
1035
|
+
* `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
|
|
1036
|
+
* no value
|
|
1037
|
+
* @returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1038
|
+
*/
|
|
1039
|
+
AbstractBinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
|
|
1040
|
+
var _this = this;
|
|
1041
|
+
pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
|
|
1042
|
+
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1043
|
+
this._resetResults();
|
|
1044
|
+
var _traverse = function (node) {
|
|
1045
|
+
switch (pattern) {
|
|
1046
|
+
case 'in':
|
|
1047
|
+
if (node.left)
|
|
1048
|
+
_traverse(node.left);
|
|
1049
|
+
_this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
1050
|
+
if (node.right)
|
|
1051
|
+
_traverse(node.right);
|
|
1052
|
+
break;
|
|
1053
|
+
case 'pre':
|
|
1054
|
+
_this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
1055
|
+
if (node.left)
|
|
1056
|
+
_traverse(node.left);
|
|
1057
|
+
if (node.right)
|
|
1058
|
+
_traverse(node.right);
|
|
1059
|
+
break;
|
|
1060
|
+
case 'post':
|
|
1061
|
+
if (node.left)
|
|
1062
|
+
_traverse(node.left);
|
|
1063
|
+
if (node.right)
|
|
1064
|
+
_traverse(node.right);
|
|
1065
|
+
_this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
1066
|
+
break;
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
this.root && _traverse(this.root);
|
|
1070
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1071
|
+
};
|
|
1072
|
+
/**
|
|
1073
|
+
* Time complexity is O(n)
|
|
1074
|
+
* Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
|
|
1075
|
+
* @param pattern
|
|
1076
|
+
* @param nodeOrPropertyName
|
|
1077
|
+
* @constructor
|
|
1078
|
+
*/
|
|
1079
|
+
AbstractBinaryTree.prototype.DFSIterative = function (pattern, nodeOrPropertyName) {
|
|
1080
|
+
pattern = pattern || 'in';
|
|
1081
|
+
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1082
|
+
this._resetResults();
|
|
1083
|
+
if (!this.root)
|
|
1084
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1085
|
+
// 0: visit, 1: print
|
|
1086
|
+
var stack = [{ opt: 0, node: this.root }];
|
|
1087
|
+
while (stack.length > 0) {
|
|
1088
|
+
var cur = stack.pop();
|
|
1089
|
+
if (!cur || !cur.node)
|
|
1090
|
+
continue;
|
|
1091
|
+
if (cur.opt === 1) {
|
|
1092
|
+
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
1093
|
+
}
|
|
1094
|
+
else {
|
|
1095
|
+
switch (pattern) {
|
|
1096
|
+
case 'in':
|
|
1097
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
1098
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1099
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
1100
|
+
break;
|
|
1101
|
+
case 'pre':
|
|
1102
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
1103
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
1104
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1105
|
+
break;
|
|
1106
|
+
case 'post':
|
|
1107
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1108
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
1109
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
1110
|
+
break;
|
|
1111
|
+
default:
|
|
1112
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
1113
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1114
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
1115
|
+
break;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1120
|
+
};
|
|
1121
|
+
/**
|
|
1122
|
+
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
|
|
1123
|
+
* in an array, based on a specified property name.
|
|
1124
|
+
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
|
|
1125
|
+
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
1126
|
+
* the tree is used as the starting node.
|
|
1127
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1128
|
+
* can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
|
|
1129
|
+
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1130
|
+
* accumulating results
|
|
1131
|
+
* @returns The function `levelIterative` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1132
|
+
*/
|
|
1133
|
+
AbstractBinaryTree.prototype.levelIterative = function (node, nodeOrPropertyName) {
|
|
1134
|
+
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1135
|
+
node = node || this.root;
|
|
1136
|
+
if (!node)
|
|
1137
|
+
return [];
|
|
1138
|
+
this._resetResults();
|
|
1139
|
+
var queue = [node];
|
|
1140
|
+
while (queue.length > 0) {
|
|
1141
|
+
var cur = queue.shift();
|
|
1142
|
+
if (cur) {
|
|
1143
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1144
|
+
if (cur.left) {
|
|
1145
|
+
queue.push(cur.left);
|
|
1146
|
+
}
|
|
1147
|
+
if (cur.right) {
|
|
1148
|
+
queue.push(cur.right);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1153
|
+
};
|
|
1154
|
+
/**
|
|
1155
|
+
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1156
|
+
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
|
|
1157
|
+
* root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
1158
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1159
|
+
* specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
|
|
1160
|
+
* values:
|
|
1161
|
+
* @returns The function `listLevels` returns a 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
1162
|
+
*/
|
|
1163
|
+
AbstractBinaryTree.prototype.listLevels = function (node, nodeOrPropertyName) {
|
|
1164
|
+
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1165
|
+
node = node || this.root;
|
|
1166
|
+
if (!node)
|
|
1167
|
+
return [];
|
|
1168
|
+
var levelsNodes = [];
|
|
1169
|
+
var collectByProperty = function (node, level) {
|
|
1170
|
+
switch (nodeOrPropertyName) {
|
|
1171
|
+
case 'id':
|
|
1172
|
+
levelsNodes[level].push(node.id);
|
|
1173
|
+
break;
|
|
1174
|
+
case 'val':
|
|
1175
|
+
levelsNodes[level].push(node.val);
|
|
1176
|
+
break;
|
|
1177
|
+
case 'node':
|
|
1178
|
+
levelsNodes[level].push(node);
|
|
1179
|
+
break;
|
|
1180
|
+
case 'count':
|
|
1181
|
+
levelsNodes[level].push(node.count);
|
|
1182
|
+
break;
|
|
1183
|
+
default:
|
|
1184
|
+
levelsNodes[level].push(node.id);
|
|
1185
|
+
break;
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
1189
|
+
var _recursive_1 = function (node, level) {
|
|
1190
|
+
if (!levelsNodes[level])
|
|
1191
|
+
levelsNodes[level] = [];
|
|
1192
|
+
collectByProperty(node, level);
|
|
1193
|
+
if (node.left)
|
|
1194
|
+
_recursive_1(node.left, level + 1);
|
|
1195
|
+
if (node.right)
|
|
1196
|
+
_recursive_1(node.right, level + 1);
|
|
1197
|
+
};
|
|
1198
|
+
_recursive_1(node, 0);
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
var stack = [[node, 0]];
|
|
1202
|
+
while (stack.length > 0) {
|
|
1203
|
+
var head = stack.pop();
|
|
1204
|
+
var _a = __read(head, 2), node_1 = _a[0], level = _a[1];
|
|
1205
|
+
if (!levelsNodes[level])
|
|
1206
|
+
levelsNodes[level] = [];
|
|
1207
|
+
collectByProperty(node_1, level);
|
|
1208
|
+
if (node_1.right)
|
|
1209
|
+
stack.push([node_1.right, level + 1]);
|
|
1210
|
+
if (node_1.left)
|
|
1211
|
+
stack.push([node_1.left, level + 1]);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
return levelsNodes;
|
|
1215
|
+
};
|
|
1216
|
+
/**
|
|
1217
|
+
* The function returns the predecessor of a given node in a binary tree.
|
|
1218
|
+
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
|
|
1219
|
+
* @returns the predecessor of the given node in a binary tree.
|
|
1220
|
+
*/
|
|
1221
|
+
AbstractBinaryTree.prototype.getPredecessor = function (node) {
|
|
1222
|
+
if (node.left) {
|
|
1223
|
+
var predecessor = node.left;
|
|
1224
|
+
while (!(predecessor) || predecessor.right && predecessor.right !== node) {
|
|
1225
|
+
if (predecessor) {
|
|
1226
|
+
predecessor = predecessor.right;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
return predecessor;
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
return node;
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
/**
|
|
1236
|
+
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
|
|
1237
|
+
* traversal algorithm and returns the results based on the specified property name.
|
|
1238
|
+
* The time complexity of Morris traversal is O(n), it's may slower than others
|
|
1239
|
+
* The space complexity Morris traversal is O(1) because no using stack
|
|
1240
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
|
|
1241
|
+
* traversal pattern of the binary tree. It can have one of three values:
|
|
1242
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1243
|
+
* property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
|
|
1244
|
+
* property. If not provided, it defaults to `'id'`.
|
|
1245
|
+
* @returns The function `morris` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1246
|
+
*/
|
|
1247
|
+
AbstractBinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
|
|
1248
|
+
var _this = this;
|
|
1249
|
+
if (this.root === null)
|
|
1250
|
+
return [];
|
|
1251
|
+
pattern = pattern || 'in';
|
|
1252
|
+
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1253
|
+
this._resetResults();
|
|
1254
|
+
var cur = this.root;
|
|
1255
|
+
var _reverseEdge = function (node) {
|
|
1256
|
+
var pre = null;
|
|
1257
|
+
var next = null;
|
|
1258
|
+
while (node) {
|
|
1259
|
+
next = node.right;
|
|
1260
|
+
node.right = pre;
|
|
1261
|
+
pre = node;
|
|
1262
|
+
node = next;
|
|
1263
|
+
}
|
|
1264
|
+
return pre;
|
|
1265
|
+
};
|
|
1266
|
+
var _printEdge = function (node) {
|
|
1267
|
+
var tail = _reverseEdge(node);
|
|
1268
|
+
var cur = tail;
|
|
1269
|
+
while (cur) {
|
|
1270
|
+
_this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1271
|
+
cur = cur.right;
|
|
1272
|
+
}
|
|
1273
|
+
_reverseEdge(tail);
|
|
1274
|
+
};
|
|
1275
|
+
switch (pattern) {
|
|
1276
|
+
case 'in':
|
|
1277
|
+
while (cur) {
|
|
1278
|
+
if (cur.left) {
|
|
1279
|
+
var predecessor = this.getPredecessor(cur);
|
|
1280
|
+
if (!predecessor.right) {
|
|
1281
|
+
predecessor.right = cur;
|
|
1282
|
+
cur = cur.left;
|
|
1283
|
+
continue;
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
predecessor.right = null;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1290
|
+
cur = cur.right;
|
|
1291
|
+
}
|
|
1292
|
+
break;
|
|
1293
|
+
case 'pre':
|
|
1294
|
+
while (cur) {
|
|
1295
|
+
if (cur.left) {
|
|
1296
|
+
var predecessor = this.getPredecessor(cur);
|
|
1297
|
+
if (!predecessor.right) {
|
|
1298
|
+
predecessor.right = cur;
|
|
1299
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1300
|
+
cur = cur.left;
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
else {
|
|
1304
|
+
predecessor.right = null;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
else {
|
|
1308
|
+
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1309
|
+
}
|
|
1310
|
+
cur = cur.right;
|
|
1311
|
+
}
|
|
1312
|
+
break;
|
|
1313
|
+
case 'post':
|
|
1314
|
+
while (cur) {
|
|
1315
|
+
if (cur.left) {
|
|
1316
|
+
var predecessor = this.getPredecessor(cur);
|
|
1317
|
+
if (predecessor.right === null) {
|
|
1318
|
+
predecessor.right = cur;
|
|
1319
|
+
cur = cur.left;
|
|
1320
|
+
continue;
|
|
1321
|
+
}
|
|
1322
|
+
else {
|
|
1323
|
+
predecessor.right = null;
|
|
1324
|
+
_printEdge(cur.left);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
cur = cur.right;
|
|
1328
|
+
}
|
|
1329
|
+
_printEdge(this.root);
|
|
1330
|
+
break;
|
|
1331
|
+
}
|
|
1332
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1333
|
+
};
|
|
1334
|
+
AbstractBinaryTree.prototype._setLoopType = function (value) {
|
|
1335
|
+
this._loopType = value;
|
|
1336
|
+
};
|
|
1337
|
+
AbstractBinaryTree.prototype._setVisitedId = function (value) {
|
|
1338
|
+
this._visitedId = value;
|
|
1339
|
+
};
|
|
1340
|
+
AbstractBinaryTree.prototype._setVisitedVal = function (value) {
|
|
1341
|
+
this._visitedVal = value;
|
|
1342
|
+
};
|
|
1343
|
+
AbstractBinaryTree.prototype._setVisitedNode = function (value) {
|
|
1344
|
+
this._visitedNode = value;
|
|
1345
|
+
};
|
|
1346
|
+
AbstractBinaryTree.prototype.setVisitedCount = function (value) {
|
|
1347
|
+
this._visitedCount = value;
|
|
1348
|
+
};
|
|
1349
|
+
AbstractBinaryTree.prototype._setVisitedLeftSum = function (value) {
|
|
1350
|
+
this._visitedLeftSum = value;
|
|
1351
|
+
};
|
|
1352
|
+
AbstractBinaryTree.prototype._setAutoIncrementId = function (value) {
|
|
1353
|
+
this._autoIncrementId = value;
|
|
1354
|
+
};
|
|
1355
|
+
AbstractBinaryTree.prototype._setMaxId = function (value) {
|
|
1356
|
+
this._maxId = value;
|
|
1357
|
+
};
|
|
1358
|
+
AbstractBinaryTree.prototype._setIsDuplicatedVal = function (value) {
|
|
1359
|
+
this._isDuplicatedVal = value;
|
|
1360
|
+
};
|
|
1361
|
+
AbstractBinaryTree.prototype._setRoot = function (v) {
|
|
1362
|
+
if (v) {
|
|
1363
|
+
v.parent = null;
|
|
1364
|
+
v.familyPosition = types_1.FamilyPosition.ROOT;
|
|
1365
|
+
}
|
|
1366
|
+
this._root = v;
|
|
1367
|
+
};
|
|
1368
|
+
AbstractBinaryTree.prototype._setSize = function (v) {
|
|
1369
|
+
this._size = v;
|
|
1370
|
+
};
|
|
1371
|
+
AbstractBinaryTree.prototype._setCount = function (v) {
|
|
1372
|
+
this._count = v;
|
|
1373
|
+
};
|
|
1374
|
+
/**
|
|
1375
|
+
* The function resets the values of several arrays used for tracking visited nodes and their properties.
|
|
1376
|
+
*/
|
|
1377
|
+
AbstractBinaryTree.prototype._resetResults = function () {
|
|
1378
|
+
this._visitedId = [];
|
|
1379
|
+
this._visitedVal = [];
|
|
1380
|
+
this._visitedNode = [];
|
|
1381
|
+
this._visitedCount = [];
|
|
1382
|
+
this._visitedLeftSum = [];
|
|
1383
|
+
};
|
|
1384
|
+
/**
|
|
1385
|
+
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1386
|
+
* a result array.
|
|
1387
|
+
* @param cur - The current binary tree node that is being checked.
|
|
1388
|
+
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes found during the
|
|
1389
|
+
* traversal.
|
|
1390
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
|
|
1391
|
+
* the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
|
|
1392
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1393
|
+
* specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
|
|
1394
|
+
* be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
|
|
1395
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1396
|
+
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1397
|
+
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1398
|
+
* @returns a boolean value indicating whether or not a node was pushed into the result array.
|
|
1399
|
+
*/
|
|
1400
|
+
AbstractBinaryTree.prototype._pushByPropertyNameStopOrNot = function (cur, result, nodeProperty, propertyName, onlyOne) {
|
|
1401
|
+
switch (propertyName) {
|
|
1402
|
+
case 'id':
|
|
1403
|
+
if (cur.id === nodeProperty) {
|
|
1404
|
+
result.push(cur);
|
|
1405
|
+
return !!onlyOne;
|
|
1406
|
+
}
|
|
1407
|
+
break;
|
|
1408
|
+
case 'count':
|
|
1409
|
+
if (cur.count === nodeProperty) {
|
|
1410
|
+
result.push(cur);
|
|
1411
|
+
return !!onlyOne;
|
|
1412
|
+
}
|
|
1413
|
+
break;
|
|
1414
|
+
case 'val':
|
|
1415
|
+
if (cur.val === nodeProperty) {
|
|
1416
|
+
result.push(cur);
|
|
1417
|
+
return !!onlyOne;
|
|
1418
|
+
}
|
|
1419
|
+
break;
|
|
1420
|
+
default:
|
|
1421
|
+
if (cur.id === nodeProperty) {
|
|
1422
|
+
result.push(cur);
|
|
1423
|
+
return !!onlyOne;
|
|
1424
|
+
}
|
|
1425
|
+
break;
|
|
1426
|
+
}
|
|
1427
|
+
};
|
|
1428
|
+
/**
|
|
1429
|
+
* The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
|
|
1430
|
+
* provided property name or a default property name.
|
|
1431
|
+
* @param node - The `node` parameter is of type `N`, which represents a node in a binary tree.
|
|
1432
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1433
|
+
* can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
|
|
1434
|
+
* the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
|
|
1435
|
+
*/
|
|
1436
|
+
AbstractBinaryTree.prototype._accumulatedByPropertyName = function (node, nodeOrPropertyName) {
|
|
1437
|
+
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1438
|
+
switch (nodeOrPropertyName) {
|
|
1439
|
+
case 'id':
|
|
1440
|
+
this._visitedId.push(node.id);
|
|
1441
|
+
break;
|
|
1442
|
+
case 'val':
|
|
1443
|
+
this._visitedVal.push(node.val);
|
|
1444
|
+
break;
|
|
1445
|
+
case 'node':
|
|
1446
|
+
this._visitedNode.push(node);
|
|
1447
|
+
break;
|
|
1448
|
+
case 'count':
|
|
1449
|
+
this._visitedCount.push(node.count);
|
|
1450
|
+
break;
|
|
1451
|
+
default:
|
|
1452
|
+
this._visitedId.push(node.id);
|
|
1453
|
+
break;
|
|
1454
|
+
}
|
|
1455
|
+
};
|
|
1456
|
+
/**
|
|
1457
|
+
* The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
|
|
1458
|
+
* to 'id'.
|
|
1459
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1460
|
+
* can accept a value of type `NodeOrPropertyName`.
|
|
1461
|
+
* @returns The method returns an object of type `AbstractBinaryTreeNodeProperties<T>`.
|
|
1462
|
+
*/
|
|
1463
|
+
AbstractBinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
|
|
1464
|
+
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1465
|
+
switch (nodeOrPropertyName) {
|
|
1466
|
+
case 'id':
|
|
1467
|
+
return this._visitedId;
|
|
1468
|
+
case 'val':
|
|
1469
|
+
return this._visitedVal;
|
|
1470
|
+
case 'node':
|
|
1471
|
+
return this._visitedNode;
|
|
1472
|
+
case 'count':
|
|
1473
|
+
return this._visitedCount;
|
|
1474
|
+
default:
|
|
1475
|
+
return this._visitedId;
|
|
1476
|
+
}
|
|
1477
|
+
};
|
|
1478
|
+
return AbstractBinaryTree;
|
|
1479
|
+
}());
|
|
1480
|
+
exports.AbstractBinaryTree = AbstractBinaryTree;
|