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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there a way to traverse a BST by levels? 1

Status
Not open for further replies.

Sedai

Programmer
Mar 4, 2001
158
US
is there a way to traverse a BST by levels?
First visit the root, then all nodes at level ONE, then all nodes at level TWO, etc.

thanks! Avendeval


 


void printLevel(int x, node* p)
{
if(!p)return;
if(x)
{
printLevel(x-1,p->Left);
printLevel(x-1,p->Right);
}
else
{
cout<<p->Data;
}
}

for(int i = 0; i<NumberOfLevels;i++)
{
printLevel(i,head);
}

Something like this would do it. A combination of recursion and itteration

this code is not tested

Matt





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top