Implementation of the priority queue (heap)

Source: Internet
Author: User

Insert/delete/print operations for heaps

#include <iostream>#include<vector>#include<math.h>using namespacestd;//the insertion of a heapvector<int> Insert (intx,vector<int>heap) {    inttemp; if(Heap.empty ()) {heap.push_back (x); }    Else{heap.push_back (x);  for(intI=heap.size (); heap[i/2-1]>heap[i-1];i=i/2) {Temp=heap[i-1]; Heap[i-1]=heap[i/2-1]; Heap[i/2-1]=temp; if(i==0) Break; }    }    returnHeap;}//Delete the smallest element in the heapvector<int> Delete_element (vector<int>heap) {Vector<int>temp; Vector<int>heap_new;  for(intI=1; I) Temp.push_back (Heap[i]);  for(intj=0; J<temp.size (); j + +) {heap_new=Insert (temp[j],heap_new); }    returnheap_new;}//printing of heap elementsvoidPrint_heap (vector<int>heap) {    intCount=1; if(Heap.empty ()) cout<<"This heap is empty!"<<Endl; Else    {         for(intI=0; I) {cout<" "; if((i+2) ==pow (2, Count)) {cout<<Endl; Count++; } }} cout<<Endl;}intMain () {//Create an empty heapvector<int>Heap; intx; CharC;  while(cin>>x) {heap=Insert (X,HEAP); C=cin.Get(); if(c=='\ n') Break;    } print_heap (heap); Vector<int>HEAP2; HEAP2=delete_element (heap);    Print_heap (HEAP2); return 0;}

Implementation of the priority queue (heap)

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.