Sunday, 8 September 2013

printing a tree level by level without using queues

printing a tree level by level without using queues

How to print a tree level by level without using queue? i have a tree in
which each node itself contains its level also.
class emp
{
string name;
emp* boss;
int level;
employee* j;
};
struct employee
{
emp* junior;
employee* next;
};
the output should be each level followed by all nodes at that level
void emp::prt_level(emp* a)
{
employee* b;
b=a->j;
while(b!=NULL)
{
cout<<b->junior->name<<"\t";
b=b->next;
}
}
void emp::prt(emp* a,int i)
{
int k =0;
employee* b;
if (i==0)
{
cout<<endl;
cout<<a->level + 1<<" : ";
prt_level(a);
}
else
prt_level(a);
b = a->j;
while(b!=NULL)
{
if (b->junior->j != NULL)
prt(b->junior,k);
b=b->next;
k++;
}
}
this is what i have tried so far but it is giving problems

No comments:

Post a Comment