二叉树toString方法-_-

时间:2014-07-14 16:40:47

标签: java binary-tree main

为什么输出父和子的空值!?逻辑对我来说似乎很好...... 它不应该为left,right和parent打印出null,因为它们是用left = left.toString()更新的;等

主要方法:

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);
}
}

方法类:

    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();
}
运行时输出

5
5 (null, null, null)

Process completed.

2 个答案:

答案 0 :(得分:2)

在您的toString方法中,您没有将leftrightparent对象作为类成员字段引用,而是将String局部变量声明为在方法中。请改用this.left

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

答案 1 :(得分:1)

在你的构造函数中:

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

你没有与父母做任何事情。因此,你最终得到的是没有连接的小树。

您需要添加以下行:

 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;
}