Name: ________________________________

Honor Code Acknowledgment: ___________________


Random Quiz # 4

CPS 100, Spring 1997

Due: February 7


Problem 1: Annie Trees (1 point)

True or False: In every non-empty binary tree there exists a node with no parent. (justify your answer)



Problem 2 (4 points)

The height of a one-node tree is 0, the height of a non-empty tree is the number of edges on the longest root-to-leaf path (on a path, the number of edges is always one less than the number of nodes.)

Ed Jones

What is the minimum height of a binary tree that contains 31 nodes? What is the maximum height of a binary tree that contains 31 nodes?



Ed Gaedel

What is the minimum height of a binary tree that contains 1,023 nodes? What is the maximum height of a binary tree that contains 1,023 nodes?


Bo Diddley Squat

Consider a tree in which each node contains a maximum of 4 children (a quad tree). What is the minimum height of a quad tree that contains 21 nodes, what is the maximum height of a quad tree that contains 21 nodes?



Bicep,Tricep

What is the minimum height of a quad tree that contains 21,845 nodes?



Problem 3: Search Me (2 points)

The function below is intended to return false if its parameter is NOT a binary search tree and true if it is a search tree. It does not work as intended. Draw a tree that is not a search tree, but for which the function returns true.

bool IsBST(Tree * t)
{
    if (t == 0)
    {
        return true;       // empty tree is a search tree
    }
    else if (IsBST(t->left) && IsBST(t->right))
    {
            return true;
    }
    else
    {
        return false;
    }
}









Problem 4: PrintMe (2 points)

What is the output of the following function:

struct Tree
{
   string info;   
   Tree * left;
   Tree * right;
};

void Print(Tree * t, ostream & out)
{
    if (t != 0)
    {
        out << t->info << endl;
        Print (t->left, out);
        Print (t->right, out);
        return false;
    }
}

If t points to the following tree on the first call to Print: