Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.

Slides:



Advertisements
Similar presentations
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Recursion CS 367 – Introduction to Data Structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Recursion. Binary search example postponed to end of lecture.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Data Structures Using C++1 Chapter 11 Binary Trees.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Ceng-112 Data Structures I Chapter 6 Recursion.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Program Development and Design Using C++, Third Edition
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Pointers and Linked Lists
Introduction to Recursion
Pointers and Linked Lists
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists.
Recursion Chapter 10.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 16-2 Linked Structures
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Stacks & Recursion.
Recursion Data Structures.
Stack Frames and Functions
Presentation transcript:

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion

2 Overview Unary Recursion Binary Recursion Examples Features Stacks Disadvantages Advantages

3 What is Recursion - Recall A procedure defined in terms of itself Components: –Base case –Recursive definition –Convergence to base case

4 double power(double x, int n) { int i double tmp = 1; if (n > 0) { for (i = 0; i < n; i++) { tmp *= x; } return tmp; }

5 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; }

6 Unary Recursion Functions calls itself once (at most) Usual format: function RecursiveFunction ( ) { if ( base case ) then return base value else return RecursiveFunction ( ) }

7 Unary Recursion /* Computes the factorial */ int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n*factorial(n-1); } function Factorial ( n ) { if ( n is less than or equal to 1) then return 1 else return n  Factorial ( n - 1 ) }

8 Binary Recursion Defined in terms of two or more calls to itself. For example – Fibonacci A series of numbers which –begins with 0 and 1 –every subsequent number is the sum of the previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21,...

9 Binary Recursion /* Compute the n-th Fibonacci number */ long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } function Fibonacci ( n ) { if ( n is less than or equal to 1 ) then return n else return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 ) }

10 Copy List HeadPtr struct LinkedListRec { int count; Node* headPtr; }; typedef struct LinkedListRec List;

11 #include “linkList.h” Node* copyNodes(Node* oldNodePtr); void copyList(List* newListPtr, List* oldListPtr) { if (listEmpty(oldListPtr)) { initializeList(newListPtr); } else { newListPtr->headPtr = copyNodes(oldListPtr->headPtr); newListPtr->count = oldListPtr->count; }

12 Node* copyNodes(Node* oldNodePtr) { Node* newNodePtr = NULL: if (oldNodePtr != NULL) { newNodePtr = makeNode(oldNodePtr->value); newNodePtr->nextPtr = copyNodes(oldNodePtr->nextPtr); } return newNodePtr; }

13 Copy Nodes OldNodePtr NewNodePtr

14 Copy Nodes OldNodePtr NewNodePtr NewNodePtr->next is set to the Result of calling copy nodes on The remainder of the list

15 Copy Nodes OldNodePtr NewNodePtr

16 Copy Nodes OldNodePtr NewNodePtr NULL

17 Copy Nodes NewNodePtr OldNodePtr

18 Copy Nodes NewNodePtr OldNodePtr

19 Recursion Process Every recursive process consists of: 1. A base case. 2. A general method that reduces to the base case.

20 Types of Recursion Direct. Indirect. Linear. n-ary (Unary, Binary, Ternary, …)

21 Stacks Every recursive function can be implemented using a stack and iteration. Every iterative function which uses a stack can be implemented using recursion.

22 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1

23 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1

24 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1 x = 2, n = 1 tmp = 1

25 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1 x = 2, n = 1 tmp = 1 x = 2, n = 0 tmp = 1

26 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 x = 2, n = 1 tmp = 1 tmp = 1*1*2 = 2

27 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 2*2 = 4

28 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 4*4*2 = 32

29 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 5, x = 2

30 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 5 Stack n = 5, x = 2

31 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 2 5 Stack n = 2, x = 2

32 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 1, x = 2

33 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 0, x = 2 tmp = 1

34 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 2 5 Stack n = 0, x = 2 tmp = 2

35 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 5 Stack n = 0, x = 2 tmp = 4

36 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 0, x = 2 tmp = 32

37 double power(double x, int n) { double tmp = 1; Stack theStack; initializeStack(&theStack); while (n != 0) { push(&theStack, n); n /= 2; } while (!stackEmpty(&theStack)) { n = pop(&theStack); if (n % 2 == 0) { tmp = tmp*tmp;} else { tmp = tmp*tmp*x;} } return tmp; }

38 Disadvantages May run slower. –Compilers –Inefficient Code May use more space.

39 Advantages More natural. Easier to prove correct. Easier to analysis. More flexible.

40 Free List – Non Recursive Head /* Delete the entire list */ void FreeList(Node* head){ Node* next; while (head != NULL) { next=head->next; free(head); head=next; } 0x2000 0x4c680x258a

41 Free List – Non Recursive head /* Delete the entire list */ void FreeList(Node* head){ Node* next; while (head != NULL) { next=head->next; free(head); head=next; } next 0x2000 0x258a0x4c68

42 Free List – Non Recursive head /* Delete the entire list */ void FreeList(Node* head){ Node* next; while (head != NULL) { next=head->next; free(head); head=next; } next 0x258a0x4c68

43 Free List – Non Recursive head /* Delete the entire list */ void FreeList(Node* head){ Node* next; while (head != NULL) { next=head->next; free(head); head=next; } next NULL 0x4c68

44 Free List – Non Recursive head /* Delete the entire list */ void FreeList(Node* head){ Node* next; while (head != NULL) { next=head->next; free(head); head=next; } next NULL Has local variables on the stack. This is performing two assignments and one comparison per iteration.

45 Free List Head /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); }

46 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a0x4c68 0x2000 Stack in memory 0x2000

47 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a0x4c68 0x2000 0x258a 0x2000 Stack in memory

48 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a0x4c68 0x2000 0x4c68 0x258a 0x2000 Stack in memory

49 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a0x4c68 0x2000 NULL 0x4c68 0x258a 0x2000 Stack in memory

50 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a0x4c68 0x2000 0x4c68 0x258a 0x2000 Stack in memory

51 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x258a 0x2000 0x258a 0x2000 Stack in memory

52 Free List list /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x2000 Stack in memory

53 Free List Head /* Delete the entire list */ void FreeList(Node* list) { if (list==NULL) return; FreeList(list->next); free(list); } 0x2000 Has no local variables on the stack. This is performing one assignment and one comparison per function call.

54 Revision Recursion

55 Revision: Reading Kruse Standish 3, 14 Langsam 3 Preparation Next lecture: Binary Trees Read Chapter 9 in Kruse et al.