Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFirst and getLast functions #19

Open
jdevelvis opened this issue Jun 16, 2018 · 0 comments
Open

getFirst and getLast functions #19

jdevelvis opened this issue Jun 16, 2018 · 0 comments

Comments

@jdevelvis
Copy link

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! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant