1 / 15

MIS 215 Module 4 – Recursion

MIS 215 Module 4 – Recursion. Where are we?. MIS215. Basic Algorithms. Introduction. List Structures. Advanced structures. Search Techniques. Intro to Java, Course. Sorting Techniques. Java lang. basics. Linked Lists. Hashtables. Binary Search. Graphs, Trees . Stacks, Queues.

alijah
Télécharger la présentation

MIS 215 Module 4 – Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIS 215 Module 4 – Recursion

  2. Where are we? MIS215 Basic Algorithms Introduction List Structures Advanced structures Search Techniques Intro to Java, Course Sorting Techniques Java lang. basics Linked Lists Hashtables Binary Search Graphs, Trees Stacks, Queues Arrays Bubblesort Fast Sorting algos (quicksort, mergesort) Newbie Programmers Designers Developers Professionals

  3. Today’s buzzwords • Recursion • A data structure where items can be added to the top and removed from the top • A LIFO (Last In, First Out) Structure • Recursion • A programming style where a method directly or indirectly calls itself. • Typically leads to simpler and more elegant looking implementations, although not necessarily more efficient • Recursive structures • A data structure including a reference to itself • Base Case/Condition • The case that forms the basis from which other cases can be built

  4. Recursion: What is it? • It is a problem solving technique • divide and conquer • It is a programming technique • let a function call itself

  5. 1 2 3 Towers of Hanoi:A Classical Example

  6. Towers of Hanoi:Recursive Function int Move(int count, int start, int finish, int temp); Pre: There are at least count disks on the tower start. The top disk (if any) on each of towers temp and finish is larger than any of the top count disks on tower start. Post: The top count disks on start have been moved to finish; temp has been returned to its starting position.

  7. An Example with Two Disks:Trace of the Function Move (2,1,3,2) Outer Call Move (1,1,2,3) First recursive call Trivial recursive call Move (0,1,3,2) First instruction printed Move disk 1 from 1 to 2 Trivial recursive Call Move (0,3,2,1) End of first recursive call Second instruction period Move disk 2 from 1 to 3 Second recursive call Move (1,2,3,1) Trivial recursive call Move (0,2,1,3) Third instruction printed Move disk 1 from 2 to 3 Trivial recursive call Move (0,3,1,2) End of second recursive call End of outer Call

  8. Designing Recursive Algorithms • Find the key step • Find a stopping rule • Outline your algorithm • Check termination • Draw a recursion tree

  9. Examples • Factorial • Fibonacci Sequence • Tower’s of Hanoi • Linear Search in LinkList? • Binary Search in Array? • Any problem that can be defined in terms of a smaller version of itself!

  10. Building A recursion • Say, I want you to write a recursion for finding power(x, n) i.e, find the nth power of x (xn) • What is x0? • Can you write x2 in terms of x? • Can you write x3 in terms of x2? • Can you write x25 in terms of x24? • So, can you write xn in terms of xn-1?

  11. So, to find recursion • Find the base case • Try out a few small examples from the base case … building on top of another • Now try to generalize

  12. Recursively define factorial fact(n) • Base case: • Recursion:

  13. Recursively define gcd(x, y) • Base case: • Recursion:

  14. Recursively define find method in a linked list • find(Node n, int target) • Base case: • Recursion:

  15. Recursively define binary search • find(int lb, int ub, int target) • Base case: • Recursion:

More Related