2

I'm having an issue getting my binary tree to print as a String. My code is suppose to take a string of numbers and create a binary tree with it. Its then suppose to return the value from tree in order. I seem to be confusing my implementation but everything that I've looked up to try doesn't seem to work with my code. I'll post my different attempts, hopefully there's just something that I missed.

class Node
{
   Node left, right;
   Double value;

   public Node()
   {
      value = null;
      left = right = null;
   }
   public Node(Double item)
   {
      value = item;
      left = right = null;
   }
   public String display() //This was my first attempt
   {
      /*This was my first attempt but it only gives me an infinite loop
      while(value != null)
      {
         System.out.println(left.toString());
         System.out.println(value.toString());
         System.out.println(right.toString());
      }
      return null;
      */

      /*This was the second attempt, but i can't implement it because it comes as different types
      StringBuffer sb = new StringBuffer();
      for(Double value:this) sb.append(value.toString());

      return sb.toString();
      */

      /* This was attempt 3, but it only prints the first node value
      String result = "";
      if(value != null)
      {
         left.toString();
         result = " " + Double.toString(value);
         right.toString();
      }   
      return result;
      */
      }

}
Node root;
public void insert(Double h)
{
   root = insertNode(root, h);
}
public Node createNodeTree(String expression)
{
   System.out.println("Expression is " + expression);
   for(String i:expression.split("\\s", 0))  
   {
      System.out.println("Current node is " + i);
      insert(Double.parseDouble(i));
   }
   System.out.println("Tree Complete");
   return root;
}
public Node insertNode(Node i, Double j)
{
   System.out.println("Inside insertNode");

   if( i == null )
   {
      i = new Node(j);
      return i;
   }
   if(j < i.value)
      i.left = insertNode(i.left, j);
   else if(j > i.value)
      i.right = insertNode(i.right, j); 

   return i;
}

Assuming that I call an expression with seven numbers like this

createNodeTree("2 3 7 4 8 6 1").display();

I want it to return as

1 2 3 4 6 7 8

Yet so far I've only been able to get it to return the last value the program reads

Please, any help with this would be appreciated!

Edit 1:

So after the suggestion, I created this method

    public String display()
    {
return (left == null && right == null && value != null) ? value.toString() :" " +  left.toString() + " " + value + " " + right.toString();
    }

But this seems to return what I think are reference values instead of the actual nodes left and right values

Please Help!

  • What is the logic behind the input "2 3 7 4 8 6 1" and the corresponding output "1 2 3 4 6 7 8"? – denvercoder9 Dec 08 '17 at 09:58
  • Try something like `left.display() + value + right.display()` in `display()`. Add null checks, obviously. You can find an example in adifferent language [here](https://stackoverflow.com/a/34077553/7653073), but the algorithm is the same. Also look at [these cool answers](https://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram) – Malte Hartwig Dec 08 '17 at 10:00
  • @MalteHartwig So I tried the method you suggested and it finally returns a value but it still seems they are just reference values. Any suggestions to getting it to return the actual values? My best guess is how my node is build maybe? I'm really not sure. – Keith Hinton Dec 08 '17 at 21:41
  • Don't call toString () on left and right, but display(). Otherwise it stops after the first level, so you need to use recursion to go all the way down, similar to your insert method. – Malte Hartwig Dec 09 '17 at 09:54
  • Possible duplicate of [printing binary search tree inorder recursively](https://stackoverflow.com/questions/29735465/printing-binary-search-tree-inorder-recursively). Look at [the second answer](https://stackoverflow.com/a/29735598/7653073) of the question for the implementation. It should perfectly apply to your case. – Malte Hartwig Dec 11 '17 at 12:53

0 Answers0