Name: ________________________________

Honor Code Acknowledgment: ___________________


Random Quiz # 8

CPS 100E, Fall 1996

Due: November 5 (vote!)


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: Tight Rope (2 points)

The function IsHeightBalanced below determines whether a tree is height-balanced (for all nodes, left and right subtrees differ by no more than one) --- assume that Height works in O(n) time for an n-node tree.

bool IsHeightBalanced(TNode * tree) // postcondition: returns true if tree is balanced, else returns false { if (tree != 0) { if (IsHeightBalanced(tree->left) && IsHeightBalanced(tree->right) && abs(Height(tree->left) - Height(tree->right)) <= 1) { return true; } return false; } return true; }

The complexity of this function is greater than O(n). What is the complexity and why?