通过在每次调用 printBalanced 之前放置 print 语句以及将 print 语句作为每个 printBalanced 方法定义的第一行,它可以帮助您查看递归。见下文:
public class BalancedStrings {
public static void printBalanced(String prefix, int a, int b) {
System.out.println( "printBalanced called prefix = " + prefix + " a = " + a + " b = " + b );
if (a > 0) {
System.out.println( "printBalanced calling printBalanced with prefix = " + prefix + " a-1 = " + (a-1) + " b = " + b );
printBalanced(prefix + "a", a - 1, b);
}
if (b > 0) {
System.out.println( "printBalanced calling printBalanced with prefix = " + prefix + " a = " + a + " b-1 = " + (b-1) );
printBalanced(prefix + "b", a, b - 1);
}
if (a == 0 && b == 0)
System.out.println(prefix);
}
public static void printBalanced(int n) {
System.out.println( "printBalanced called n = " + n );
if (n % 2 == 0) {
printBalanced("", n / 2, n / 2);
}
}
public static void main(String[] args) {
printBalanced(4);
}
}
此外,printBalanced 也被称为重载方法,因为它有两个“方法签名”,这基本上意味着它被定义了不止一次,并且每个方法定义都有一组不同的变量传递给方法。
基本上,printBalanced 会一直调用自己,直到变量 a 和 b 减为零。然后它将打印出它不断累积的结果前缀。
此外,所有这些魔法都可能发生,因为每个方法调用都会将前缀、a 和 b 的当前状态推送到调用堆栈上。当方法最终返回而不进行递归调用时,堆栈就会展开。
我希望这个对你有用!递归可能很难理解。您还可以通过自己玩计算机并通过在纸上写下将被压入调用堆栈的前缀、a 和 b 的值来跟踪方法的执行来手动跟踪调用。
这是包含跟踪打印的程序的输出:
C:\Users\>java BalancedStrings
printBalanced called n = 4
printBalanced called prefix = a = 2 b = 2
printBalanced calling printBalanced with prefix = a-1 = 1 b = 2
printBalanced called prefix = a a = 1 b = 2
printBalanced calling printBalanced with prefix = a a-1 = 0 b = 2
printBalanced called prefix = aa a = 0 b = 2
printBalanced calling printBalanced with prefix = aa a = 0 b-1 = 1
printBalanced called prefix = aab a = 0 b = 1
printBalanced calling printBalanced with prefix = aab a = 0 b-1 = 0
printBalanced called prefix = aabb a = 0 b = 0
aabb
printBalanced calling printBalanced with prefix = a a = 1 b-1 = 1
printBalanced called prefix = ab a = 1 b = 1
printBalanced calling printBalanced with prefix = ab a-1 = 0 b = 1
printBalanced called prefix = aba a = 0 b = 1
printBalanced calling printBalanced with prefix = aba a = 0 b-1 = 0
printBalanced called prefix = abab a = 0 b = 0
abab
printBalanced calling printBalanced with prefix = ab a = 1 b-1 = 0
printBalanced called prefix = abb a = 1 b = 0
printBalanced calling printBalanced with prefix = abb a-1 = 0 b = 0
printBalanced called prefix = abba a = 0 b = 0
abba
printBalanced calling printBalanced with prefix = a = 2 b-1 = 1
printBalanced called prefix = b a = 2 b = 1
printBalanced calling printBalanced with prefix = b a-1 = 1 b = 1
printBalanced called prefix = ba a = 1 b = 1
printBalanced calling printBalanced with prefix = ba a-1 = 0 b = 1
printBalanced called prefix = baa a = 0 b = 1
printBalanced calling printBalanced with prefix = baa a = 0 b-1 = 0
printBalanced called prefix = baab a = 0 b = 0
baab
printBalanced calling printBalanced with prefix = ba a = 1 b-1 = 0
printBalanced called prefix = bab a = 1 b = 0
printBalanced calling printBalanced with prefix = bab a-1 = 0 b = 0
printBalanced called prefix = baba a = 0 b = 0
baba
printBalanced calling printBalanced with prefix = b a = 2 b-1 = 0
printBalanced called prefix = bb a = 2 b = 0
printBalanced calling printBalanced with prefix = bb a-1 = 1 b = 0
printBalanced called prefix = bba a = 1 b = 0
printBalanced calling printBalanced with prefix = bba a-1 = 0 b = 0
printBalanced called prefix = bbaa a = 0 b = 0
bbaa