如何工作递归并绘制递归树 [英] How work recursion and draw recursion tree

查看:38
本文介绍了如何工作递归并绘制递归树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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).

推荐答案

只需从纸的顶部开始,为第一个调用绘制一个框.然后跟踪代码,寻找任何对自身的调用,在第一个调用下为新调用绘制一个框.然后遍历第二层的每个框并重复该过程.

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.

这篇关于如何工作递归并绘制递归树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆