C++之Binary Heap/Max Heap
#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的更多相关文章
- [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 ...
- 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 ...
- JAVA Shallow heap & Retained heap
最近在研究内存泄漏的问题,在使用MAT工具中发现了Shallow heap & Retained heap,不懂. 然后在网上找了一些资料. Shallow Size 对象自身占用的内存大小, ...
- 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. ...
- Heap Dump (heap=dump)
Heap Dump (heap=dump) 转储堆内容使用heap=dump选项.可以是ASCII或者是二进制格式,根据设定的格式,jhat解析二进制格式.format=b. 如果指定格式是二进制,转 ...
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...
- 每个线程分配一个stack,每个进程分配一个heap;heap没有结构,因此寻址慢(转)
学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词其实有三种含义,适用于不同的场合,必须加以区分. ...
- Android 内存管理中的 Shallow heap Retained heap
所有包含Heap Profling功能的工具(MAT,Yourkit,JProfiler,TPTP等)都会使用到两个名词,一个是Shallow heap Size,另一个是 Retained heap ...
- (算法)Binary Tree Max Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...
随机推荐
- Salesforce开发入门
云计算风起云涌,已成势不可挡之势.公司好多项目都依托于云平台了,网络安全采用了zscaler,人力资源系统用的workday,我们case系统也用了salesforce,我自己也在用运行于Google ...
- sql的sum函数(与group by,having子句混合使用)
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Bush' OR Customer='Adams' GROUP BY Custo ...
- 【nginx】4xx,5xx 保持自定义header
问题 nginx使用中,如果请求返回的状态code类似404或者50x这种,仍然返回自定义的header. 分析和解决 nginx文档中关于 add_header的部分 有这么一句 Adds the ...
- c++ list 合并操作函数实例
#include <list> #include <iostream> using namespace std; //list 链表的打印 void print(list< ...
- cocos2d-x 控制台输出日志
在2dx中用CCLog输出日志,但是在vs的控制台中由于信息很多,很难发现.可以用下面方法,会重新启动一个黑色的控制台来输出日志 修改main.c文件,如下: #include "main. ...
- Nginx + IIS实现负载均衡 Session多站点共享
日子过得太索然无味了,研究了一下,所谓的负载均衡(主要是windows服务器IIS下的).先看看分析图:环境:linux服务器: centos 6.3windows服务器: windows serve ...
- mongodb3.6 (四)net 客户端如何连接、访问mongodb集群
前言 在是一篇文章mongodb如何做数据备灾 中已经介绍mongodb集群是如何工作,可能很多人都有这样一个疑问:客户端如何知道主服务挂了呢?这一篇文章将介绍如何在net中访问这个集群. 第一步.安 ...
- java基础语法(一)
java基础语法(一) 1.类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成 对象(Object).对象又叫做实例(Instance). 2.类由两大部分构成:属性 ...
- 免费私有gitLab服务推荐
阿里云code :https://code.aliyun.com/,可以免费开50个私有项目. 配套的持续交付:https://crp.aliyun.com
- 使用Navicat for MySQL把本地数据库上传到服务器
服务器系统基本都是基于linux的,这个数据库上传的方式适用于linux的各种版本,比如Ubuntu和Centos(尽管这两个版本各种大坑小坑,但至少在数据库传输上保持了一致性) 当然本地数据库上传到 ...