Tutorial Exercises Week 03

Exercise 1

Function Growth Rates

Calculate how long T(n) steps would take for different sizes of n for the T(n) functions in the table below. Assume you are running it on a computer that performs one million steps per millisecond. Note: A millisecond is a thousandth of a second.

nT(n) = log nT(n) = nT(n) = n log nT(n) = n2T(n) = n3T(n) = 2n
100.003 microsec 0.01 microsec 0.03 microsec 0.1 microsec 0.001ms0.001ms
200.004 microsec 0.02 microsec 0.09microsec 0.4 microsec0.008ms 1ms
500.006 microsec 0.05 microsec 0.28 microsec 0.0025ms 0.125ms 13 days
1000.007 microsec 0.1 microsec 0.67 microsec 0.01ms 1ms 4x1013 years
10000.01 microsec 0.001 ms 0.01 ms 1ms 1 secondvery long time
100000.013 microsec 0.01 ms 0.13 ms 100ms 17 minutes very very long time

For what size of n does the computation time for T(n) = 2n become too large to be practical? Would it help if we used a computer that was a million times faster?

Solution

when n >= 50, the computation time for T(n) = 2n has started to become too large to be practical. At n >= 100 it would be impossible to wait for it to finish - unless you are blessed with the gift of eternal life.

Even if we were to increase the speed of the machine by a million, 2n for n = 100, it would still take 40,000,000 years.

Exercise 2

Write a recursive function

int allEven (int a[], int l, int r);

which takes an array, the left-most and right-most index of the current segment of the array as an argument and checks if all elements in an array are even.

It must use a divide and conquer approach, by splitting the array in half, first checking if all the elements in the left half are even, and then (only if necessary) checking the right half.

int allEven(int a[], int l, int r) {
    int mid, first;
    if(r < l) return 1;
    if (l == r){
        return (a[l] % 2 == 0);
    }else{
        mid = (l + r)/2;
        first = allEven(a, l, mid);
        if(first){
           return allEven(a, mid + 1, r);
        } else {
           return 0;
        }
    }
}

What would the worst-case time complexity be in Big O notation?

In the worst case - that they are all even, all elements in the array must be checked so the time complexity ends up being O(n).

Exercise 3

Binary Search Tree Insertion, Deletion and Traversal

Insert the following keys into a BST:  10 20 5 30 15 25 24

What is the height of the resulting tree?

Delete 5 30 20 (assume we replace nodes with the left-most of the right sub-tree when necessary)

What is the height of the resulting tree?

Show the output obtained by traversing the tree and printing out each node in the following orders:

Solution

 10
 / \
5  20
   / \
  /   \
 15   30
      /
     25
    /
   24

Height: 4

Delete 5 30 20

What is the height of the resulting tree?

Solution


After deleting 5

10 
  \ 
  20
  / \
 /   \
15   30
     / 
    25 
   / 
  24 

After deleting 30
10
  \
  20
  / \
 /   \
15   25
     /
    24

After deleting 20
       
10   
  \  
  24
  / \
 /   \
15   25
   
Height: 2

Solution Traversals of the final tree

Exercise 4

BST Functions

Assume the following representation of a BST

typedef struct treenode *treelink;

struct treenode{
    Item item;
    treelink left;
    treelink right;
}