๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JAVA ์žฌ๊ท€ ํ˜ธ์ถœ(Recursive Call) ์˜ˆ์ œ

by Leica 2020. 4. 7.
๋ฐ˜์‘ํ˜•

JAVA ์žฌ๊ท€ ํ˜ธ์ถœ(Recursive Call) ์˜ˆ์ œ

์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ๊ท€ ๋ฉ”์†Œ๋“œ(recursive method)๋ผ ํ•œ๋‹ค.

์žฌ๊ท€ ๋ฉ”์†Œ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•˜์ง€๋งŒ ๋น„๊ต์  ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

 

JAVA ์žฌ๊ท€ ๋ฉ”์†Œ๋“œ์˜ ๊ตฌ์กฐ

returnType methodName() {
    // some codes..
    methodName();	// ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœ
}

 

JAVA ์žฌ๊ท€(Recursion) ์˜ˆ์ œ1 - StackOverFlow ๋ฐœ์ƒ

static void printHelloInfinite() {
    System.out.println("hello");
    printHelloInfinite();
}

public static void main(String[] args) {
    printHelloInfinite();
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ:

hello
hello
hello
hello
...
java.lang.StackOverFlow

 

JAVA ์žฌ๊ท€(Recursion) ์˜ˆ์ œ2 - ์ข…๋ฃŒ ์กฐ๊ฑด์ด ์žˆ๋Š” ์žฌ๊ท€ ๋ฉ”์†Œ๋“œ

static void printHello(int endNum) {
    if (endNum > 0) {
        System.out.println("hello " + endNum);
        printHello(endNum-1);
    }
}

public static void main(String[] args) {
    printHello(5);
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ:

hello5
hello4
hello3
hello2
hello1

 

JAVA ์žฌ๊ท€(Recursion) ์˜ˆ์ œ3 - Factorial(๊ณ„์Šน)

static int factorial(int i) {
    if (i == 1) return 1;
    else return i * factorial(i - 1);
}

public static void main(String[] args) {
    System.out.println("5!=" + factorial(5));
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ:

5!=120

 

JAVA ์žฌ๊ท€(Recursion) ์˜ˆ์ œ4 - Fibonacci ์ˆ˜ ๊ตฌํ•˜๊ธฐ

// 0 ์ด์ƒ์˜ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ
static int getNthFibonacciNumber(int n) {
    if (n < 2) return n;
    else return getNthFibonacciNumber(n - 2) + getNthFibonacciNumber(n - 1);
}

public static void main(String[] args) {
    System.out.println("10th Fibonacci number is " + getNthFibonacciNumber(10));
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ:

10th Fibonacci number is 55

 

JAVA ์žฌ๊ท€(Recursion) ์˜ˆ์ œ5 - Fibonacci ์ˆ˜์—ด ์ถœ๋ ฅ

// 2 ์ด์ƒ์˜ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ nํ•ญ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ผ
static int num1 = 0, num2 = 1, num3 = 0;    // ์ดˆ๊ธฐ๊ฐ’
static void printFibonacchiNumber(int count) {  // 10, 9, 8, 7, 6, 5, 4, 3, 2

    if (count > 1) {
        num3 = num1 + num2; // 1, 2, 3, 5, 8, 13, 21, 34, 55
        num1 = num2;    // 1, 1, 2, 3, 5, 8, 13, 21, 34
        num2 = num3;    // 1, 2, 3, 5, 8, 13, 21, 34, 55

        System.out.printf(num3 + " ");
        printFibonacchiNumber(count-1);
    }
}

public static void main(String[] args) {
    int num = 10;
    System.out.printf("0 1 ");
    printFibonacchiNumber(num);
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ:

0 1 1 2 3 5 8 13 21 34 55 

 

์ „์ฒด ์˜ˆ์ œ ์ฝ”๋“œ

public class RecursionTest {

    // Recursion Example1
    static void printHelloInfinite() {

        System.out.println("hello");
        printHelloInfinite();
    }

    // Recursion Example2
    static void printHello(int endNum) {

        if (endNum > 0) {
            System.out.println("hello " + endNum);
            printHello(endNum-1);
        }
    }

    // Recursion Example3
    static int factorial(int i) {

        if (i == 1) return 1;
        else return i * factorial(i - 1);
    }

    // Recursion Example4
    // 0 ์ด์ƒ์˜ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ
    static int getNthFibonacciNumber(int n) {

        if (n < 2) return n;
        else return getNthFibonacciNumber(n - 2) + getNthFibonacciNumber(n - 1);
    }

    // Recursion Example5
    // 2 ์ด์ƒ์˜ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ nํ•ญ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ผ
    static int num1 = 0, num2 = 1, num3 = 0;    // ์ดˆ๊ธฐ๊ฐ’
    static void printFibonacchiNumber(int count) {  // 10, 9, 8, 7, 6, 5, 4, 3, 2

        if (count > 1) {
            num3 = num1 + num2; // 1, 2, 3, 5, 8, 13, 21, 34, 55
            num1 = num2;    // 1, 1, 2, 3, 5, 8, 13, 21, 34
            num2 = num3;    // 1, 2, 3, 5, 8, 13, 21, 34, 55

            System.out.printf(num3 + " ");
            printFibonacchiNumber(count-1);
        }
    }

    public static void main(String[] args) {

        System.out.println("========= Recursion Example1 =========");
        System.out.println("This code causes an error.");
//        printHelloInfinite();

        System.out.println("========= Recursion Example2 =========");
        printHello(5);

        System.out.println("========= Recursion Example3 =========");
        System.out.println("5!=" + factorial(5));

        System.out.println("========= Recursion Example4 =========");
        System.out.println("10th Fibonacci number is " + getNthFibonacciNumber(10));

        System.out.println("========= Recursion Example5 =========");
        int num = 10;
        System.out.printf("0 1 ");
        printFibonacchiNumber(num);
    }
}

 

References

https://www.javatpoint.com/recursion-in-java

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€