#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. mysql源码:关于innodb中两次写的探索

    两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...

  2. 【JAVA、C++】LeetCode 009 Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...

  3. poj 3750 小孩报数问题 解题报告

    题目链接:http://poj.org/problem?id=3750 约瑟夫问题,直接模拟即可. #include <iostream> #include <string> ...

  4. jQuery工具函数

    要点:1.字符串操作2.数组和对象操作3.测试操作4.URL 操作5.浏览器检测6.其他操作 工具函数是指直接依附于 jQuery 对象,针对 jQuery 对象本身定义的方法,即全局性的函数.它的作 ...

  5. C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)

    #include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...

  6. 必须正确理解的---ng指令中的compile与link函数解析

    这个绝对是深入的知识,但看完之后,对NG的理解就很利害啦. http://www.ifeenan.com/angularjs/2014-09-04-%5B%E8%AF%91%5DNG%E6%8C%87 ...

  7. SQL Server:查询当前服务器有多少连接请求

    有时DBA需要检查当前服务器有多少连接请求,以及连接请求的登录名,客户端版本,主机名,程序名等等之类的信息,我们就可以对服务器的连接状况有所了解,防止不明用户使用. SQL Server本身提供了这么 ...

  8. [转载]“浅拷贝”与“深拷贝”

    对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a=88; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. C++中对象的复制就如同&quo ...

  9. 8.Smack类库

    1.登陆IM Connection.DEBUG_ENABLED = true;//首先激活调试模式 1.1建立连接 首先,在启动DSM Message时,客户端通过XMPPConnection与服务器 ...

  10. javascript优化--01高质量编码

    javascript的浮点数: Javascript的数字都是双精度浮点数: 64位编码数字: 能表达53位精度的整数: 进行位运算时会隐式地转化为32位整数(0,1序列)后计算: 浮点数运算可能会有 ...