当前位置: 首页 > news >正文

转:最小堆的数组实现

最小堆的数组实现 

//**************minHeap.h****************//
//******最小堆的类定义和各操作的实现*******//
//C++源码

#ifndef MINHEAP_H
#define MINHEAP_H

#include <iostream>

using namespace std;

template<class T>
class MinHeap {
public:
    MinHeap(const int size=20) { 
        maxSize=(size>0)?size:20;
        array=new T[maxSize];
        currentPosition=0;
    }
    ~MinHeap() { delete [] array; }
    void insert(const T &);  //插入
    void deleteNode(const T &);  //删除
    int search(const T &) const; //查找
    void printAll() const { //打印
        for (int i=0; i<currentPosition; i++)
            cout<<array[i]<<", ";
        cout<<endl;
    }
private:
    T *array;
    int maxSize;
    int currentPosition;
    int parent(const int &) const; //传入参数所在位置的父亲节点位置
    int leftChild(const int &) const; 
    int rightChild(const int &) const;
    void swap(const int &, const int &); //交换两个位置的数据
    void moveUp(int);
    void moveDown(int);
};

template<class T>
int MinHeap<T>::parent(const int &pos) const {
    return int((pos-1)/2);
}

template<class T>
int MinHeap<T>::leftChild(const int &pos) const {
    return pos*2+1;
}

template<class T>
int MinHeap<T>::rightChild(const int &pos) const {
    return pos*2+2;
}

template<class T>
void MinHeap<T>::swap(const int &pos1, const int &pos2) {
    T tmp=array[pos1];
    array[pos1]=array[pos2];
    array[pos2]=tmp;
}

template<class T>
void MinHeap<T>::moveUp(int pos) {
    int par=parent(pos);
    while (par>=0) {
        if (par==0) {
            if (array[pos]<array[par]) {
                swap(pos, par);
                break;
            }
            else break;
        }
        if (array[pos]<array[par]) {
            swap(pos, par);
            pos=par;    
            par=parent(pos);
        }
        else break;
    }
}

template<class T>
void MinHeap<T>::moveDown(int pos) {
    int left=leftChild(pos);
    while (left<currentPosition) {
        if (array[pos]>array[left] && array[left]>array[left+1]) {
            swap(pos, left+1);
            pos=left+1;
            left=leftChild(pos);
        }
        else if (array[pos]>array[left]) {
            swap(pos, left);
            pos=left;
            left=leftChild(pos);
        }
        else break;
    }
}

template<class T>
void MinHeap<T>::insert(const T &data) {
    if (currentPosition==maxSize)
        return;
    array[currentPosition]=data;
    moveUp(currentPosition);
    currentPosition++;
}

template<class T>
void MinHeap<T>::deleteNode(const T &data) {
    int pos=search(data);
    if (pos!=-1) {
        if (pos==currentPosition-1)
            currentPosition--;
        else {
            array[pos]=array[--currentPosition];
            moveDown(pos);
        }
    }
}

template<class T>
int MinHeap<T>::search(const T &data) const {
    for (int i=0; i<currentPosition; i++)
        if (array[i]==data)
            return i;
    return -1;
}

#endif //minHeap.h end


//********************main.cpp**********************//

#include "minHeap.h"

int main() {
    MinHeap<int> heap(30);
    heap.insert(32);
    heap.insert(23);    
    heap.insert(9);
    heap.insert(4);
    heap.insert(21);
    heap.insert(13);
    heap.insert(15);
    heap.insert(2);
    heap.printAll();
    
    heap.deleteNode(2);
    heap.printAll();
    return 0;
}
//main.cpp end

转载于:https://www.cnblogs.com/kira2will/p/4294354.html

相关文章:

  • mathematica 8.0.0 使用体验(一)
  • PHP_004 运算符
  • 云雾升腾时,你准备好了吗?
  • 递归算法
  • 第八节 多线程基本知识
  • Samba再报安全漏洞
  • 什么叫一层交换机,二层交换机,三层交换机?
  • iPad不是大号的iPod touch
  • 安装和配置SQL Server 2014
  • Linux 再爆 root 帐号提权漏洞
  • 【转】Cygwin的包管理器:apt-cyg
  • FreeBSD入门级命令查阅表
  • JS动态修改页面EasyUI datebox不生效、EasyUI动态添加Class、EasyUI动态渲染解析解决方案...
  • EIGRP度量值详解
  • 黄聪:jquery mobile通过a标签页面跳转后,样式丢失、js失效的解决方法
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • ES6系列(二)变量的解构赋值
  • JSDuck 与 AngularJS 融合技巧
  • mysql_config not found
  • Python_OOP
  • REST架构的思考
  • Vue.js 移动端适配之 vw 解决方案
  • Vue官网教程学习过程中值得记录的一些事情
  • Vue学习第二天
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 批量截取pdf文件
  • 写给高年级小学生看的《Bash 指南》
  • 原生js练习题---第五课
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 自制字幕遮挡器
  • 走向全栈之MongoDB的使用
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​如何在iOS手机上查看应用日志
  • ​用户画像从0到100的构建思路
  • #微信小程序(布局、渲染层基础知识)
  • (26)4.7 字符函数和字符串函数
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (独孤九剑)--文件系统
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (四)Android布局类型(线性布局LinearLayout)
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • (转)用.Net的File控件上传文件的解决方案
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .NET Framework与.NET Framework SDK有什么不同?
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .net操作Excel出错解决
  • .net分布式压力测试工具(Beetle.DT)
  • .Net转前端开发-启航篇,如何定制博客园主题
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • [ Linux ] git工具的基本使用(仓库的构建,提交)
  • [ 蓝桥杯Web真题 ]-布局切换
  • [ACTF2020 新生赛]Include
  • [Android]常见的数据传递方式
  • [ARC066F]Contest with Drinks Hard
  • [caffe(二)]Python加载训练caffe模型并进行测试1