#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的更多相关文章

  1. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  2. Leetcode: All O`one Data Structure

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  3. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  4. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  5. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  8. 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 ...

  9. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

随机推荐

  1. 如何在Ubuntu下安装”.deb“、”.bin“、”.tar.gz“、”.tar.bz2“格式的软件包!

    今天在Ubuntu11.10中安装Google chrome浏览器是遇到了问题,下载好的“.deb”格式的安装文件google-chrome-stable.deb双击后或者右键快捷菜单选择 Synap ...

  2. java静态与非静态区别

    这里的静态,指以static关键字修饰的,包括类,方法,块,字段. 非静态,指没有用static 修饰的. 静态有一些特点: 1.全局唯一,任何一次的修改都是全局性的影响 2.只加载一次,优先于非静态 ...

  3. 【图文详解】python爬虫实战——5分钟做个图片自动下载器

    python爬虫实战——图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识,(没看的先去看!!)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap sho ...

  4. 手机/平板 连接局域网访问局域网电脑Web服务器进行移动端页面测试

    1.开启本地服务器(我用的是XAMPP) 2.查看本机IP Mac:点击左上角的苹果标志,选择系统偏好设置,弹出系统偏好设置面板-----点击网络,选择高级,切换到tcp/ip 选项卡 会看到本机IP ...

  5. MVC中session创建并获取问题

    有两个ActionResult分别为A和B,如下 public ActionResult A() { Session["test"]="123"; return ...

  6. centos7下安装vsftpd配置

    0. 首先安装ftp服务 yum install -y ftp 1. 通过yum install -y vsftp安装vsftp 2.    修改vi /etc/vsftpd/vsftpd.conf, ...

  7. Wcf for wp8 使用iis Express 承载Wcf服务部署发布网站(三)

    我们接下来要做的是 本地电脑当作服务器(模拟外网服务器)来承载Wcf服务程序,通过引用本地电脑ip地址访问wcf服务程序接口 http://192.168.1.123/Service1.svc 一.先 ...

  8. 深入理解 KVC\KVO 实现机制 — KVO

    KVC和KVO都属于键值编程而且底层实现机制都是isa-swizzing,所以本来想放在一起讲的.但是篇幅有限所以就分成了两篇博文. KVC实现机制传送门 KVO概述 键值观察Key-Value-Ob ...

  9. js:数据结构笔记7--哈希表

    哈希表(散列表):通过哈希函数将键值映射为一个字典; 哈希函数:依赖键值的数据类型来构建一个哈希函数: 一个基本的哈希表:(按字符串计算键值) function HashTable() { this. ...

  10. css 样式 图片平铺整个界面

    比如一个容器(body,div,span)中设定一个背景.这个背景的长宽值在css2.1之前是不能被修改的. 所以实际的结果是只能重复显示,所以出现了repeat,repeat-x,repeat-y, ...