Beispiel #1
0
   void p_queue::push(const value_type& entry, size_type priority)
   {
	   if(used >= capacity) 
		   resize( p_queue::size_type(1.5 * capacity) + 1);
	   
	   heap[used].data = entry;
	   heap[used].priority = priority;
	   ++used;
	   
	   if(used > 1)
	   {
		   bool swap;
		   int temp_used = used;
		   
		   do
		   {
			   if(heap[temp_used - 1].priority > parent_priority(temp_used - 1))
			   {
				   swap_with_parent(temp_used - 1);
				   temp_used = temp_used / 2;
				   swap = true; 
			   }
			   else 
				   swap = false;
		   
		   } while(swap);
	   	
	   }
   }
Beispiel #2
0
 void p_queue::pop()
 {
     assert(size() > 0);
     
     if(size() == 1)
     {
         --used;
         return;
     }
     heap[0] = heap[used - 1];
     used --;
     size_type childIndex = 0;
     size_type childPriority = 0;
     size_type currentPriority = heap[0].priority;
     while(is_leaf(childIndex) == false)
     {
         childPriority = big_child_priority(childIndex);
         if(childPriority >= currentPriority)
         {
             childIndex = big_child_index(childIndex);
             swap_with_parent(childIndex);
         }
         else
         {
             break;
         }
     }
 }
Beispiel #3
0
 void p_queue::push(const value_type& entry, size_type priority)
 {
     if(capacity <= used)
     {
         size_type newCapacity = 1 + (capacity * 1.25);
         resize(newCapacity);
     }
     ItemType newItem;
     newItem.data = entry;
     newItem.priority = priority;
     size_type newItemIndex = used;
     heap[newItemIndex] = newItem;
     ++used;
     while(newItemIndex != 0)
     {
         size_type parentIndex = parent_index(newItemIndex);
         if(priority <= heap[parentIndex].priority)
         {
             return;
         }
         else
         {
             swap_with_parent(newItemIndex);
             newItemIndex = parentIndex;
         }
     }
 }
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
bubble_to_top(node_pointer p_nd)
{
  node_pointer p_parent = parent(p_nd);
  while (p_parent != 0)
    {
      swap_with_parent(p_nd, p_parent);
      p_parent = parent(p_nd);
    }
}
Beispiel #5
0
   void p_queue::pop()
   {
	  assert(used > 0); 
	  
	  heap[0].data = heap[used - 1].data;
	  heap[0].priority = heap[used - 1].priority;
	  
	  used--;
	  
	  size_type i = 0; 
	  
	  while( !is_leaf(i) )
	  {
		  size_type index = big_child_index(i);
		   
		  swap_with_parent(index);
		  
		  i = index; 
	  }
	  
	  
	  
   }
void pq_decrease_key( binomial_queue *queue, binomial_node *node,
    key_type new_key )
{
    node->key = new_key;
    binomial_node *current, *parent;
    while( node->parent != NULL )
    {
        parent = node->parent;
        current = node;
        while( parent->right == current )
        {
            current = current->parent;
            parent = current->parent;
        }

        if( node->key < parent->key )
            swap_with_parent( queue, node, parent );
        else
            break;
    }

    if( new_key < queue->minimum->key )
        queue->minimum = node;
}