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
HOW TO SOLVE A NUMERICAL OF LRU IN OS ??????
I need a sort of an approximate strcmp routine?
How do I determine whether a character is numeric, alphabetic, and so on?
What does sizeof return c?
What are the differences between new and malloc in C?
How to draw the flowchart for structure programs?
What is the difference between if else and switchstatement
What is meant by gets in c?
Do you know what are bitwise shift operators in c programming?
Can include files be nested?
Why is it important to memset a variable, immediately after allocating memory to it ?
Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..
What is the difference between typedef and #define?
"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above
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.