#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. GLSL

    变量修饰符 修饰符给出了变量的特殊含义,GLSL中有如下修饰符: ·const – 声明一个编译期常量. ·attribute– 随不同顶点变化的全局变量,由OpenGL应用程序传给顶点shader. ...

  2. Win8 Cisco VPN Client 442错误解决办法

    进入注册表regedit,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CVirtA找到DisplayName, x86系统的将值" ...

  3. time_wait 过多 造成网络慢 实战

    sh-3.2# scripts]# netstat -an|awk '/tcp/ {++S[$NF]}END {for (a in S) print a,S[a]}' TIME_WAIT ESTABL ...

  4. svn 提交冲突(目录下删除文件)

    [root@v01 webtest]# svn ci -m "delete kkk" svn: Commit failed (details follow): svn: Abort ...

  5. Lucene查询索引(分页)

    分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...

  6. 【Ubuntu日常技巧】VirtualBox多网卡路由配置,保障虚拟机连接上外网

    [背景]: 配置Ubuntu 虚拟机双网卡,一个是Host-Only网络,一个是桥接网络.当在虚拟机中同时连接到两个网络后,虚拟机能够ping通内部网络,不能ping通外部网络,如www.baidu. ...

  7. CI如何接受POST请求中的JSON数据

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型 “php://input可以读取没有处理过的POST数据.相较于$HTTP_RAW_POST_DATA ...

  8. PhpStorm 配置Xdebug

    IDE => Xdebug => Apache(XAMPP) => Firefox + easist Xdebug 1>XAMPP停止apache服务;2>在安装目录下找 ...

  9. UVa 11524:In-Circle(解析几何)

    Problem EIn-CircleInput: Standard Input Output: Standard Output In-circle of a triangle is the circl ...

  10. Java Hour3

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为2 Hour,请各位不吝赐教. Hour3 包和i ...