Welcome To CIS 068 !: Stacks and Recursion
Welcome To CIS 068 !: Stacks and Recursion
CIS 068
Overview
Subjects:
Stacks
Structure
Methods
Recursion
Principle
Recursion and Stacks
Recursion vs. Iteration
Examples
CIS 068
Stacks: Introduction
What do these tools have in common ?
Plate Dispenser
PEZ Dispenser
CIS 068
Stack: Properties
Answer:
They both provide LIFO (last in first out)
Structures
6
5
4
3
2
1
6
5
4
3
2
1
CIS 068
Stacks: Properties
Possible actions:
PUSH an object (e.g. a plate) onto dispenser
POP object out of dispenser
CIS 068
Stacks: Definition
Stacks are LIFO structures, providing
Add Item (=PUSH) Methods
Remove Item (=POP) Methods
Stacks
Application Areas
LIFO order is desired
See JVM-example on next slides
CIS 068
Stacks
Q:
How would you implement a Stack ?
CIS 068
int a = 3;
int b = timesFive(a);
System.out.println(b+);
5 }
int b = 5;
int c = a * b;
return (c);
10 }
CIS 068
CIS 068
int a = 3;
int b = timesFive(a);
System.out.println(b+);
int b = 5;
int c = a * b;
return (c);
10
b=5
a=3
Return to LINE 3
b
a=3
CIS 068
Temporary storage
15
return (c);
10
...
Return to LINE 3
Return to LINE 3
c = 15
b=5
a=3
Return to LINE 3
c = 15
Clear Stack
a=3
a=3
a=3
CIS 068
int a = 3;
int b = timesFive(a);
System.out.println(b+);
c = 15
b
b = 15
a=3
a=3
Temporary storage
CIS 068
int a = 3;
int b = timesFive(a);
System.out.println(b+);
CIS 068
CIS 068
Finding Palindromes
Bracket Parsing
RPN
RECURSION !
CIS 068
Recursion
CIS 068
Recursion
CIS 068
Recursion
When you turn that into a program, you end
up with functions that call themselves:
Recursive Functions
CIS 068
Recursion
Whats behind this function ?
public int f(int a){
if (a==1)
return(1);
else
return(a * f( a-1));
}
It computes f! (factorial)
CIS 068
Factorial
Factorial:
a! = 1 * 2 * 3 * ... * (a-1) * a
Note:
a! = a * (a-1)!
remember:
...splitting up the problem into a smaller problem of the same type...
a!
(a-1)!
CIS 068
CIS 068
a=5
Initial
a=4
Return to L4
a=5
After 1 recursion
a=1
Return to L4
a=2
Return to L4
a=3
Return to L4
a=4
Return to L4
a=5
After 4th recursion
a = 2*1 = 2
Return to L4
a=3
Return to L4
a=4
Return to L4
a=5
a = 3*2 = 6
Return to L4
a=4
Return to L4
a=5
a = 4*6 = 24
Return to L4
a=5
a = 5*24 = 120
Result
CIS 068
CIS 068
Solution
The recursive algorithms we write generally consist of an if statement:
IF
the stopping case is reached solve it
ELSE
split the problem into simpler cases using recursion
Solution on stack
Solution on stack
Solution on stack
CIS 068
CIS 068
Exercise
Define a recursive solution for the following function:
f(x) = x
CIS 068
CIS 068
CIS 068
CIS 068
Review
A stack is a simple LIFO datastructure used e.g.
by the JVM to create local variable spaces
Recursion is a divide and conquer designing
technique that often provides a simple algorithmic
structure
Recursion can be replaced by iteration for reasons
of efficiency
There is a connection between recursion and the
use of stacks
There are interesting problems out there that can
be solved by recursion
CIS 068