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

Answers were Sorted based on User's Feedback



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

Answer / ds

Use a queue to achieve this.
1. push root to queue
2. if root!=NULL, pop root and print data.
3. visit left child and right child of root and push them to
queue
4. pop leftchild from queue , print data, push left and
right child.
5. pop rightchild from queue, print data, push left and
right child.
6. carry on till queue is empty.

Is This Answer Correct ?    34 Yes 7 No

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

Answer / janraj cj

Use Breadth First search algorithm. This is using queue
as the data structure .

Is This Answer Correct ?    13 Yes 5 No

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

Answer / sucharit

This is the C# version

private void PrintLevelOrder(BinaryTreeNode node)
{
// Do a level Order Traversal

Queue<BinaryTreeNode> queue = new
Queue<BinaryTreeNode>();

queue.Enqueue(node);

while (queue.Count != 0)
{

Console.WriteLine((node = queue.Dequeue
() as BinaryTreeNode).IntValue.ToString());

if (node.Left !=null)
queue.Enqueue(node.Left as
BinaryTreeNode);
if (node.Right!=null)
queue.Enqueue(node.Right as
BinaryTreeNode);


}


}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / vadim

print tree by levels not recursive in C language


typedef struct treeNode{
int data;
struct treeNode* left;
struct treeNode* right;
} TreeNode;


typedef struct tree{
TreeNode* root;
} Tree;


typedef struct listNode{
TreeNode* dataPtr;
struct listNode* next;
struct listNode* prev;
} ListNode;

typedef struct list
{
ListNode* head;
ListNode* tail;
} List;



//main function : you still will need to write all the mini
functions that i have used here ...


void printByLevels(Tree tr)
{
TreeNode *curr;
List *lst;

lst=(List *)malloc(sizeof(List));
makeEmptyList(lst);
insertDataToStartDList(lst,tr.root);


while(isEmptyList(lst)!=TRUE)
{
curr=lst->tail->dataPtr;

if (curr->left!=NULL)
insertDataToStartDList(lst,curr->left);

if(curr->right!=NULL)
insertDataToStartDList(lst,curr->right);

printf("%d ",curr->data);
RemoveLastNodeInList(lst);

}//while

}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / sridhar

By using inorder,preorder,postorder.the data may print as
LDR,DLR,LRD.this only the chance to print data in a bionary
tree.

Is This Answer Correct ?    4 Yes 14 No

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

Answer / 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

More C Interview Questions

what will be the output of this program main() { int i=1; while (i<=10); { i++; } }

11 Answers  


What are the primitive data types in c?

0 Answers  


The difference between printf and fprintf is ?

0 Answers   Baan Infotech,


what is the difference between getch() and getche()?

7 Answers   Infosys,


write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1

0 Answers  






What is c token?

0 Answers  


what is the differance between pass by reference and pass by value.

7 Answers   Infosys,


What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25

0 Answers  


Without Computer networks, Computers will be half the use. Comment.

0 Answers  


Write a C++ program to give the number of days in each month according to what the user entered. example: the user enters June the program must count number of days from January up to June

0 Answers  


what is the difference between structural,object based,object orientd programming languages?

1 Answers   PanTerra,


main(){char *str;scanf("%s",str);printf("%s",str); }The error in the above program is: a) Variable 'str' is not initialised b) Format control for a string is not %s c) Parameter to scanf is passed by value. It should be an address d) none

0 Answers  


Categories