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

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

大家好,又见面了,我是全栈君,今天给大家准备了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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java的三种工厂模式「建议收藏」

    java的三种工厂模式「建议收藏」一,简单的工厂模式首先举一个例子:我们现在开了一家饭馆:然后呢我们的大厨可以做三种菜,还有一句潇洒的抱怨:   下面客人进场,开始点餐:我们观察上面的代码,虽然很好的完成了任务,但是,我们的三个实现类和和借口紧密的绑定到了一起,这意味着我们的代码耦合出现严重问题,不利于以后的维护,试想顾客点餐需要与后厨大厨直接接触,这肯定是一个不好的体验,那…

    2022年7月7日
    23
  • 网站开发html_网页基础代码大全

    网站开发html_网页基础代码大全网页的标准是W3C,目前模式是HTML、CSS和JavaScript。HTML标签<!DOCTYPEHTML>是文档声明,必须写在HTML文档的第一行,位于<html>

    2022年8月2日
    9
  • Android浏览器多窗口webview界面截屏心得「建议收藏」

    Android浏览器多窗口webview界面截屏心得「建议收藏」做Android浏览器多窗口的时候,需要使用到浏览器的webview快照,当前有三种方法,都尝试过,对第二种方法做了一点改进,整理说一下他们各自的优势:

    2022年5月14日
    45
  • XFF漏洞利用[通俗易懂]

    XFF漏洞利用[通俗易懂]作者:小刚一位苦于信息安全的萌新小白帽,记得关注给个赞,谢谢本实验仅用于信息防御教学,切勿用于其它用途XFF漏洞X-Forwarded-For(XFF)利用方式1.绕过服务器过滤2.XFF导致sql注入补充X-Forwarded-For(XFF)XFF是header请求头中的一个参数是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段。代表了HTTP的请求端真实的IP。X-Forwarded-For:client1,proxy1,p.

    2022年6月16日
    62
  • IDEA安装yarn

    IDEA安装yarn当我们拿到一个vue项目,导入到idea后发现有yarn.lock文件,则该前端项目需要通过yarn来进行启动。如下图:未安装前,如图下:安装步骤:1.各位看官注意,我们安装yarn,必须得把Nodejs的环境先配置好。以及npm的淘宝镜像安装好,也就是说启动vue的环境的是正常的。2.在Terminal面板下,通过cnpminstall-gyarn命令进行全局安装:3.执…

    2022年5月9日
    361

发表回复

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

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