RECURSION Flashcards

(23 cards)

1
Q

Can recursion be used in tree traversals

A

Yessssssss

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Def recursion

A

When an algorithm calls itself from within the function, until it satisfies the base case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

examples of where recursion can be used

A

factorials
fibonaccccci
sorting algos

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

3 essential characteristics of recursion

A

Must have base case - when met the algorithm will stop calling itself and will start to ‘unwind’

When base case not satisfied, func must call itself

Base case must be reached after finite number of calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is infinite num of calls in recursion bad

A

Program will never run

Items will forever be added onto the call stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

recursive vs iterative - stack overflow

A

recursive can cause it

iterative cant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is faster - iterative or recursive

A

iterative for less items

recursion for more

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is iterative faster for less items

A

Dont have to read and write from call stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

iterative vs recursive - which is easier to follow

A

iterative

recursives hard to trace through

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which has less lines of code usually - iterative or recursive

A

recursive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When the recursive routine unwinds, how are values outputted

A

FILO manner

values returned reverse in order they were first calculated

eg even number 1-10:
returned as: 10, 8 ,6 ,4 ,2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why are recursive routine outputs in FILO manner

A

stack operates in FILO manner

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Converting iteration to recursion

A

make function call itself

pass values w/ parameter

set base case

check any other commands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

use of the call stack in recursion

A

everytime subroutine is called, return address, local variables and arguments are put into call stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

recursion vs iteration - termination

A

recursion - defined in recursive function by base case

iteration - defined in loops definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

recursion vs iteration - size of cdoe

A

recursion - less

iteration - more

17
Q

recursion vs iteration - the stack

A

recursion - requires it
iteration - doesnt

18
Q

recursion vs iteration - usability

LEAVE

A

reursion - when size of code small and wont take long

iterative - balance speed with code size

19
Q

recursion vs iteration - memory

A

rec - more mem

iter - less mem

20
Q

recursion vs iteration - overhead

LEAVE

A

rec - has extended overhead

iter - none

21
Q

Why do recursion require more mem

A

accumulation of function calls on call stack
- need to store all the local varialbes
- need to store return address
- need to store parameters

22
Q

converting recursion to iteration

A

returns a value - doesnt call itself
change base case to while loop
put commands in the loop
review parameters

23
Q

why should the recursive algo not take a very long time before base case satisfied

A

even w/ stop condition, recursive routine can only be called few times else stack overflow