本文整理汇总了C++中MaxHeap::size方法的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap::size方法的具体用法?C++ MaxHeap::size怎么用?C++ MaxHeap::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaxHeap
的用法示例。
在下文中一共展示了MaxHeap::size方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: MedianMaintenance
int MedianMaintenance(MaxHeap<int>& heapLow, MinHeap<int>& heapHigh, int elem)
{
if(heapLow.size() == heapHigh.size())
{
if(heapLow.size())
{
if(elem > heapHigh.get_min())
{
heapHigh.insert(elem);
heapLow.insert(heapHigh.extract_min());
}
else
heapLow.insert(elem);
}
else
heapLow.insert(elem);
}
else
{
if(elem < heapLow.get_max())
{
heapLow.insert(elem);
heapHigh.insert(heapLow.extract_max());
}
else
heapHigh.insert(elem);
}
return heapLow.get_max();
}
开发者ID:happyWinner,项目名称:Design_and_Analysis_of_Algorithms_Part_1_Stanford,代码行数:30,代码来源:MedianMaintenance.cpp
示例2: main
int main() {
MaxHeap<int> maxheap = MaxHeap<int>(100);
cout<<maxheap.size()<<endl;
return 0;
}
示例3: findMedian
double findMedian() {
/*
Three cases: since abs(max_heap_.size() - min_heap_.size()) <= 1
denote x as min(max_heap_.size() - min_heap_.size())
1) size x and x means even elements so it just the average of max of first
heap and min of second heap 2) size x + 1 and x means odd elements so the
max of the first heap is the median element 3) size x and x + 1 means odd
elements so the min of the second heap is the median element
*/
if (max_heap_.size() == min_heap_.size()) {
return (double)(max_heap_.top() + min_heap_.top()) / 2.0;
} else if (max_heap_.size() > min_heap_.size()) {
return max_heap_.top();
} else {
return min_heap_.top();
}
}
示例4: addNum
void addNum(int num) {
if (max_heap_.empty()) {
max_heap_.push(num);
} else {
if (num > max_heap_.top()) {
min_heap_.push(num);
} else {
max_heap_.push(num);
}
// Maintain abs(max_heap_.size() - min_heap_.size()) <= 1
if (max_heap_.size() > min_heap_.size() + 1) { // max_heap_ too large
min_heap_.push(max_heap_.top());
max_heap_.pop();
} else if (min_heap_.size() >
max_heap_.size() + 1) { // min_heap_ too large
max_heap_.push(min_heap_.top());
min_heap_.pop();
}
}
}
示例5: test_maxheap
bool test_maxheap()
{
MaxHeap<int> maxheap;
int lim = 1000;
for(int i = 0;i < lim; i++)
{
maxheap.insert(rand() % lim + (rand() < (RAND_MAX/8)?-lim/10:lim));
}
int *max_sort = new int[maxheap.size()];
int i = 0;
while(maxheap.size())
{
max_sort[i++] = maxheap.remove_max();
}
i = 1;
for(int i = 1; i < lim; i++)
{
if(max_sort[i] > max_sort[i-1])return false;
}
return true;
}
示例6: mexFunction
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
if( nrhs!=1 )
mexErrMsgTxt("This function requires 1 arguments\n");
if( !mxIsNumeric(prhs[0]) )
mexErrMsgTxt("parameter 1 missing!\n");
// retrieve the heap
MaxHeap<double>* heap;
retrieve_heap( prhs[0], heap);
// pop top element in the PQ
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(plhs[0]) = heap->size();
}
示例7: REQUIRE
#include "../../data_structures/max_heap.hpp"
namespace max_heap_test {
TEST_CASE( "Basic FIFO operations maintains max ordering", "[min_heap]" ) {
MaxHeap<int> h;
REQUIRE( h.size() == 0 );
h.insert(5);
REQUIRE( h.peek() == 5 );
h.insert(4);
REQUIRE( h.peek() == 5 );
h.insert(6);
REQUIRE( h.peek() == 6 );
REQUIRE( h.pop() == 6 );
REQUIRE( h.pop() == 5 );
REQUIRE( h.pop() == 4 );
for(int i=0; i<500; i++){
h.insert(rand() % 100);
}
for(int i=0; i<h.size(); i++){
REQUIRE( h.pop() >= h.peek() );
}
}
}