Basic operation and application of data structure-----heap

Source: Internet
Author: User

(a) The realization of a large heap of small piles with the function of imitation

A heap data structure is an array object that can be considered a complete binary tree structure.

The two-fork tree storage for the heap structure is

Maximum heap: Each parent node is larger than the child node.

Minimum heap: Each parent node is smaller than the child node.


copy function (functor), is to make the use of a class look like a function. Its implementation is to implement a operator () in a class, and this class has a function-like behavior, which is a pseudo-function class.
In the implementation of large, small heap of the process, some of the functions of the code, will be used in different member functions, want to reuse the code, there are two ways.

1) Public functions , this is a workaround, but some variables used by the function may become public global variables, and in order to reuse such a piece of code, it is necessary to single out a function, is not very good maintenance.

2) functor, write a simple class, in addition to those that maintain a class of member functions, just implement a operator (), in the class instantiation, will be used, non-parametric elements passed into the class.

In C + +, we use a function object instead of a normal function by overloading the parentheses operator in a class

heap.h#include <iostream>   #include  < Algorithm>    using namespace std;  template<typename t>   class display  {  public:      void  Operator () (const t &x)       {           cout<<x<< " ";       }    };       int main ()   {       int ia[]={1,2,3,4,5};      for_each (ia,ia+5,display<int> ());              return 0;    }

The basic structure of a large heap, a small heap, with an imitation function

#include <iostream> #include <vector>using namespace std;template<class t>struct  less//less than {bool operator () (Const t&l,const t&r) {return l<r;}}; Template<class t>struct greater{bool operator () (Const t&l,const t&r) { return l>r;}}; template<class t,class comper=greater<t>>//By default build a large heap of class heap{private:vector<t>  _a;public:heap (const t* a,size_t size) {assert (a);//The data in the array is pressed into the stack for (i=0;i<size;i++) {_ A.push_back (A[i]);} Build a large heap for (int i= (_a.size () 2)/2;i>=0;i--) {//Downward adjustment _adjustdown (i);}} Inserting data into the heap Void push (const t& x) {_a.push_back  (x); _adjustup (_a.size ()-1)}/************** The method used when ejecting is to first swap the root node of the complete binary tree with the last leaf node, pop the current leaf node, and then adjust the ************************/void pop () {swap (_a[0] _a[_a.size ()-1]);_a.pop_back  (); _adjustdown (0);} Size_t size ()//the size of the heap {  return _a.size ();} Bool empty ()//heap whetheris empty {return _a.empty ();} Protected:void _adjustdown (size_t parent) {size_t child=2*parent+1;while (Child<_a.size ()) { comper com;//find the largest of two children if (COM (_a[child+1],_a[child])  && child+1<_a.size ())// Right child may not exist {child=child+1;} because it is a completely binary tree If the child is larger than the father then the exchange continues to adjust if (COM (_a[child],_a[parent)) {swap (_a[child],_a[parent]);p arent=child;child=2*parent+1;} Otherwise satisfies the large root heap, exits the loop Else{break;}}} Adjust up Void _adjustup (size_t child) { size_t parent= (child-1)/2; while (child>0)// cannot be written as while (parent>0), because child is a size_t  type, it will constitute a dead loop  { comper com; //if the inserted sub-node is greater than the root node, the interchange  if (COM (_a[child],_a[parent]))  { swap (_a[child],_a[parent]);  child=parent; parent= ( CHILD-1)/2; } //Otherwise meet large piles, exit the loop  else { break; } }}};


(ii) heap sequencing


 #define  _crt_secure_no_warnings 1#include<iostream># include<assert.h>using namespace std;//built the first heap, a large pile of void adjustdown (int a[],int n,int  parent) {int child=parent*2+1;while (child<n) {if (child+1<n&&a[child]<a[child+1]) {+ + Child;} if (A[child]>a[parent]) {swap (a[child],a[parent]);p arent=child;child=parent*2+1;} Else{break;}}} Void heapsort (int a[],int n) {assert (a);//Build Large heap for (int i= (n-2)/2;i>=0;i--) {Adjustdown (A,n, i);} Select a data exchange to the end, using the handsome selection method, the former n-i elements of the re-handsome selected to build a heap for (int i=n-1;i>0;i--) {swap (a[0],a[i]); Adjustdown (a,i,0);}} Void test () {int a[8]={98,77,35,62,55,14,35,48};int size=sizeof (a)/sizeof (a[0]); Heapsort (a,size); for (int i=0;i<size;i++) {cout<<a[i]<< " ";} Cout<<endl;} Int main () {test (); System ("pause"); return 0;} 



Basic operation and application of data structure-----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.