May 9, 2002
Starting with this tree:
Consider the tree below. Assume it was built by a series of insertions
into an initially empty tree, with no deletions.
Circle the right answer:
Which of the following alternative ways of making that decision will also result in trees with fast (=logarithmic) lookups?
10 8 6 2 1 4 5
10 8 6 7 1 4 5 2
10 8 6 7 1 4 5 2 3
8 7 6 3 1 4 5 2
Assume we are using open hashing with an array of size 1024; we are using the last three digits of the phone number as our hash function; and that we are handling collisions by moving down the array one step at a time.
As you know there is a need to distinguish between two kinds of locations that are currently empty: those that have never been occupied and those that have previously been occupied. Let's call them "NEVER USED" and "DELETED."
True or false ...
Which of the following ways to chose the height of a new node will result in linear-time lookups (as opposed to fast logarithmic lookups)?
height = 1 + rand () % 2;
height = 1 + rand () % 10;
height = 2;
while (rand () % 2 == 0)
height += 1;
height = 2;
while (rand () % 2 == 0)
height += 2;
height = 2;
while (rand () % 2 == 0)
height += 2;
if (height > 5)
height = 5;
height = 2;
while (rand () % 2 == 0)
height += 2;
if (height > 5)
height = 1;
( x + 1 ) * x * ( 1 + x )
1 + 1 + x * x
Complete the function below so that it returns the degree of the expression (the highest power of x if you were to multiply everything out; for the examples above the degrees are 3 and 2).
int Expression::degree ()
{
if (constant ())
return 0;
if (op == 'x')
return 1;
switch (op) {
case '+':
return max (left->degree (), right->degree());
case '*':
return left->degree () + right->degree();
]
}
bool balanced (Node* t)
{
if (t == NULL)
return TRUE;
return (abs (shortest (t) - longest (t)) <= 2);
}
int shortest (Node* t)
{
if (t == NULL)
return 0;
return 1 + min (shortest (t->left), shortest (t->right));
}
int longest (Node* t)
{
if (t == NULL)
return 0;
return 1 + max (longest (t->left), longest (t->right));
}
Write a function that finds out if in fact such a tree has a node with three non-NULL children.
bool three (Node* t)
{
if (t == NULL)
return false;
if (t->left != NULL && t->middle != NULL && t->right != NULL)
return true;
return
three (t->left) || three (t->middle) || three (t->right);
}
© 2002 Karl Winklmann