简体   繁体   中英

Recursively generating a tree in Java

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.

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 && node.pe2==null && node.pe3==null && node.ee1==null && node.ee2==null && 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;
    }
}

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.

Found your problem:

pdepth = parent.depth++;

should be:

pdepth = parent.depth + 1;

You are post-incrementing which means the assignment is executed first and then the increment. The value of pdepth is always zero.

The culprit is
pdepth = parent.depth++;
line.

You are using the post increment operator instead of the pre increment one. This way parent.depth is always 0 thus leading to an infinite recursion.

-JB-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM