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

p*=(++q)++*--p when p=q=1 while(q<=6)

0 Answers   KINPOE,


Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?

0 Answers   Celstream,


What does dm mean sexually?

0 Answers  


What is the most efficient way to count the number of bits which are set in a value?

4 Answers  


main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); } what is the output?

10 Answers   Ramco,


What does *p++ do? What does it point to?

0 Answers  


In c language can we compile a program without main() function?

0 Answers  


Blade logic interview question. 1st round is a written tests with 15 multiple questions from c and c++. All are simple basic question. Like int main () { Int i=65; Return printf(“%c”, i); } 2nd and 3rd round is technical interview. The position for which I was interview was core UNIX and c. Yes it is for system programming. The company has product name blade server. For their server they are creating their own command for their purpose. Example cd command. We can implement it in a c program by using the chdir() function. So the question asks related to PID, fork, pipe, shared memory, signal. Write a program in c which will act as cp command.

1 Answers   BladeLogic, Infosys,


Sir,please help me out with the code of this question. Write an interactive C program that will encode or decode multiple lines of text. Store the encoded text within a data file, so that it can be retrieved and decoded at any time. The program should include the following features: (a) Enter text from the keyboard, encode the text and store the encoded text in a data file. (b) Retrieve the encoded text and display it in its encoded form. (c) Retrieve the encoded text, decode it and then display the decoded text. (d) End the computation. Test the program using several lines of text of your choice.

0 Answers  


How can I discover how many arguments a function was actually called with?

0 Answers  


Explain argument and its types.

0 Answers  


Where does the name "C" come from, anyway?

0 Answers   Celstream,


Categories