Adam Staples
Adam Staples

Reputation: 406

Binary Tree toString method -_-

Why is it outputting null for parent and childs!? The logic seems fine to me... It shouldn't be printing out null for the left, right, and parent since they are updated with left = left.toString(); etc.

The main method:

public class TreeInfo 
{

public static void main(String[] args) 
{
    BinaryTree<Integer> left = new BinaryTree<Integer>(5);
    System.out.println(left);
    BinaryTree<Integer> right = new BinaryTree<Integer>(9);
    BinaryTree<Integer> parent = new BinaryTree<Integer>(1);
    BinaryTree<Integer> a = new BinaryTree<Integer>(parent, 5, left, right);
    System.out.println(a);
}
}

.

The methods class:

    import java.math.*;
import java.lang.*;
import java.util.*;

public class BinaryTree<E> implements BinaryTreeNode<E>
{
    protected E data;
    protected BinaryTreeNode<E> parent;
    protected BinaryTreeNode<E> left;
    protected BinaryTreeNode<E> right;

    public BinaryTree(BinaryTree<E> parent, E data, BinaryTree<E> left, BinaryTree<E> right) 
    {
        this.data = data;
        this.left = left;
        this.right = right;
    }


    public BinaryTree(E data) 
    {
        this(null, data, null, null);
    }


    public E getData()
    {
        return this.data;   
    }


    public BinaryTreeNode<E> getParent()
        {
        return this.parent;
    }


    public boolean isEmpty()
    {
        return data == null;
    } 


    public boolean isLeaf()
    {
        return left == null && right == null;
    }

    public boolean hasLeft()
    {
        return left != null;
    }

    public boolean hasRight()
    {
        return right != null;
    }

    public int getNONodes()
    {
        int count = 1;
        if (hasLeft()) 
        {
            count +=  left.getNONodes();
        }
        if (hasRight()) 
        {
            count +=  right.getNONodes();
        }
        return count;
    }


public String toString() {
      if (isLeaf()) {
          return data.toString();
      }
      else {
          String root = "null", parent = "null", left = "null", right = "null";
          root = data.toString();
          if (left != null) {
              left = left.toString();
          }
          if (right != null) {
              right = right.toString();
          }
          if (parent != null)
          {
            parent = parent.toString();
          }
          return root + " (" + parent + ", " + left + ", " + right + ")";
      }
  }

 }

inferface:

public interface BinaryTreeNode<E>
{
    E getData(); // Returns a reference to data and a reference to its left and right subtrees.
    BinaryTreeNode<E> getParent(); // Returns the parent of this node, or null if this node is a root.
    boolean isEmpty(); // returns false if the tree is non-empty and true if it is.
    boolean hasLeft();
    boolean hasRight();
    boolean isLeaf(); 
    int getNONodes(); // returns total number of nodes in the Binary Tree.
    String toString();
}

output when ran:

5
5 (null, null, null)

Process completed.

Upvotes: 2

Views: 3458

Answers (2)

M A
M A

Reputation: 72864

In your toString method you're not referencing the left, right and parent objects as class member fields but as the String local variables declared in the method. Use this.left instead:

if(this.left != null) {
    left = this.left.toString();
}

Upvotes: 2

jcjunction
jcjunction

Reputation: 325

In your constructor:

 public BinaryTree(BinaryTree<E> parent, E data, BinaryTree<E> left, BinaryTree<E> right) 
{
    this.data = data;
    this.left = left;
    this.right = right;
}

You aren't doing anything with the parent. therefore you end up with small trees that aren't connected.

you need to add the line:

 public BinaryTree(BinaryTree<E> parent, E data, BinaryTree<E> left, BinaryTree<E> right) 
{
    this.data = data;
    this.left = left;
    this.right = right;
    this.parent = parent;
}

Upvotes: 1

Related Questions