【问题标题】:Binary Tree toString method -_-二叉树 toString 方法 -_-
【发布时间】:2014-07-14 16:40:46
【问题描述】:

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

 }

接口:

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.

【问题讨论】:

  • 在调试器中启动它,或者放入一些额外的打印输出,您应该很快就能找到问题所在。命中:this.data 设置在哪里?

标签: java binary-tree main


【解决方案1】:

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

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

【讨论】:

  • 谢谢!就是这样。我想重点是从构造函数中调用正确的“左”和“右”变量,而不是 toString() 方法中的初始化变量。实际上是有道理的,我应该看到这个。
【解决方案2】:

在你的构造函数中:

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

【讨论】:

  • 哦,是的,感谢您的关注!不幸的是,我仍然得到空值,但这是一个很好的收获。是的,没有那个变量,构造函数就不能完全有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
  • 2013-07-20
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多