Turn: Array implementation of the minimum heap

Source: Internet
Author: User

Array implementation of the minimum heap

minheap.h****************//
The class definition of the minimum heap and the implementation of each operation *******//
C + + source

#ifndef Minheap_h
#define Minheap_h

#include <iostream>

using namespace Std;

Template<class t>
Class Minheap {
Public
MINHEAP (const int size=20) {
Maxsize= (size>0) size:20;
Array=new T[maxsize];
currentposition=0;
}
~minheap () {delete [] array;}
void Insert (const T &); Insert
void Deletenode (const T &); Delete
int search (const T &) const; Find
void printall () const {//Print
for (int i=0; i<currentposition; i++)
cout<<array[i]<< ",";
cout<<endl;
}
Private
T *array;
int maxSize;
int currentposition;
int parent (const int &) const; The parent node location where the incoming parameter is located
int leftchild (const int &) const;
int rightchild (const int &) const;
void swap (const int &, const int &); Swap data in two locations
void moveUp (int);
void MoveDown (int);
};

Template<class t>
int minheap<t>::p arent (const int &pos) Const {
return int ((POS-1)/2);
}

Template<class t>
int minheap<t>::leftchild (const int &pos) Const {
return pos*2+1;
}

Template<class t>
int minheap<t>::rightchild (const int &pos) Const {
return pos*2+2;
}

Template<class t>
void Minheap<t>::swap (const int &AMP;POS1, const int &pos2) {
T TMP=ARRAY[POS1];
ARRAY[POS1]=ARRAY[POS2];
array[pos2]=tmp;
}

Template<class t>
void minheap<t>::moveup (int pos) {
int par=parent (POS);
while (par>=0) {
if (par==0) {
if (Array[pos]<array[par]) {
Swap (POS, par);
Break
}
else break;
}
if (Array[pos]<array[par]) {
Swap (POS, par);
Pos=par;
Par=parent (POS);
}
else break;
}
}

Template<class t>
void Minheap<t>::movedown (int pos) {
int Left=leftchild (POS);
while (left<currentposition) {
if (Array[pos]>array[left] && array[left]>array[left+1]) {
Swap (POS, left+1);
pos=left+1;
Left=leftchild (POS);
}
else if (Array[pos]>array[left]) {
Swap (POS, left);
Pos=left;
Left=leftchild (POS);
}
else break;
}
}

Template<class t>
void Minheap<t>::insert (const T &data) {
if (currentposition==maxsize)
Return
Array[currentposition]=data;
MoveUp (currentposition);
currentposition++;
}

Template<class t>
void Minheap<t>::d eletenode (const T &data) {
int Pos=search (data);
if (pos!=-1) {
if (pos==currentposition-1)
currentposition--;
else {
Array[pos]=array[--currentposition];
MoveDown (POS);
}
}
}

Template<class t>
int Minheap<t>::search (const T &data) Const {
for (int i=0; i<currentposition; i++)
if (Array[i]==data)
return i;
return-1;
}

#endif//minheap.h End


main.cpp**********************//

#include "MinHeap.h"

int main () {
Minheap<int> Heap (30);
Heap.insert (32);
Heap.insert (23);
Heap.insert (9);
Heap.insert (4);
Heap.insert (21);
Heap.insert (13);
Heap.insert (15);
Heap.insert (2);
Heap.printall ();

Heap.deletenode (2);
Heap.printall ();
return 0;
}
Main.cpp End

Turn: Array implementation of the minimum 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.