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 ...
随机推荐
- BDA大数据处理流程
可以看出,数据处理用云,可以高效完成.而分析部分应该利用传统的bi工具.
- mongodb系列之--mongodb 主从配置与说明
一.为什么要配置mongodb的主从: 1.做主从,可以说是做数据的备份,有利于故障的恢复 2.做主从,可以做到读写分离,主节点负责写操作,从节点负责读操作,这样就把读写压力分开,保证系统的稳定性. ...
- iOS 网络编程模式总结
IOS 可以采用三类api 接口进行网络编程,根据抽象层次从低到高分别为socket方式.stream方式.url 方式. 一 .socket 方式 IOS 提供的socket 方式的网络编程接口为C ...
- C语言实现快速翻转数组的顺序
#include <stdio.h> void Reverse(int *p , int size) { int i , tmp; for(i = 0 ; i < size/2 ; ...
- Bloom filter 2
1 Bloom filter 计算方法 如需要判断一个元素是不是在一个集合中,我们通常做法是把所有元素保存下来,然后通过比较知道它是不是在集合内,链表.树都是基于这种思路,当集合内元素个数的变大,我们 ...
- LeetCode(34)-Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. 思路: 求一个整数是不是回文树.负数不是, ...
- mysql基础优化-explain的使用-mysql死锁
MySQL的优化 主要包括三个方面,首先是SQL语句的优化,其次是表结构的优化(这里主要指索引的优化),最后是服务器配置的优化. 一.SQL语句的优化 在 where 及 order by 涉及的列上 ...
- 终结python协程----从yield到actor模型的实现
把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...
- XSS攻击过滤处理
关于XSS攻击 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中. XSS漏洞的危害 网络钓鱼,包括盗取各类用户账号: 窃取用户cooki ...
- java 如何使的float保留2位或者多位小数 (转载)
转载自 http://blog.csdn.net/com_stu_zhang/article/details/7214565 方法1: float f = 34.232323; Big ...