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
What is integer constants?
How can I find out if there are characters available for reading?
Write a program which returns the first non repetitive character in the string?
Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?
What is variable declaration and definition in c?
List some applications of c programming language?
What is difference between %d and %i in c?
what are non standard function in c
Which one would you prefer - a macro or a function?
Why & is used in c?
What is int main () in c?
Write a C program to count the number of email on text
Write a program to implement queue.
What are the different types of pointers used in c language?
How can I remove the leading spaces from a string?