本文整理汇总了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;
}
}
示例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.");
}
示例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];
}
}
示例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]));
}
}
}
}
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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();
}
示例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;
}
示例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();
}
示例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.
}
示例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);
}
示例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();
}
示例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();
}