You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the great module! I get that you're not maintaining it anymore, so instead of a Pull request, I'm just going to leave this here for any future coders who happen to wander by.
I needed the ability to get the first & last n elements from the tree, regardless of value, and didn't see a function that could do that. I started using the betweenBounds function, but had to pass {} to that to get the whole tree, then only use 10 (the number I needed) out of thousands of elements. Since my code needs to be as efficient as possible, I built a few functions into this module to simply walk the tree and pull out the first/last n elements and return them in an array.
In case it helps anyone in the future, I added the following functions to my bst.js:
/**
* Returns the leftmost n element(s) in the tree
* @param {Integer} n The number of elements to return from the beginning of the tree
*/
BinarySearchTree.prototype.getFirst = function (n, list) {
var tmp = [];
if (!n) n=1; //Default to 1 element
if (!list) list=[]; //Default list to an empty array so we can concat it
if (this.left) { //Does it have a left leg?
tmp = this.left.getFirst(n,list);
if (tmp.length <= n) { //Make sure we're not already over the n limit
list = tmp;
}
}
if (list.length < n) { //Make sure we're not already over the n limit
list.push(this.data);
}
if (this.right && list.length < n) { //Does it have a right leg, and do we need to walk down it?
tmp = this.right.getFirst(n,list);
if (tmp.length <= n) { //Make sure we're not already over the n limit
list = tmp;
}
}
return list;
}
/**
* Returns the rightmost element(s) in the tree
* @param {Integer} n The number of elements to return from the end of the tree
*/
BinarySearchTree.prototype.getLast = function (n, list) {
var tmp = [];
if (!n) n=1; //Default to 1 element
if (!list) list=[]; //Default list to an empty array so we can concat it
if (this.right) { //Does this have a right leg?
tmp = this.right.getLast(n,list);
if (tmp.length <= n) { //Make sure we're not already over the n limit
list = tmp;
}
}
if (list.length < n) { //Make sure we're not already over the n limit
list.push(this.data);
}
if (this.left && list.length < n) { //Does this have a left leg, and do we need to walk down it?
tmp = this.left.getLast(n,list);
if (tmp.length <= n) { //Make sure we're not already over the n limit
list = tmp;
}
}
return list;
}
Then, in order to use them in the AVLTree, I added 'getFirst', 'getLast' to the array on line 447 (the line toward the end of the file that says "Other functions we want to use on an AVLTree as if it were the internal _AVLTree"
Again, thanks for this great module! 👍
The text was updated successfully, but these errors were encountered:
Thanks for the great module! I get that you're not maintaining it anymore, so instead of a Pull request, I'm just going to leave this here for any future coders who happen to wander by.
I needed the ability to get the first & last n elements from the tree, regardless of value, and didn't see a function that could do that. I started using the betweenBounds function, but had to pass {} to that to get the whole tree, then only use 10 (the number I needed) out of thousands of elements. Since my code needs to be as efficient as possible, I built a few functions into this module to simply walk the tree and pull out the first/last n elements and return them in an array.
In case it helps anyone in the future, I added the following functions to my bst.js:
Then, in order to use them in the AVLTree, I added 'getFirst', 'getLast' to the array on line 447 (the line toward the end of the file that says "Other functions we want to use on an AVLTree as if it were the internal _AVLTree"
Again, thanks for this great module! 👍
The text was updated successfully, but these errors were encountered: