!!!Chapter 6 Priority Queues (Heaps)

来源:互联网 发布:校园二手 源码 编辑:程序博客网 时间:2024/05/17 04:03

6.1 Model

A priority queue is a data structure that allows at least the following two operations: Insert, which does the obvious thing; and DeleteMin, which finds, returns, and removes the minimum element in the priority queue.

6.2 Simple Implementation

Linked List:

1. Insert at the front( O(1) ) and traversing the list to do DeleteMin(O(N) ).

2. Insert and sort the queue( O(N) ) then delete the first element(O(1) ).

Binary Search Tree: O(logN) for Insert and DeleteMin

6.3 Binary Heap

Binary heap is the standard way to implement heap, so we will refer to binary heap merely as heap.

Heaps have two properties: a structure property and a heap order property.

6.3.1 Structure Property

A heap is a complete binary tree: A binary tree that is completely filled, with the possible exception of the bottom level, which is filled from the left to the right.

A complete binary tree of height h has between 2^h and 2^(h+1)-1 nodes.

Because a complete binary tree is so regular, it can be represented in an array and no pointers are necessary.

For any element in array position i, the left child is in position 2i, the right child is in (2i + 1), and the parent is in position [i/2].


A heap implementation requires an array and an integer representing the maximum and current heap sizes.

6.3.2 Heap Order Property

For heap, any node should be smaller than all of its descendants, so the smallest element should be at the root.

In a heap, for every node X, the key in the parent of X is smaller than(or equal to) the key in X, with the exception of the root, which has no parent.

#ifndef _BinHeap_Hstruct HeapStruct;typedef struct HeapStruct *PriorityQueue;PriorityQueue Initialize( int MaxElements ); void Destroy( PriorityQueue H );void MakeEmpty( PriorityQueue H );void Insert( ElementType XZ, PriorityQueue H );ElementType DeleteMin( PriorityQueue H ); ElementType FindMin( PriorityQueue H );int IsEmpty( PriorityQueue H );int IsFull( PriorityQueue H );#endif//place in implementation filestruct HeapStruct{    int Capacity;    int Size;    ElementType *Elements;};

Initialize a priority queue:

PriorityQueue Initialize( int MaxElements ){    PriorityQueue H;        if( MaxElements < MinPQSize ) // check if size is too small        ...    H = malloc( sizeof( struct HeapStruct ) ); //check space//allocate the array with one extra sentinel    H->Elements = malloc( ( MaxElements + 1)*sizeof(ElementType)); // check space    H->Capacity = MaxElements;    H->Size = 0;    H->Elements[0] = MinData;    return H;}

6.3.3 Basic Heap Operations

Insert:

We use precolate up (上滤) to insert new element. The new element is precolated up the heap until the correct location is found.

// H->Element[0] is a sentinelvoid Insert( ElementType X, PriorityQueue H ){    int i;    if( IsFull(H) )    {        Error("Priority queue is full");        return;    }    for( i = ++(H->Size); H->Elements[i/2] > X; i/=2)        H->Elements[i] = H->Elements[i/2];    H->Elements[i] = X;} 
Time Complexity:O(logN)

* if the for loop above run d times, the above code only do assignment for d+1 times, but the swap method will do assignment for 3d times.
If the element we input is the smallest one, then we have two choices: 1. test if i>=1; 2. add a dummy piece of info at Element[0].(Here we use the second method)

DeleteMin:

We use percolate down(下滤) to delete minimum element: We delete the root element, then put the last element at the root, then try to compare the element with its descend until we find the proper hole.

When percolate down, we need to find the smaller child then continue. (When percolate up, we only need to compare the parent and the element)! So we need to test if the node contains two children or just one child:

ElementType DeleteMin( PriorityQueue H ){    int i, Child;    ElementType MinElement, LastElement;    if( IsEmpty(H) )   // check if queue is empty        ....    MinElement = H->Elements[1];    LastElement = H->Elements[H->Size--]; //first return, then decrease Size        for( i = 1; i*2 <= H->Size; i = Child)    {        //Find smaller child, first check if there is only one child        Child = i*2;        if( Child != H->Size && H->Elements[Child +1] < H->Elements[Child] )            Child++;        //Percolate down        if( LastElement > H->Elements[Child]] )                H->Elements[i] = H->Elements[Child];        else break;    }    H->[i] = LastElement;    return MinElement;}
Time Complexity: O(logN)

6.3.4 Other Heap Operations (Left)

6.4 Application of Priority Queues(Left)

6.5 d-Heaps

A simple generalization of binary heap is a d-heap, which is exactly like a binary heap except that all nodes have d children.

A binary heap is a 2-heap.

The running time of  Insert d-heap is O( log(d)N ).

But the DeleteMin becomes more expensive, as we need to do d - 1 comparisons to find the smallest child. So the time complexity of DeleteMin is O(d*log(d)N )

原创粉丝点击