本文整理汇总了C++中BinaryHeap类的典型用法代码示例。如果您正苦于以下问题:C++ BinaryHeap类的具体用法?C++ BinaryHeap怎么用?C++ BinaryHeap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BinaryHeap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runSim
int runSim(int a1, int a2, int a3){
BinaryHeap<Soldier> test;
test.buildSoldiers(a2, a3);
return test.events();
test.makeEmpty();
}
示例2: removeElement
void removeElement(BinaryHeap<int> & h, int index){
cout<< "Removing the number a position " << index << " from the heap." <<endl;
int x = h.remove(index);
cout<< x << " was successfully removed."<<endl;
int size = h.size();
cout<<"The size of the heap is now: "<<size<<endl;
}
示例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: addValuesInHeap
void addValuesInHeap(BinaryHeap<int> &h){
cout<<"Adding 3, 17, 92, 44, 2, and 13 to the binary heap"<<endl;
h.add(3);
h.add(17);
h.add(92);
h.add(44);
h.add(2);
h.add(13);
}
示例7: getAlpha
//getAlpha uses a binaryHeap to get the word I want alphabetized.
string getAlpha(string letters) {
BinaryHeap<char,char> getInOrder;
for (int i = 0; i < letters.length(); i++) {
getInOrder.insert(letters[i], letters[i]);
}
string alphaWord = "";
for (int j = 0; j < letters.length(); j++) {
alphaWord += getInOrder.removeMin();
}
return alphaWord;
}
示例8: 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);
}
示例9: 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()
示例10: main
void main(int argc, char* argv[])
{
Dijkstra* sPath = new Dijkstra();
Vertex* head = new Vertex(0,"v1");
//BUG: the comparison on the Vertex* will fail during insert/remove
BinaryHeap<Vertex*>* pQ = new BinaryHeap<Vertex*>();
pQ->Insert(head);
Vertex* current = pQ->RemoveMin();
sPath->ShortestPath(current, pQ);
getchar();
}
示例11: init
void Pathfinder::search()
{
init();
BinaryHeap openHeap;
mStart->g = 0;
mStart->h = manhattan(mStart, mGoal);
mStart->opened = true;
openHeap.push(mStart);
while (openHeap.size() > 0)
{
// Pop the node with the smallest f value
Node* currentNode = openHeap.pop();
// Check if we hit the target
if (currentNode == mGoal)
{
// Compute the path and exit
generatePath(currentNode);
return;
}
currentNode->closed = true;
std::vector<Node*> neighbors = getNeighborsAdjacentTo(currentNode);
for (int i = 0; i < neighbors.size(); i++)
{
Node* neighbor = neighbors[i];
if (neighbor->closed || neighbor->worldRef->getMaterial()->isCollidable())
continue;
int gScore = currentNode->g + 1;
if(!neighbor->opened || gScore < neighbor->g)
{
neighbor->g = currentNode->g + 1;
neighbor->h = manhattan(currentNode, neighbor);
neighbor->parent = currentNode;
neighbor->opened = true;
openHeap.push(neighbor);
}
}
}
}
示例12: 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;
}
示例13: generate_Soldiers
void generate_Soldiers(BinaryHeap<soldier>& heap, int number_of_Spartans, int number_of_Persians, vector<int>& spartans, vector<int>& persians)
{
soldier newSoldier, minimum;
int faction_Selection, current_Spartans, current_Persians;
current_Spartans = 0, current_Persians = number_of_Spartans;
int timeValue = 0;
while((number_of_Spartans > 0 || number_of_Persians > 0) )
{
faction_Selection = rand() % 2;//Select from 1 to 0 for the faction
if(faction_Selection == 1 && (number_of_Spartans > 0)){
newSoldier.set_Faction(true);//Set the faction of our new soldier.
timeValue = rand() % 51 + 1;
newSoldier.set_actionTime(timeValue);
newSoldier.set_iD(current_Spartans);//Sets the iD to the current value of i. We will step through this i times, creating i soldiers.
spartans.push_back(current_Spartans);//The ID is pushed into the vector of soldiers.
current_Spartans++;
number_of_Spartans--;
heap.insert(newSoldier);//Insert our newly created soldier
}
else if(faction_Selection == 0 && (number_of_Persians > 0)){
newSoldier.set_Faction(false);
timeValue = rand() % 900 + 51;
newSoldier.set_actionTime( timeValue );
newSoldier.set_iD(current_Persians);//Sets the iD to the current value of i. We will step through this i times, creating i soldiers.
persians.push_back(current_Persians);
current_Persians++;
number_of_Persians--;
heap.insert(newSoldier);//Insert our newly created soldier
}
}
}
示例14: processTurn
//Will pick a soldier from our remaining soldiers alive.
//
void processTurn(BinaryHeap<soldier>& heap, vector<int>& soldiers, int& remaining_Soldiers)
{
int position_of_Soldier;
if(remaining_Soldiers == 1) position_of_Soldier = 0;//A condition to ensure kill a soldier is not called when only one soldier is left
else position_of_Soldier = rand() % remaining_Soldiers;
bool target_Condition;
soldier nextUp = heap.findMin(); //Return the smallest value; our next up.
target_Condition = heap.interact_with_Soldier(nextUp.return_Faction(), soldiers[position_of_Soldier]);//False if alive, true if killed
if(target_Condition && nextUp.return_Faction() ) kill_a_Soldier(soldiers, remaining_Soldiers, position_of_Soldier);//Will removed the soldier if dead.
else if(target_Condition && !(nextUp.return_Faction()) ) {//Will invigorate if a soldier is killed, AND the soldier target was a Persian
kill_a_Soldier(soldiers, remaining_Soldiers, position_of_Soldier);//Remove the soldier as per usual
invigorate(heap, soldiers, remaining_Soldiers);//function invigorate will go through the entire array to invigorate soldiers.
}
}
示例15: invigorate
void invigorate(BinaryHeap<soldier>& heap, vector<int>& soldiers, int remaining_Soldiers)
//Stepping through all remaining soldiers, we randomly subtract a small value to allow them to act faster.
{
int currentSoldier, delta;
if(remaining_Soldiers == 0){//We cannot invigorate an array of dead soldiers.
return;
}
for(int i = 0; i < remaining_Soldiers; i++)
{
soldier current = heap.find_by_ID(i);
currentSoldier = soldiers[i];
delta = (rand() % 2 + 1);//determine a random amount to decrease by
heap.decrease_by_iD(currentSoldier, delta);//Manipulate our current heap so that spartans act a little quicker.
}
}