Recursion: base case and recursive case · Resursif: kasus dasar dan kasus rekursif
A method that calls itself
- Recursion is when a method calls itself to solve a smaller piece of the same problem.
- On the AP CSA exam you mostly trace recursion (follow the calls by hand). Writing small recursive methods helps you trace well.
- Every recursive method needs two parts: a base case and a recursive case.
Metode yang memanggil dirinya sendiri
- Rekursi adalah ketika sebuah metode memanggil dirinya sendiri untuk memecahkan bagian masalah yang lebih kecil dari masalah yang sama.
- Pada ujian AP CSA, Anda sebagian besar melacak rekursi (mengikuti pemanggilan secara manual). Menulis metode rekursif kecil membantu Anda melacak dengan baik.
- Setiap metode rekursif membutuhkan dua bagian: kasus dasar dan kasus rekursif.
Base case and recursive case
- Base case: the simplest input, where you return an answer without calling yourself. This stops the recursion.
- Recursive case: you call the same method with a smaller input, then build the answer.
- Without a base case, the method calls forever and crashes (a stack overflow).
Kasus dasar dan kasus rekursif
- Kasus dasar: input paling sederhana, di mana Anda mengembalikan jawaban tanpa memanggil diri sendiri. Ini menghentikan rekursi.
- Kasus rekursif: Anda memanggil metode yang sama dengan input yang lebih kecil, lalu membangun jawabannya.
- Tanpa kasus dasar, metode akan memanggil dirinya sendiri selamanya dan menyebabkan crash (stack overflow).
Example: factorial
factorial(n)meansn * (n-1) * ... * 1. For examplefactorial(4) = 24.- Base case:
factorial(1)is1. - Recursive case:
factorial(n)isn * factorial(n - 1).
Contoh: faktorial
factorial(n)berartin * (n-1) * ... * 1. Sebagai contohfactorial(4) = 24.- Kasus dasar:
factorial(1)adalah1. - Kasus rekursif:
factorial(n)adalahn * factorial(n - 1).
public class Recursion {
public static int factorial(int n) {
if (n <= 1) { // base case
return 1;
}
return n * factorial(n - 1); // recursive case
}
}
How the calls unwind
- Each call waits for the smaller call to finish. Then it multiplies and returns.
- Trace
factorial(3). Calls go down, then answers come back up.
Bagaimana pemanggilan terurai
- Setiap pemanggilan menunggu pemanggilan yang lebih kecil selesai. Kemudian mengalikan dan mengembalikan nilai.
- Jejak
factorial(3). Pemanggilan turun, lalu jawaban kembali naik.
factorial(3) = 3 * factorial(2) <- waits
factorial(2) = 2 * factorial(1) <- waits
factorial(1) = 1 <- base case, returns 1
factorial(2) = 2 * 1 = 2 <- returns 2
factorial(3) = 3 * 2 = 6 <- returns 6
Example: sum to n
sumTo(n)adds1 + 2 + ... + n. For examplesumTo(4) = 10.- Base case:
sumTo(0)is0(nothing to add). - Recursive case:
sumTo(n)isn + sumTo(n - 1).
Contoh: jumlah hingga n
sumTo(n)menambahkan1 + 2 + ... + n. MisalnyasumTo(4) = 10.- Kasus dasar:
sumTo(0)adalah0(tidak ada yang ditambahkan). - Kasus rekursif:
sumTo(n)adalahn + sumTo(n - 1).
public class Recursion {
public static int sumTo(int n) {
if (n <= 0) { // base case
return 0;
}
return n + sumTo(n - 1); // recursive case
}
}
Recursion over an array
- We can also recurse with an index that moves toward the end.
- Base case: when the index is past the last spot, return
0. - Recursive case: add
a[i]to the sum of the rest,arraySum(a, i + 1).
Rekursi pada array
- Kita juga bisa melakukan rekursi dengan indeks yang bergerak menuju akhir.
- Kasus dasar: ketika indeks berada melewati posisi terakhir, kembalikan
0. - Kasus rekursif: tambahkan
a[i]ke jumlah sisa,arraySum(a, i + 1).
- To sum the whole array you start at index
0:arraySum(a, 0). - Each call handles one value and trusts the next call for the rest.
public class Recursion {
public static int arraySum(int[] a, int i) {
if (i >= a.length) { // base case: past the end
return 0;
}
return a[i] + arraySum(a, i + 1);
}
}
- Untuk menjumlahkan seluruh array, mulailah dari indeks
0:arraySum(a, 0). - Setiap pemanggilan menangani satu nilai dan mempercayakan pemanggilan berikutnya untuk sisanya.
Common mistakes
- Recursion needs a base case, or it throws StackOverflowError.
- Each call must move closer to the base case.
Kesalahan umum
- Rekursi memerlukan kasus dasar, atau akan menyebabkan StackOverflowError.
- Setiap pemanggilan harus bergerak lebih dekat ke kasus dasar.
Now you try
- Each task gives you a method skeleton with a TODO. Write the base case and the recursive case.
- A hidden Harness calls your method with several values and checks the result.
- Press Run to compile, then Check answer.
Sekarang Anda coba
- Setiap tugas memberikan kerangka metode dengan TODO. Tulis kasus dasar dan kasus rekursif.
- Harness tersembunyi memanggil metode Anda dengan beberapa nilai dan memeriksa hasilnya.
- Tekan Run untuk mengkompilasi, lalu Check answer.
Recursion: base + recursive case · Resursif: kasus dasar + kasus rekursif
Calls split until a base case, then answers return up. · Panggilan terpecah hingga kasus dasar, kemudian jawaban kembali naik.
Complete factorial(int n) so it returns n * (n-1) * ... * 1 using recursion. Use a base case for n <= 1. The checker tests several values. · Lengkapi factorial(int n) agar mengembalikan n * (n-1) * ... * 1 menggunakan resursi. Gunakan kasus dasar untuk n <= 1. Pengecek menguji beberapa nilai.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete sumTo(int n) so it returns 1 + 2 + ... + n using recursion. Use a base case for n <= 0. The checker tests several values. · Lengkapi sumTo(int n) agar mengembalikan 1 + 2 + ... + n menggunakan resursi. Gunakan kasus dasar untuk n <= 0. Pengecek menguji beberapa nilai.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete arraySum(int[] a, int i) so it returns the sum of a[i] to the end, using recursion. Base case: when i is past the last spot, return 0. The checker tests several arrays. · Lengkapi arraySum(int[] a, int i) agar mengembalikan jumlah dari a[i] hingga akhir, menggunakan rekursif. Kasus dasar: ketika i berada di luar posisi terakhir, kembalikan 0. Penguji menguji beberapa array.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.