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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

HOW TO SOLVE A NUMERICAL OF LRU IN OS ??????

2259


I need a sort of an approximate strcmp routine?

647


How do I determine whether a character is numeric, alphabetic, and so on?

615


What does sizeof return c?

589


What are the differences between new and malloc in C?

602






How to draw the flowchart for structure programs?

8755


What is the difference between if else and switchstatement

1306


What is meant by gets in c?

597


Do you know what are bitwise shift operators in c programming?

577


Can include files be nested?

619


Why is it important to memset a variable, immediately after allocating memory to it ?

1546


Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..

638


What is the difference between typedef and #define?

531


"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

603


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create.

1468