本文整理汇总了C++中BinaryHeap::deleteMin方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryHeap::deleteMin方法的具体用法?C++ BinaryHeap::deleteMin怎么用?C++ BinaryHeap::deleteMin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryHeap
的用法示例。
在下文中一共展示了BinaryHeap::deleteMin方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//need this to compare two BinaryTreePtr Objects
//bool BinaryTreePtr::operator<(const BinaryTreePtr& b) const
//{
// return frequency < b.frequency;
//}
int main(int argc, char** argv)
{
ifstream inf(argv[1]);
char character;
//BinaryTreePtr p;
int ascii[256] = {};
int temp[256];
int counter = 0;
BinaryHeap <BinaryTreePtr> heap; //not sure what size
while (inf.get(character)) //reads each character as char
{
int num_char = (int)character; //casts char into int
ascii[num_char]++; //increments array based on asci
}
for (int j = 0; j < 256; j++)
{
if (ascii[j] != 0)
{
int freq = ascii[j];
heap.insert(BinaryTreePtr((char)j,ascii[j]));
}
}
while(!heap.isEmpty())
{
BinaryTreePtr min_1;
heap.deleteMin(min_1);
//cout << (char)min_1.asci_value << min_1.frequency << endl;
if (heap.isEmpty())
{
heap.insert(min_1);
break;
}
else
{
BinaryTreePtr min_2;
heap.deleteMin(min_2);
// cout << (char)min_2.asci_value << min_2.frequency << endl;
BinaryTreePtr parent;
int temp_freq = min_1.Node->getInfo() + min_2.Node->getInfo();
char temp_char = 'Y';
BinaryTreePtr Parent(temp_char, temp_freq, min_1.Node, min_2.Node);
heap.insert(Parent);
}
}
char* random = new char[500]();
int i = 0;
BinaryTreePtr last;
heap.deleteMin(last);
last.Node->print(random, i);
}
示例2: main
int main(int argc, char **argv) {
BinaryHeap<int> testHeap;
std::string input;
std::string numbers = "0123456789";
std::cout << "Input vals to add to BinaryHeap (val1 val2 val3 val4 ... end)\n";
while (std::cin >> input)
{
// if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int)
// since any num higher than 1 will be set to true, add 1 to max int to start at 0(false)
if (input.find_first_not_of(numbers)+1 ) {
if (input == "end")
{
break;
}
else
{
std::cerr << input << " not recognized\n";
continue;
}
} // end if
int num = std::atoi(input.c_str()); // transform string into number, would have normally use boost::lexical_cast though
testHeap.insert(num);
}
std::cout << "Input vals to remove from BinaryHeap (val1 val2 val3 val4 ... end)\n";
while (std::cin >> input)
{
// if 'std::String::find_first_not_of' doesnt find anything, then it outputs max_int (long int)
// since any num higher than 1 will be set to true, add 1 to max int to start at 0(false)
if (input.find_first_not_of(numbers)+1 ) {
if (input == "end")
break;
else
{
std::cerr << input << " not recognized\n";
continue;
}
} // end if
int num = std::atoi(input.c_str());
testHeap.removeKey(num);
}
std::cerr << "\n";
while (!testHeap.isEmpty())
{
std::cerr << testHeap.findMin() << " ";
testHeap.deleteMin();
}
std::cerr << "\n";
return 0;
}
示例3: FindMinUnknown
////////////////////////////////////////////////////////////////
// FINDING MINIMUM UNKNOWN DISTANCE //
////////////////////////////////////////////////////////////////
HeapObj Router::FindMinUnknown(BinaryHeap<HeapObj> &heap){
while(vArray[(heap.findMin()).index]->known)
{ //while the min vertex is unknown, we ignore it and keep popping
heap.deleteMin();
}
return heap.findMin();
}
示例4: generateHuffmanTree
void Encoder::generateHuffmanTree(){
BinaryHeap<node<int> > heap;
node<int> nodes[256];
int numNodes = 0;
for(int i = 0; i < 256; i++)
if(freq[i] > 0 && i!= 10)
cout << i << " " << freq[i] << endl;
for(int i = 0; i < 256; i++){
if(freq[i]>0 && i!=10){
nodes[numNodes].probability = freq[i];
nodes[numNodes].internal = 0;
nodes[numNodes].value = i;
heap.insert(nodes[numNodes]);
numNodes++;
}
}
// nodes disappear after this...
for(int i = 0; i < numNodes; i++){
cout << nodes[i].probability << " " << (char)nodes[i].value << endl;
}
node<int> lnode, rnode;
node<int> parentNodes[300];
int treeNumber = 0;
while(heap.currentSize > 1) //while 2 or more elements in heap
{
heap.deleteMin(lnode);
heap.deleteMin(rnode);
cout << "Lnode = " << lnode.probability << " " << lnode.value << endl;
cout << "Rnode = " << rnode.probability << " " << rnode.value << endl;
parentNodes[treeNumber].internal = 1;
parentNodes[treeNumber].probability = lnode.probability + rnode.probability;
parentNodes[treeNumber].left = &lnode;
parentNodes[treeNumber].right = &rnode;
parentNodes[treeNumber].value = treeNumber;
cout << "Tree " << treeNumber+1 << " has weight: " << lnode.probability + rnode.probability << endl;
cout << "Left child: " << parentNodes[treeNumber].left->probability << " " << parentNodes[treeNumber].left->value << " Right child: " << parentNodes[treeNumber].right->probability << " " << parentNodes[treeNumber].right->value << endl << endl;
heap.insert(parentNodes[treeNumber]);
parentNodes[treeNumber].value = treeNumber++;
}
treeNumber--; // to take away last unneeded ++
//parentNodes[treeNumber--].returnCodes(parentNodes[treeNumber--]);
//returnCodes(&parentNodes[treeNumber--]);
returnCodes(&parentNodes[0]);
returnCodes(&parentNodes[1]);
returnCodes(&parentNodes[2]);
returnCodes(&parentNodes[3]);
}
示例5: find_min_dist_id
int find_min_dist_id(BinaryHeap<HeapVt> & heap_vt, const vector<Vertex> & vt_path) {
HeapVt tmp_vt(-1, -1);
do{
if(heap_vt.isEmpty()) {
return -1;
}
heap_vt.deleteMin(tmp_vt);
}while(true == vt_path[tmp_vt.id].known);
return tmp_vt.id;
}
示例6: main
int main() {
BinaryHeap<int> H;
cout << endl;
vector<int> list = {30,17,20,15,10,12,5,7,8,5,2,9};
for (int i : list)
{
H.insert(i);
}
H.printHeap();
cout << " .....Inserting 1,10,6,4...." << endl;
vector<int> list2 = {1,10,6,4};
for (int i : list2)
{
H.insert(i);
}
H.printHeap();
cout << " ......Performing 3 DeleteMin() ......" << endl;
for (int i = 0 ; i<3; i++ )
{
H.deleteMin();
}
H.printHeap();
cout << endl << H.HeapHeight();
//{30,17,20,15,10,12,5,7,8,5,2,9};
return 0;
}
示例7: writeHeap
void Encoder::writeHeap(stringstream &out, BinaryHeap<HuffmanNode *> heap, int elements) const
{
out << elements; // store how many heap elements at beginning
HuffmanNode *node; // to store temporary node
while(!heap.isEmpty())
{
heap.deleteMin(node);
out << node->data;
out << node->frequency;
} // traverse the entire heap
return;
} // writeHeap()