Bugle Recursion - Lecture Slides | CS 111, Study notes of Computer Science

Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-46z
koofers-user-46z 🇺🇸

10 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
There and back again
Buggle recursion
Friday, October 5, 2007
Recursion 10-2
Recursion
oRecursion solves a problem
by solving a smaller
instance of the same
problem.
oThink divide, conquer, and
glue when all the
subproblems have the
same “shape” as the
original problem.
oA recursive method is a
method that invokes itself
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Bugle Recursion - Lecture Slides | CS 111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

There and back again

Buggle recursion

Friday, October 5, 2007

Recursion 10-

Recursion

o Recursion solves a problem

by solving a smaller

instance of the same

problem.

o Think divide, conquer, and

glue when all the

subproblems have the

same “shape” as the

original problem.

o A recursive method is a

method that invokes itself

Recursion 10-

bagelForward(3)

Write a method that teaches a buggle to leave behind a trail

of bagels of a given length. Assume brushUp().

Recursion 10-

We could solve …

bagelForward(3); by

dropBagel();

forward(); and solving …

bagelForward(2); by

dropBagel();

forward(); and solving …

bagelForward(1); by

dropBagel();

forward(); and solving …

bagelForward(0); by

Doing nothing at all

Recursion 10-

BagelWorld

BagelBuggle betty = new BagelBuggle(); betty.brushUp(); betty.bagelForward(3);

Object Land

Execution

Land

.run()

JEM demonstrating bagelForward method

this

BW

BW

BW

Recursion 10-

BagelWorld

BagelBuggle betty = ; betty.brushUp(); betty.bagelForward(3);

Object Land

Execution

Land

.run()

A buggle named betty is constructed

this

BW

BW

BW

betty BB

color red

brushDown true

BagelBuggle BB

heading EAST

position (1,1)

BB

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); betty.bagelForward(3);

Object Land

Execution

Land

.run()

betty raises her brush

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (1,1)

BB

BB

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

The message bagelForward(3) is sent to betty

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (1,1)

BB

BB

BB

.bagelForward(3)

this

if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 3

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Drop a bagel and move forward

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (2,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 3

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Evaluate the method’s argument

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (2,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Execute the method bagelForward(2)

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (2,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 2

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Evaluate the boolean expression and execute the else clause

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (2,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 2

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Execute the method bagelForward(1)

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (3,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

.bagelForward(1)

this

if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Evaluate the boolean expression and execute the else clause

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (3,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

.bagelForward(1)

this

if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Drop a bagel and move forward

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

.bagelForward(1)

this

if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

Evaluate the method’s argument

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

.bagelForward(1)

this

if (false) { } else { dropBagel(); forward(); bagelForward(0); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

The bagelForward(0) execution frame finishes

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

.bagelForward(1)

this

if (false) { } else { dropBagel(); forward(); bagelForward(0); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

The bagelForward(1) execution frame finishes

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

.bagelForward(2)

this

if (false) { } else { dropBagel(); forward(); bagelForward(1); }

BB

BB1 n 2

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

The bagelForward(2) execution frame finishes

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

.bagelForward(3)

this

if (false) { } else { dropBagel(); forward(); bagelForward(2); }

BB

BB1 n 3

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelForward(3);

Object Land

Execution

Land

.run()

The bagelForward(3) execution frame finishes

this

BW

BW

BW

betty BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

BB

BB

BB

Recursion 10-

bagelLine(int n)

public void bagelLine(int n) {

if (n == 0) {

// do nothing

} else {

dropBagel();

forward();

bagelLine(n-1);

backward();

Recursion 10-

BagelWorld

BagelBuggle betty = new BagelBuggle(); betty.brushUp(); betty.bagelLine(3);

Object Land

Execution

Land

.run()

JEM demonstrating bagelLine method

this

BW

BW

BW

Recursion 10-

BagelWorld

BagelBuggle betty = ; betty.brushUp(); betty.bagelLine(3);

Object Land

Execution

Land

.run()

A buggle named betty is constructed

this

BW

BW

BW

betty BB

BB

color red

brushDown true

BagelBuggle BB

heading EAST

position (1,1)

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); betty.bagelLine(3);

Object Land

Execution

Land

.run()

betty raises her brush

this

BW

BW

BW

betty BB

BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (1,1)

BB

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelLine(3);

Object Land

Execution

Land

.run()

Evaluate the boolean expression, drop a bagel, move forward, and

execute bagelLine(1)

this

BW

BW

BW

betty BB

BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (3,1)

.bagelLine(3)

this

if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }

BB

BB1 n 3

BB

BB

.bagelLine(2)

this

if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }

BB

BB1 n 2

.bagelLine(1)

this

if (n==0) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }

BB

BB1 n 1

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelLine(3);

Object Land

Execution

Land

.run()

Evaluate the boolean expression, drop a bagel, move forward, and

execute bagelLine(0)

this

BW

BW

BW

betty BB

BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

.bagelLine(3)

this

if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }

BB

BB1 n 3

BB

BB

.bagelLine(2)

this

if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }

BB

BB1 n 2

.bagelLine(1)

this

if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }

BB

BB1 n 1

.bagelLine(0)

this

if (n==0) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }

BB

BB1 n 0

Recursion 10-

if (true) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelLine(3);

Object Land

Execution

Land

.run()

Evaluate the boolean expression and execute the then clause

this

BW

BW

BW

betty BB

BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

.bagelLine(3)

this

if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }

BB

BB1 n 3

BB

BB

.bagelLine(2)

this

if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }

BB

BB1 n 2

.bagelLine(1)

this

if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }

BB

BB1 n 1

.bagelLine(0)

this

BB

BB1 n 0

Recursion 10-

BagelWorld

BagelBuggle betty = ; .brushUp(); .bagelLine(3);

Object Land

Execution

Land

.run()

The bagelLine(0) execution frame finishes

this

BW

BW

BW

betty BB

BB

color red

brushDown false

BagelBuggle BB

heading EAST

position (4,1)

.bagelLine(3)

this

if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }

BB

BB1 n 3

BB

BB

.bagelLine(2)

this

if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }

BB

BB1 n 2

.bagelLine(1)

this

if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }

BB

BB1 n 1