本文整理汇总了C++中PriorityQueue::Top方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::Top方法的具体用法?C++ PriorityQueue::Top怎么用?C++ PriorityQueue::Top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::Top方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
// Builds the tree
TreeNode<SymbolPriority>* MakeTree(const string& message)
{
char currentChar;
vector<SymbolPriority> temp;
PriorityQueue<TreeNode<SymbolPriority>*> myPriorityQueue;
for (int i = 0; i < int(message.size()); i++)
{
currentChar = message[i];
if ( temp.empty() )
temp.push_back( SymbolPriority(currentChar, 1) );
else if ( characterExists(temp, currentChar) )
{
for (int c = 0; c < int (temp.size()); i++)
if (currentChar == temp[i].Symbol)
temp[i].Priority++;
}
else
temp.push_back( SymbolPriority(currentChar, 1) );
}
for (int i = 0; i < int(temp.size()); i++)
{
if (myPriorityQueue.GetSize() <= 1)
myPriorityQueue.Push( new TreeNode<SymbolPriority>( temp[i]) );
else
{
char aChar;
TreeNode<SymbolPriority>* tempNode;
// create a new TreeNode<SymbolPriority>* and
// make the first popped element its left child
// make the second popped element its right child
// set its value to the sum of its left and right child priorities
tempNode->Left = myPriorityQueue.Top();
aChar = myPriorityQueue.Top()->Data.Priority;
myPriorityQueue.Pop();
tempNode->Right = myPriorityQueue.Top();
aChar += myPriorityQueue.Top()->Data.Priority;
myPriorityQueue.Pop();
myPriorityQueue.Push( tempNode );
}
}
return myPriorityQueue.Top();
}
示例2: FindMedian
/***************************************************************************
* Que 7. Find the median dynamically for given input array.
*
* Comments :
* 1) A median heap can be implemented using two heaps each
* containing half the elements.
* 2) One the max-heap containing the smallest elements, the
* other is a min-heap, containing the largest elements.
* 3) The size of the max-heap may be equal to the size of
* the min-heap.
* 4) If the total number of elements are even, the median is
* the average of the maximum elements of the max-heap and
* the minimum element of the min heap.
* 5) if there are an odd number of elements, the max-heap will
* contain one more element than the min-heap. The median in
* this case is simply the maximum element of the max-heap.
*
* **************************************************************************/
int FindMedian(int val, int& median, PriorityQueue& right, PriorityQueue& left)
{
int lcount = left.Size();
int rcount = right.Size();
int returnVal = 0;
// both same number of elements after adding one more element
if(lcount == rcount)
{
if(val < left.Top())
{
//Element will go to left
left.InsertElement(val);
returnVal = left.Top();
}
else
{
//Element will go to right
right.InsertElement(val);
returnVal = right.Top();
}
}
else if( lcount > rcount ) //Left heap has more elements (max heap)
{
if(val < left.Top())
{
right.InsertElement(left.DeleteTop());
left.InsertElement(val);
}
else
{
right.InsertElement(val);
}
returnVal = Average(left.Top(),right.Top());
}
else if( lcount < rcount ) //Right heap has more elements (min heap)
{
if(val < left.Top())
{
left.InsertElement(val);
}
else
{
left.InsertElement(right.DeleteTop());
right.InsertElement(val);
}
returnVal = Average(left.Top(),right.Top());
}
return returnVal;
}
开发者ID:pushpendragold,项目名称:ADARevision,代码行数:68,代码来源:07Heap+(goldie-desktop's+conflicted+copy+2014-01-13).cpp
示例3: bestFirstSearch
void bestFirstSearch(state_t *state) {
PriorityQueue<state_t> pq;
pq.Add(0,0,*state);
state_map_t *map = new_state_map();
state_map_add(map,state,0);
state_t currentState;
state_t child;
int *cost;
int ruleid, priority, childCost;
ruleid_iterator_t iter;
while (!pq.Empty()) {
priority = pq.CurrentPriority();
currentState = pq.Top();
pq.Pop();
nodes++;
cost = state_map_get(map,¤tState);
if (cost == nullptr || priority <= *cost) {
/* The state added to map is Gray */
state_map_add(map,¤tState,priority);
if (is_goal(¤tState)) {
fprintf(output, ": - %i %lu ", priority, nodes);
destroy_state_map(map);
return;
}
init_fwd_iter(&iter,¤tState);
while((ruleid = next_ruleid(&iter)) >= 0) {
apply_fwd_rule(ruleid,¤tState,&child);
if (h.getHeuristic(&child) < UINT_MAX) {
childCost = priority + get_fwd_rule_cost(ruleid);
state_map_add(map,&child,childCost);
pq.Add(childCost,childCost,child);
}
}
}
}
destroy_state_map(map);
cout << "No goal found.\n";
}
示例4: main
int main( int argc, char **argv )
{
// VARIABLES FOR INPUT
char str[ MAX_LINE_LENGTH +1 ] ;
ssize_t nchars;
state_t state; // state_t is defined by the PSVN API. It is the type used for individual states.
PriorityQueue<state_t> open;
//state_map_t *map = new_state_map();//******
// VARIABLES FOR ITERATING THROUGH state's SUCCESSORS
state_t child;
ruleid_iterator_t iter; // ruleid_terator_t is the type defined by the PSVN API successor/predecessor iterators.
int ruleid ; // an iterator returns a number identifying a rule
int64_t totalNodes;
// READ A LINE OF INPUT FROM stdin
printf("Please enter a state followed by ENTER: ");
if ( fgets(str, sizeof str, stdin) == NULL ) {
printf("Error: empty input line.\n");
return 0;
}
// CONVERT THE STRING TO A STATE
nchars = read_state( str, &state );
if (nchars <= 0) {
printf("Error: invalid state entered.\n");
return 0;
}
printf("The state you entered is: ");
print_state( stdout, &state );
printf("\n");
List StateList = List();
// state_map_add( map, &state, 0 );//******dont know if its a must
open.Add(0,0,state);
StateList.add(&state);
StateList.change_color(&state,1);// Gray
// StateList.change_distance(&state,0);
totalNodes = 0;
int d = 0;
/* Search */
while ( !open.Empty() ) {
// g.n
d = open.CurrentPriority();
printf("D: %d\n",d);
state = open.Top();
open.Pop();
totalNodes++;
/* DDD */
if (StateList.get_color(&state)==0 || (StateList.get_distance(&state)>(d-1))){
StateList.change_distance(&state,d);
if (is_goal(&state)==1){
//PRINT STUFF
printf("Estado: ");
print_state( stdout, &state);
printf(" Estados Generados: %"PRId64" , costo: %d",totalNodes,StateList.get_distance(&state));
return 1;//SUCCES
}
/* expand node */
init_fwd_iter(&iter, &state);
while((ruleid = next_ruleid(&iter)) >= 0){
apply_fwd_rule(ruleid,&state,&child);//'child' is a succesor state_t of 'state'
StateList.add(&child);
StateList.change_color(&child, 1);//Gray
const int child_d = d + get_fwd_rule_cost(ruleid);
StateList.change_distance( &child , child_d );
open.Add( child_d , child_d , child );
}
StateList.change_color(&state,2); //Black
}
}
printf("FAIL!");
return 2; //FAIL
}