chapter 2 recursion: the mirrors cs 302 - data structures mehmet h gunes modified from authors’...

74
Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides

Upload: miles-douglas

Post on 12-Jan-2016

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Chapter 2

Recursion: The Mirrors

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

Page 2: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Contents

• Recursive Solutions• Recursion That Returns a Value• Recursion That Performs an Action• Recursion with Arrays• Organizing Data• More Examples• Recursion and Efficiency

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 3: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Solutions

• Recursion breaks a problem into smaller identical problems

• Some recursive solutions are inefficient, impractical

• Complex problems can have simple recursive solutions

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 4: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Solutions

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 5: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

What Is Recursion?• Recursive call: A method call in which the

method being called is the same as the one making the call

• Direct recursion: Recursion in which a method directly calls itself

• Indirect recursion: Recursion in which a chain of two or more method calls returns to the method that originated the chain

Page 6: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursion

• You must be careful when using recursion.• Recursive solutions are typically less efficient

than iterative solutions.• Still, many problems lend themselves to

simple, elegant, recursive solutions.• We must avoid making an infinite sequence of

function calls– infinite recursion

Avoid them !!!

Page 7: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Solutions

• A recursive solution calls itself• Each recursive call solves an identical, smaller

problem• Test for base case enables recursive calls to

stop• Eventually one of smaller calls will be base

case

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 8: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

General format for many recursive functions

if (some condition for which answer is known) // base case

solution statement else // general case

recursive function call

• Each successive recursive call should bring you closer to a situation in which the answer is known.

• Each recursive algorithm must have at least one base case, as well as the general (recursive) case

Page 9: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Query

Page 10: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Query

Page 11: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursive Solution

Page 12: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Valued Function

The factorial of n

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 13: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Valued Functionfact(3)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 14: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Box Trace

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The beginning of the box trace

Page 15: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Box Trace

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 16: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Box Trace

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 17: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Box Trace

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 18: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 19: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The function writeBackwards

Page 20: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")

Page 21: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")

Page 22: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Box trace of writeBackward("cat")

Page 23: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")in pseudocode

Page 24: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013`

Box trace of writeBackward("cat")in pseudocode

Page 25: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")in pseudocode

Page 26: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")in pseudocode

Page 27: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")in pseudocode

Page 28: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward("cat")in pseudocode

Page 29: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward2("cat")in pseudocode

Page 30: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward2("cat")in pseudocode

Page 31: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward2("cat")in pseudocode

Page 32: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward2("cat")in pseudocode

Page 33: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

A Recursive Void Function

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace of writeBackward2("cat")in pseudocode

Page 34: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Writing an Array’s Entries in Backward Order

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pseudocode

Page 35: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Writing an Array’s Entries in Backward Order

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Source code

Page 36: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RevPrint(listData);

A B C D E

FIRST, print out this section of list, backwards

THEN, print this element

listData

Page 37: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Using recursion with a linked listvoid RevPrint ( NodeType* listPtr )

/** Reverse print a linked list

@Pre listPtr points to an element of a list.

@Post all elements of list pointed to by listPtr

have been printed out in reverse order. **/

{

if ( listPtr != NULL ) // general case

{

RevPrint ( listPtr-> next ); // process the rest

std::cout << listPtr->info << std::endl;

// print this element

}

// Base case : if the list is empty, do nothing

}

Page 38: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Binary Search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A high-level binary search for the array problem

Page 39: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Binary Search

• Issues to consider1.How to pass a half array to recursive call2.How to determine which half of array has

target value3.What is the base case?4.How will result of binary search be indicated?

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 40: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Binary Search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box traces of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>: (a) a successful search for 9

Page 41: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Binary Search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box traces of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>: (b) an unsuccessful search for 6

Page 42: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Binary Search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Box trace with a reference argument

Page 43: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Finding the Largest Value in an Array

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Recursive solution to the largest-value problem

Page 44: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Finding the Largest Value in an Array

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The recursive calls that maxArray(<1,6,8,3>)generates

Page 45: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Finding the kth Smallest Value of an Array

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The recursive solution proceeds by:1.Selecting a pivot value in array2.Cleverly arranging/partitioning, values in array about this pivot value3.Recursively applying strategy to one of partitions

Page 46: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Finding the kth Smallest Value of an Array

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A partition about a pivot

Page 47: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Finding the kth Smallest Value of an Array

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

High level pseudo code solution

Page 48: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Organizing Towers of Hanoi

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

(a) the initial state; (b) move n – 1 disks from A to C;

Page 49: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Towers of Hanoi

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

(c) move 1 disk from A to B; (d) move n – 1 disks from C to B

Page 50: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Towers of Hanoi

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pseudocode solution

Page 51: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Towers of Hanoi

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The order of recursive calls that results from solve Towers(3, A, B, C)

Page 52: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Towers of Hanoi

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Source code for solveTowers

Page 53: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Multiplying Rabbits

Assumed “facts” about rabbits:•Rabbits never die.•A rabbit reaches maturity exactly two months after birth•Rabbits always born in male-female pairs. •At the beginning of every month, each mature male-female pair gives birth to exactly one male-female pair.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 54: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Multiplying Rabbits

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 55: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Multiplying Rabbits (The Fibonacci Sequence)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

to find the next number in the sequence, add together the previous two numbers

Page 56: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Fibonacci Sequence (Multiplying Rabbits)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A C++ function to compute rabbit(n)

Page 57: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

The Fibonacci Sequence (Multiplying Rabbits)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The recursive calls that rabbit(7) generates

Page 58: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

An example where recursion comes naturally

Combinations• how many combinations of a certain size can

be made out of a total group of elements

Page 59: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Combinations(4, 3)

Page 60: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Choosing k Out of n Things

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Recursive function:

Page 61: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Choosing k Out of n Things

The recursive calls that g (4, 2) generatesData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 62: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Stack Activation Frames• The activation record stores

• the return address for this function call, • the parameters, • local variables, and • the function’s return value, if non-void.

• The activation record for a particular function call is popped off the run-time stack when • the final closing brace in the function code is reached, or • when a return statement is reached in the function code.

• At this time the function’s return value, if non-void, is brought back to the calling block return address for use there.

Page 63: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

// Another recursive function int Func ( int a, int b )

// Pre: a and b have been assigned values// Post: Function value = ??

{ int result;

if ( b == 0 ) // base caseresult = 0;

else { if ( b > 0 ) // first general

caseresult = a + Func ( a , b - 1 ) ); // instruction

50

else // second general caseresult = Func ( - a , - b ); // instruction

70 } return result;}

What operation does Func(a, b) simulate?

Page 64: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL ? result ? b 2 a 5Return Address 100

x = Func(5, 2); // original call is instruction 100

original call at instruction 100 pushes on this record for Func(5,2)

Run-Time Stack Activation Records

Page 65: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL ? result ? b 1 a 5Return Address 50

FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100

record for Func(5,2)

call in Func(5,2) codeat instruction 50 pushes on this recordfor Func(5,1)

x = Func(5, 2); // original call at instruction 100

Run-Time Stack Activation Records

Page 66: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL ? result ? b 0 a 5Return Address 50

FCTVAL ? result 5+Func(5,0) = ? b 1 a 5Return Address 50

FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100

record for Func(5,2)

record for Func(5,1)

call in Func(5,1) codeat instruction 50pushes on this record for Func(5,0)

Run-Time Stack Activation Records

Page 67: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL 0 result 0 b 0 a 5Return Address 50

FCTVAL ? result 5+Func(5,0) = ? b 1 a 5Return Address 50

FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100

record for Func(5,2)

record for Func(5,1)

record for Func(5,0)is popped first with its FCTVAL

Run-Time Stack Activation Records

Page 68: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5Return Address 50

FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100

record for Func(5,2)

record for Func(5,1)is popped nextwith its FCTVAL

Run-Time Stack Activation Records

Page 69: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5Return Address 100

x = Func(5, 2); // original call at line 100

record for Func(5,2)is popped lastwith its FCTVAL

Run-Time Stack Activation Records

Page 70: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Tail Recursion

• The case in which a function contains only a single recursive call and it is the last statement to be executed in the function.

• Tail recursion can be replaced by iteration to remove recursion from the solution as in the next example.

Page 71: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

// USES TAIL RECURSION

bool ValueInList ( ListType list , int value , int startIndex )

/** Searches list for value between positions startIndex

and list.length-1

@Pre list.info[ startIndex ] . . list.info[ list.length - 1 ]

contain values to be searched

@Post Function value = ( value exists in list.info[ startIndex ]

. . list.info[ list.length - 1 ] ) **/

{

if ( list.info[startIndex] == value ) // one base case

return true;

else

{

if (startIndex == list.length -1 ) // another base case

return false;

else

return ValueInList( list, value, startIndex + 1 );

}

}

Page 72: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

// ITERATIVE SOLUTION

bool ValueInList ( ListType list , int value , int startIndex )

/** Searches list for value between positions startIndex and list.length-1 @Pre list.info[ startIndex ] . . list.info[ list.length - 1 ] contain values to be searched @Post Function value = ( value exists in list.info[ startIndex ]

. . list.info[ list.length - 1 ] ){ bool found = false; while ( !found && startIndex < list.length )

{ if ( value == list.info[ startIndex ] ) found = true;

else startIndex++;

}return found;

}

Page 73: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Recursion and Efficiency

• Inefficiency factors– Overhead associated with function calls– Inherent inefficiency of some recursive algorithms

• Principle:– Do not use recursive solution if inefficient and

clear, efficient iterative solution exists

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 74: Chapter 2 Recursion: The Mirrors CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Use a recursive solution when:• The depth of recursive calls is relatively “shallow”

compared to the size of the problem

• The recursive version does less amount of work than the nonrecursive version

• The recursive version is [much] shorter and simpler than the nonrecursive solution

SHALLOW DEPTH EFFICIENCY CLARITY