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


C++ MinHeap类代码示例

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


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

示例1: RunOperation

void RunOperation(char operation, const std::vector<std::string>& arguments,  MinHeap<T>& heap, std::ostream& file)
{
	if(operation == 'I')
	{
		auto value = ToInt(arguments[1]);
		heap.Insert(value);
	}
	else if(operation == 'D')
	{
		auto value = ToInt(arguments[1]);
		heap.Delete(value);
	}
	else if(operation == 'C')
	{
		auto oldValue = ToInt(arguments[1]);
		auto newValue = ToInt(arguments[2]);
		heap.Change(oldValue, newValue);
	}
	else if(operation == 'P')
	{
		heap.PrintPostOrder(file);
		file << std::endl;
	}

}
开发者ID:chadsmith12,项目名称:Min-Heap,代码行数:25,代码来源:program4.cpp

示例2: pushPopTest

void pushPopTest() {
  MinHeap minHeap;
  std::vector<Pipe> data;
  data.push_back(std::make_pair(9, 1.0));
  data.push_back(std::make_pair(8, 2.0));
  data.push_back(std::make_pair(7, 3.0));
  data.push_back(std::make_pair(6, 4.0));
  data.push_back(std::make_pair(5, 5.0));
  data.push_back(std::make_pair(4, 6.0));
  data.push_back(std::make_pair(3, 7.0));
  data.push_back(std::make_pair(2, 8.0));
  data.push_back(std::make_pair(1, 9.0));
  std::random_shuffle(data.begin(), data.end());
  for (auto const& pipe : data)
    minHeap.push(pipe);
  int sum = 0;
  int last = INT_MAX; 
  while (!minHeap.empty()) {
    const auto& pipe = minHeap.top();
    sum += pipe.first;
    if (pipe.first > last)
      throw std::runtime_error("The output of the heap is not in the right order.");
    last = pipe.first;
    minHeap.pop();
  }
  if (sum != 45)
    throw std::runtime_error("The sum of the heap output is incorrect.");
}
开发者ID:whyzidane,项目名称:CO2_project,代码行数:28,代码来源:min_heap_class_test.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: dijkstra

void dijkstra(int gf, int gc)
{
	MinHeap H;
	init(H);	
	while(not H.empty()) 
	{
	    Node current = H.top();	    
	    H.pop();	    	    
	    if(current.f == gf and current.c == gc)
	    	return;
	    visited[current.f][current.c] = true;
	    for (int i = 0; i < 4; ++i)
	    {
	    	int nf = current.f + dx[i];
	    	int nc = current.c + dy[i];	    	
	    	if ((nf >= 0 and nf < N) and (nc >= 0 and nc < M) and not visited[nf][nc])
	    	{
	    		if(range[nf][nc] > maze[nf][nc] + range[current.f][current.c])
	    		{
	    			range[nf][nc] = maze[nf][nc] + range[current.f][current.c];
	    			H.push(Node(nf, nc, range[nf][nc]));
	    		}	    		
	    	}
	    }
	}
}
开发者ID:SantiagoONE,项目名称:Programming-Challenge,代码行数:26,代码来源:929.cpp

示例5: test_minheap

bool test_minheap()
{
	srand(time(NULL));
	MinHeap<long long> minheap;
	int lim = 1000;

	minheap.insert(12);minheap.insert(15);minheap.insert(11);minheap.insert(1);minheap.insert(12);

	if(DEBUG)printf("heap find (15) = %s\n",minheap.find(15)?"TRUE":"FALSE");
	for(long long i = 0; i < lim; i++)
	{
		minheap.insert(rand() % lim  + (rand() < (RAND_MAX/8)?-lim/10:lim));
	}
	if(DEBUG)
	{
		printf("First 15 of 1000 sorted is = [");

		for(long long i = 0; i < 15; i++)
		{
			printf("%lld,", minheap.remove_min());
		}
		printf("...]\n");

		printf("get nth = %lld\n", minheap.get_nth(15));
	}

	MinHeap<int> heap2;
	for(int i = 0; i < 1000; i ++)
	{
		heap2.insert(i);
		while(heap2.size()>50)heap2.remove_min();
	}

	return true;
}
开发者ID:jacobcalvert,项目名称:GenericDataStructures,代码行数:35,代码来源:tests.cpp

示例6: mexFunction

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
	if( nrhs!=4 )
		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");
    if( !mxIsNumeric(prhs[3]) )
		mexErrMsgTxt("parameter 3 missing!\n");

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

	// push in the PQ
	try{
        heap->push( key1, key2, index-1 );
    } 
    catch( InvalidKeyIncreaseException exc ){
        return;
    }
	// return control to matlab
	return;
}
开发者ID:USTfgaoaa,项目名称:Motor-Control-Model,代码行数:33,代码来源:pq_pushMin.cpp

示例7: heapSortUsingMinHeap

void heapSortUsingMinHeap(T arr[], int n){

    MinHeap<T> minheap = MinHeap<T>(n);
    for( int i = 0 ; i < n ; i ++ )
        minheap.insert(arr[i]);

    for( int i = 0 ; i < n ; i ++ )
        arr[i] = minheap.extractMin();

}
开发者ID:liuyubobobo,项目名称:Play-with-Algorithms,代码行数:10,代码来源:main.cpp

示例8: main

int main() {
    int array[] = {10, 4, 5, 6, 9, 1, 7};

    MinHeap *minHeap = new MinHeap(array, 6);

    /*
     * General heap functions
     */
    cout << "Min: " << minHeap->get_min() << endl;
    return 0;
}
开发者ID:g12mcgov,项目名称:Interview_Practice,代码行数:11,代码来源:main.cpp

示例9: heapSortDesc

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

	MinHeap h = buildMinHeap(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

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

示例11: main

int main(int argc,char*argv[]) {
	if(argc !=3) {
		cout<<"Error Incorrect Syntax"<<endl;
	} else {
		string inputFile = argv[1];
		string outputFile = argv[2];
		MinHeap minHeap;
		ifstream in;
		int noOfArgs = 0,jobId,priority,duration;	
		string fileLine;
		in.open(inputFile.c_str());
		in >> noOfArgs;
		//dout<<" total args is "<<noOfArgs<<endl;
	//while (!in.eof()) {
		for(int i=0;i<noOfArgs;i++) {
			in >>jobId>>priority>>duration;
			//cout<<" arg "<<jobId<<" "<<priority<<" "<<duration<<endl;
			//cout<<"insertingggggggg......"<<endl;
			//dout<<" i is "<<
			HeapNode *heapNode = new  HeapNode(jobId,priority,duration);
			minHeap.insertHeapNode(heapNode);
			//dout<<" heap is ==="<<endl;
			//preorder();
			//dout<<" heap print over"<<endl;
		}
	//}
	cout<<"Insert over"<<endl;
	minHeap.debug_image("debug_after_insert");
	ofstream myfile;
	myfile.open (outputFile.c_str());
 	
  	
	if(!in.eof()) {
		char ch;
		in>>ch;
		if(ch == 'P'){	
		string str = minHeap.preorderString();
		cout<<"preorderString is "<<	str<<endl;
		myfile << str<<"\n";
		}
	}
	string hs = heapSort(minHeap);
	myfile << hs;
	myfile.close();
	in.close();
	//dout<<"At last"<<endl;
	//preorder();
	}
开发者ID:nithinjs88,项目名称:ADSA_LAB_2014,代码行数:48,代码来源:main.cpp

示例12: MinHeap

/*
Creates the Huffman tree reusing the given HuffmanNodes.

nodes: an array of HuffmanNode*
length: the length of nodes array

returns: the pointer to the root of the huffman tree
*/
HuffmanNode *genHuffmanTree(HuffmanNode **nodes, int length) {

	MinHeap min = MinHeap(nodes, length);//create the array with minHeap constructor

	while (min.heapSize > 1)//it will run until the array has size one
	{
		HuffmanNode *L, *R;//we make the nodes for left and right
		L = min.extractMin();//we extract the min from the heap and assign it to left
		R = min.extractMin();//we do the same for the right
		HuffmanNode *internal = new HuffmanNode(L->frequency + R->frequency, L, R);//we create an internal node with the sum of the frequency left and right.
																				   //we also assign left and right to be child of internal
		min.insert(internal);//we insert the internal node back into the heap
	}

	return min.extractMin();//we extract the min but since we only got one element left in the array, this should be the root.
}
开发者ID:Sunny3oy,项目名称:Huffman-code,代码行数:24,代码来源:huffmancode.cpp

示例13: BlockManager

void RetManager::retrieval(unsigned retNum)
{
	retDocID = new unsigned[retNum];
	retDocScore = new float[retNum];

	BlockManager *BM = new BlockManager(r,num,retNum);

	int i,l,n;
	while((curDoc = findNextDoc())!=MaxNum)
	{
		for(i=0;i<num;i++) if(r[i].curDocID<curDoc) r[i].curDocID = moveToDoc(i,curDoc);
		BM->putDoc(curDoc,r);
	}
	const float okapiK1=1.2;
	const float okapiB=0.2;
	BlockNode ** blockList = BM->getBlockList();
	vector<pair<unsigned,unsigned*> >::iterator it;
	for(i=0;i<BM->getBlockNum();i++)
	{
		n=blockList[i]->termScores.size();
		int theSize = blockList[i]->content->record.size();
		if(theSize+retN>retNum) theSize = retNum-retN;
		if(theSize==0) continue;
		topDocs = new MinHeap(theSize);
		for(it=blockList[i]->content->record.begin();it!=blockList[i]->content->record.end();it++)
		{
			unsigned* theTF = it->second;
			float score = 0;
			float docLength = theIndex -> getDocLength(it->first);
			for(l=0;l<n;l++)
			{
				float tf = theTF[l];
				float weight = ((okapiK1+1.0)*tf) / (okapiK1*(1.0-okapiB+okapiB*docLength/theIndex->docLengthAvg)+tf);
				score+=weight*blockList[i]->termScores[l];
			}
			if(score > topDocs->smallest) topDocs->push(it->first,score);
			evalCounter++;
		}
		for(l=retN+theSize-1;l>=retN;l--)
		{
			retDocID[l] = topDocs->pop(retDocScore[l]);
		}
		retN+=theSize;
		delete(topDocs);
	}
	delete(BM);
}
开发者ID:Adam57,项目名称:SIGIR2016,代码行数:47,代码来源:retrieval-block.cpp

示例14: heapSort

string heapSort(MinHeap minHeap){
	ostringstream oss("");
	HeapNode*ptr = minHeap.extractMin();
	while(ptr !=NULL) {
		oss<<ptr->getJobId();
		oss<<" ";
		oss<<ptr->getPriority();
		oss<<" ";
		oss<<ptr->getDuration();
		oss<<" ";	
		oss<<endl;
		ptr = minHeap.extractMin();
	}
	cout<<"heap sort over"<<endl;
	cout <<oss.str()<<endl;
	return oss.str();	
}
开发者ID:nithinjs88,项目名称:ADSA_LAB_2014,代码行数:17,代码来源:main.cpp

示例15: lessMoney

int lessMoney(Arr& data) {
	int cost = 0;
	MinHeap result;
	for (auto e : data)
		result.push(e);
	while (result.size() != 1) {
		auto tmp = result.top();
		result.pop();
		tmp += result.top();
		result.pop();
		result.push(tmp);
	}
	return result.top();
}
开发者ID:AverJing,项目名称:LeetCode,代码行数:14,代码来源:Code01_lessMoney.cpp


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