码迷,mamicode.com
首页 > 其他好文 > 详细

堆的基本操作

时间:2017-04-09 16:56:04      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:display   algorithm   pre   cstring   for   调整   can   print   bsp   

大顶堆

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;
const int maxn=10007;

int heap[maxn];
int n;
///堆调整
void AdjustHeap(int root)
{
    int lchild=2*root+1;
    int rchild=2*root+2;
    int temp=heap[root];

    while(lchild<n)
    {
        if(rchild<n && heap[rchild]>heap[lchild])
            lchild++;
        if(heap[lchild]<=temp)
            break;
        heap[root]=heap[lchild];
        root=lchild;
        lchild=2*root+1;
        rchild=2*root+2;
    }
    heap[root]=temp;
}

/*插入函数,  将要插入的数字首先加入到该二叉树最后的一个节点,
自底而上调整*/
void InsertHeap(int num)
{
    int child=n;
    int root=(child-1)/2;
    heap[child]=num;

    while(root>=0 && child!=0)
    {
        if(heap[root]>num)
            break;
        heap[child]=heap[root];
        child=root;
        root=(child-1)/2;
    }
    heap[child]=num;
    n++;
}

/*删除函数,对于最小堆和最大堆而言,删除是针对于根节点而言。
将二叉树的最后一个节点替换到根节点,然后自顶向下调整。*/
void DeleteHeap()
{
    heap[0]=heap[n-1];
    n--;
    AdjustHeap(0);
}

void BuildHeap()
{
    for(int i=(n-2)/2; i>=0; i--)
        AdjustHeap(i);
}
void Display()
{
    for(int i=0; i<n; i++)
        printf("%d%c", heap[i], i==n-1?\n: );
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i=0; i<n; i++)
            scanf("%d", &heap[i]);
        BuildHeap();
        Display();

        int a;
        scanf("%d", &a);
        InsertHeap(a);
        Display();

        DeleteHeap();
        Display();
    }
    return 0;
}
/*
6
16 7 3 20 17 8
30
*/

小顶堆

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;
const int maxn=10007;

int heap[maxn];
int n;
///堆调整
void AdjustHeap(int root)
{
    int lchild=2*root+1;
    int rchild=2*root+2;
    int temp=heap[root];

    while(lchild<n)
    {
        if(rchild<n && heap[rchild]<heap[lchild])
            lchild++;
        if(heap[lchild]>=temp)
            break;
        heap[root]=heap[lchild];
        root=lchild;
        lchild=2*root+1;
        rchild=2*root+2;
    }
    heap[root]=temp;
}

/*插入函数,  将要插入的数字首先加入到该二叉树最后的一个节点,
自底而上调整*/
void InsertHeap(int num)
{
    int child=n;
    int root=(child-1)/2;
    heap[child]=num;

    while(root>=0 && child!=0)
    {
        if(heap[root] <= num)
            break;
        heap[root]=heap[child];
        child=root;
        root=(child-1)/2;
    }
    heap[child]=num;
    n++;

}

/*删除函数,对于最小堆和最大堆而言,删除是针对于根节点而言。
将二叉树的最后一个节点替换到根节点,然后自顶向下调整。*/
void DeleteHeap()
{
    heap[0]=heap[n-1];
    n--;
    AdjustHeap(0);
}

void BuildHeap()
{
    for(int i=(n-2)/2; i>=0; i--)
        AdjustHeap(i);
}
void Display()
{
    for(int i=0; i<n; i++)
        printf("%d%c", heap[i], i==n-1?\n: );
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i=0; i<n; i++)
            scanf("%d", &heap[i]);
        BuildHeap();
        Display();

        int a;
        scanf("%d", &a);
        InsertHeap(a);
        Display();

        DeleteHeap();
        Display();
    }
    return 0;
}
/*
6
16 7 3 20 17 8
30
*/

 

堆的基本操作

标签:display   algorithm   pre   cstring   for   调整   can   print   bsp   

原文地址:http://www.cnblogs.com/w-y-1/p/6685390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!