当前位置: 首页>>代码示例>>C++>>正文


C++ MaxHeap类代码示例

本文整理汇总了C++中MaxHeap的典型用法代码示例。如果您正苦于以下问题:C++ MaxHeap类的具体用法?C++ MaxHeap怎么用?C++ MaxHeap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了MaxHeap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {

    MaxHeap<int> maxheap = MaxHeap<int>(100);
    cout<<maxheap.size()<<endl;

    return 0;
}
开发者ID:JachinYin,项目名称:Play-with-Algorithms,代码行数:7,代码来源:main.cpp

示例2: mexFunction

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=3 )
		mexErrMsgTxt("This function requires 3 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");
	if( !mxIsNumeric(prhs[1]) )
		mexErrMsgTxt("parameter 2 missing!\n");
	if( !mxIsNumeric(prhs[2]) )
		mexErrMsgTxt("parameter 3 missing!\n");


	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);
	// retrieve the parameters
	int index;
	retrieve_index( prhs[1], index );
	double cost;
	retrieve_cost( prhs[2], cost);

	// push in the PQ
	try{
        heap->push( cost, index-1 );
    } 
    catch( InvalidKeyIncreaseException exc ){
        return;
    }
	// return control to matlab
	return;
}
开发者ID:AleksanderSas,项目名称:GeometricModeling_2016,代码行数:30,代码来源:pq_push.cpp

示例3: handleQuery

float handleQuery(float prev,int next,MaxHeap& maxheap,MinHeap& minheap){

    if(next>prev){
        if(minheap.n>maxheap.n){
            maxheap.insert(minheap.extractMin());
        }

        minheap.insert(next);
    }else{
        if(maxheap.n>minheap.n){
            minheap.insert(maxheap.extractMax());
        }
        maxheap.insert(next);
    }

    if((maxheap.n+minheap.n)%2==0){


        float l = maxheap.a[0];
        float r = minheap.a[0];

        return (l+r)/2;

    }else{

        if(maxheap.n>minheap.n)
            return maxheap.a[0];

        return minheap.a[0];
    }


}
开发者ID:suraj0208,项目名称:practice,代码行数:33,代码来源:running_median.cpp

示例4: main

int main(int arg, char* argv[])
{
    int dummy;
    MaxHeap<int> mh;

    mh.Insert(9).Insert(3).Insert(8).Insert(2).Insert(7).Insert(6).Insert(4).Insert(5).Insert(1);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 60);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    mh.DeleteMax(dummy);
    cout << "MaxHeap elements by level: " << mh << endl;
    mh.PrintTreeVertically(cout, 40);

    // Sorting
    int a[] = { 0, 1, 4, 8, 9, 2, 1, 4, 3, 5, 7, 6, 3 };
#define ELEM_COUNT(a)   (sizeof(a) / sizeof(a[0]))

    MaxHeap<int>::Sort(a, ELEM_COUNT(a));
    cout << "Sorting with MaxHeap:" << endl;
    for (int i = 0; i < ELEM_COUNT(a); i++) {
        cout << a[i] << ", ";
    }
    cout << endl;

    return 0;
}
开发者ID:forawhile,项目名称:DSCPP,代码行数:34,代码来源:09_MaxHeap.cpp

示例5: 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

示例6: main

int main()
{
    MaxHeap<int> heap;
    int n=5;
    cout<<"初始数组大小:"<<n<<endl;;
    //cin>>n;
    cout<<"输入的数组:";
    //a = new int[n];
    int a[6]={0,5,4,11,3,7};
    for(int i=1;i<=n;i++)
        //cin>>a[i];
        cout<<a[i]<<" ";
    cout<<endl;
    cout<<"初始化为最大堆:"<<endl;
    heap.Initialize(a,n,100);
    heap.Output();
    cout<<"\n插入10之后的最大堆:\n";
    heap.Insert(10);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
    cout<<"\n删除最大数之后的最大堆:\n";
    int b=0;
    heap.DeleteMax(b);
    heap.Output();
    cout<<"当前堆的大小:"<<heap.Size()<<endl;
	getchar();
    return 0;
}
开发者ID:jingyihiter,项目名称:Software-design-and-Practice,代码行数:28,代码来源:最大堆.cpp

示例7: heapSortAsc

void heapSortAsc(int array[], int arrLength) {

	MaxHeap h = buildMaxHeap(array, arrLength);
	int temp = h.size;
	for(int i = arrLength-1; i > 0; i--) {
		swap(&array[0], &array[i]);
		h.size -= 1;
		h.heapify(0);
	}
	h.size = temp;
	h.printHeap();
}
开发者ID:gauravmurade,项目名称:Geek_Algorithms,代码行数:12,代码来源:Heap.cpp

示例8: greedyKnapsack

double greedyKnapsack(knapsack &k){
	MaxHeap<float, int>* heap = new MaxHeap<float, int>(&compare);
	for(int counter = 0; counter < k.getNumObjects(); counter++){
		double key = (double) k.getValue(counter) / k.getCost(counter);
		heap->add(key, counter);
	}
	while(! heap->empty()){
		int next = heap->extractMaxHeapMaximum();
		if(k.getCost(next) + k.getCurrentCost() <= k.getCostBound()){
			k.select(next);
		}
	}
	return k.getCurrentValue();
}
开发者ID:bcrafton,项目名称:Algorithms3,代码行数:14,代码来源:branch_and_bound_knapsack.cpp

示例9: main

int main()
{
	MinHeap<int> minHeap;
	MaxHeap<int> maxHeap;

	minHeap.push(10);
	minHeap.push(5);
	minHeap.push(12);
	minHeap.push(3);
	minHeap.push(4);

	maxHeap.push(10);
	maxHeap.push(5);
	maxHeap.push(12);
	maxHeap.push(3);
	maxHeap.push(4);

	while ( !minHeap.empty()) {
		std::cout << minHeap.top() << " ";
		minHeap.pop();
	}
	std::cout << std::endl;

	while ( !maxHeap.empty()) {
		std::cout << maxHeap.top() << " ";
		maxHeap.pop();
	}
	std::cout << std::endl;
}
开发者ID:faterer,项目名称:groof_off,代码行数:29,代码来源:MaxMinHeap_v1.cpp

示例10: 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();
}
开发者ID:USTfgaoaa,项目名称:Motor-Control-Model,代码行数:14,代码来源:pq_size.cpp

示例11: main

int main()
{
	int num[] = {8,3,1,5,9,4,2,45,7,22,23,6,-1};
	int size = sizeof(num)/sizeof(num[0]);
	vector<int> arr1(num,num+size);
	vector<int> arr2;
    
    MaxHeap *heap = new MaxHeap(arr1);
    if(!heap)return 0;
    
	arr2 = heap->heapSort();
	for_each(arr2.begin(),arr2.end(),[](int a){cout<< a <<" ";});
	cout<<endl;
    delete heap;
	return 0;
}
开发者ID:Bittree,项目名称:algorithm,代码行数:16,代码来源:heapSort.cpp

示例12: printLocalMax

void printLocalMax(const int *arr, size_t len, size_t window) {
  MaxHeap<ValueWrapper> h;
  for (int i = 0; i < window - 1; ++i) {
    h.add(ValueWrapper(arr[i], i));
  }
  for (int i = window - 1; i < len; ++i) {
    h.add(ValueWrapper(arr[i], i));
    for(;;) {
      ValueWrapper *r = h.top();
      if (i - r->pos < window) {
	cout << r->value << endl;
	break;
      }
      h.pop();
    }
  }
}
开发者ID:jiaz,项目名称:algorithms,代码行数:17,代码来源:local_max.cpp

示例13: main

int main()
{
/*    MaxHeap<int> h = {10,8,7,7,6,3,2,0,1};
    h.Add(11);
    h.print();
    h.RemoveTop();
    h.print(); */

    MaxHeap<int> h = {4,6,2,8,9,2,10};

    std::vector<int> s_vec;
    s_vec = h.Sort();

    for(int v : s_vec)
        std::cout<<v<<" ";
    std::cout<<"\n";

    return 0;
}
开发者ID:valdeski,项目名称:sdp2.3,代码行数:19,代码来源:MaxHeap.cpp

示例14: 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;
}
开发者ID:jacobcalvert,项目名称:GenericDataStructures,代码行数:21,代码来源:tests.cpp

示例15: mexFunction

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=1 )
		mexErrMsgTxt("This function requires 3 arguments\n");
	if( !mxIsNumeric(prhs[0]) )
		mexErrMsgTxt("parameter 1 missing!\n");

	// retrieve the heap
	MaxHeap<double>*  heap;
	retrieve_heap( prhs[0], heap);


	// extract head before popping
	pair<double, int> curr = heap->top();
	plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[0]) = curr.second+1;
	plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
	*mxGetPr(plhs[1]) = curr.first;

	// pop top element in the PQ
	heap->pop();
}
开发者ID:Elucidation,项目名称:UAV-Motion-Planner-Ensemble,代码行数:21,代码来源:pq_pop.cpp


注:本文中的MaxHeap类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。