Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

implementing FindKth on Binary Trees!

Status
Not open for further replies.

dumbgeek

Programmer
May 22, 2003
8
ZA
Hello again!

I have the recursive version of the findKth operation, i know that it doesn't have to be recursive but i don't know how to implement it iteratively. Please could you help me with this.

This should be the recursive operation..
//internal method to find Kth item in a subtree
// k is the desired rank
// t is the node that roots the tree
template <class Comparable>
BinaryNode<Comparable> * BinarySearchTreeWithRank<Comparable>::
findKth int k, Node * t) const
{
if (t == NULL )
return NULL;

int leftsize = treeSize(t -> left); \\find sizze of left subtree

if ( k <= leftSize )
return findKth( k, t ->left);
else if(k ==leftSize + 1)
return t;
else
return findKth( k - leftSize - 1, t->right );
}

Please can you tell me how the iterative version will be done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top