0

I need to understand the best way to display my linkedBinaryTree. right noe the driver is passing in integers as elements to each node of the tree, For the toString i have tried the following snippet of code but all it returns is for instance is javafoundations.ArrayIterator@ca0b6.

public String toString() { 


  String thing = "BinaryTreeNode: ";
      if (root.getLeft() != null ) {
      thing += root.getLeft().toString()+" ";
      } 
      if (root.getRight() != null) {
      thing += root.getRight().toString();
      }
      thing += "}";
      return thing;

}

Tigh
  • 19
  • 2
  • 7

1 Answers1

0

You just need to override the method

public String toString()

in the node class you are using. Doing that will replace the useless representation to a more sense one.

Of course you need to understand which is your data object inside the tree (I don't know if a node is a value of if it contains a value) so that you'll be able to call the correct toString method.

In any case you don't need to explicitly call it whenever you are using string concatenation operator eg "" + root.getLeft()

Jack
  • 131,802
  • 30
  • 241
  • 343
  • can you word what you said in the parenthesis again . it sounds very obscure – Tigh Apr 18 '12 at 03:57
  • also there is no toString in the node class for this program for me to need to override the method in the first place. – Tigh Apr 18 '12 at 04:02
  • `toString` is a method of `Object` class. In the parenthesis I meant that if your value IS your node (eg the integer is directly into the node class) you should have a `toString` which returns the value. If the value is deep inside the node (because of the structure), you'll have to call `toString` on the inner data value. – Jack Apr 18 '12 at 04:07
  • so are you saying i need to call the element within the node with the toString and not the node itself? im not sure i understand. – Tigh Apr 18 '12 at 04:09