Data Structures and Algorithms -- CSci 230
Chapter 6 -- Priority Queues

Overview


Motivation


Binary Heaps


Exercise


1.
Suppose the following operations are applied to an initially empty binary heap of integers. Show the resulting heap after each delete_min operation. (Remember, the tree must be complete!)
        insert 5, insert 3, insert 8, insert 10, insert 1, insert 6,
        delete_min,
        insert 14, insert 2, insert 4, insert 7,
        delete_min,
        delete_min,
        delete_min

Array implementation


Exercises


1.
What are some of the limitations of the author's code? What might you do differently? We will investigate these issues thoroughly in lab.
2.
Suppose we used locations 0 through n-1 as the heap locations instead of 1 through n. For a value at location i, what are the array locations of its parent, its left child and its right child?

Build Heap


Exercise


1.
Consider the following list of values stored in locations 1 through 10 of an array.
        10, 6, 12, 18, 5, 9, 7, 2, 4, 3
Show the contents of the array after Build_Heap.

Leftist Heaps


Leftist Heap Operations


//  Here are the two functions used to implement leftist
//  heap merge operations.  Class "Left_Node" is a
//  normal tree node augmented with a member variable
//  "Npl" to record the minimum null path length from a
//  node. 
//
//  Function Merge is the driver.  Function Merge1 does
//  most of the work.  These functions call each other
//  recursively.

template <class Etype>
Left_Node<Etype> *
Left_Heap<Etype>::
Merge( Left_Node<Etype> *H1, Left_Node<Etype> *H2 )
{
    if( H1 == NULL )
        return H2;
    if( H2 == NULL )
        return H1;
    if( H2->Element > H1->Element )
        return Merge1( H1, H2 );
    return( Merge1( H2, H1 ) );
}

template <class Etype>
Left_Node<Etype> *
Left_Heap<Etype>::
Merge1( Left_Node<Etype> *H1, Left_Node<Etype> *H2 )
{
    if( H1->Left == NULL )	// Single node.
        H1->Left = H2;
    else
    {
        H1->Right = Merge( H1->Right, H2 );
        if( H1->Left->Npl < H1->Right->Npl )
            Swap( H1->Left, H1->Right );
        H1->Npl = H1->Right->Npl + 1;
    }
    return H1;
}

Exercises


1.
How can merge be used to implement insert and delete_min? Write pseudo-code to solve implement these.
2.
Show the state of a leftist heap at the end of
        insert 1, 2, 3, 4, 5, 6
        delete_min
        insert 7, 8
        delete_min
        delete_min

Review Problems


1.
Consider a binary heap, implemented, of course, as an array. Write a function to delete the element stored at location i of the heap. Assume that functions Percolate_Down and Percolate_Up exist, with prototypes
        void Percolate_Down( ElementType * heap, int size, int i);
        void Percolate_Up( ElementType * heap, int size, int i);
where heap is the array of elements, and size is the current number of elements. Assume that i is correctly given to you, i.e., assume 1 <= i <= size. Start from the following prototype
        void Delete( ElementType * heap, int & size, int i);

2.
Consider a priority queue implemented as a heap. The heap is implemented using a vector, and it contains n values stored in subscript locations 1 through n. Assume it is a ``max heap'', so that the largest value is stored in subscript location 1.
(a)
What are the possible subscript locations for the third largest value in the heap (assuming all values are distinct)?
(b)
What is the range of possible subscript locations for the smallest value in the heap (again assuming all values are distinct)?

(c)
What is the worst-case time complexity required to find the minimum value in the heap?

3.
Given an empty binary heap of integers, show the structure of the binary heap after the values 5, 2, 4, 6, 7, 1, 8 are inserted into it. (Note, you are not being asked to simulate the Build_Heap function.) Then show the heap after the minimum value is removed. You may show the heap as a tree rather than as an array. The heap is ordered so that the minimum value is at the root.

4.
Suppose you are given a pointer, Root, to the root of a leftist heap of floating point values. Suppose you are also given a pointer, P, to a particular node in the leftist heap. Assume each Leftist node has the structure
    struct Leftist {
      float Value;
      Leftist *Parent, *Left, *Right;
      int Npl;   // the null path length
    };
You may assume the existence of a merge function with the following prototype:
     Leftist* Merge( Leftist* t1_root, Leftist* t2_root )
which merges two leftist heaps, updates the null path lengths as necessary, and returns a pointer to the root of the resulting leftist tree.

(a)
Write a function to remove the node pointed to by P. The function should be as efficient as possible. Here is the prototype:
     void LeftistRemove( Leftist* & Root, Leftist* & P )
(b)
What is the worst-case time required by the function? Assume N is the number of nodes in the entire tree. Explain briefly, but carefully.



 

Charles Stewart
10/13/1998