堆的建立&堆排序

2017 / 3 / 31
    很久没写博客了 最近除了上课 睡觉 吃饭 其他时间几乎都待在图书馆里飙车 其实也谈不上什么飙车 就是一个跌破低谷的咸鱼在努力尝试着翻身
    今天和朋友看了场电影 终于克服掉进电影院的阴影 可能害怕看到一对对情侣想到以前的我们 因为和你待在一起最久地方除了家就是电影院
    曲终人散 人走茶凉 我就不要想起你 撒油啦啦
    渍渍渍 在这车手如云的论坛上竟然写起了随笔 好尴尬 勿喷
    接下来我们说正题 这次介绍数据结构中简单的二叉堆。

堆的建立


二叉堆是一种特殊的堆,二叉堆是完全二元树或者是近似完全二元树。
二叉堆有两种:最大堆和最小堆。
最大堆:父结点的键值总是大于或等于任何一个子节点的键值;
最小堆:父结点的键值总是小于或等于任何一个子节点的键值。
堆一般用数组存放:根节点为1节点,i节点的左子节点为2*i,右子节点为2*i+1。
插入:因为从根节点到叶节点是一个有序序列。将新插入的节点放到最后然后将其放到这个有序序列。
删除:删除某一个节点, 将最后一个节点填充到删除的节点处,然后将此节点看做成根节点构造一个堆。

多BB一句 这里让数组下标从1开始比从0开始方便些 比如1的左右儿子分别就是1*2和1*2+1
#include <iostream>
#include <malloc.h>
#define MAXSIZE 100
#define ElemType int
using namespace std;

typedef struct Heap{
    int capacity;
    int size;
    ElemType elements[MAXSIZE + 1];
}Heap;

void CreatHeap(Heap *&H)
{
    H = (Heap *) malloc (sizeof(Heap));
    H->capacity = MAXSIZE;
    H->size = 0;
}

bool IsFull(Heap *H)
{
    if(H->size == H->capacity) return true;
    return false;
}

void Insert(Heap *&H, ElemType x)
{
    if(IsFull(H)){
        cout<<"Heap is full"<<endl;
        return;
    }
    for(int i = ++H->size; H->elements[i / 2] > x; i /= 2)
        H->elements[i] = H->elements[i / 2];
    H->elements[i] = x;
}

bool IsEmpty(Heap *H)
{
    if(H->size) return false;
    return true;
}

ElemType DeleteMin(Heap *&H)
{
    if(IsEmpty(H)){
        cout<<"The heap is empty"<<endl;
        exit(-1);
    }
    int i, child, MinElement, LastElement;
    MinElement = H->elements[1];
    LastElement = H->elements[H->size--];
    for(i = 1; 2 * i <= H->size; i = child){
        child = 2 * i;
        if(H->elements[child] > H->elements[child + 1])
            ++child;
        if(LastElement > H->elements[child])
            H->elements[i] = H->elements[child];
        else break;
    }
    H->elements[i] = LastElement;
    return MinElement;
}

int main()
{
    int i, x;
    Heap *h;
    CreatHeap(h);
    for(i = 1; i <= 10; i++){
        cin>>x;
        Insert(h, x);
    }
    cout<<"Heap is: ";
    for(i = 1; i <= h->size; i++)
        cout<<h->elements[i]<<' ';
    cout<<endl;
    cout<<"Now insert an element"<<endl;
    cin>>x;
    Insert(h, x);
    for(i = 1; i <= h->size; i++)
        cout<<h->elements[i]<<' ';
    cout<<endl;
    cout<<"Delete min element: "<<DeleteMin(h)<<endl;
    for(i = 1; i <= h->size; i++)
        cout<<h->elements[i]<<' ';
    cout<<endl;
    return 0;
}

堆排序


堆的排序:因为堆的根节点是最小堆值,所以每次取根节点,然后将其删除,剩余的节点重新构造成堆以此递归完成排序。
使用最小堆得到递增序列,最大堆得到递减序列。
二次操作时间相加还是O(N * logN)。故堆排序的时间复杂度为O(N * logN)。
#include <iostream>
#define MAXSIZE 100
using namespace std;

void swap(int *a, int *b)
{
    int t;
    t = *a; *a = *b; *b = t;
}

void HeapAdjust(int a[], int i, int n)
{
    int child, tmp;
    for(tmp = a[i]; 2 * i <= n; i = child){
        child = 2 * i;
        if(a[child] < a[child + 1])
            ++child;
        if(child <= n &&a[child] > tmp)
            a[i] = a[child];
        else break;
    }
    a[i] = tmp;
}

void HeapSort(int a[], int n)
{
    int i;
    for(i = n / 2; i > 0; i--)      //Build heap
        HeapAdjust(a, i, n);
    for(i = n - 1; i > 0; i--){     //Sort
        swap(&a[1], &a[i + 1]);     //这里切记是a[i + 1] 数组中下标是从1到n的 0号位置相当于废了Orz
        HeapAdjust(a, 1, i);
    }
}

int main()
{
    int i, n, a[MAXSIZE];
    cin>>n;
    for(i = 1; i <= n; i++)
        cin>>a[i];
    cout<<"Before: ";
    for(i = 1; i <= n; i++)
        cout<<a[i]<<' ';
    cout<<endl;
    HeapSort(a, n);
    cout<<"After: ";
    for(i = 1; i <= n; i++)
        cout<<a[i]<<' ';
    cout<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值