Heap basic operation to achieve maximum heap _c language

Source: Internet
Author: User
Tags rand

Copy Code code as follows:

/**
* Achieve maximum heap
*
*/

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>

using namespace Std;
const int M = 10003;

Defining Data nodes
Class dnode{
Public
String name;
int age;
Double score;
Dnode (): Name ("No Name"), age (0), score (0.0) {}
Dnode (string name, int age, double score): Name (name), age (age), score (score) {}
BOOL operator < (const Dnode & D) {
Return score < D.score;
}
BOOL operator > (const dnode &d) {
return score > D.score;
}
BOOL operator = (const Dnode &d) {
name = D.name;age=d.age;score=d.score;
}
BOOL operator = = (Const Dnode &d) {
return name = = D.name && age = = D.age && score = = D.score;
}
void Swap (Dnode & A, Dnode & B) {
Dnode tmp = A;
A = b;
b = tmp;
}
void Show () {
cout << "***************" << Endl;
cout << "Name:" << name << Endl;
cout << ' Age: ' << age << Endl;
cout << "score:" << score << Endl;
}
};
Definition heap
Template<class t>
Class heap{
Public
Dnode H[m];
void heapify (int cur);
int n;
Array subscript starting from 0
int L (int i) {return (I << 1) + 1;}
int R (int i) {return (I << 1) + 2;}
int P (int i) {return (i-1) >> 1;}
Public
Heap (): N (0) {}
Heap (T data[], int len) {
memcpy (h, data, sizeof (T) * len);
n = len;
}
Array h builds stacks
void build ();
Insert an Element
void Insert (T data);
Pop-up heap top element
void Pop () {
H[0] = H[--n];
Heapify (0);
};
Heap Top Element
T Top () {return h[0];}
Print all the elements in an array
void Show () {
for (int i = 0; i < n; i++) {
cout << "***************" << Endl;
cout << "cur:" << i << Endl;
cout << "Name:" << h[i].name << Endl;
cout << "Age:" << h[i].age << Endl;
cout << "score:" << h[i].score << Endl;
}
}

};


Template<class t>
void Heap<t>::build () {
 for (int i = (N/2)-1; I >= 0; i--) {
  ;  heapify (i);
 } The
}
/**
*  Insert process is where data is placed in the
*  of the array labeled N, which is followed by the last element of the array
*  
*  What you need to do after inserting is to do the work of keeping the heap, which is very simple
*  because the job is to put the new data in an "ordered chain" of the appropriate place (in the data is still in order)
*  This orderly chain is " The last element of data "to a chain between root, this chain is definitely ordered.
*  you will completely take this chain as an array (just the subscript of the previous element is not minus one)
*  if the position you want is M, then M ———— (n-1) Elements are moved down, and then the end of the chain is pushed by the
*  data into position m.
*/

Template<class t>
void Heap<t>::insert (T data) {
 h[n++] = data;
 t TMP = data;  //Saves the new node
 int cur = n-1;//The current node, because it was previously n++, so minus one
 //loop to find the appropriate place to place TMP. And keep moving the elements backwards to make room for the pending TMP
 while (cur > 0 && h[p (cur)] < TMP) {  //When TMP is older than Cur's father
  h[cur] = h[p (cur)];
  cur = P (cur);
 }
 //now cur position is the appropriate place to place tmp
 h[cur] = tmp;
}
/**
*  adjustment cur This tree satisfies the heap (max Heap)
*  Find maximum value A and cur exchange from two children in cur and then recursively adjust from the position of the node in the Max. A (downward adjustment)
* /

Template<class t>
void heap<t>::heapify (int cur) {
T Mmax = h[l (cur)] > h[r (cur)]? H[l (cur)]: h[r (cur)];
if (Mmax < h[cur])
Return
cout << "##########" << Endl;
Mmax.show ();
if (h[l (cur)] = = Mmax) {
H[0].swap (H[cur], h[l (cur));
Heapify (L (cur));
}else{
H[0].swap (H[cur], h[r (cur));
Heapify (R (cur));
}
cout << "##########" << Endl;

}

int main () {
int num = 7;
Dnode D[m];
for (int i = 0; i < num; i++) {
D[i] = Dnode ("Luo", rand ()%, rand ()% 11);
}
D[0].score = 10;
D[1].score = 33;
D[2].score = 22;
D[3].score = 43;
D[4].score = 7;
D[5].score = 66;
D[6].score = 1;

heap<dnode> *h = new heap<dnode> (d, NUM);
H->build ();

H->insert (d[1]);
H->insert (D[3]);
H->show ();
cout << "########### test top and pop" << Endl;
H->top (). Show ();
H->pop ();
H->top (). Show ();


return 0;
}

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.