#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入门第一季》之面向对象(构造方法)

    /* 构造方法: 给对象的数据进行初始化 格式: A:方法名与类名相同 B:没有返回值类型,连void都没有 C:没有具体的返回值 */ class Student { private String ...

  2. C++多重继承与虚拟继承

    本文只是粗浅讨论一下C++中的多重继承和虚拟继承. 多重继承中的构造函数和析构函数调用次序 我们先来看一下简单的例子: #include <iostream> using namespac ...

  3. 基于Reactjs实现webapp(加精)

    git原文链接:https://github.com/my-fe/wiki/issues/1 由于最近的reactjs实在太火,而且距离第一版已经快2年的时间了,已经相对稳定和成熟了,基于这两个前提下 ...

  4. 如何调整DOS窗口的宽高

    运行->cmd->dos窗口上沿点右键->默认值所选字体字体: 新宋体大小: 13窗口大小宽度: 100高度: 40屏幕缓冲区大小宽度: 100高度: 300 在这里还可以设置依他常 ...

  5. 初探linux子系统集之led子系统(三)

    世界杯结束了,德国战车夺得了大力神杯,阿根廷最终还是失败了.也许3年,5年,或者10年后,人们就不知道巴西世界杯的亚军是谁,但是总是会记得冠军是谁.就像什么考试,比赛,第一永远会被人们所记住,所以我们 ...

  6. 面试之路(10)-BAT面试之java实现单链表的插入和删除

    链表的结构: 链表在空间是不连续的,包括: 数据域(用于存储数据) 指针域(用于存储下一个node的指针) 单项链表的代码实现: 节点类 构造函数 数据域的get,set方法 指针域的get,set方 ...

  7. LeetCode(52)-Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...

  8. getContext在谷歌浏览器中,使用时要先加载canvas对象,否则会提示'getContext is null'

    <body> <canvas id=" style="border:1px solid #c3c3c3;"> Your browser does ...

  9. 《MySQL必知必会》学习笔记_1

    #选择数据库 USE mysql #返回可用数据库列表 SHOW DATABASES #返回当前数据库中可用表 SHOW TABLES #返回表列 SHOW COLUMNS FROM db #显示特定 ...

  10. weblogic上JDBC的配置

    weblogic上JDBC的配置