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

Tell us two differences between new () and malloc ()?

610


Explain how do you search data in a data file using random access method?

691


Which programming language is best for getting job 2020?

605


What is pass by value in c?

593


Why is c used in embedded systems?

606






What is the scope of global variable in c?

551


develop algorithms to add polynomials (i) in one variable

1734


how to print the character with maximum occurence and print that number of occurence too in a string given ?

2029


Is c is a procedural language?

591


Dont ansi function prototypes render lint obsolete?

601


What is cohesion and coupling in c?

587


i want to know the procedure of qualcomm for getting a job through offcampus

1929


Why c is called procedure oriented language?

575


What is the concatenation operator?

606


When a c file is executed there are many files that are automatically opened what are they files?

587