简体   繁体   中英

Converting an iterative function to recursive

I am trying to convert an iterative function to Recursion. But once I tried to do that it is runnning continuously like an infinite loop.

This is my iterative code

private static Node buildModelTree(String[] args) {
        // TODO Auto-generated method stub
        String clsIndex = args[3];
        splitted.add(currentsplit);
        double entropy = 0;
        int total_attributes = (Integer.parseInt(clsIndex));// class index
        int split_size = splitted.size();
        GainRatio gainObj = new GainRatio();
        while (split_size > current_index) { //iterate through all distinct pair for building children
            currentsplit = (SplitInfo) splitted.get(current_index);
            System.out.println("After currentsplit --->" + currentsplit);
            gainObj = new GainRatio();
            int res = 0;
            res = ToolRunner.run(new Configuration(),new CopyOfFunID3Driver(), args);
            gainObj.getcount(current_index);
            entropy = gainObj.currNodeEntophy();
            clsIndex = gainObj.majorityLabel();
            currentsplit.classIndex = clsIndex;
            if (entropy != 0.0 && currentsplit.attr_index.size() != total_attributes) { //calculate gain ration
                bestGain(total_attributes,entropy,gainObj);
            } else {
            //When entropy is zero build tree
            Node branch = new Node();
            String rule = "";
            Gson gson = new Gson();
            int temp_size = currentsplit.attr_index.size();
            for (int val = 0; val < temp_size; val++) {
            int g = 0;
            g = (Integer) currentsplit.attr_index.get(val);
            if (val == 0) {
                rule = g + " " + currentsplit.attr_value.get(val);
                //JSON
            //  branch.add(g, currentsplit.attr_value.get(val).toString(), new Node(currentsplit.classIndex, true));
            } else {
                rule = rule + " " + g + " "+ currentsplit.attr_value.get(val);
                //branch.add(g, currentsplit.attr_value.get(val).toString(), buildModelTree(args));
            }
           }
           rule = rule + " " + currentsplit.classIndex;
          }
            split_size = splitted.size();
            current_index++;
        }
    }

where all should I make change? I am trying to build tree. So inoredr to get the tree structure I am trying to make my id3 code recursive. with my current code I am only getting output as this ,But I want it as tree structure

Please suggest.

The Recursion algorithm must have following

1.Each time the function invokes itself, the Problem size has to be reduced.

(ie. If suppose first you are calling the function with array of size n, then the next time it has to be lesser than n.

  1. Base Case - the condition for the return statement.

(For example, if the array size is 0 then return)

In your code, these two are missing.

You're keep on calling the function with the same size of array. That's the problem.

Thanks

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