Data Structures and Algorithms - Chapter 6 -Recursion pot

85 531 1
Data Structures and Algorithms - Chapter 6 -Recursion pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 6 - Recursion  Subprogram implementation  Recursion  Designing recursive algorithms  Recursion removal  Backtracking  Examples of backtracking and recursive algorithms:  Factorial  Fibonacci  The towers of Hanoi  Eight Queens Problem  Tree-structured program: Look-ahead in Game 1 Subprogram implementation 2 Subprogram implementation 3 Subprogram implementation 4 Subprogram implementation 5 Tree and Stack frames of function calls 6 Tree and Stack frames of function calls Stack frames: Each vertical column shows the contents of the stack at a given time There is no difference between two cases: o when the temporary storage areas pushed on the stack come from different functions, and o when the temporary storage areas pushed on the stack come from repeated occurrences of the same function. 7 Recursion 8 Recursion 9 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.  Stack frames illustrate the storage requirements for recursion.  Separate copies of the variables declared in the function are created for each recursive call. 10 [...]... function is invoked (the number of nodes in recursive tree) determines the amount of running time of the program 13 Recursion 14 Print List 6 10 14 20 15 Print List A list is • empty, or • consists of an element and a sublist, where sublist is a list 16 Print List 6 10 14 20 17 Print List Algorithm Print(val head ) Prints Singly Linked List Pre Post Uses head points to the first element of the... Uses head points to the first element of the list needs to be printed Elements in the list have been printed recursive function Print 1 if (head = NULL) // stopping case 1 return 2 write (head- >data) 3 Print(head->link) // recursive case End Print 18 Print List in Reverse 19 Print List in Reverse 20 Print List in Reverse Algorithm PrintReverse(val head ) Prints Singly Linked List in reverse... element of the list needs to be printed Elements in the list have been printed in reverse recursive function PrintReverse 1 if (head = NULL) // stopping case 1 return 2 PrintReverse(head->link) // recursive case 3 write (head- >data) End PrintReverse 21 Factorial: A recursive Definition 22 Iterative Solution Algorithm IterativeFactorial (val n ) Calaculates the factorial of a number using a loop... Uses recursive function RecursiveFactorial 1 if (n = 0) 1 factN = 1 // stopping case 2 else 1 factN = n * RecursiveFactorial(n-1) // recursive case 3 return factN End RecursiveFactorial 24 Recursive Solution  The recursive definition and recursive solution can be both concise and elegant  The computational details can require keeping track of many partial computations before the process is complete... 1 2 else 1 factN = n * RecursiveFactorial(n-1) 3 return factN End RecursiveFactorial 26 Recursive Solution Stack 27 Thinking of Recursion  Remembering partial computations: computers can easily keep track of such partial computations with a stack, but human mind can not  It is exceedingly difficult for a person to remember a long chain of partial results and then go back through it to complete the... particular case to one or more of the smaller cases, thereby making progress toward eventually reducing the problem all the way to the base case 30 Designing Recursive Algorithms 31 Designing Recursive Algorithms 32 Designing Recursive Algorithms 33 Fibonacci Numbers 34 Fibonacci Numbers 35 Fibonacci Numbers Algorithm Fibonacci (val n ) Calculates the nth Fibonacci number Pre Post Uses n is... is the ordinal of the Fibonacci number returns the nth Fibonacci number Recusive function Fibonacci 1 if (n = 0) OR (n = 1) // stopping case 1 return n 2 return ( Fibonacci(n -1 )+Fibonacci(n -2 ) ) // recursive case End Fibonacci 36 ...Tree and Stack frames of function calls D E F F E E E 11 Tree and Stack frames of function calls Recursive calls: M M M M M M M M M M M M M M M M M M M M M M M M M M M M 12 Recursion  In the usual implementation of recursion, there are... then go back through it to complete the work  Ex.:  When we use recursion, we need to think in somewhat difference terms than with other programming methods  Programmers must look at the big picture and leave the detailed computations to the computer 28 Recursion The essence of the way recursion works: To obtain the answer to a large problem, a general method is used that reduces the large problem . Chapter 6 - Recursion  Subprogram implementation  Recursion  Designing recursive algorithms  Recursion removal  Backtracking . backtracking and recursive algorithms:  Factorial  Fibonacci  The towers of Hanoi  Eight Queens Problem  Tree-structured program: Look-ahead in Game 1 Subprogram

Ngày đăng: 15/03/2014, 17:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan