Module 01 Stack and Recursion

25
See through C Module 1 Stack and Recursion Tushar B Kute http://tusharkute.com

description

The presentation given at PCCOE, Nigdi, Pune by Prof. Tushar B Kute in workshop on "See through C". http://tusharkute.com

Transcript of Module 01 Stack and Recursion

Page 1: Module 01 Stack and Recursion

See through C

Module 1

Stack and Recursion

Tushar B Kutehttp://tusharkute.com

Page 2: Module 01 Stack and Recursion

Overview

Subjects:

• Stacks– Structure

– Methods

– Stacks and Method – Calls

• Recursion– Principle

– Recursion and Stacks

– Examples

Page 3: Module 01 Stack and Recursion

Stack: PropertiesAnswer:

They both provide LIFO (last in first out) Structures

12345

6

123456

Page 4: Module 01 Stack and Recursion

Stacks: PropertiesPossible actions:

• PUSH an object (e.g. a plate) onto dispenser

• POP object out of dispenser

Page 5: Module 01 Stack and Recursion

Stacks: DefinitionStacks are LIFO structures, providing

• Add Item (=PUSH) Methods

• Remove Item (=POP) Methods

They are a simple way to build a collection

• No indexing necessary

• Size of collection must not be predefined

• But: extremely reduced accessibility

Page 6: Module 01 Stack and Recursion

A look into the JVM1 int main( ) {

2 int a = 3;

3 int b = times(a);

4 printf(“%d“, b);

5 }

6 int times(int a) {

7 int b = 5;

8 int c = a * b;

9 return (c);

10 }a = 3

a = 3

b

Return to LINE 3

b = 5

c = 15

Page 7: Module 01 Stack and Recursion

A look into the compiler...

9 return (c);

10 ...

a = 3

a = 3

b

Return to LINE 3

b = 5

c = 15

15

a = 3

b

Return to LINE 3

a = 3

b

c = 15

Return to LINE 3

Temporary storage

Clear Stack

Page 8: Module 01 Stack and Recursion

A look into the CompilerA look into the 1 int main() {

2 int a = 3;

3 int b = times(a);

4 printf(“%d“, b);

5 }

a = 3

b

c = 15

Temporary storage

a = 3

b = 15

Page 9: Module 01 Stack and Recursion

A look into the CompilerA look into the 1 int main() {

2 int a = 3;

3 int b = times(a);

4 printf(“%d“, b);

5 }

clear stack from local variables

Page 10: Module 01 Stack and Recursion

A look into the CompilerA look into the Important:

Every call to a function creates a new set of local variables !

These Variables are created on the stack and deleted when the function returns

Page 11: Module 01 Stack and Recursion

Applications using a Stack

Examples:

• Finding Palindromes

• Bracket Parsing

• RPN

• RECURSION !

Page 12: Module 01 Stack and Recursion

RecursionRecursion

Page 13: Module 01 Stack and Recursion

RecursionRecursion

• Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first

• Recursion is a technique that solves a problem by solving a smaller problem of the same type

• A function that is defined in terms of itself

Page 14: Module 01 Stack and Recursion

RecursionRecursion

When you turn that into a program, you end up with functions that call themselves:

Recursive Functions

Page 15: Module 01 Stack and Recursion

RecursionRecursion

int fact (int a){

if (a==1) return(1);else return (a * fact( a-1));

}

It computes f! (factorial)

What’s behind this function ?

Page 16: Module 01 Stack and Recursion

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 * (a-1)!

Factorial

Page 17: Module 01 Stack and Recursion

int factorial(int a){if (a==0) return(1);else return(a * factorial( a-1));

}

Tracing the example

Page 18: Module 01 Stack and Recursion

int factorial (int a){if (a==1) return(1);else return(a * factorial( a-1));

}

Watching the Stack

a = 5 a = 5 a = 5Return to L4

a = 4Return to L4

a = 4Return to L4

a = 3Return to L4

a = 2Return to L4

a = 1

Initial After 1 recursion After 4th recursion

Every call to the method creates a new set of local variables !

Page 19: Module 01 Stack and Recursion

int factorial(int a){if (a==1) return(1);else return(a * factorial( a-1));

}

Watching the Stack

a = 5Return to L4

a = 4Return to L4

a = 3Return to L4

a = 2Return to L4

a = 1

After 4th recursion

a = 5Return to L4

a = 4Return to L4

a = 3Return to L4a = 2*1 = 2

a = 5Return to L4

a = 4Return to L4a = 3*2 = 6

a = 5Return to L4a = 4*6 = 24

a = 5*24 = 120

Result

Page 20: Module 01 Stack and Recursion

Problems that can be solved by recursion have these characteristics:

• One or more stopping cases have a simple, nonrecursive solution

• The other cases of the problem can be reduced (using recursion) to problems that are closer to stopping cases

• Eventually the problem can be reduced to only stopping cases, which are relatively easy to solve

Follow these steps to solve a recursive problem:

• Try to express the problem as a simpler version of itself

• Determine the stopping cases

• Determine the recursive steps

Properties of Recursive Functions

Page 21: Module 01 Stack and Recursion

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

Solution on stackSolution on stack

Solution on stack

Page 22: Module 01 Stack and Recursion

Recursion does not terminate properly: Stack Overflow !

Common Programming Error

Page 23: Module 01 Stack and Recursion

Examples: Fractal Tree

http://id.mind.net/~zona/mmts/geometrySection/fractals/tree/treeFractal.html

Page 24: Module 01 Stack and Recursion

Examples: The 8 Queens Problem

http://mossie.cs.und.ac.za/~murrellh/javademos/queens/queens.html

Eight queens are to be placed on a chess board

in such a way that no queen checks against

any other queen

Page 25: Module 01 Stack and Recursion

Thank you

This presentation is created using LibreOffice Impress 3.6.2.2