简体   繁体   中英

Find the nth Node in Binary Search Tree

Using this bootstrap:

template <class Comparable>
const Comparable& AugmentedBinarySearchTree<Comparable>::NthElement(int n)
{
  int *i = 0;
  return NthElement(root, i, n)->element;
}

How would you find the nth node in a BST using the nodesVisited pointer to keep track of the nodes checked?

template <class Comparable>
BinaryNode<Comparable>* AugmentedBinarySearchTree<Comparable>::
NthElement(BinaryNode<Comparable> *t, int *nodesVisited, int n) const
{

}

Each node in the BST has a pointer left and right and a value of template <class Comparable> called element .

It sounds to me (from your comment) like you're looking for Boost's multi-index container ( http://www.boost.org/doc/libs/1_59_0/libs/multi_index/doc/index.html ). You would have vector and map views, and insert with push_back into the vector view while using the map view to search by key. Then you would use the vector view to get the nth-inserted element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM