#include <iostream>
#include <time.h>
#include <random> using namespace std; //Binary Heap; Max Heap; class BinaryHeap
{
public:
BinaryHeap();
BinaryHeap(int capacity);
~BinaryHeap(); int insert(int value);
int getIndex(int value);
int removeRoot();
void print();
bool isEmpty(); private:
void sortUp(int start);
void sortDown(int start, int end); int *heap;
int capacity;
int size ;
}; BinaryHeap::BinaryHeap()
{
this->size = ;
this->capacity = ;
heap = new int[this->capacity];
} BinaryHeap::BinaryHeap(int capacity)
{
this->size = ;
this->capacity = capacity;
heap = new int[this->capacity];
} BinaryHeap::~BinaryHeap()
{
this->size = ;
this->capacity = ;
delete[] heap;
} int BinaryHeap::insert(int value)
{ if (this->size==this->capacity) //The heap is full
{
return -;
}
heap[this->size] = value;
this->sortUp(this->size);
this->size++;
return ;
} int BinaryHeap::getIndex(int value)
{
for (int i = ; i < this->size; i++)
{
if (value==this->heap[i])
{
return i;
}
}
return -;
} int BinaryHeap::removeRoot()
{ int index = ;
if (this->size==)
{
return ;//The heap is empty
} this->heap[index] = this->heap[--this->size];
this->sortDown(index, this->size - ); return ;
} void BinaryHeap::print()
{
for (int i = ; i < this->size; i++)
{
cout << "No." << i + << " : " << heap[i] << " " << endl;;
}
} bool BinaryHeap::isEmpty()
{
if (this->size == )
{
return true;
}
else
{
return false;
}
} void BinaryHeap::sortUp(int start)
{
int c = start; //The location of current node
int p = (c - ) / ; //The location of parent node
int temp = heap[c]; //The value of current node while (c>)
{
if (heap[p] > temp)
{
break;
}
else
{
heap[c] = heap[p];
c = p;
p = (p - ) / ;
}
}
heap[c] = temp;
} void BinaryHeap::sortDown(int start, int end)
{
int c=start; //the location of current node
int l = *c + ; //The location of left child
int temp = heap[c]; //The value of current node while (l <= end)
{
if (l<end && heap[l]<heap[l+])
{
l++; //Choose the bigger one between left child and right child
}
if (temp>=heap[l])
{
break;
}
else
{
heap[c] = heap[l];
c = l;
l = * l + ;
}
}
heap[c] = temp;
} int main()
{ BinaryHeap *b = new BinaryHeap(); default_random_engine random(time(NULL)); //C++ 11
uniform_int_distribution<int> num(, );//C++ 11 cout << "Insert 50 randon number which between 0~999 " << endl ;
for (int i = ; i <; i++)
{
b->insert(num(random));
} cout << "Print: " << endl;
b->print(); cout << endl << endl;
cout << "Is the heap empty? " << endl;
cout << boolalpha << b->isEmpty() << endl << endl; cout << "Remove" << endl;
switch (int n=b->removeRoot())
{
case :
cout << "Success! The root node has been removed!" << endl; break;
case :
cout << "Heap is empty! " << endl; break;
}
cout << endl; cout << "Print: " << endl;
b->print(); system("pause");
return ;
}

C++之Binary Heap/Max Heap的更多相关文章

  1. [Algorithm] How to use Max Heap to maintain K smallest items

    Let's say we are given an array: [,,,,,,] We want to get K = 3 smallest items from the array and usi ...

  2. Android内存管理(9)*MAT:Heap Dump,Shallow Heap,Retained Heap,Dominating Tree,GC Roots等的含义

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fheapdump.ht ...

  3. JAVA Shallow heap & Retained heap

    最近在研究内存泄漏的问题,在使用MAT工具中发现了Shallow heap & Retained heap,不懂. 然后在网上找了一些资料. Shallow Size 对象自身占用的内存大小, ...

  4. java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz

    java.lang.OutOfMemoryError: PermGen space PermGen space 由-XX:PermSize  -XX:MaxPermSize 引起 java.lang. ...

  5. Heap Dump (heap=dump)

    Heap Dump (heap=dump) 转储堆内容使用heap=dump选项.可以是ASCII或者是二进制格式,根据设定的格式,jhat解析二进制格式.format=b. 如果指定格式是二进制,转 ...

  6. Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

    Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...

  7. 每个线程分配一个stack,每个进程分配一个heap;heap没有结构,因此寻址慢(转)

    学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词其实有三种含义,适用于不同的场合,必须加以区分. ...

  8. Android 内存管理中的 Shallow heap Retained heap

    所有包含Heap Profling功能的工具(MAT,Yourkit,JProfiler,TPTP等)都会使用到两个名词,一个是Shallow heap Size,另一个是 Retained heap ...

  9. (算法)Binary Tree Max Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

随机推荐

  1. 《java入门第一季》之面向对象(final关键字)

    /* final可以修饰类,方法,变量 特点: final(可以修饰类),该(类)(不能被继承).一旦修饰了一个类,这个类就不能被继承了! final以修饰方法,该方法可以被继承但是不能被重写.(覆盖 ...

  2. SpriteBuilder中音频波长超过Timeline结尾的情况

    见如下图: 注意最后一个音频波长延续到Timeline结尾之后.表明这个音频文件播放长度超过Timeline(动画)播放的长度.这是否成为一个问题要视情况而定.而在这里无所谓. 如果节点所拥有的Tim ...

  3. 轻量级网络库libevent概况

    Libevent is a library for writing fast portable nonblocking IO. libevent是一个为编写快速可移植的非阻塞IO程序而设计的. lib ...

  4. Java-HttpServlet

    /** * * Provides an abstract class to be subclassed to create * an HTTP servlet suitable for a Web s ...

  5. 一张图了解cocos2d坐标系

    一张图了解cocos2d坐标系 平面直角坐标系

  6. ITU-T Technical Paper: QoS的构建模块与机制

    本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...

  7. SVN版本控制器中各符号的含义

    SVN符号的含义 项目开发过程中,随着学习的不断深入,开始慢慢接触到版本管理控制工具,其实这个工具主要用于团队开发之中,但对于个人项目的备份也有好处,可以避免在电脑出现不可预知的故障时,最大化的保护自 ...

  8. Android进阶(十九)AndroidAPP开发问题汇总(三)

    Android进阶(十九)AndroidAPP开发问题汇总(三) Java解析XML的几种方式: http://inotgaoshou.iteye.com/blog/1012188 从线程返回数据的两 ...

  9. how tomcat works 读书笔记 十一 StandWrapper 下

    StandardWrapperValve StandardWrapperValve是StandardWrapper的基础阀,主要完成了三个工作. 1 调用StandardWrapper的allocat ...

  10. Mac OS X汇编语言常识

    首先OS X的syscall表位置在 /usr/include/sys/syscall.h