printBalanced への各呼び出しの前に print ステートメントを配置し、各 printBalanced メソッド定義の最初の行として print ステートメントを配置すると、再帰を確認するのに役立つ場合があります。下記参照:
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 はオーバーロードされたメソッドとも呼ばれます。2 つの「メソッド シグネチャ」があるためです。これは、基本的に複数回定義され、各メソッド定義にはメソッドに渡される変数の異なるセットがあることを意味します。
基本的に、変数 a と b がゼロになるまで、printBalanced は自分自身を呼び出し続けます。次に、蓄積し続ける結果のプレフィックスを出力します。
また、各メソッド呼び出しがプレフィックス 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