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());
}
}
Read More:
- nested exception is java.lang.StackOverflowError [How to Solve]
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:
- Java.lang.stackoverflowerror error [How to Solve]
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror
- Lotti triggered Java lang.StackOverflowError [How to Solve]
- [Solved] IDEA startup Error: Java lang.StackOverflowError
- [Solved] nacos Startup Error: nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- [Solved] Hbase-shell 2.x Error: Unhandled Java exception: java.lang.IncompatibleClassChangeError: Found class jline.Terminal…
- [Solved] Mvel2 Error: java.lang.VerifyError method: getKnownEgressType signature
- [Solved] Java.lang.BootstrapMethodError: call site initialization exception
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE
- [Solved] qrcode-error: Exception in thread “main” java.lang.NoClassDefFoundError…
- [Solved] Exception in thread “main“ java.lang.NoSuchFieldError: level
- [Solved] nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- keytool error: java.lang.Exception: Input not an X.509 certificate
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx