RECURSION CITS1001. 2 Scope of this lecture Concept of recursion Simple examples of recursion.
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
-
date post
20-Dec-2015 -
Category
Documents
-
view
219 -
download
0
Transcript of CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
![Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/1.jpg)
CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms
Lecture A11 – RecursionLecture A11 – Recursion
![Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/2.jpg)
2
OverviewOverview
• Unary Recursion
• Binary Recursion
• Examples
• Features
• Stacks
• Disadvantages
• Advantages
![Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/3.jpg)
3
What is Recursion - RecallWhat is Recursion - Recall
• A procedure defined in terms of itself
• Components:
– Base case
– Recursive definition
– Convergence to base case
![Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/4.jpg)
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;}
![Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/5.jpg)
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;}
![Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/6.jpg)
6
Unary RecursionUnary Recursion
• Functions calls itself once (at most)• Usual format:
function RecursiveFunction ( <parameter(s)> ){
if ( base case ) thenreturn base value
elsereturn RecursiveFunction ( <expression> )
}
![Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/7.jpg)
7
Unary RecursionUnary 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 )}
![Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/8.jpg)
8
Binary RecursionBinary 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,...
![Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/9.jpg)
9
Binary RecursionBinary 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 )}
![Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/10.jpg)
10
Copy ListCopy ListHeadPtr
struct LinkedListRec{ int count; Node* headPtr;};
typedef struct LinkedListRec List;
![Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/11.jpg)
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; }}
![Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/12.jpg)
12
Node* copyNodes(Node* oldNodePtr){ Node* newNodePtr = NULL:
if (oldNodePtr != NULL) { newNodePtr = makeNode(oldNodePtr->value); newNodePtr->nextPtr = copyNodes(oldNodePtr->nextPtr); }
return newNodePtr;}
![Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/13.jpg)
13
Copy NodesCopy NodesOldNodePtr
NewNodePtr
![Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/14.jpg)
14
Copy NodesCopy NodesOldNodePtr
NewNodePtr
NewNodePtr->next is set to the Result of calling copy nodes onThe remainder of the list
![Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/15.jpg)
15
Copy NodesCopy NodesOldNodePtr
NewNodePtrNewNodePtr
![Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/16.jpg)
16
Copy NodesCopy NodesOldNodePtr
NewNodePtrNewNodePtr NewNodePtr
NULL
![Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/17.jpg)
17
Copy NodesCopy Nodes
NewNodePtr NewNodePtr
OldNodePtr
![Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/18.jpg)
18
Copy NodesCopy Nodes
NewNodePtr
OldNodePtr
![Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/19.jpg)
19
Recursion ProcessRecursion Process
Every recursive process consists of:
1. A base case. 2. A general method that reduces to the base case.
![Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/20.jpg)
20
Types of RecursionTypes of Recursion
• Direct.
• Indirect.
• Linear.
• n-ary (Unary, Binary, Ternary, …)
![Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/21.jpg)
21
StacksStacks
• Every recursive function can be implemented using a stack and iteration.
• Every iterative function which uses a stack can be implemented using recursion.
![Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/22.jpg)
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
![Page 23: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/23.jpg)
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
![Page 24: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/24.jpg)
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
![Page 25: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/25.jpg)
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
![Page 26: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/26.jpg)
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
![Page 27: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/27.jpg)
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
![Page 28: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/28.jpg)
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
![Page 29: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/29.jpg)
29
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
n
tmp
5
1
1
Third call
x
n
tmp
5
2
1
Second call
5
2
1
x
n
tmp
First call
x
n
tmp
5
0
1
Fourth call
Runtime stack
![Page 30: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/30.jpg)
30
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
n
tmp
5
1
2
Return to Third call
x
n
tmp
5
2
1
Second call
5
2
1
x
n
tmp
First call
Runtime stack
![Page 31: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/31.jpg)
31
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
n
tmp
5
2
4
Return to Second call
5
2
1
x
n
tmp
First call
Runtime stack
![Page 32: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/32.jpg)
32
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;}
Return to First call5
2
32
x
n
tmp
Runtime stack
![Page 33: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/33.jpg)
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} }}
Stackn = 5, x = 2
5
2
Runtime stackx
n
tmp
![Page 34: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/34.jpg)
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} }}
5
Stackn = 5, x = 2
5
2
Runtime stackx
n
tmp
![Page 35: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/35.jpg)
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} }}
2
5
Stackn = 2, x = 2
2
2
Runtime stackx
n
tmp
![Page 36: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/36.jpg)
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} }}
1
2
5
Stackn = 1, x = 2
1
2
Runtime stackx
n
tmp
![Page 37: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/37.jpg)
37
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} }}
1
2
5
Stackn = 0, x = 2
tmp = 1
1
0
2
Runtime stackx
n
tmp
![Page 38: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/38.jpg)
38
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
Stackn = 0, x = 2
tmp = 2
2
0
2
Runtime stackx
n
tmp
![Page 39: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/39.jpg)
39
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
Stackn = 0, x = 2
tmp = 4
4
0
2
Runtime stackx
n
tmp
![Page 40: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/40.jpg)
40
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} }}
Stackn = 0, x = 2
tmp = 32
32
0
2
Runtime stackx
n
tmp
![Page 41: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/41.jpg)
41
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;}
![Page 42: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/42.jpg)
42
DisadvantagesDisadvantages• May run slower.
– Compilers– Inefficient Code
• May use more space.
AdvantagesAdvantages• More natural.
• Easier to prove correct.
• Easier to analysis.
• More flexible.
![Page 43: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/43.jpg)
43
Free List – Non RecursiveFree List – Non RecursiveheadPtr
/* Delete the entire list */void FreeList(Node* headPtr){ Node* nodePtr;
while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; }}
0x2000 0x4c680x258a
0x2000
Runtime stack
headPtr
nodePtr
![Page 44: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/44.jpg)
44
Free List – Non RecursiveFree List – Non RecursivenodePtr
0x2000 0x258a 0x4c68
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ Node* nodePtr;
while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; }}
0x258a
0x2000
Runtime stack
headPtr
nodePtr
![Page 45: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/45.jpg)
45
Free List – Non RecursiveFree List – Non RecursivenodePtr
0x258a 0x4c68
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ Node* nodePtr;
while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; }}
0x4c68
0x258a
Runtime stack
headPtr
nodePtr
![Page 46: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/46.jpg)
46
Free List – Non RecursiveFree List – Non RecursivenodePtr
NULL
0x4c68
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ Node* nodePtr;
while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; }}
NULL
0x4c68
Runtime stack
headPtr
nodePtr
![Page 47: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/47.jpg)
47
Free List – Non RecursiveFree List – Non RecursivenodePtr
NULL
Has local variables on the stack. This is performing two assignments and one comparison per iteration.
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ Node* nodePtr;
while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; }}
NULL
NULL
Runtime stack
headPtr
nodePtr
![Page 48: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/48.jpg)
48
Free ListFree List
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
0x258a 0x4c680x2000
Runtime stack
0x2000
headPtr
![Page 49: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/49.jpg)
49
Free ListFree List
0x258a 0x4c680x2000
0x258a
0x2000
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
Runtime stack
![Page 50: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/50.jpg)
50
Free ListFree List
0x258a 0x4c680x2000
0x4c68
0x258a
0x2000
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
Runtime stack
![Page 51: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/51.jpg)
51
Free ListFree List
0x258a 0x4c680x2000
NULL
0x4c68
0x258a
0x2000
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
Runtime stack
![Page 52: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/52.jpg)
52
Free ListFree List
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
0x258a 0x4c680x2000
0x4c68
0x258a
0x2000
headPtr
Runtime stack
![Page 53: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/53.jpg)
53
Free ListFree List
0x258a0x2000
0x258a
0x2000
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
Runtime stack
![Page 54: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/54.jpg)
54
Free ListFree List
0x2000
0x2000
headPtr
/* Delete the entire list */void FreeList(Node* headPtr){ if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr);}
Has no local variables on the stack. This is performing one assignment and one comparison per function call.
Runtime stack
![Page 55: CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d485503460f94a24933/html5/thumbnails/55.jpg)
55
RevisionRevision• Recursion
Revision: ReadingRevision: Reading• Kruse 3.2 - 3.4• Standish 3, 14• Langsam 3
PreparationPreparationNext lecture: Binary Trees• Read Chapter 9 in Kruse et al.