二叉堆(1)BinaryHeap
封装一个简单二叉堆,亦可视为优先队列。
测试文件 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的更多相关文章
- python下实现二叉堆以及堆排序
python下实现二叉堆以及堆排序 堆是一种特殊的树形结构, 堆中的数据存储满足一定的堆序.堆排序是一种选择排序, 其算法复杂度, 时间复杂度相对于其他的排序算法都有很大的优势. 堆分为大头堆和小头堆 ...
- binary-heap(二叉堆)原理及C++代码实现
二叉堆可以看做一个近似的完全二叉树,所以一般用数组来组织. 二叉堆可以分为两种形式:最大堆和最小堆.最大堆顾名思义,它的每个结点的值不能超过其父结点的值,因此堆中最大元素存放在根结点中.最小堆的组织方 ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- 二叉堆(binary heap)
堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因 ...
- 用于A*的 二叉堆 AS3实现
package com.copper.isometric.pathing { import flash.sampler.startSampling; /** * ...
- C# 二叉堆
二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html C#代码实现 using System ...
- python 二叉堆
BinaryHeap() 创建一个新的,空的二叉堆. insert(k) 向堆添加一个新项. findMin() 返回具有最小键值的项,并将项留在堆中. delMin() 返回具有最小键值的项,从堆中 ...
- python---使用二叉堆实现的优先队列(列表)
哟,有实用价值 可以看到,加入是随机的,而吐出是顺序的. # coding = utf-8 # 使用二叉堆实现的优先队列(列表) class BinaryHeap: def __init__(self ...
- C# 实现简单的 Heap 堆(二叉堆)
如题,C# 实现简单的二叉堆的 Push() 和 Pop(), 如有不足欢迎指正. 另外,在C#中使用 Heap 的相似功能可以考虑使用:Priority Queues,SortedDictiona ...
随机推荐
- Linux 邮件服务
三个要点 1.smtp协议 2.搭建本地邮件服务器 3.使用外部邮件服务器 实现邮件功能 1.smtp协议 SMTP(Simple Mail Transfer Protocol)即 ...
- ibtmp1文件过大
有个数据库发现磁盘告警 已经100% 经过排查发现数据库的data目录下有个 ibtmp1是个什么东西呢?查看官方文档后发现 The temporary tablespace is a tablesp ...
- 使用Java, AppleScript对晓黑板进行自动打卡
使用Java, AppleScript对晓黑板进行自动打卡 由于我们学校要求每天7点起床打卡,但是实在做不到,遂写了这个脚本. 绪论 由于晓黑板不支持网页版,只能使用App进行打卡,所以我使用网易的安 ...
- vue-cli3点滴
1.如果你不在构造函数中声明private的变量,那么久会提示错误. 2.
- PAT-1005 Spell It Right 解答(C/C++/Java/python)
1.Description: Given a non-negative integer N, your task is to compute the sum of all the digits of ...
- js—求数组中的最大最小值
参考链接:https://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html Math.min.appl ...
- Python3标准库:copy复制对象
1. copy复制对象 copy模块包括两个函数copy()和deepcopy(),用于复制现有的对象. 1.1 浅副本 copy()创建的浅副本(shallow copy)是一个新容器,其中填充了原 ...
- StarUML之九、starUML的一些特殊属性的说明
UML的扩充性机制允许你在控制的方式下扩充UML语言. 这一类的机制包括:stereotype,标记值.约束. Stereotype扩充了UML的词汇表,允许你创建新的建筑块,这些建筑块从已有的继承而 ...
- Android中点击按钮获取星级评分条的评分
场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为Lin ...
- beego orm的使用
在使用beego model 去操作数据库时 有一些疑惑 找到了一个比较好的博文 原文地址 : https://my.oschina.net/u/252343/blog/829912 (Kelvin ...