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

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

    807580
      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.