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
Explain how can I remove the trailing spaces from a string?
What is break statement?
What is a static function in c?
Hai what is the different types of versions and their differences
Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;
Why flag is used in c?
How can I get the current date or time of day in a c program?
Is void a keyword in c?
Differentiate fundamental data types and derived data types in C.
How can a number be converted to a string?
if a is an integer variable, a=5/2; will return a value a) 2.5 b) 3 c) 2 d) 0
What is a keyword?
What is s in c?
what are bit fields? What is the use of bit fields in a structure declaration?
What is a memory leak? How to avoid it?