Friday, July 31, 2009

Wht is the programming in c language to list all the nodes of binary tree?

You would have to keep searching from the root node...every time you hit the bottom of a tree (or a situation where you've checked both sides of the tree)....start moving back up and check the other direction (IE if the bottom of the tree is left...go to the parent node and look right).





//-------------


You should have one variable whose structure looks like this


struct nodecheckedtype


{


int numericvalueofnode;


bool isleftchecked;


bool isrightchecked;


}


nodecheckedtype isnodechecked[100];


//check up to 100 nodes





and then simply loop to match the value/number stored in the node you are in to "numericvalueofnode" to find out which node you are in before you check if you've already covered the paths on the left and right of that node.





This structure is key to stopping you from covering the same path twice...and ends up storing the values of ALL nodes in your binary tree (so you can print them).


//----------





Keep track of all the nodes where you have checked left and/or right...and never go down a tree you have checked both the left and right side of before. Once you are stuck at the top level with both the left and right nodes relative to it checked (IE you have been to the left and right sides of both of those nodes) you are done. :-)


No comments:

Post a Comment