二叉堆(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 ...
随机推荐
- [WPF 自定义控件]在MenuItem上使用RadioButton
1. 需求 上图这种包含多选(CheckBox)和单选(RadioButton)的菜单十分常见,可是在WPF中只提供了多选的MenuItem.顺便一提,要使MenuItem可以多选,只需要将MenuI ...
- 智和网管平台SugarNMS 2019年度IT综合监控突破性成果概览
一元复始,万象更新,欢辞旧岁,喜迎新年. 智和信通,精益求精,携手并进,迎战鼠年! 2020年1月10日,北京智和信通技术有限公司(以下简称“智和信通”)以“2020携手并进”为主题的年度庆典暨201 ...
- 一起了解 .Net Foundation 项目 No.7
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Entity Framew ...
- MFC/QT 学习笔记(四)——MFC基于对话框学习控件(下)
//5.列表控件 ListControl 属性 报表模式 view:Report:添加变量 //Cdemo5Dlg.cpp ps:资源视图 右键 类向导 成员变量 查看对象所属类 // TODO: 在 ...
- MySQL的JDBC驱动(8.0版本)
1.引用外部库 mysql-connector-java-8.0.版本的jar 2.jdbc驱动类:com.mysql.jdbc.Driver 改成 com.mysql.cj.jdbc.Drive ...
- MySQL安装详细步骤(附迅雷下载链接)
环境:windows10.64bit.mysql 8.0.19 迅雷下载链接8.0版本 https://cdn.mysql.com//Downloads/MySQLInstaller/mysql-in ...
- 服务器控件Repeater
在使用aspx开发中,如果一个页面做纯数据展示,Repeater会比GridView更适合,因为它是轻量级的 下面是最基本的用法: aspx: <table> <asp:Repea ...
- C#在屏幕画点
Graphics类没有提供直接画点的方法,最开始想使用填充圆形区域來实现,结果发现点很大,占据了4个像素.使用起点和终点一样来划线什么也没画出.画矩形,画椭圆都没实现.最后试到填充矩形,这次成功了. ...
- 一起学Vue之条件判断
在Vue进行前端开发中,条件判断主要用于根据不同的条件来决定显示或隐藏,或者进行视图之间的切换,本文以一个简单的小例子简述v-if的常见用法,仅供学习分享使用,如有不足之处,还请指正. v-if 指令 ...
- Dart中类的getter和setter
Dart类Getters和Setter Getters和Setter(也称为访问器和更改器)允许程序分别初始化和检索类字段的值. 使用get关键字定义getter或访问器.Setter或存取器是使用s ...