学习算法 – 优先级队列二叉堆实现

学习算法 – 优先级队列二叉堆实现

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

PriorityQuenue

优先队列就是作业调度类的ADT,这里用二叉堆来实现。

优先队列最少有两个操作:插入(Insert)和删除最小者(DeleteMin)。

插入操作图解:

学习算法 - 优先级队列二叉堆实现
  • 图片来源:www.educity.cn

删除操作图解:

学习算法 - 优先级队列二叉堆实现
  • 图片来源:www.cfanz.cn

代码实现:

<pre name="code" class="cpp">//
//  main.cpp
//  binaryHeap
//
//  Created by Alps on 14-8-17.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#include <iostream>
#include "binaryHeap.h"
//#define ElementType int
using namespace std;

PriorityQuenue Initialize(int MaxNum){
    PriorityQuenue P = (PriorityQuenue)malloc(sizeof(struct HeapStruct));
    if (P == NULL) {
        //error("can't malloc memory");
        exit(1);
    }
    P->elements = (ElementType *)malloc(MaxNum * sizeof(ElementType));
    if (P->elements == NULL) {
        //error("can't malloc memory");
        exit(1);
    }
    P->Capacity = MaxNum;
    P->Size = 0;
    P->elements[0] = 0;
    return P;
}

void Destory(PriorityQuenue P){
    if (P != NULL && P->elements != NULL) {
        free(P->elements);
        free(P);
    }
}

void MakeEmpty(PriorityQuenue P){
    if (P != NULL && P->elements != NULL) {
        free(P->elements);
        P->elements = (ElementType *)malloc(P->Capacity * sizeof(ElementType));
        P->Size = 0;
    }
}

int IsEmpty(PriorityQuenue P){
    if (P != NULL) {
        return P->Size == 0;
    }else{
        return 1;
    }
}

int IsFull(PriorityQuenue P){
    if (P != NULL) {
        return P->Size == P->Capacity-1;
    }else{
        //error("P is not exist");
        return 1;
    }
}

void Insert(PriorityQuenue P, ElementType X){
    int i = 0;
    if (!IsFull(P)) {
        P->elements[P->Size+1] = X;
        P->Size++;
        for (i = P->Size; P->elements[i/2] > X; i /=2) {
            P->elements[i/2] = P->elements[i] + P->elements[i/2];
            P->elements[i] = P->elements[i/2] - P->elements[i];
            P->elements[i/2] = P->elements[i/2] = P->elements[i];
        }
        P->elements[i] = X;
    }else{
        //error("the PriorityQuenue is already full");
        return;
    }
}

ElementType FindMin(PriorityQuenue P){
    if (!IsEmpty(P)) {
        return P->elements[1];
    }else{
        return NULL;
    }
}

ElementType DeleteMin(PriorityQuenue P){
    ElementType MinElement, LastElement;
    int temp;
    int i = 1;
    if (!IsEmpty(P)) {
        MinElement = P->elements[1];
        LastElement = P->elements[P->Size];
        for (i = 1; i*2 < P->Size; i = temp) {
            temp = i*2;
            if (temp != P->Size) {
                if (P->elements[temp] < P->elements[temp+1]) {
                    P->elements[i] = P->elements[temp];
                }else{
                    P->elements[i] = P->elements[temp+1];
                    temp++;
                }
            }
            if (LastElement < P->elements[i]) {
                P->elements[i] = LastElement;
//                P->Size--;
                break;
            }
        }
        P->elements[i] = LastElement;
        P->Size--;
        return MinElement;
    }else{
        return NULL;
    }
}

int Min(ElementType &a ,ElementType &b, ElementType &c){
    if (b < c) {
        if (b < a) {
            b = a+b;
            a = b-a;
            b = b-a;
            return 1;
        }else{
            return -1;
        }
    }else{
        if (c < a) {
            c = a+c;
            a = c-a;
            c = c-a;
            return 0;
        }else{
            return -1;
        }
    }
}

//void BuildHeap(PriorityQuenue P){
//    int i = 0;
//    int j = 0;
//    for (i = P->Size/2; i > 0; i--) {
//        for (j = i; j*2 <= P->Size;) {
//            if (j * 2 != P->Size) {
//                int temp = Min(P->elements[i],P->elements[i*2],P->elements[i*2+1]);
//                if (temp == 0) {
//                    j*=2;
//                }
//                if (temp == 1) {
//                    j = j*2+1;
//                }
//                if (temp == -1) {
//                    j *=2;
//                }
//            }else{
//                //            P->elements[i] = P->elements[i]<P->elements[i*2]?P->elements[i]:P->elements[i*2];
//                if (P->elements[j] > P->elements[j*2]) {
//                    int tmp = P->elements[j*2];
//                    P->elements[j*2] = P->elements[j];
//                    P->elements[j] = tmp;
//                    j *= 2;
//                }else{
//                    break;
//                }
//            }
//        }
//        
//    }
//}



int main(int argc, const char * argv[])
{
    PriorityQuenue P = Initialize(20);
    Insert(P, 13);
    Insert(P, 21);
    Insert(P, 16);
    Insert(P, 24);
    Insert(P, 31);
    Insert(P, 19);
    Insert(P, 68);
    Insert(P, 65);
    Insert(P, 26);
    Insert(P, 32);
    Insert(P, 14);
//    int a[]={13,21,16,24,31,19,68,65,26,32,14};
//    int a[]={53,26,41,59,97,31,58};
//    for (int i = 0; i < 11; i++) {
//        P->elements[i+1] = a[i];
//    }
//    P->Size = sizeof(a)/sizeof(int);
    for (int i = 1; i <= P->Size; i++) {
       printf("%d ",P->elements[i]);
    }
    printf("\n");
//    BuildHeap(P);
    DeleteMin(P);
    for (int i = 1; i <= P->Size; i++) {
        printf("%d ",P->elements[i]);
    }
    printf("\n");
    return 0;
}


以上是main.cpp文件。


以下是binaryHeap.h文件代码:
//
//  binaryHeap.h
//  binaryHeap
//
//  Created by Alps on 14-8-17.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#ifndef binaryHeap_binaryHeap_h
#define binaryHeap_binaryHeap_h
#define ElementType int

struct HeapStruct;
typedef HeapStruct *PriorityQuenue;

PriorityQuenue Initialize(int MaxNum);
void Destory(PriorityQuenue P);
void MakeEmpty(PriorityQuenue P);
void Insert(PriorityQuenue P);
ElementType DeleteMin(PriorityQuenue P);
ElementType FindMin(PriorityQuenue P);
int IsEmpty(PriorityQuenue P);
int IsFull(PriorityQuenue P);

struct HeapStruct{
    int Capacity;
    int Size;
    ElementType *elements;
};


#endif

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117012.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 为什么要使用Iocomp控件?

    为什么要使用Iocomp控件?为什么要使用Iocomp控件?作为一个程序员,编写软件的过程中,我们常常需要用一些工控图表和工控仪表,想要自己制作出漂亮极品的控件的非常费时费力的,这时候使用专业的第三方控件就是非常不错的选择。一来节约了开发时间,降低了开发难度;二来第三方控件更加专业更精细;三来降低项目风险。Iocomp控件包括多种用来创建专业的仪表和测量、工业控制、工业监控等相关的应用程序的控件包,如仪表盘控件、开

    2022年7月17日
  • nginx 静态文件配置(nginx常用配置)

    Nginx的配置文件nginx.conf配置详解如下:usernginxnginx;Nginx用户及组:用户组。window下不指定worker_processes8;工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。error_loglogs/error.log;error_loglogs/error.logno

    2022年4月14日
  • 鸿蒙3部曲先看哪部,星辰变是“鸿蒙”系列的作品,那“鸿蒙”系列到底有多少部曲?…

    鸿蒙3部曲先看哪部,星辰变是“鸿蒙”系列的作品,那“鸿蒙”系列到底有多少部曲?…说到《星辰变》,相信很多人都知道他的作者就是番茄吧?而番茄笔下最出名系列的小说,无疑就是“鸿蒙”系列了,其中《星辰变》就是“鸿蒙”系列中的一部作品。那“鸿蒙”系列小说到底有多少部曲呢?对此也有很多人有了这个疑问。原先的“鸿蒙三部曲”相信很多熟悉番茄小说的朋友都知道,原本番茄的《盘龙》《星辰变》《吞噬星空》被很多粉丝称之为“鸿蒙三部曲”。因为在《星辰变》的结局中我们也知道,鸿蒙创立了三个不同的世界,…

    2022年6月15日
  • RapidXml用法[通俗易懂]

    RapidXml用法[通俗易懂]一、写xml文件生成的xml例如以下:写文件样例2:生成的xml例如以下:二、读取xml文件生成的xml为:三、删除节点输出信息例如以下:四、编辑节点信息临时找到的编辑方法就是先删

    2022年7月1日
  • convert命令参数

    convert命令参数-adaptive-blur  自适应的模糊操作,在边缘处降低模糊效果-adaptive-resize采用数据依赖的三角滤波算法进行图像尺寸调整-adaptive-sharpen自适应的锐化操作,在边缘处加强锐化效果-adjoin 连接多张图片到同一张图片中-affine 仿射变换矩阵-alpha对阿尔法通道(透明通道)进行开关等操作-annotate 给…

    2022年7月13日
  • 常见的关系型数据库和非关系型数据及其区别是什么_mysql数据库数据类型

    常见的关系型数据库和非关系型数据及其区别是什么_mysql数据库数据类型&#13; 一、关系型数据库&#13;&#13; &#13;关系型数据库最典型的数据结构是表,由二维表及其之间的联系所组成的一个数据组织优点:1、易于维护:都是使用表结构,格式一致;2、使用方便:SQL语言通用,可用于复杂查询;3、复杂操作:支持SQL,可用于一个表以及多个表之间非常复杂的查询。缺点:1、读写性能比较差,尤其是海量数据的高效率读写;2、固定的表结构,灵活…

    2022年10月23日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号