cs171 introduction to computer science ii
play

CS171 Introduction to Computer Science II Recursion Li Xiong - PowerPoint PPT Presentation

CS171 Introduction to Computer Science II Recursion Li Xiong 2/28/2012 1 What we have learned so far Basic data structure Arrays Linked list Abstract data types Abstract data types Stacks Queues Linked List A


  1. CS171 Introduction to Computer Science II Recursion Li Xiong 2/28/2012 1

  2. What we have learned so far � Basic data structure � Arrays � Linked list � Abstract data types � Abstract data types � Stacks � Queues

  3. Linked List � A Linked List is a sequence of nodes chained together. � Each node , element, or link contains a data item , and a reference to next node

  4. Stacks and Queues � Can be implemented by both (resizing) arrays and linked list

  5. Today � Quiz on stacks, queues, linked list � Recursion

  6. Recursion � Recursion concept � Examples � Factorial � Fibonacci � GCD � GCD � Recursive graph Htree � Next lecture � Divide and conquer � Binary search � Tower of Hanoi � Cost analysis of recursive algorithms

  7. 2/28/2012 7

  8. What is recursion?

  9. Factorial N! = N*(N-1)*(N-2)*…..* 2 * 1 ��� ��������� �� ��� ��������� �� � �� ������ ������ �� ���� ������ �� � ������������ �

  10. Recursive Method � A method that calls itself (direct recursion) ���� ���� ����������������� ����������������� � � � � ������������������ ��� �

  11. Recursive Method � A method that calls itself (direct recursion) � Every recursive method must have a base case that is not recursive ������������������������ � ���������������� ���������������� ��� � ������ ��� ������������������ ��� � �

  12. Better version of recursion definition Recursion n. If you still don't get it, see Recursion.

  13. Recursion � A method calls itself � Calls a “clone” of itself to solve a smaller problem � Buck Passing � Must have a base case � Must have a base case � The buck stops here! (does not call the method)

  14. Example: Fibonacci Numbers � Recursive formula: � � � � = � � � − �� + � � � − �� � ��� = �� � ��� = � � 0, 1, 1, 2, 3, 5, 8, 13, …..

  15. Fibonacci Numbers: Java Code ��� ����� �� � �� ������ ������ �� ���� �� ������ ���� �� ������ ������ �� ���� ������ �������������� �

  16. Visual Recursion

  17. Fractals 22 2/28/2012

  18. Recursion � Recursive method � Examples � Factorial � Fibonacci � GCD � GCD � Recursive graph Htree � Next lecture � Divide and conquer � Binary search � Tower of Hanoi � Cost analysis of recursive algorithms

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend