封装一个简单二叉堆,亦可视为优先队列。

测试文件 main.cpp:

#include <iostream>
#include "BinaryHeap.h"

using std::cout;
using std::endl;

int main()
{
    BinaryHeap<int> bh(BinaryHeap<int>::HeapType::MINIMEM);

    auto il = { ,,,,, };
    bh.push(il.begin(), il.end());
    cout << "Elements:\n\t";
    bh.show();
    cout << endl << endl;
    cout << "Pop head: " << bh.top() << endl << endl;
    bh.pop();
    cout << "Elements:\n\t";
    bh.show();
    cout << endl << endl;
    try
    {
        cout << ] << endl;
        cout << "bh[5]: ";
        cout << bh[] << endl;
    }
    catch (std::exception & e) { cout << e.what() << endl; }

    ;
}

头文件 "BinaryHeap.h":

#pragma once
#ifndef __BINARYHEAP_H__
#define __BINARYHEAP_H__

template<typename _Ty>
class BinaryHeap
{
public:
    , MAXIMEM };

public:
    BinaryHeap() = default;
    BinaryHeap(HeapType _heapType) { heapType = _heapType; }
    ~BinaryHeap() { delete[] heapArr; heapArr = nullptr; }
    ; }
    size_t size() { return size_n; }

    template<typename _Iter>
    void push(_Iter, _Iter);
    void push(const _Ty&);
    void pop();
    _Ty& top() const;
    void show() const;

    _Ty& operator [] (int);

private:
    bool compare(const _Ty& _a, const _Ty& _b)
    {
        return (heapType == HeapType::MAXIMEM) ? (_a > _b) : (_a < _b);
    }

private:
    size_t size_n = ;
    size_t MaxSize = ;
    _Ty* heapArr = nullptr;
    HeapType heapType = HeapType::MAXIMEM;
};

template<typename _Ty>
template<typename _Iter>
void BinaryHeap<_Ty>::push(_Iter _it1, _Iter _it2)
{
    while (_it1 != _it2)
    {
        push(*_it1);
        ++_it1;
    }
}

template<typename _Ty>
void BinaryHeap<_Ty>::push(const _Ty& _val)
{
    ++size_n;
    if (heapArr == nullptr)
    {
        MaxSize = ;
        heapArr = new _Ty[MaxSize];
    }
    )
    {
        MaxSize << ;
        _Ty* tempArr = new _Ty[MaxSize];
        ; it < size_n - ; ++it)
            tempArr[it] = heapArr[it];
        delete[] heapArr;
        heapArr = tempArr;
        tempArr = nullptr;
    }
    heapArr[size_n - ] = _val;
    size_t childInex = size_n - ;
    )
    {
        ) / ]))
        {
            heapArr[childInex] = heapArr[(childInex - ) / ];
            childInex = (childInex - ) / ;
        }
        else
            break;
    }
    heapArr[childInex] = _val;
}

template<typename _Ty>
void BinaryHeap<_Ty>::pop()
{
    ) return;
    --size_n;
    heapArr[] = heapArr[size_n];
    size_t childInex = ;
    _Ty temp = heapArr[];
    while (childInex < size_n)
    {
         < size_n && compare(heapArr[childInex + ], heapArr[childInex]))
            ++childInex;
        if (compare(temp, heapArr[childInex]))
            break;
        heapArr[(childInex - ) / ] = heapArr[childInex];
        childInex =  * childInex + ;
    }
    heapArr[(childInex - ) / ] = temp;
}

template<typename _Ty>
_Ty& BinaryHeap<_Ty>::top() const
{
    )
        throw std::exception("Heap is empty.");
    ];
}

template<typename _Ty>
void BinaryHeap<_Ty>::show() const
{
    ; i < size_n; ++i)
        std::cout << heapArr[i] << " ";
}

template<typename _Ty>
_Ty& BinaryHeap<_Ty>::operator [] (int _index)
{
    if (_index >= size_n) throw std::exception("Index out of range.");
    return heapArr[_index];
}

#endif // !__BINARYHEAP_H__

二叉堆(1)BinaryHeap的更多相关文章

  1. python下实现二叉堆以及堆排序

    python下实现二叉堆以及堆排序 堆是一种特殊的树形结构, 堆中的数据存储满足一定的堆序.堆排序是一种选择排序, 其算法复杂度, 时间复杂度相对于其他的排序算法都有很大的优势. 堆分为大头堆和小头堆 ...

  2. binary-heap(二叉堆)原理及C++代码实现

    二叉堆可以看做一个近似的完全二叉树,所以一般用数组来组织. 二叉堆可以分为两种形式:最大堆和最小堆.最大堆顾名思义,它的每个结点的值不能超过其父结点的值,因此堆中最大元素存放在根结点中.最小堆的组织方 ...

  3. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  4. 二叉堆(binary heap)

    堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因 ...

  5. 用于A*的 二叉堆 AS3实现

    package com.copper.isometric.pathing {     import flash.sampler.startSampling;           /**      * ...

  6. C# 二叉堆

    二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html   C#代码实现 using System ...

  7. python 二叉堆

    BinaryHeap() 创建一个新的,空的二叉堆. insert(k) 向堆添加一个新项. findMin() 返回具有最小键值的项,并将项留在堆中. delMin() 返回具有最小键值的项,从堆中 ...

  8. python---使用二叉堆实现的优先队列(列表)

    哟,有实用价值 可以看到,加入是随机的,而吐出是顺序的. # coding = utf-8 # 使用二叉堆实现的优先队列(列表) class BinaryHeap: def __init__(self ...

  9. C# 实现简单的 Heap 堆(二叉堆)

    如题,C#  实现简单的二叉堆的 Push() 和 Pop(), 如有不足欢迎指正. 另外,在C#中使用 Heap 的相似功能可以考虑使用:Priority Queues,SortedDictiona ...

随机推荐

  1. Qt Python Scriptable Application

    Qt Python Scriptable Application eryar@163.com Abstract. Python and C++ are in many ways as differen ...

  2. 小程序在wxml页面格式化类似的2019-02-16T10:54:47.831000时间

    其实新建小程序的时候,会有一个util.js文件,这个文件里已经有时间格式化的方法了,可是它却不能再wxml页面调用, 不过wxml页面是支持引入.wxs文件的,我们重新写一个这样子的工具文件就解决了 ...

  3. 珠峰-6-http和http-server原理

    ???? websock改天研究下然后用node去搞. websock的实现原理. ##### 第9天的笔记内容. ## Header 规范 ## Http 状态码 - 101 webscoket 双 ...

  4. mac系统目录结构

    1 符合unix传统的目录 /bin 传统unix命令的存放目录,如ls,rm,mv等. /sbin 传统unix管理类命令存放目录,如fdisk,ifconfig等等. /usr 第三方程序安装目录 ...

  5. Python性能优化方案

    Python性能优化方案 从编码方面入手,代码算法优化,如多重条件判断有限判断先决条件(可看 <改进python的91个建议>) 使用Cython (核心算法, 对性能要求较大的建议使用C ...

  6. Spring boot内置Tomcat的临时目录被删除导致文件上传不了-问题解析

    目录 1.问题 2.1. 为什么需要使用这个/tmp/tomcat*? 2.2.那个 /tmp/tomcat* 目录为什么不存在? 三.解决办法 修改 springboot 配置,不要在/tmp 下创 ...

  7. App 抓包提示网络异常怎么破?

    背景 当你测试App的时候,想要通过Fiddler/Charles等工具抓包看下https请求的数据情况,发现大部分的App都提示网络异常/无数据等等信息.以"贝壳找房"为例: F ...

  8. JavaScript中基本数据类型之间的转换

    在JavaScript中共有六种数据类型,其中有五种是基本数据类型,还有一种则是引用数据类型.五种基本数据类型分别是:Number 数值类型.String 字符串类型.Boolean 布尔类型, nu ...

  9. 解决Fail to post notification on channel "null"的方法

    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);mNotifyMgr.cancelAll(); St ...

  10. HUE下载HDFS文件时报ERR_CONNECTION_TIMED_OUT错误的解决办法

    1.故障描述 这是运行在公有云上的一套Hadoop集群,有一个公网IP将部分服务的端口映射出来供办公室访问. 数据分析师报告说:在HUE上面浏览HDFS文件,点击"download" ...