How would you print out the data in a binary tree, level by
level, starting at the top?

Answer Posted / hardik

To Print data in binary tree..a recursive function should be
used here post for postorder, in for inorder & pre for
rpeorder...


void post(struct node *temp)
{
if(temp->lptr!=NULL)
post(temp->lptr);
if(temp->rptr!=NULL)
post(temp->rptr);
if(temp!=NULL)
printf("%d\t%s\t%d\n",temp->rollno,temp->name,temp->marks);
}

void pre(struct node *temp)
{
if(temp!=NULL)
printf("%d\t%s\t%d\n",temp->rollno,temp->name,temp->marks);
if(temp->lptr!=NULL)
pre(temp->lptr);
if(temp->rptr!=NULL)
pre(temp->rptr);
}

void in(struct node *temp)
{
if(temp->lptr!=NULL)
in(temp->lptr);
if(temp!=NULL)
printf("%d\t%s\t%d\n",temp->rollno,temp->name,temp->marks);
if(temp->rptr!=NULL)
in(temp->rptr);
}

Is This Answer Correct ?    2 Yes 27 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an endless loop?

796


What is switch in c?

637


What is void c?

558


What is getch() function?

639


Tell me what is the purpose of 'register' keyword in c language?

609






Are local variables initialized to zero by default in c?

541


Write a program to print all permutations of a given string.

636


How can you restore a redirected standard stream?

601


What are the different types of constants?

636


What are structure types in C?

655


What is the basic structure of c?

549


Explain what is a 'locale'?

576


Why string is used in c?

572


Can you write a programmer for FACTORIAL using recursion?

604


What is the scope of static variables in c language?

622