Tag Archives: Method threw ‘java.lang.StackOverflowError‘ exception

[Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate

Cause of problem

This error occurs when traversing the binary tree hierarchy. At first, it is thought that the stack overflow is caused by circular reference
later, it was found that the causes of the problem are as follows:
because the attribute references the child attribute, the toString method will constantly call the attribute of the child node. Finally, the stack overflowed.

Solution

Rewrite the toString method. Be careful not to print the properties of the calling child nodes
finally, paste a section of tree level traversal implementation code.

package com.zouch.onetoten;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author Zouch
 * @date 2021/8/25 下午1:13
 * Hierarchical tree traversal (no restriction that it must be a binary tree)
 * Idea.
 * Use a queue, and the condition for the outgoing loop is that the queue element is empty.
 * judge each time a node is out of the queue, and when there is a node in the queue, add all its children to the queue
 * Then add the nodes to the list
 */

@Getter
@Setter
public class TreeOfBfs {

    private TreeNode rootNode;

    @Override
    public String toString() {
        return "TreeOfBfs{" +
                "rootNode=" + rootNode +
                '}';
    }

    @Data
    public static class TreeNode {
        private Object key;

        private TreeNode parent;

        private List<TreeNode> treeNodes;

        @Override
        public String toString() {
            return "TreeNode{" +
                    "key=" + key +
                    ", parent=" + parent;
        }
    }

    public static List<TreeNode> levelTraverse(TreeNode rootNode) {
        if (Objects.isNull(rootNode)) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(rootNode);
        List<TreeNode> list = new LinkedList<>();
        while (queue.size() > 0) {
            TreeNode poll = queue.poll();
            list.add(poll);
            List<TreeNode> treeNodes = poll.getTreeNodes();
            if (CollectionUtils.isEmpty(treeNodes)){
                continue;
            }
            queue.addAll(treeNodes);
        }
        return list;
    }

    public static void main(String[] args) {
        TreeOfBfs treeOfBfs = new TreeOfBfs();
        TreeNode rootNode = new TreeNode();
        treeOfBfs.setRootNode(rootNode);

        TreeNode node1 = new TreeNode();
        node1.setParent(rootNode);
        node1.setKey("Left 1");

        TreeNode node2 = new TreeNode();
        node2.setParent(rootNode);
        node2.setKey("Right 2");

        TreeNode node3 = new TreeNode();
        node3.setParent(node2);
        node3.setKey("Left 3");

        TreeNode node4 = new TreeNode();
        node4.setParent(node2);
        node4.setKey("Right4");

        List<TreeNode> treeNodes = new ArrayList<>();
        treeNodes.add(node1);
        treeNodes.add(node2);
        rootNode.setTreeNodes(treeNodes);

        List<TreeNode> treeNodes2 = new ArrayList<>();
        treeNodes2.add(node3);
        treeNodes2.add(node4);
        node2.setTreeNodes(treeNodes2);

        List<TreeNode> result = levelTraverse(rootNode);
        System.out.println(result.toString());

    }

}