cs161: Data Structures and Algorithms Handout #6 Michael Goldwasser Stanford University Tuesday, July 9, 1996

Homework #3: Priority Queues, Search Trees Due Date: Tuesday, 16 July 1996

Reading

Read Ch. 7, 13.1–13.3, and pp.197–198 of CLR.

Practice

Recall, these exercises are purely for your own practice. You need only turn in the official problems.

• Do CLR 7.5-6.

• Do CLR 13.1-2, 13.2-1, 13.2-6, 13.3-3.

Problems

Consider maintaining a collection of people in a binary tree. For each person, we have both a name and an age. We will call this structure structure a treap (tree + heap) if it is a binary search tree with respect to the names and, simultaneously, a heap with respect to the ages. For example, for the set,

{(Alice, 13), (Bob, 26), (Cathy, 19), (David, 30), (Eve, 27), (F red, 22)}

the tree shown below satisfies the conditions.

1. You must prove the following claim. Let S be a set of people such that the names and ages are distinct. There always exists such a tree which satisfies the above properties, and in fact, such a tree is unique for a given set.

2. Do CLR 10-1.

3. Do CLR 13.3-4.

4. Give a nonrecursive algorithm that performs a postorder tree walk. (Hint: There is an easier solution that uses a stack as an auxiliary data structure, and a more complicated but elegant solution that uses only constant amount of auxiliary storage, but assumes that two pointers can be tested for equality.)

Extra Credit

For extra credit, give the “elegant” solution to problem 4 above which uses only a constant amount of additional storage.

Do not jeopardize your solution to problem 4 by attempting this extra credit. That is, you may certainly write up problem 4 with the easier solution to get credit for the main part of the homework, and then you may write up a second solution, here, for the extra credit attempt.

If you feel your original solution to problem 4, satisfies the extra credit, than you may simply write a note which refers us to your original solution, but if that solution is incorrect, not only will you not receive extra credit, but you will have also lost points on the actual homework.