为什么对父母和孩子输出null!逻辑对我来说似乎很好...
它不应该为左,右和父项输出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 + ")";
      }
  }

 }


介面:

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.

最佳答案

在您的toString方法中,您不是将leftrightparent对象引用为类成员字段,而是引用了方法中声明的String局部变量。使用this.left代替:

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

关于java - 二叉树toString方法-_-,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24741492/

10-11 22:28
查看更多