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
Tell us two differences between new () and malloc ()?
Explain how do you search data in a data file using random access method?
Which programming language is best for getting job 2020?
What is pass by value in c?
Why is c used in embedded systems?
What is the scope of global variable in c?
develop algorithms to add polynomials (i) in one variable
how to print the character with maximum occurence and print that number of occurence too in a string given ?
Is c is a procedural language?
Dont ansi function prototypes render lint obsolete?
What is cohesion and coupling in c?
i want to know the procedure of qualcomm for getting a job through offcampus
Why c is called procedure oriented language?
What is the concatenation operator?
When a c file is executed there are many files that are automatically opened what are they files?