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

It is possible to perform a binary indexOf?? #15

Open
pedritin opened this issue Oct 27, 2015 · 3 comments
Open

It is possible to perform a binary indexOf?? #15

pedritin opened this issue Oct 27, 2015 · 3 comments

Comments

@pedritin
Copy link

I mean, is it possible to perform a binary search and retrieve the index of the item or get the item at the specified index?

@pedritin pedritin changed the title It is possible to performance indexOf?? It is possible to perform a binary indexOf?? Oct 28, 2015
@vadimg
Copy link
Owner

vadimg commented May 11, 2016

Yes, this is possible to do for a binary tree. Unfortunately, I don't have the time to implement every method someone requests, so if you want it, you'll have to add the method yourself and submit a pull request.

The indexOf (0-based index) of a node is the size of the node's left subtree + (the parent's indexOf + 1, if the node is its parent's right child)

@vadimg
Copy link
Owner

vadimg commented May 11, 2016

To get an item at a specific index, you could do

function getByIndex(i) {
  var item;
  var j = 0;
  tree.each(function(data) {
    if (j === i) {
      item = data;
      return false;
    }
    j++;
  });
  return item;
}

@vadimg
Copy link
Owner

vadimg commented May 11, 2016

However, if you are doing these types of operations often, I would suggest a built-in array ([]) would be much more performant and easier to use.

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

2 participants