Homework 8

CS409 - Spring 2000

Due: Thursday, Apr 6

For this assignment, you are to implement another kind of "priority queue".  The data structure is not a standard PQ because it allows some nonstandard operations.  Also, this structure is more restricted in some ways than a typical PQ.  To simplify the discussion, I'm going to call this structure a Binary Priority Queue (BPQ).

A BPQ is restricted to holding integers in the range 0 to u-1; u is called the universe size.  The binary part of the data type's name comes from the restriction that each item in the universe-range is either in or not-in the BPQ; multiple copies of an item are not represented in the BPQ.  Operations for a BPQ include Insert, Delete, GetMin, and GetMax.  In addition, there are two new operations: GetPreceding and GetFollowing.  GetPreceding(k) returns the item currently in the BPQ that most closely precedes k. Similarly, GetFollowing(k) returns the item currently in the BPQ that most closely follows k.  Note that for both operations, k itself may or may not be in the BPQ.

Our implementation will be written to force u to be a power of 2.  Note that if an arbitrary integer is rounded up to a power of 2 then the size at most doubles, so rounding to the next higher power of 2 does not have a significant effect on either the space used by the data structure or the running times of the operations. 

A BPQ can be implemented so that O(u) space is used and so that all the operations mentioned above run in time O(log u) where u is the universe size.  Note that the running time does not depend on n, the number of items in the BPQ. 

The BPQ is held in a binary tree with u leaves, numbered u to 2u-1.  The tree is actually held in an array.  We use the same trick we used for the heap data structure to determine the parents and children of a given node: the parent of node i is located at i/2 and the children of node i are in locations 2i and 2i+1.  Each node of the tree holds a single bit.

Each item in the universe corresponds to a leaf in the tree.  Note that the leaf index-numbers do not match the item numbers: item k corresponds to the leaf with index u+k.  When an item is inserted, we turn on the bit in the corresponding leaf node.  We also turn on all the bits on the path from the tree's root to this leaf node.  Since the tree's height is O(log u), this takes time O(log u).  The Delete operation is a little more complicated because a bit in a nonleaf node can be on due to more than one leaf node.  The GetPreceding and GetFollowing operations can be implemented by starting at a leaf and exploring the path toward the root until we find a branch in the right direction; the desired item is found by following that branch back to a leaf node.

Here are the operations, in Java form, that your BPQ needs to handle:

What to Turn in:

Some Notes/Hints on the BPQ: