Fundamental Algorithms: Problem 1 (10 Points)
Fundamental Algorithms: Problem 1 (10 Points)
Fundamental Algorithms
A binary tree is f ull if all of its vertices have either zero or two children. Let Bn denote
the number of full binary trees with n vertices.
1. By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact
values of B3 , B5 , and B7 . Why have we left out even numbers of vertices, like B4 ?
Solution
1. By drawing out all full binary trees with 3, 5, or 7 nodes, determine the exact values
of B3 , B5 , and B7 . Why have we left out even numbers of vertices, like B4 ?
The figure shows all the full binary trees with 3, 5 or 7 nodes. The the number of
trees are 1, 2 and 5 respectively.
There are no even number of nodes because, a tree with even number of nodes
cannot be a full tree.
B3 = 1
x x
B5 = 2
x x
x x x x
x x x x
B7 = 5
x x x x x
x x x x x x x x x x
x x x x x x x x x x x x
x x x x x x x x
2 Bn−2 + Bn−4 B3 + . . . + B n B n if n = 4k + 1
⌈ 2 ⌉ ⌊ 2 ⌋−1
Bn =
2 Bn−2 + Bn−4 B3 + . . . + B n B n − B n B n if n = 4k + 3
⌊ ⌋ ⌊ ⌋ ⌊ ⌋ ⌊ ⌋ 2 2 2 2
Review all the sort algorithms taken in the class. Compare their complexities. If possible,
try to explain them with day-to-day examples.
2
Solution
Sort Average Best Worst Remarks
Bubble sort n2 n2 n2
Selection sort n2 n2 n2
Insertion sort n2 n n2 In best case, insert requires constant time
Merge sort n lg n n lg n n lg n
Heap sort n lg n n lg n n lg n
Quick sort n lg n n lg n n2
Proof:
For an input of size n, the decision tree has n! leaves. Which leaves the tree with a height
h ≥ lg(n!)
h ≥ lg(n!)
n
n 2
≥ lg
2
n
= (lg(n) − 1)
2 n
≥ lg n
4
Problem 3
2. How can one simulate a queue with two stacks! (no counting)
Solution
1. Stack
3
{
if (top < STACKSIZE)
stack[top++] = data;
else
printf("Stack Full");
}
int pop()
{
if(top != 0)
return stack[--top];
else
print("Stack Empty");
return -1;
}
int del()
{
while possible to pop from Stack1
{
Stack2.push(Stack1.pop());
}
return Stack2.pop();
while possible to pop from Stack2
{
Stack1.push(Stack2.pop());
}
}
3. Circular Queue
A circular queue is a queue which has a maximum capacity at a givem point of time.
It acts as if its head and tail are connected.
It is usually implemented with a normal array. Once the head/tail reaches the end
of the array, the count starts again from the beginning.
4
Problem 4
Solution
1. insert(data)
3. delte(data)
5
{
free(tree);
return ;
}
else
{
if(isleaf(tree))
{
if(vater->left == tree)
vater->left = NULL;
else // if (vater->right == tree)
vater->right = NULL;
return;
}
// if tree has only one child, we can replace tree by it’s kid.
if((onlykid = single_kid(tree)) != NULL)
{
if(vater->left == tree)
vater->left = onlykid;
else // if (vater->right == tree)
vater->right = onlykid;
return;
}
}
// not a leaf, nor the father of only one child -
// hence replace tree with leftmost child of right child or
// rightmost child of left child
// random == 1 --> left child’s rightmost child and
// random == 2 --> right child’s leftmost child
The number of recursive calls is the same as the number of iterations in the iterative
loops. Hence the complexities of both the methods are the same. And it is O(lg n), where
n is the number of nodes in the tree.