data structure | heap
#include <iostream>
#include <string.h>
using namespace std; template <class T>
class Heap {
public:
Heap():n(), capacity() {
this->arr = new T[capacity];
} ~Heap() {
delete[] arr;
} void insert(T v) {
if (n >= capacity) {
T* tmp = new T[capacity << ];
memcpy(tmp, arr, sizeof(T) * capacity);
capacity <<= ;
}
arr[n++] = v;
shiftUp(n - );
} void del(int index) {
if (index < || index >= n) return;
swap(arr[index], arr[n - ]);
shiftDown(index, --n);
} void shiftDown(int st, int ed) {
T tmp = arr[st];
while (st < ed) {
int next = -;
int left = st * + ; // left child node
if (left < ed) next = left;
else break;
int right = st * + ;
if (right < ed && arr[right] < arr[next]) next = right;
if (arr[next] < arr[st]) {
arr[st] = arr[next];
st = next;
} else break;
}
arr[st] = tmp;
} void shiftUp(int ed) {
T tmp = arr[ed];
while (ed > ) {
int parent = (ed - ) / ;
if (arr[ed] < arr[parent]) {
arr[ed] = arr[parent];
ed = parent;
} else break;
}
arr[ed] = tmp;
} void sort() {
for (int j = n - ; j >= ; --j) {
swap(arr[], arr[j]);
shiftDown(, j); // here should be j
}
} void print() const {
for (int i = ; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
} T get(int pos) {
return arr[pos];
} void update(int pos, int v) {
if (pos < || pos >= n) return;
if (arr[pos] < v) {
arr[pos] = v;
shiftDown(pos, n);
} else {
shiftUp(pos);
}
}
void increase(int pos, int v) {
if (pos < || pos >= n) return;
arr[pos] = arr[pos] + v;
shiftDown(pos, n);
} void decrease(int pos, int v) {
if (pos < || pos >= n) return;
arr[pos] = arr[pos] - v;
shiftUp(pos);
} bool empty() const {
return n <= ;
} void pop() {
if (empty()) return;
del();
} T top() {
if (empty()) return;
return arr[];
} private:
T* arr;
int n;
int capacity;
}; int main()
{
int arr[] = {, ,,,,};
Heap<int> heap;
for (int i = ; i < ; ++i) {
heap.insert(arr[i]);
}
//heap.sort();
heap.print();
heap.del();
heap.print();
heap.increase(, );
heap.print();
return ;
}
data structure | heap的更多相关文章
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
随机推荐
- Android 模拟器genymotion安装,eclipse 插件
genymotion是一款号称速度最快性能最好的android模拟器,它基于Oracle VM VirtualBox.支持GPS.重力感应.光.温度等诸多传感器:支持OpenGL 3D加速:电池电量模 ...
- Linux的文件管理
绝对路径和相对路径: 绝对路径: /home/tony/Desktop 相对路径:Desktop 或者./Desktop不可写成/Desktop(这是绝对路径的写法) 其中.代表本层目录,..代表上层 ...
- sql,联合主键,按id分组求版本号最大值的集合
表结构如下: /* SQLyog v10.2 MySQL - 5.5.39 ************************************************************** ...
- 防止黑客对服务器IP地址的攻击
下面的参数都是系统默认的: [root@ok etc]# cat /proc/sys/net/ipv4/conf/eth0/accept_source_route [root@ok etc]# cat ...
- Oracle主键操作
http://blog.csdn.net/zhanggnol/article/details/6221895
- git branch -D 大写的D 删除分支
今天删除本地分支 git branch -d XXX 提示: the branch XXX is not fully merged 原因:XXX分支有没有合并到当前分支的内容 解决方法:使用大写的 ...
- sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...
- loj 1004(dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25830‘ 思路:类似与数塔问题,自底向上处理,输入的时候稍微注意一 ...
- Hark的数据结构与算法练习之鸽巢排序
算法说明 鸽巢排序是分布排序的一种,我理解其实鸽巢就是计数排序的简化版,不同之处就是鸽巢是不稳定的,计数排序是稳定的. 逻辑很简单,就是先找出待排数组的最大值maxNum,然后实例一个maxNum+1 ...
- Bash的基础知识man手册
Bash的基础知识man手册 由于基于Android类设备的渗透测试都是通过各类终端实现.所以掌握Shell相关操作就显得尤为重要.Bash是一个为GNU计划编写的Unix Shell本文选自基于An ...