#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. 并发服务器--02(基于I/O复用——运用Select函数)

    I/O模型 Unix/Linux下有5中可用的I/O模型: 阻塞式I/O 非阻塞式I/O I/O复用(select.poll.epoll和pselect) 信号驱动式I/O(SIGIO) 异步I/O( ...

  2. 如何设计一个可用的web容器

    之前在另外一个平台(http://www.jointforce.com/jfperiodical/article/1035)发表的一篇文章,现在发布到自己的博客上. 开发一个web容器涉及很多不同方面 ...

  3. 64位linux下安装ps模拟器ePSxe

    早就想在爱机上玩ps游戏,特别是彩京的1945一代和非常经典的实况足球2002版.在ubuntu64位下可以通过wine模拟的方式运行windows版的ePSxe,但是总觉得差些呢?非原生啊!网上搜了 ...

  4. LeetCode之旅(22)-House Robber

    题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...

  5. SVN中更改连接用户

    Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在 windows > ...

  6. javascript原始值和对象引用

    一句话来说:原始值是不可变的,而对象引用是可变的. js中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着本质的区别.原始值是不可更改的,任何方法都无法更改一 ...

  7. ruby抓取web页面

    一种方法是Net::HTTP.new方法,返回resp码和实际的data: require 'net/http' h = Net::HTTP.new("www.baidu.com" ...

  8. asp.net mvc控制器激活全分析

    控制器的激活默认情况下使用反射来实现的,这其中采用了DI,单例等设计模式.对于控制器的主要涉及到如下的类:ControllerBuilder.DefaultControllerFactory.Defa ...

  9. IIS部署asp.net mvc网站

    iis配置简单的ASP.NET MVC网站编译器:VS 2013本地IIS:IIS 7操作系统:win 7MVC版本:ASP.NET MVC4sql server版本: 2008 r2 打开VS 20 ...

  10. 如何将程序集安装到全局程序集缓存GAC

    针对一些类库项目或用户控件项目(一般来说,这类项目最后编译生成的是一个或多个dll文件),在程序开发完成后,有时需要将开发的程序集(dll文件)安装部署到GAC(全局程序集缓存)中,以便其他的程序也可 ...