Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursively generating a tree in Java
    primarykey
    data
    text
    <p>I'm trying to build a tree, where each node has six children. The method I wrote for generating the tree, genB, results in a stack overflow.</p> <pre><code>public class TreeTest { public static void main(String Args[]) { sBNode bTree; sWNode wTree; bTree = new sBNode(); wTree = new sWNode(); TreeTest ai = new TreeTest(); bTree.depth = 0; System.out.println(bTree.depth); ai.genB(bTree, 2); ai.printTree(bTree); } public Treetest() { } public boolean leaf(sBNode node) { if(node.pe1==null &amp;&amp; node.pe2==null &amp;&amp; node.pe3==null &amp;&amp; node.ee1==null &amp;&amp; node.ee2==null &amp;&amp; node.ee3==null) return true; else return false; } public void genB(sBNode parent, int ddepth) { int pdepth; if(parent.depth != ddepth) { System.out.println(parent.depth); pdepth = parent.depth++; sBNode pe1 = new sBNode(); sBNode pe2 = new sBNode(); sBNode pe3 = new sBNode(); sBNode ee1 = new sBNode(); sBNode ee2 = new sBNode(); sBNode ee3 = new sBNode(); pe1.depth = pdepth; pe2.depth = pdepth; pe3.depth = pdepth; ee1.depth = pdepth; ee2.depth = pdepth; ee3.depth = pdepth; pe1.parent = parent; pe2.parent = parent; pe3.parent = parent; ee1.parent = parent; ee2.parent = parent; ee3.parent = parent; genB(pe1, ddepth); //genB(pe2, ddepth); //genB(pe3, ddepth); //genB(ee1, ddepth); //genB(ee2, ddepth); //genB(ee3, ddepth); } } public void printTree(sBNode node) { while(!leaf(node)) { System.out.println(node.depth); printTree(node.pe1); printTree(node.pe2); printTree(node.pe3); printTree(node.ee1); printTree(node.ee2); printTree(node.ee3); } } } final class sBNode { public String state = "BCXXXCXXX"; //utility value public boolean war; public int score; public int oscore; public int utility; public int min; public int max; public int depth; sBNode pe1; sBNode pe2; sBNode pe3; sBNode ee1; sBNode ee2; sBNode ee3; sBNode parent; public sBNode() { war = false; score = 0; oscore = 0; utility = 0; min = 0; max = 0; depth = 0; } } </code></pre> <p>This is part of a homework project, where the end goal is to design a game using search of a tree, so I don't really want an outright answer, but a hint to what's causing (near) infinite recursion.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload