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
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 |
Answer / janraj cj
Use Breadth First search algorithm. This is using queue
as the data structure .
Is This Answer Correct ? | 13 Yes | 5 No |
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 |
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 |
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 |
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 |
N O S I E R + A S T R A L ---------------- 7 2 5 6 1 3
int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above
Why is c platform dependent?
Write a program which returns the first non repetitive character in the string?
how to find your architecture is LittleEndian or BigEndian?
Why is not a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it is been freed?
write a c programming using command line argument,demonstrate set operation(eg;union,intersection,difference) example output is c:>setop 12 34 45 1 union 34 42 66 c:>setop 12 34 1 42 66 c:>setop 12 34 diff 12 56 67 78 setop 12 34
Difference between pass by reference and pass by value?
i want the code for printing the output as follows 4 4 3 3 2 2 1 1 0 1 1 2 2 3 3 4 4
What happens if a header file is included twice?
why wipro wase
15.what is the disadvantage of using macros? 16.what is the self-referential structure? 17.can a union be self-referenced? 18.What is a pointer? 19.What is the Lvalue and Rvalue? 20.what is the difference between these initializations? 21.Char a[]=”string”; 22.Char *p=”literal”; 23.Does *p++ increment p, or what it points to?