Introduction to algorithms-5. Chapter 6 (2) priority queue

Source: Internet
Author: User

We recommend that you first look at the preface: http://www.cnblogs.com/tanky_woo/archive/2011/04/09/2010263.html

 

The previous chapter summarizes the heap sorting algorithm. This chapter also uses the heap data structure to implementPriority queue.

It is divided into the maximum heap and the minimum heap according to the heap, so the priority queue can also be divided into the maximum priority queue and the minimum priority queue.

The concept and purpose of priority queue have been clearly written, so I will not repeat it again. Write down the specific implementation directly.

 

Before implementation:

1. As mentioned in the previous chapter, the heapsize and length of the heap must be clearly distinguished. This chapter is used in the priority queue.

2. priority queue uses some functions in the previous chapter, such as MaxHeapify (). If you do not remember, you can review the previous chapter.

The following is the code and explanation (the maximum priority queue is implemented here ):

// Heap application priority queue
// Tanky Woo
// Blog: www.WuTianQi.com
# Include <iostream>
Using namespace std;
Constint INF = 999999;
 
//////////////////////////////////////// /////////////////
//// // The following code has been explained in heap sorting ///////////////
Void MaxHeapify (int * a, int I, int len)
{
Int lt = 2 * I, rt = 2 * I + 1;
Int largest;
If (lt <= len & a [lt]> a [I])
Largest = lt;
Else
Largest = I;
If (RT <= Len & A [RT]> A [Largest])
Largest = RT;
If (largest! = I)
{
Int temp = A [I];
A [I] = A [Largest];
A [Largest] = temp;
Maxheapify (A, largest, Len );
}
}
 
Void buildmaxheap (int * a, int size)
{
For (int I = size/2; I> = 1; -- I)
MaxHeapify (a, I, size );
}
 
Void PrintArray (int data [], int size)
{
For (int I = 1; I <= size; ++ I)
Cout <data [I] <"";
Cout <endl;
}
//////////////////////////////////////// ////////////////////
 
// Returns the element with the maximum keyword.
Int HeapMaximum (int *)
{
Return a [1];
}
 
// Remove and return the element with the maximum keyword
// Note: here, the value of maxheapify is heapsize.
Int heapextractmax (int * a, Int & heapsize)
{
If (heapsize <1)
Cout <"heap underflow" <Endl;
Int max = A [1];
A [1] = A [heapsize];
-- Heapsize;
Maxheapify (A, 1, heapsize );
Return Max;
}
 
// Add the value of element a [I] to the key
Void heapincreasekey (int * a, int I, int key)
{
If (Key <A [I])
Cout <"New key is smaller than current key" <endl;
A [I] = key;
While (I> 1 & a [I/2] <a [I])
{
Int temp = a [I];
A [I] = a [I/2];
A [I/2] = temp;
I/= 2;
}
}
 
// Insert an element with the key keyword
Void MaxHeapInsert (int * a, int key, int & heapsize)
{
++ Heapsize;
A [heapsize] =-INF;
HeapIncreaseKey (a, heapsize, key );
}
 
 
 
Int main ()
{
Int len, heapsize;
Int arr [100] = {0, 15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1 };
// Differentiate len and heapsize
// Heapsize indicates the heap size, while len indicates the total size of the initial array.
Len = heapsize = 12;
 
// Create a heap first
BuildMaxHeap (arr, len );
Cout <"after heap creation:" <endl;
PrintArray (arr, len );
 
// Use HeapMaximum
Cout <"the largest element currently is:" <endl;
Cout <HeapMaximum (arr) <endl;
 
// Use HeapExtractMax
Cout <"after HeapExtractMax is used:" <endl;
HeapExtractMax (arr, heapsize );
PrintArray (arr, heapsize );
 
// Use HeapExtractMax again
Cout <"after HeapExtractMax is used again:" <endl;
HeapExtractMax (arr, heapsize );
PrintArray (arr, heapsize );
 
// Use HeapIncreaseKey
Cout <"after HeapIncreaseKey is used:" <endl;
HeapIncreaseKey (arr, 2, 15 );
PrintArray (arr, heapsize );
 
// Use MaxHeapInsert
Cout <"after MaxHeapInsert is used:" <endl;
MaxHeapInsert (arr, 28, heapsize );
PrintArray (arr, heapsize );

}


The running result is as follows:

Result:

After using HeapExtractMax for the second time, set the second number (6) to 15. After the update, the result is the output of HeapIncreaseKey.

Tanky Woo Tag: priority queue, heap sorting, heap, algorithm Introduction

Personal Blog synchronous posting: http://www.wutianqi.com /? P = 2349

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.