2 Replies Latest reply: Sep 20, 2009 2:32 PM by 807580

# need help with In-Order traversal of a binary search tree

Hi guys, I have to write a program where I have a list of numbers inserted into a binary search tree and then I have to print out the in-order traversal of the tree. Here's what I have so far:
``````public String inOrderTraversal(Node object)
{
String list = ", ";

if(object != null)
{
inOrderTraversal(object.left);
poly = poly + object.data;
inOrderTraversal(object.right);
return list;
}
return list;
}``````
What I'm trying to do is everytime I get to the leftmost node I store the number in the string and then go back to the previous node, store that, then check for a right...ect For some reason I can only manage to get one of the number to store and not the rest in the tree. I'm not quite sure where I'm going wrong, if somone can point me in the right direction I would appreciate it. If I had to guess the problem lies with my return statements because I think I have the algorithm right for an in order traversal.
• ###### 1. Re: need help with In-Order traversal of a binary search tree
You never use the String that you create in each traversal method call, and you don't show us what poly is. So it is hard to tell what you want to do. My guess is that you want to append the value returned from the left and right traversal onto the list String.
``````            list = list + inOrderTraversal(object.left);
list = list + object.data;
list = list + inOrderTraversal(object.right);``````
• ###### 2. Re: need help with In-Order traversal of a binary search tree
stevejiluke, thanks a lot and yes the 'poly' variable was suppose to be 'list'. So I'm guessing the problem with my code was that the results weren't getting stored each time I make the recursive call thus thats why you have 'list = list...'