Lecture 19 Flashcards

1
Q

One classic application for recursion is for use in

A

sorting

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

Merge Sort

A

merge two pre-sorted arrays.
–If we have two presorted arrays, then the smallest overall element must be in the first slot of one of the two arrays.
–It dramatically reduces the effort needed to find each element in its proper order.

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

Binary tree code

A
template 
class TreeNode
{
public:
K key;
V value;
TreeNode* left;
TreeNode* right;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Binary tree print

A
void print(TreeNode* node)
{
if(node == 0) return;
print(node.left);
cout << node.value << “ ”;
print(node.right);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Code for using recursion with factorials, but stopping at a certain number.

A
int factorial(int i, int n)
{
if(i >= n)
return n;
else return i * factorial(i+1, n);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

factorial helper method

A
int factorialStarter(int n)
{
if(n < 0) throw exception();
else if(n==0) return 1;
else return factorial(1, n);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a helper method

A

It’s so named because its sole reason to
exist is to help setup the needed
parameters and such for the true,
underlying method.

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

A tail-recursive method is one in which

A

all of the computation is done during the initial method calls.

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

factorial tail recursion

A
int factorial(int part, int n)
{
if(n == 0 || n == 1)
return part;
else
return factorial(part * n, n-1);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly