|
58 | 58 | * @private |
59 | 59 | * @method |
60 | 60 | * @param {Node} node Node to be splayed. |
| 61 | + * @returns {Node} The same node from the parameter, post splayed. |
61 | 62 | */ |
62 | 63 | exports.SplayTree.prototype._splay = function (node) { |
63 | 64 | while (this._root !== node) { |
|
87 | 88 | * @private |
88 | 89 | * @method |
89 | 90 | * @param {Node} node Node to be zig-zig'd. |
| 91 | + * @returns {Node} The same node from the parameter, post splayed. |
90 | 92 | */ |
91 | 93 | exports.SplayTree.prototype._zigZig = function (node) { |
92 | 94 |
|
|
136 | 138 | * @private |
137 | 139 | * @method |
138 | 140 | * @param {Node} node Node to be zig-zag'd. |
| 141 | + * @returns {Node} The same node from the parameter, post splayed. |
139 | 142 | */ |
140 | 143 | exports.SplayTree.prototype._zigZag = function (node) { |
141 | 144 |
|
|
185 | 188 | * @private |
186 | 189 | * @method |
187 | 190 | * @param {Node} node Node to be zig'd. |
| 191 | + * @returns {Node} The same node from the parameter, post splayed. |
188 | 192 | */ |
189 | 193 | exports.SplayTree.prototype._zig = function (node) { |
190 | 194 |
|
|
334 | 338 | * Average time complexity: O(log N). |
335 | 339 | * |
336 | 340 | * @public |
337 | | - * @param {Number|String} Value of the node which should be found. |
| 341 | + * @param {Number|String} value of the node which should be found. |
338 | 342 | */ |
339 | 343 | exports.SplayTree.prototype.search = function (value) { |
340 | 344 | var node = this._search(value, this._root); |
|
346 | 350 | * Average time complexity: O(log N). |
347 | 351 | * |
348 | 352 | * @public |
349 | | - * @param {Number|String} Value of the node which should be found. |
| 353 | + * @param {Number|String} value of the node which should be found. |
350 | 354 | */ |
351 | 355 | exports.SplayTree.prototype._splaylessSearch = function (value) { |
352 | 356 | return this._search(value, this._root); |
|
357 | 361 | * Average time complexity: O(log N). |
358 | 362 | * |
359 | 363 | * @private |
360 | | - * @param {Number|String} Value of the node which should be found. |
361 | | - * @param {Node} Current node to be checked. |
| 364 | + * @param {Number|String} value of the node which should be found. |
| 365 | + * @param {Node} current node to be checked. |
362 | 366 | */ |
363 | 367 | exports.SplayTree.prototype._search = function (value, current) { |
364 | 368 | if (!current) { |
|
512 | 516 | return this._getHeight(this._root); |
513 | 517 | }; |
514 | 518 |
|
| 519 | + /** |
| 520 | + * Recursive worker function for getHeight() |
| 521 | + * |
| 522 | + * @public |
| 523 | + * @param {Node} node The node of the current recursive frame. |
| 524 | + * @returns {Number} The height of the tree. |
| 525 | + */ |
515 | 526 | exports.SplayTree.prototype._getHeight = function (node) { |
516 | 527 | if (!node) { |
517 | 528 | return 0; |
|
531 | 542 | return this._lowestCommonAncestor(firstNode, secondNode, this._root); |
532 | 543 | }; |
533 | 544 |
|
| 545 | + /** |
| 546 | + * Obtains the lowest common ancestor for the given nodes. |
| 547 | + * |
| 548 | + * @private |
| 549 | + * @param {Node} firstNode First node to be considered when checking |
| 550 | + * for ancestor. |
| 551 | + * @param {Node} secondNode Second node to be considered when checking |
| 552 | + * for ancestor. |
| 553 | + * @param {Node} current Current node. |
| 554 | + * @returns {Node} The lowest common ancestor of the two nodes or null. |
| 555 | + */ |
534 | 556 | exports.SplayTree.prototype._lowestCommonAncestor = |
535 | 557 | function (firstNode, secondNode, current) { |
536 | 558 | var firstNodeInLeft = this._existsInSubtree(firstNode, current._left); |
|
551 | 573 | return null; |
552 | 574 | }; |
553 | 575 |
|
| 576 | + /** |
| 577 | + * Checks if a given node exists in a subtree. |
| 578 | + * |
| 579 | + * @private |
| 580 | + * @param {Node} node Node to check for. |
| 581 | + * @param {Node} root Root node of a given subtree. |
| 582 | + * @returns {Node} The lowest common ancestor of the two nodes or null. |
| 583 | + */ |
554 | 584 | exports.SplayTree.prototype._existsInSubtree = function (node, root) { |
555 | 585 | if (!root) { |
556 | 586 | return false; |
|
0 commit comments