|
34 | 34 | } |
35 | 35 |
|
36 | 36 | /** |
37 | | - * Inserts a node into the binary tree. The method's complexity is O(log n) in the average case and |
| 37 | + * Inserts a node into the binary tree. The method's |
| 38 | + * complexity is O(log n) in the average case and |
38 | 39 | * O(n) in the worst case. |
39 | 40 | * |
40 | 41 | * @public |
|
48 | 49 | } |
49 | 50 | var insertKey; |
50 | 51 | current = current || this._root; |
51 | | - if (current.value > value) |
| 52 | + if (current.value > value) { |
52 | 53 | insertKey = '_left'; |
53 | | - else |
| 54 | + } else { |
54 | 55 | insertKey = '_right'; |
55 | | - if (!current[insertKey]) |
| 56 | + } |
| 57 | + if (!current[insertKey]) { |
56 | 58 | current[insertKey] = new Node(value, null, null, current); |
57 | | - else |
| 59 | + } else { |
58 | 60 | this.insert(value, current[insertKey]); |
| 61 | + } |
59 | 62 | }; |
60 | 63 |
|
61 | 64 | /** |
62 | | - * Prints the nodes of the tree in order. It starts the tree traversal from a given node. |
| 65 | + * Prints the nodes of the tree in order. |
| 66 | + * It starts the tree traversal from a given node. |
63 | 67 | * |
64 | 68 | * @private |
65 | 69 | * @param {Node} Node from which to start the traversal |
66 | 70 | * @param {Function} Callback which will be called for each traversed node |
67 | 71 | */ |
68 | 72 | BinaryTree.prototype._inorder = function (current, callback) { |
69 | | - if (!current) |
| 73 | + if (!current) { |
70 | 74 | return; |
| 75 | + } |
71 | 76 | this._inorder(current._left, callback); |
72 | | - if (typeof callback === 'function') |
| 77 | + if (typeof callback === 'function') { |
73 | 78 | callback(current); |
| 79 | + } |
74 | 80 | this._inorder(current._right, callback); |
75 | 81 | }; |
76 | 82 |
|
|
92 | 98 | * @param {Function} Callback which will be called for each traversed node |
93 | 99 | */ |
94 | 100 | BinaryTree.prototype._postorder = function (current, callback) { |
95 | | - if (!current) |
| 101 | + if (!current) { |
96 | 102 | return; |
97 | | - if (typeof callback === 'function') |
| 103 | + } |
| 104 | + if (typeof callback === 'function') { |
98 | 105 | callback(current); |
| 106 | + } |
99 | 107 | this._postorder(current._left, callback); |
100 | 108 | this._postorder(current._right, callback); |
101 | 109 | }; |
|
118 | 126 | * @param {Function} Callback which will be called for each traversed node |
119 | 127 | */ |
120 | 128 | BinaryTree.prototype._preorder = function (current, callback) { |
121 | | - if (!current) |
| 129 | + if (!current) { |
122 | 130 | return; |
123 | | - if (typeof callback === 'function') |
| 131 | + } |
| 132 | + if (typeof callback === 'function') { |
124 | 133 | callback(current); |
| 134 | + } |
125 | 135 | this._preorder(current._left, callback); |
126 | 136 | this._preorder(current._right, callback); |
127 | 137 | }; |
|
147 | 157 | }; |
148 | 158 |
|
149 | 159 | /** |
150 | | - * Finds a node by it's value in given sub-tree. Average runtime complexity: O(log n). |
| 160 | + * Finds a node by it's value in |
| 161 | + * given sub-tree. Average runtime complexity: O(log n). |
151 | 162 | * |
152 | 163 | * @private |
153 | 164 | * @param {Number|String} Value of the node which should be found |
154 | 165 | * @param {Node} Current node to be checked |
155 | 166 | */ |
156 | 167 | BinaryTree.prototype._find = function (value, current) { |
157 | | - if (!current) |
| 168 | + if (!current) { |
158 | 169 | return null; |
| 170 | + } |
159 | 171 |
|
160 | | - if (current.value === value) |
| 172 | + if (current.value === value) { |
161 | 173 | return current; |
| 174 | + } |
162 | 175 |
|
163 | | - if (current.value > value) |
| 176 | + if (current.value > value) { |
164 | 177 | return this._find(value, current._left); |
| 178 | + } |
165 | 179 |
|
166 | | - if (current.value < value) |
| 180 | + if (current.value < value) { |
167 | 181 | return this._find(value, current._right); |
168 | | - |
| 182 | + } |
169 | 183 | }; |
170 | 184 |
|
171 | 185 | /** |
|
182 | 196 | this._root._parent = null; |
183 | 197 | } else { |
184 | 198 |
|
185 | | - if (parent._left === oldChild) |
| 199 | + if (parent._left === oldChild) { |
186 | 200 | parent._left = newChild; |
187 | | - else |
| 201 | + } else { |
188 | 202 | parent._right = newChild; |
| 203 | + } |
189 | 204 |
|
190 | 205 | if (newChild) { |
191 | 206 | newChild._parent = parent; |
|
198 | 213 | * |
199 | 214 | * @public |
200 | 215 | * @param {Node} Node to be removed |
201 | | - * @returns {boolean} True/false depending on whether the given node is removed |
| 216 | + * @returns {boolean} True/false depending |
| 217 | + * on whether the given node is removed |
202 | 218 | */ |
203 | 219 | BinaryTree.prototype.remove = function (node) { |
204 | | - if (!node) |
| 220 | + if (!node) { |
205 | 221 | return false; |
| 222 | + } |
206 | 223 |
|
207 | 224 | if (node._left && node._right) { |
208 | 225 | var min = this._findMin(node._right), |
|
212 | 229 | min.value = temp; |
213 | 230 | return this.remove(min); |
214 | 231 | } else { |
215 | | - if (node._left) |
| 232 | + if (node._left) { |
216 | 233 | this._replaceChild(node._parent, node, node._left); |
217 | | - else if (node._right) |
| 234 | + } else if (node._right) { |
218 | 235 | this._replaceChild(node._parent, node, node._right); |
219 | | - else |
| 236 | + } else { |
220 | 237 | this._replaceChild(node._parent, node, null); |
| 238 | + } |
221 | 239 | return true; |
222 | 240 | } |
223 | 241 | }; |
|
232 | 250 | */ |
233 | 251 | BinaryTree.prototype._findMin = function (node, current) { |
234 | 252 | current = current || { value: Infinity }; |
235 | | - if (!node) |
| 253 | + if (!node) { |
236 | 254 | return current; |
237 | | - if (current.value > node.value) |
| 255 | + } |
| 256 | + if (current.value > node.value) { |
238 | 257 | current = node; |
| 258 | + } |
239 | 259 | return this._findMin(node._left, current); |
240 | 260 | }; |
241 | 261 |
|
|
249 | 269 | */ |
250 | 270 | BinaryTree.prototype._findMax = function (node, current) { |
251 | 271 | current = current || { value: -Infinity }; |
252 | | - if (!node) |
| 272 | + if (!node) { |
253 | 273 | return current; |
254 | | - if (current.value < node.value) |
| 274 | + } |
| 275 | + if (current.value < node.value) { |
255 | 276 | current = node; |
| 277 | + } |
256 | 278 | return this._findMax(node._right, current); |
257 | 279 | }; |
258 | 280 |
|
|
331 | 353 | if (!node) { |
332 | 354 | return 0; |
333 | 355 | } |
334 | | - return 1 + Math.max(this._getHeight(node._left), this._getHeight(node._right)); |
| 356 | + return 1 + Math.max(this._getHeight(node._left), |
| 357 | + this._getHeight(node._right)); |
335 | 358 | }; |
336 | 359 |
|
337 | 360 | /** |
|
340 | 363 | * @public |
341 | 364 | * @returns {Node} The lowest common ancestor of the two nodes or null |
342 | 365 | */ |
343 | | - BinaryTree.prototype.lowestCommonAncestor = function (firstNode, secondNode, current) { |
| 366 | + BinaryTree.prototype.lowestCommonAncestor = |
| 367 | + function (firstNode, secondNode) { |
344 | 368 | return this._lowestCommonAncestor(firstNode, secondNode, this._root); |
345 | 369 | }; |
346 | 370 |
|
347 | | - BinaryTree.prototype._lowestCommonAncestor = function (firstNode, secondNode, current) { |
| 371 | + BinaryTree.prototype._lowestCommonAncestor = |
| 372 | + function (firstNode, secondNode, current) { |
348 | 373 | var firstNodeInLeft = this._existsInSubtree(firstNode, current._left), |
349 | 374 | secondNodeInLeft = this._existsInSubtree(secondNode, current._left), |
350 | 375 | firstNodeInRight = this._existsInSubtree(firstNode, current._right), |
|
369 | 394 | if (node === root.value) { |
370 | 395 | return true; |
371 | 396 | } |
372 | | - return this._existsInSubtree(node, root._left) || this._existsInSubtree(node, root._right); |
| 397 | + return this._existsInSubtree(node, root._left) || |
| 398 | + this._existsInSubtree(node, root._right); |
373 | 399 | }; |
374 | 400 |
|
375 | 401 | exports.BinaryTree = BinaryTree; |
|
0 commit comments