Tuesday, July 14, 2009

Write a program in 'c' programming language to list the nodes of a binary tree in the following way: List the

Write a program in 'c' programming language to list the nodes of a binary tree in the following way: List the root, then nodes at depth 1, foollowed by nodes at depth 2, and so on.

Write a program in 'c' programming language to list the nodes of a binary tree in the following way: List the
This is an interesting problem, because typical recursive tree traversals visit the left subtree, then the right subtree. Nodes at a particular depth can appear on both sides of the tree. What you can do is modify a normal traversal to keep track of what depth you're at as you're visiting nodes. I'll give you some of the parts of the solution, and leave you to fill in the details.





Suppose we have a tree node defined like this:





struct treeNode {


int val;


struct treeNode *left;


struct treeNode *right;


};





A simple traversal where you identify nodes on your way down each subtree would look like this:





void traverse(struct treeNode *n) {


if (n == NULL) return;


printf("\n%d",n-%26gt;val);


if (n-%26gt;left != NULL) traverse(n-%26gt;left);


if (n-%26gt;right != NULL) traverse(n-%26gt;right);


}





in main() I create a tree:


struct treeNode *root = newTreeNode(0);


populate it, then traverse(root);





(This is where you create you're own newTreeNode( ) function, and a way to populate your tree.)





That's not bad, but we never know what level we're at. Modifying this to maintain and print a level, a traverseLevels function might look like this:





void traverseLevels(struct treeNode *n, int *level) {


if (n == NULL) return;


printf("\n%d: %d",*level,n-%26gt;val);


if (n-%26gt;left != NULL) {


++(*level);


traverseLevels(n-%26gt;left,level);


}


if (n-%26gt;right != NULL) {


++(*level);


traverseLevels(n-%26gt;right,level);


}


--(*level);


}





Now I need to declare:


int level = 0;


before calling the function:


traverseLevels(root,%26amp;level);





Each time we traverse down a branch of the tree, level gets incremented, and level is decremented on each step up from a child node to its parent.





It's getting better, each node and its level are displayed, but it doesn't quite solve the problem you stated, since all nodes at a particular level are not listed together in the output. I.e., we don't get all nodes at level 1, followed by all nodes at level 2, etc. They're listed as encountered in the traversal, which, as mentioned before, visits left subtree then right subtree.





So, we need some way to get these nodes in order. I came up with one way to do it, maybe you can find a better way. I decided I wanted to have traverseLevels create a linked list, sorted in order of increasing level:





struct listNode {


int level;


int val;


struct listNode *next;


};


typedef void (*listInsertFuncPtr_t)(struct listNode*,int,int);


void traverseLevels(struct treeNode*,


struct listNode*,


int *level,


listInsertFuncPtr_t);


void listInsert(struct listNode*, int level, int val);





void traverseLevels(struct treeNode *n,


struct listNode *l,


int *level,


listInsertFuncPtr_t listInsert) {


if (n == NULL) return;


listInsert(l,*level,n-%26gt;val);


if (n-%26gt;left != NULL) {


++(*level);


traverseLevels(n-%26gt;left,l,level,listInser...


}


if (n-%26gt;right != NULL) {


++(*level);


traverseLevels(n-%26gt;right,l,level,listInse...


}


--(*level);


}





And to call it:





struct listNode *head = newListNode(-1, -1);


traverseLevels(root,head,%26amp;level,%26amp;listI...





Passing around the pointer to the list head and list insert function isn't really necessary, but I thought it would be a nice way to do it. You could have traverseLevels know about a global list, or devise some other way for the function to create the list of nodes in the correct order.
Reply:I recommended you that you can get help from


http://expert.asptutorials.info/


and let many programmers bid for your project.


You can hire whoever you like.





Do not pay any money afront however

baby breath

No comments:

Post a Comment