user2771059
user2771059

Reputation: 308

How work recursion and draw recursion tree

 public static void allCombination(char[] S, int start, int r, String output) {
        int length = S.length;
        if (r == 1) {
            for (int i = start; i < length; i++) {

                System.out.println(output + S[i]);
            }
        } else {
            for (int k = start; k < length - r + 1; k++) {

                allCombination(S, k + 1, r - 1, output + S[k]);
            }
        }

Hey tied to run above code to make possible combination of a given String(I took it from internet).Can you tell me how this recursion work and how i draw recursion tree for that(I am new to programming).

Upvotes: 0

Views: 435

Answers (1)

Tim B
Tim B

Reputation: 41178

Just start at the top of the paper, draw a box for the first call. Then trace through the code looking for any call back to itself, draw a box for the new call(s) under the first one. Then go through each of the boxes at the second level and repeat the process.

Upvotes: 4

Related Questions