cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion cấu trúc dữ liệu và giải thuật Recursion
Chapter RECURSION Chapter Subprogram implementation Recursion Designing Recursive Algorithms Towers of Hanoi Backtracking Eight Queens problem Function implementation Code segment (static part) Activation record (dynamic part) Parameters Function result Local variables Return address Function implementation Function implementation #include int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } int main() { int a, b, c, d1, d2; A1 cout > a >> b >> c; A3 d1 = maximum (a, b, c); A4 cout max ) max = z; return max; } Function implementation d1 = maximum (a, b, c); // a = 7; b = 9; c = A4 Return Address Return value x y z max int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } Function implementation d1 = maximum (a, b, c); // a = 7; b = 9; c = A4 Return Address Return value x y z max int maximum( int x, int y, int z) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } 10 Backtracking 64 Backtracking – Eight Queens problem 65 Backtracking – Eight Queens problem 66 Backtracking – Eight Queens problem 67 Backtracking – Eight Queens problem 68 Backtracking – Eight Queens problem 69 Backtracking – Eight Queens problem bool Queens::unguarded(int col) const { int i; bool ok = true; for (i = 0; ok && i < count; i++) ok = !queen_square[i][col]; for (i = 1; ok && count - i >= && col - i >= 0; i++) ok = !queen_square[count - i][col - i]; for (i = 1; ok && count - i >= && col + i < board_size; i++) ok = !queen_square[count - i][col + i]; return ok; } 70 Backtracking – Eight Queens problem 71 Backtracking – Eight Queens problem 72 Backtracking – Eight Queens problem 73 Backtracking – Eight Queens problem 74 Backtracking – Eight Queens problem 75 Backtracking – Eight Queens problem 76 Backtracking – Eight Queens problem 77 Backtracking – Eight Queens problem 78 [...]... requirements for recursion Separate copies of the variables declared in the function are created for each recursive call 16 Recursion 17 Recursion 18 Recursion 19 Recursion In C++, it’s possible for a function to call itself Functions that do so are called seft-referential or recursive functions In some problems, it may be natural to define the problem in terms of the problem itself Recursion is... come from repeated occurrences of the same function 13 Recursion An object contains itself 14 Recursion 15 Recursion Recursion is the name for the case when: A function invokes itself, or A function invokes a sequence of other functions, one of which eventually invokes the first function again In regard to stack frames for function calls, recursion is no different from any other function call... same task has a non-recursive solution Example: The factorial function n! = n * (n-1)! and 1! = 1 24 25 Recursion - Print List 26 Recursion - Print List pTemp = pHead; 27 Recursion - Print List A list is empty, or consists of an element and a sublist, where sublistis a list pHead pHead 28 Recursion - Print List Algorithm Print(val head) Prints Singly Linked List Pre headpoints to the... A4 2 A4 2 A4 0 1 A4 1 1 A4 2 A0 3 A0 3 A0 3 A4 1 A0 3 A4 1 1 A4 2 A0 3 A4 2 2 A0 3 A0 3 6 22 23 Recursion We must always make sure that the recursion bottoms out: A recursive function must contain at least one nonrecursive branch The recursive calls must eventually lead to a nonrecursive branch Recursion is one way to decompose a task into smaller subtasks At least one of the subtasks is a... printed Post Elements in the list have been printed Uses recursive function Print A1.if(head= NULL) // stopping case A1.1.return A2.write (head->data) A3.Print(head->link) // recursive case A4.End Print 29 Recursion - Print List Addr Value 100 Create List Print(pHead) Addr 800 pHead Value 800 6 804 810 810 10 814 820 820 14 824 830 830 20 834 0 30 Addr Value 100 800 pHead 200 800 head 204 A0 retAdd output