Name: ________________________________
Honor Code Acknowledgment: ___________________
Random Quiz #9
CPS 100, Spring 1997
Due: April 2
Problem 1: Tight Rope (4 points)
The function IsHeightBalanced below
determines whether a tree is height-balanced. A tree
is height-balanced if, for all nodes, the left and
right subtrees differ in height by no more than one.
int Height(TNode * tree)
{
if (tree == 0) return -1;
else
{
int leftHeight = Height(tree->left);
int rightHeight = Height(tree->right);
return max(leftHeight, rightHeight) + 1;
}
}
bool IsHeightBalanced(TNode * tree)
// postcondition: returns true if tree is balanced, else returns false
{
if (tree != 0)
{
bool leftBalanced = IsHeightBalanced(tree->left);
bool rightBalanced = IsHeightBalanced(tree->right);
bool heightOk = abs(Height(tree->left) - Height(tree->right)) <= 1);
return leftBalanced && rightBalanced && heightOk;
}
return true;
}
The complexity of this function is greater than O(n). What is
the complexity and why?
Problem 2: Prime of Life (2 points)
A postive integer n
is a prime number if its only divisors are 1
and n. There are infinitely many prime numbers. Using big-Oh
notation, give any correct bound on the number of primes less than a
positive integer p.
Problem 3: ginorst stuff (0 points)
These problems are from a previous tests. The answers can be found
via the class web page. You are urged to try these to see if you
understand sorting and other topics we've covered.
There is no credit/no penalty
for these problems. Part A
Suppose a struct Student
for students in a class is (partially) declared as below. Each student
has a name, a list of grades, the number of grades stored in the list,
and a class identification number. In a class of N
students, each
student is given an ID number in the range
1 ... N, e.g., in a
class of 20 students the numbers 1 ... 20 are assigned one per student.
struct Student
{
int classID; // ID number
string name; // name of student "Jane Doe"
Vector grades; // list of grades
int numGrades; // # of items stored in grades
};
Write the function SortStudents whose header is given below.
SortStudents sorts the information in list so that it
is in order from smallest (1) to largest (N) student ID number.
Your solution MUST sort in O(N) time!!
(hint: in what slot does the student with ID number 16 belong?)
void SortStudents(Vector & list, int numElts)
// precondition: numElts = # of students in list
// ID numbers are unique and in range 1...numElts
// postcondition: list is sorted into increasing order by
// student ID number
// performance: O(n)
Part B
You must solve the problem of determining if
two numbers in an array of n
numbers sum to a given number k. For
example, if the array contains 8, 4, 1, 6, there are numbers that
sum to 10 (4 and 6); to 9 (8 and 1); to 16 (use 8 twice); but NOT to 8
(exactly two numbers must be used).
Trying all pairs of numbers yields an O(n^2)
algorithm. Instead, you
must describe how to:
In both cases, describe how your algorithm will work (briefly) and
justify the running time.
Owen L. Astrachan
Last modified: Fri Mar 28 10:14:50 EST