Ejemplo n.º 1
0
void decreaseKey( struct MinHeap * minHeap, int target_element, int target_priority ) {

        // get the index of target element 
	int target_element_index = minHeap -> current_position[target_element];

	// get the node and update it's dist value 
	struct MinHeapNode * target_node = minHeap -> array[ target_element_index];

	// check if the new priority is lesser then current priority or not 
	if ( target_priority >= target_node -> priority )
		return;
	target_node -> priority = target_priority;

	// now travel up to fix if any other node up has priority lesser than the current priority 
	int parent_index = get_parent( target_element_index );

	while ( parent_index != -1 && minHeap -> array[parent_index] -> priority > minHeap -> array[ target_element_index]->priority ) {
              
	      // set the index of parent to be of target_element_index
	      minHeap -> current_position[ minHeap -> array[parent_index] -> element ] = target_element_index;
	      // set the index of target element to be of parent 
	      minHeap -> current_position[ minHeap -> array[target_element_index] -> element ] = parent_index;
	      // swap the two pointers
              swapMinHeapNodes( &minHeap -> array[parent_index], &minHeap -> array[target_element_index]);
              // now the target_index will be parent index 
	      target_element_index = parent_index;
	      // new parent index will be parent of previous parent index 
	      parent_index = get_parent(parent_index);

	}
}
Ejemplo n.º 2
0
void minHeapify ( struct MinHeap * heap, int index ) {

        // get the left child index 
	int left_child_index = left_child ( index );
	// get the right child index 
	int right_child_index = right_child ( index );
	// get the current size 
	int current_size = heap -> current_size;
	int smallest = index;
  
	if ( left_child_index < current_size && heap -> array[left_child_index]->priority < heap -> array[smallest]->priority )
	           smallest =  left_child_index; 
	if ( right_child_index < current_size && heap -> array[right_child_index]->priority < heap -> array[smallest]->priority)
	  	   smallest = right_child_index;

        if ( smallest != index ) {

		MinHeapNode * smallestNode = heap -> array[smallest];
		MinHeapNode * currentNode = heap -> array[index];

		// set position of smallest node to 'index'
		// set position of index node to 'smallest' 
		heap -> current_position[smallestNode->element] = index;
		heap -> current_position[currentNode->element] = smallest;

		// swap nodes 
		swapMinHeapNodes( &heap->array[smallest], &heap->array[index] );

		minHeapify( heap, smallest);
	}

}
Ejemplo n.º 3
0
// This is the standard minHeapify function. It does one thing extra.
// It updates the minHapIndex in Trie when two nodes are swapped in
// in min heap
void minHeapify( MinHeap* minHeap, int idx )
{
    int left, right, smallest;
 
    left = 2 * idx + 1;
    right = 2 * idx + 2;
    smallest = idx;
    if ( left < minHeap->count &&
         minHeap->array[ left ]. frequency <
         minHeap->array[ smallest ]. frequency
       )
        smallest = left;
 
    if ( right < minHeap->count &&
         minHeap->array[ right ]. frequency <
         minHeap->array[ smallest ]. frequency
       )
        smallest = right;
 
    if( smallest != idx )
    {
        // Update the corresponding index in Trie node.
        minHeap->array[ smallest ]. root->indexMinHeap = idx;
        minHeap->array[ idx ]. root->indexMinHeap = smallest;
 
        // Swap nodes in min heap
        swapMinHeapNodes (&minHeap->array[ smallest ], &minHeap->array[ idx ]);
 
        minHeapify( minHeap, smallest );
    }
}