自己实现的一个大顶堆

1 篇文章 0 订阅
1 篇文章 0 订阅
heap.h

#ifndef HEAP_H
#define HEAP_H 

#include <vector>

class Heap{
public:
    Heap(std::vector<int> &);//Constructor function
    void Insert(int );//Insert a value into heap
    void Delete(int );//Delete a value from heap,if can't find the value in heap,do nothing
    int Max();//Return the maximum value if heap
    void Ext_Max();//Delete the maximum value in heap
    std::vector<int> Heap_Sort();//Return the sorted value of heap,BUT the value in heap didn't change
private:
    void Max_Heapify(int );//Fix the heap from top to bottom form a position
    int Find(int,int);//Find the value from a position,return the position ,if can't find return -1
    std::vector<int > data;
    int len=0;

};
#endif
heap.cpp

#include "heap.h"

Heap::Heap(std::vector<int> &dat){
    data=dat;
    len=data.size();
    for(int i=len/2-1;i>=0;--i){
        Max_Heapify(i);
    }
}

void Heap::Max_Heapify(int i){
    int largest=0;
    if((2*i+1<len)&&data[i]<data[2*i+1]){
        largest=2*i+1;
    }
    else largest=i;
    if((2*i+2<len)&&data[largest]<data[2*i+2]){
        largest=2*i+2;
    }
    if(largest!=i){
        int tmp=data[i];
        data[i]=data[largest];
        data[largest]=tmp;
        Max_Heapify(largest);
    }

}

std::vector<int> Heap::Heap_Sort(){
    std::vector<int >res;
    auto tmp_data = data;
    if(len==0)return res;
    int tmp_len=len;

    int tmp=data[0];
    data[0]=data[len-1];
    data[len-1]=tmp;
    len--;
    while(len>0){
        Max_Heapify(0);
        int tmp=data[0];
        data[0]=data[len-1];
        data[len-1]=tmp;
        len--;
    }
    len=tmp_len;
    res=data;
    data = tmp_data;
    return res;
}

int Heap::Max(){
    return data[0];
}

void Heap::Insert(int a){
    data.push_back(a);
    len++;
    for(int i=len/2-1;i>=0;--i){
        Max_Heapify(i);
    }
}


int Heap::Find(int a,int i){
    if(data[i]==a){
        return i;
    }
    else if(a>data[i])return -1;
    else {
        int left=-1;
        int right=-1;
        if(2*i+1<len){
            int left=Find(a,2*i+1);
        }
        if(2*i+2<len){
            int right=Find(a,2*i+2);
        }
        if(left!=-1){
            return left;
        }
        if(right!=-1){
            return right;
        }
    }

}
void Heap::Delete(int a){
    int i=Find(a,0);
    if(i==-1){
        return;
    }
    else{
        data.erase(data.begin()+i);
        len--;
        for(int i=len/2-1;i>=0;--i){
            Max_Heapify(i);
        }
        return;
    }
}

void Heap::Ext_Max(){
    if(len==0){
        return;
    }
    else{
        data.erase(data.begin());
        len--;
        for(int i=len/2-1;i>=0;--i){
            Max_Heapify(i);
        }
        return;
    }
}
main.cpp

#include "heap.h"
#include <iostream>
using namespace std;

int main(){
    vector<int>vec = { 3, 1, 5, 7, 6, 8, 10, 33, 4 };
    Heap hp(vec);
    auto res=hp.Heap_Sort();
    hp.Insert(2);
    int max = hp.Max();
    hp.Delete(5);
    hp.Ext_Max();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值