最小堆和最大堆 heap (zz)
来源:互联网 发布:建立网络销售服务平台 编辑:程序博客网 时间:2024/06/06 03:14
http://www.cppblog.com/NicYun/archive/2008/08/04/57934.html
#include <iostream>#include <string>#include <stdio.h>using namespace std;#define SIZE 500000void swap(int &a,int &b){ int temp = a; a = b; b = temp;}class Heap{ int size; int heap[SIZE];public: virtual bool cmp(int a,int b) = 0;private: inline int fathter(int p) { return p / 2; } inline int LeftSon(int p) { int son = 2 * p; if (son > size) return 0; return son; } inline int RightSon(int p) { int son = 2 * p + 1; if (son > size) return 0; return son; } int ShiftUp(int p) { if (p == 1) return p; if (cmp(heap[p],heap[fathter(p)])) { swap(heap[p],heap[fathter(p)]); return fathter(p); } return p; } int ShiftDown(int p) { int lagest = p; if ((LeftSon(p)) && (cmp(heap[LeftSon(p)],heap[lagest]))) lagest = LeftSon(p); if ((RightSon(p)) && (cmp(heap[RightSon(p)],heap[lagest]))) lagest = RightSon(p); if (lagest != p) swap(heap[lagest],heap[p]); return lagest; }public: Heap() { size = 0; } int insert(int n); void del(int p); void DelHead(); int head(); void init(); bool IsEempty();};int Heap::insert(int n){ size++; heap[size] = n; int where = size; int p; while (((p = ShiftUp(where)) != where)) { where = p; continue; } return where;}void Heap::del(int p){ heap[p] = heap[size]; size--; int where; while (((where = ShiftDown(p)) != p)) { p = where; continue; }}void Heap::DelHead(){ del(1);}int Heap::head(){ if (size == 0) return -1; return heap[1];}void Heap::init(){ size = 0;}bool Heap::IsEempty(){ if (size == 0) return 1; else return 0;}class MaxHeap : public Heap{ bool cmp(int a,int b) { return a > b; }};class MinHeap : public Heap{ bool cmp(int a,int b) { return a < b; }};int main(){ return 0;}
- 最小堆和最大堆 heap (zz)
- 最大堆(max-heap)和最小堆(min-heap)
- 最大堆和最小堆
- 最大堆和最小堆
- 最大堆和最小堆
- 最大堆和最小堆
- 双端堆/最小-最大堆/Double-Ended Heap ?
- 【zz】堆(heap)和栈(stack)
- STL源码—heap最大堆,最小堆
- STL源码—heap最大堆,最小堆(TT)
- 最大堆/最小堆
- 最小堆。最大堆。
- 最大堆/最小堆
- 最大堆/最小堆
- 最大堆/最小堆
- 最大堆、最小堆
- 最大堆/最小堆
- 最大堆/最小堆
- ubuntu给firefox安装插件(转)
- 应聘时漂亮的回答
- c++ 容器类概括性介绍
- 实验: openssl engine
- 时光启程了岁月里所有的狼狈
- 最小堆和最大堆 heap (zz)
- 老子的软件之道 - 道篇 18 标准之上
- Matlab与OpenCV在实际应用中的选择问题(转自机器视觉在线)
- 加油!加油!
- Address already in use: JVM_Bind:8080
- Oil Deposits
- 分享当今最牛们的图像处理资源
- Linux常用命令:cat
- mysql数据库备份