0

I have a BinaryTree class, a TreeNode class and a BinaryTestClass that creates a BinaryTree where values can be inserted as a root into the TreeNode. However when I run the program on the test class it doesn't show everything that should be shown.

BinaryTree Class:

public class BinaryTree <T extends Comparable<T>> implements BTree<T>{


TreeNode<T> root;

public BinaryTree(){
    root = null;
}


public BTree<T> left() {
    // TODO Auto-generated method stub
    return left();
}

public BTree<T> right() {
    // TODO Auto-generated method stub
    return right();
}

public void insert(T value) {
    if (root == null){
        root = new TreeNode(value);
    }else if (value.compareTo(value()) < 0){
        root.left().insert(value);
    } else{
        root.right().insert(value);
    }

}

public T value() {
    return value();
  }

}

TreeNode class:

public class TreeNode<T extends Comparable<T>>{

 T value;
 BinaryTree<T> left, right;


public TreeNode(T value) {
    this.value = value;
    left = new BinaryTree<T>();
    right = new BinaryTree<T>();
}


public T value() {
  return value;
}

public BinaryTree<T> left() {
    return left;
}

public BinaryTree<T> right() {
    return right;
  }

 }

In this test class I have the method that instantiates an empty BinaryTree object. However when I run the test method, it only appears with the "Inserting the Values: " part of the code.

public class BinaryTreeTest {

/**
 * @throws java.lang.Exception
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@AfterClass
public static void tearDownAfterClass() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
}

@Test
public void test() {
       BinaryTree tree = new BinaryTree(); // empty tree
          int intVal;
          // insert 10 random ints
          System.out.print("Inserting the values: ");
          for (int i = 1; i <= 10; i++) {
             intVal = (int) (Math.random()*100);
             System.out.print(intVal + " ");
             tree.insert(intVal);
          }
          System.out.println(tree);
      } 
  }

What do I do so that it shows with the full list of values that are put in the tree?

3 Answers 3

0

Simply doing System.out.println(tree) will not print out the values in your tree. You need to create a method such as printTree() in your BinaryTree class that will travel through your tree and print the numbers.

0

You cannot just say System.out.println(tree), You have to write a custom method. I have drafted a basic method which you can refer. This may not be entirely correct.

public static void printTree(Treenode root)
{
if(root == null)
return;
else
    {
        System.out.println(root.value);
        printTree(root.left());
        printTree(root.right());
     }
}

This is for preorder. You can write either postorder or inorder

0

If you want to print your class you must Override toString() method of Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.