Name: ________________________________

Honor Code Acknowledgment: ___________________


Random Quiz # 12

CPS 100, Spring 1996

Due: March 8


Problem 1: Big and Bushy (4 points)

In a complete binary tree, every non-leaf node has two children, and all the leaves are at the same level. Two examples of complete trees are shown below.

If there are n levels in a complete tree (the tree on the left above has two levels, the tree on the right has four) what is the total number of nodes in the complete tree (both leaves and internal nodes) and the total number of leaves in the complete tree. Express answers in terms of n.









Problem 2: Q-tip (4 points)

Write a function to print the nodes of a binary tree in level order, that is print all the nodes in one level, left-to-right, before any nodes in a lower level are printed. The first node printed is the root (at level 1), followed by its children (at level 2), followed by the root's grandchildren (at level 3) etc. You should use a queue in your function, put the root on the queue, then repeatedly dequeue an element, print it, and put its children on the queue. This function is NOT recursive. void PrintInLevelOrder(Tnode * tree) // post: all nodes of tree printed in level order { Queue<Tnode *> q; // use this queue to print levels }