Priority Queue is very commonly used in operating system to schedule task, priority Queue can very easily and very effectively can implement in Heap.
Insert A new Key in Heap :
Start from end of heap and find appropriate position then insert.
Heap-Insert(A,key)
- Heap-Size[A] <- Heap-Size[A]+1
- i=Heap-Size[A]
- while(i>1 and A[parant(i)]<key)
- A[i]<-A[Parant(i)]
- i=Parent(i)
- A[i]=Key
Note: If We remove root node then to hepify again we just have to call heapify(A,1)
See Carefully these algorithm in below program
- Insert New Element in heap => Heap-Insert(A,key)
- Extract max from maxHeap => Heap-Max-Extract(A)
- Delete I'th node from Heap => Heap-Delete(A,i)
Priority Queue Implementation in Heap (C++ )
- //Priority Queue implementation in heap
- #include<iostream>
- #include<new>
- using namespace std;
- void heapify(int *heap,int i,int heapSize);
- int Heap_Extraxt_max(int *heap,int &heapsize);
- void Heap_Insert(int *heap,int &heapsize,int key);
- void Heap_Delete(int *heap,int i,int &heapsize);
- void Heap_Display(int *heap, int &heapsize);
- main(){
- //initialise array of required heap size
- int heapSize;
- cout<<"Enter max element of heap:_ "<<endl;
- cin>>heapSize;
- int *heap;
- heap =new int[heapSize+10];
- heap[0]=0;
- //now insert all element in heap
- //after that we hepify it
- cout<<"Enter element of heap :"<<endl;
- int key,j;
- for(int i=1;i<=heapSize;i++){
- cin>>key;
- j=i-1;
- Heap_Insert(heap,j,key);
- }
- Heap_Display(heap,heapSize);
- //max extraction
- cout<<"Max Extraction :"<<Heap_Extraxt_max(heap,heapSize)<<endl;
- cout<<"Max Extraction :"<<Heap_Extraxt_max(heap,heapSize)<<endl;
- Heap_Display(heap,heapSize);
- //Delete Ith node
- cout<<"Delete 2nd node :"<<endl;
- Heap_Delete(heap,2,heapSize);
- cout<<"Delete 10th node :"<<endl;
- Heap_Delete(heap,10,heapSize);
- Heap_Display(heap,heapSize);
- }
- void Heap_Insert(int *heap,int &heapsize,int key){
- heapsize=heapsize+1;
- int i= heapsize;
- while(i>1 && key>heap[i/2]){
- heap[i]=heap[i/2];
- i=i/2;
- }
- heap[i]=key;
- }
- void heapify(int *heap,int i,int heapSize){
- int left=2*i; //index of left child of ith node
- int right=left+1; //index of right child of ith node
- int largest;
- //find index of largest among (ith node and its left and right child)
- if(left<=heapSize && heap[left]>heap[i])
- largest=left;
- else
- largest=i;
- if(right<=heapSize && heap[right]>heap[largest])
- largest=right;
- //now swap largest with ith node
- if(largest !=i){
- int temp =heap[i];
- heap[i]=heap[largest];
- heap[largest]=temp;
- heapify(heap,largest,heapSize);
- }
- }
- int Heap_Extraxt_max(int *heap,int &heapsize){
- if(heapsize<1){
- cout<<"Underflow Condition"<<endl;
- return -1;
- }
- else{
- int max=heap[1];
- heap[1]=heap[heapsize];
- heapsize--;
- heapify(heap,1,heapsize);
- return max;
- }
- }
- void Heap_Delete(int *heap,int i,int &heapsize){
- if(heapsize<i){
- cout<<"Index Overflow "<<endl;
- }
- else{
- if(heapsize != i){
- heap[i]=heap[heapsize];
- heapsize--;
- heapify(heap,i,heapsize);
- }
- else
- heapsize--;
- }
- }
- void Heap_Display(int *heap, int &heapsize){
- cout<<"\n\n Heap Status :"<<endl;
- for(int i=1;i<=heapsize;i++){
- cout<<heap[i]<<", ";
- }
- cout<<endl;
- }
Thank you for sharing with us! Good luck!
ReplyDelete