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!

binary search tree

Status
Not open for further replies.

akashvij

Technical User
Mar 11, 2002
19
US
hello everybody
I was just lookin at binary search tree some of the member functions like insert,remove, search etc. and i started thinking is there any way i can get nodecount,leafcount and tree height. Plz share some ideas about them.
akash
 
well, there is a standard tree traversal method which uses recursion. A node is made up of data and a left & right pointer to other nodes. The traversal looks something like

void traverse(node* n)
{
if(!n)
return;

traverse(n->left);
traverse(n->right);
}

now you could modify this to return a value. THe other step to count leaves would be

if( !( n->right && n->left))
// it is a leaf


Hope this helps

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top