java - toString 导致 StackOverflow 错误

标签 java data-structures tree stack-overflow

我在 BinaryTree 的 Node 类中覆盖了 toString()。现在它会导致 StackOverflow,我无法弄清楚原因。

我的 BinaryTree.Java 文件不完整,但它适用于 iv 到目前为止完成的内容。

所以基本上,如果我在 Node.java 中包含重写的 toString(),我的代码就会中断。我不知道为什么。我在想 toString() 不知何故以某种方式递归调用节点

完整代码:

节点.Java

public class Node {
    Node right;
    Node left;
    String element;
    Node parent;

    public Node(){  

    }

    public Node(String element){
        this.element = element;

    }

    public String getElement(){
        return element;
    }

    public Node getLeft(){
        return left;
    }

    public Node getRight(){
        return right;
    }

    public void setElement(String str){
        element = str;
    }

    public void setRight(Node right){
        this.right = right;
    }

    public void setLeft(Node left){
        this.left = left;
    }

    public void setParent(Node parent){
        this.parent = parent;
    }

    public Node getParent(){
        return parent;
    }

    @Override
    public String toString() {
        return "Node [right=" + right + ", left=" + left + ", element=" + element + ", parent=" + parent + "]";
    }

}

二叉树.Java

import java.util.ArrayList;

public class BinaryTree {
    Node root;
    ArrayList<Node> tree = new ArrayList<>();


    //create a tree with a root node, and two empty nodes
    //root has no data
    public BinaryTree(Node root){
        tree = new ArrayList<>();
        this.root = root;
        root = new Node();  //parent of root will be set to null
        tree.add(root); // root at position 1 in arrayList
    }


    public BinaryTree(){

    }


    //assuming one of the children are null and there is a root
    public void insertNode(Node insertNode, Node parent){
        //setthis nodes parent to parent index.



        tree.add(insertNode); // add the node to end of the arraylist.
        insertNode.setParent(parent);  //sets the nodes parent node
        //if parents left is null, 
        if (parent.getLeft()==null){
            parent.setLeft(insertNode);
        }

        //else, its right is null
        else{
            parent.setRight(insertNode);
        }

    }

    public void insertRoot(Node node){
        if (tree.isEmpty()){  //add the root
            root = node;
            tree.add(node);

        }
        else{tree.add(node);}// add it, get its location in tree and then get its parent.
        //int index = tree.indexOf(node);// found the index of the nod  
    }


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

    @Override
    public String toString() {
        return "BinaryTree [root=" + root + ", tree=" + tree + ", isEmpty()=" + isEmpty() + "]";
    }
}

树测试.java

public class TreeTest {



    public static void main(String[] args){
        //create empty tree & test
        BinaryTree myTree = new BinaryTree();
        System.out.println(myTree.isEmpty());
        System.out.println(myTree.toString());

        //create root node & test
        Node myRoot = new Node();  //Node@7852e922
        myTree.insertRoot(myRoot);
        myRoot.setElement("foo");
        System.out.println(myTree.isEmpty());
        System.out.println(myTree.toString());
        System.out.println(myTree.isEmpty());
        System.out.println(myTree.toString());


        //create a child node and test
        Node secondNode = new Node();  //Node@4e25154f
        myTree.insertNode(secondNode, myRoot);
        System.out.println(myTree.toString());   //code breaks here
        System.out.println(myRoot.getLeft());

    }}

最佳答案

看看您的 toString() 方法,我认为您对递归调用的看法是正确的。

假设某个节点 A 有一个子节点 B。那么为了让 A 打印它的输出,它需要打印子节点 B。为了让 B 打印它的输出,它需要打印父节点 A。在为了让 A 打印它的输出,它需要打印子节点 B,等等。

您可以更改 toString() 以仅打印出父元素,这将停止循环。

关于java - toString 导致 StackOverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43212298/

相关文章:

java - 将(标题-)段落和表格放在一页上?

java - 如何避免嵌套循环

具有真实示例的 Angular Material 树复选框

java - JBoss Gradle

java - 如何使用 JasperReports 将分页设置为在 X 行之后发生?

java - 在JAVA中创建不固定大小的字节数据结构的最佳方法是什么

algorithm - 将不平衡树转换为生成树

python - 将模拟树的字典转换为列表

java - MySQL 插入数据时出现错误 "Duplicate entry ' ' for key "

java - Java 中的循环缓冲区?