Overview
Motivation
Binary Heaps
Percolate_Down
function. This function is written here in terms of tree nodes with
child pointers, but later it will be written in terms of array
subscripts.
Percolate_Down( node * T )
{
while ( T->Left != NULL ) {
if ( T->Right != NULL
&& T->Right->Element < T->Left->Element )
child = T->Right;
else
child = T->Left;
if ( child->Element < T->Element ) {
swap(child->Element, T->Element);
T = child;
}
else
break;
}
}
Percolate_Up
function. It assumes each node has a pointer to its parent.
Percolate_Up( node * T )
{
while ( T->Parent != NULL ) {
if ( T->Element < T->Parent->Element ) {
swap(T->Parent->Element, T->Element);
T = T->Parent;
}
else
break;
}
}
Exercise
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
priority_queue is implemented
as a binary heap.
Exercises
Build Heap
Percolate_Down
reimplemented to use array subscripts),
Build_Heap()
{
for ( i=N/2; i>=1; i--) Percolate_Down(i);
}
Exercise
10, 6, 12, 18, 5, 9, 7, 2, 4, 3
Show the contents of the array after Build_Heap.
Leftist Heaps
Leftist Heap Operations
insert and delete_min operations will depend
on the merge operation.
h1 and h2 pointers to their root nodes, and with
h1->Element <= h2->Element, recursively merge h1->Right
with h2, making the resulting heap h1->Right.
Merge requires merge in class.
// 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
merge be used to implement insert and
delete_min? Write pseudo-code to solve implement these.
insert 1, 2, 3, 4, 5, 6
delete_min
insert 7, 8
delete_min
delete_min
Review Problems
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);
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.
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.
P. The
function should be as efficient as possible.
Here is the prototype:
void LeftistRemove( Leftist* & Root, Leftist* & P )