Implementation of size heap and heap ordering by imitating function

Source: Internet
Author: User

"2.h" #include <iostream> #include <vector> #include <assert.h>using namespace std; Template<class t>struct less{bool operator () (const t& left,const t & right) {return left<right;}}; Template<class t>struct greater{bool operator () (const t& left,const t & right) {return left>right;}}; template<class t,class compare=less<t>>//Small heap//modify Class heap{public:heap (); Heap (const t* a,size_t size); Void push (const t& x); Void Pop (); T& top (); Void print (); Void heapsort (); Void heapsort1 ();p Rotected:void adjustdown ( int parent); Void adjustup (Int child); Void _adjustdown (int child,int size); protected:vector<t> _array; compare _compare;}; Template<class t,class compare>heap<t,compare>::heap (Const t* a,size_t siZe) {for (size_t i=0;i<size;i++) {_array.push_back (a[i]);} For (int i= (_array.size ()-2)/2;i>=0;i--)//The subscript for the parent node is: ((sub-1)/2), and size () is one more than the maximum subscript, so it is reduced to 2{adjustdown (i);} /*for (Int i=_array.size () -1;i>0;i--) {adjustup (i);} */}template<class t,class compare>void heap<t,compare>::adjustdown (int parent) {Int child=parent*2+1;while (Child<_array.size ()) {if ((Child+1<_array.size ()) &&_compare (_ Array[child+1],_array[child])//right child whether cross-border conditions easy to forget swap (_array[child],_array[child+1]); if (Compare () (_array[child],_ Array[parent]) {swap (_array[child],_array[parent]);p arent=child;child=parent*2+1;} Elsebreak;}} Template<class t,class compare>void heap<t,compare>::adjustup (int child) {int  parent= (child-1)/2;while (child>0)//conditions cannot be written parent>=0 because the parent cannot be less than 0{if (Child+1<_array.size () & &_compare (_array[child+1],_array[child])   swap (_array[child],_array[child+1]); if (compare () (_ Array[child],_array[parent])) {SWAP (_array[child],_array[parent]); child=parent;parent= (child-1)/2;} Elsebreak;}} Inserted in the last sub-node position template<class t,class compare>void heap<t,compare>::P ush (const &NBSP;T&AMP;&NBSP;X)//Remember to add const and &{_array.push_back (x);//adjustdown (0);   //think clearly not available adjustdown (0);  //can not, mainly see where moved Adjustup (_array.size ()-1);} Pop out is the root, subscript for 0template<class t,class compare>void heap<t,compare>::P op () {//swap (_ Array[0],_array[_array.size ()-1]); _array.pop_back (); Adjustdown (0);} Template<class t,class compare>t& heap<t,compare>::top () {return _array[0];} Template<class t,class compare>void heap<t,compare>::P rint () {for (size_t i=0;i <_array.size (); i++) {cout<<_array[i]<< " ";} Cout<<endl;} Pile up the heap, swap the heap top and the end of the heap, each time the rest of the data to get rid of the heap tail data continue to be a large heap ...//understand the error, although the heap sorting can be achieved, but misuse Adjustup (int child) (two lines without removing the comment)-- The sort up is only used when the two-fork tree is inserted, and a few changes in the data cannot be constructed directly in the constructor with an upward sort Template<class t,class compare>voId heap<t,compare>::heapsort () {for (int i=_array.size () -1;i>=0;i--) {for (int j=_ Array.size () -1;j>0;j--)//Remove the heap tail data error Adjustup (j); swap (_array[0],_array[i]);}} To change the size, the adjustdowntemplate<class t,class compare>void heap<t,compare&gt must be rebuilt;:: HeapSort1 () {for (Int size=_array.size (); size>1;size--) {for (int i= (size-2)/2;i>=0;i--) {_ Adjustdown (i,size);} Swap (_array[0],_array[size-1]);}} Template<class t,class compare>void heap<t,compare>::_adjustdown (Int parent, Int size) {int child=parent*2+1;while (child<size) {if (child+1<size) &&_compare (_array[ Child+1],_array[child]) swap (_array[child],_array[child+1]), if (Compare () (_array[child],_array[parent)) {Swap (_ Array[child],_array[parent]);p arent=child;child=parent*2+1;} Elsebreak;}} #include   "2.h" #include <iostream>using namespace std;void test1 () {int a[]={ 1,2,9,5,8,7,4,3};//HEAP&LT;INT&GT;&NBSP;HP1 (a,8); heap<INT,GREATER&LT;INT&GT;&GT;&NBSP;HP1 (a,8); Hp1. Print (); Hp1. Push (0); Hp1. Print (); Hp1. Pop (); Hp1. Print (); Int top=hp1. Top (); Cout<<top<<endl;hp1. Heapsort (); Hp1. Print ();} Int main () {Test1 (); System ("pause"); return 0;}

This article is from the "sunshine225" blog, make sure to keep this source

Implementation of size heap and heap ordering by imitating function

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: 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.