二叉堆(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 ...
随机推荐
- Django-1.10支持中文用户注册登录
让django 支持中文注册登录,支持中文用户名cat django的models文件发现调的如下两个类 class AbstractUser(AbstractBaseUser, Permission ...
- 管理 使用 FastDFS
启动管理tracker: 1. 启动文件+配置文件+命令 /usr/bin/fdfs_trackerd <config_file> [start | stop | restart] 举例: ...
- Linux 配置ip 子接口 多网卡绑定
linux系统配置ip地址,图形化界面略过,这里只介绍文本行.做以下设置注意是否有此权限 查看当前路由及网关信息: [root@localhost ~]# netstat -r Kernel IP r ...
- THINKPHP-RCE-POC
thinkphp-RCE-POC 官方公告: 1.https://blog.thinkphp.cn/869075 2.https://blog.thinkphp.cn/910675 POC: thin ...
- Linux运维--实践-Rally
1.rally简介 OpenStack Rally 是一个自动化测试工具,社区希望通过 Rally 来解答 "How does OpenStack work at scale?(如何规模化运 ...
- C#使用Environment.TickCount 自定义的定时器类
Environment.TickCount, 官网介绍:一个 32 位带符号整数,它包含自上次启动计算机以来所经过的时间(以毫秒为单位). *由于 TickCount 属性值的值是32位有符号整数,因 ...
- Shiro -- (三) 自定义Realm
简介: Realm:域,Shiro 从从 Realm 获取安全数据(如用户.角色.权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定 ...
- JS中new的实现原理及重写
提到new,肯定会和类和实例联系起来,如: function Func() { let x = 100; this.num = x + } let f = new Func(); 上面的代码,我们首先 ...
- MySQL基础(4) | 视图
MySQL基础(4) | 视图 基本语法 1.创建 CREATE VIEW <视图名> AS <SELECT语句> 语法说明如下. <视图名>:指定视图的名称.该名 ...
- Oracle导出警告“EXP-00003: 未找到段 (0,0) 的存储定义”解决
环境:CentOS7.4 Oracle11.2.0.4(搭建rac集群) 问题描述:在使用exp命令执行导出的时候,部分表提示“EXP-00003: 未找到段 (0,0) 的存储定义”警告. 问题 ...