Пример #1
0
// repairs the heap if this_node is in the wrong place in the heap
void updateHeapPositionOfNode(node* this_node)
{ 
  tempInd = this_node->heapIndex;  
  
  // need to repair heap differently depending on the key vector of heapNode[tempInd]
  if(tempInd > parentOfLast) // then can't go down any more so only try to go up
    bubbleUp(tempInd);
  else if(tempInd == 0) // can't go up any more so only try to go down
    bubbleDown(tempInd);
  else if(nodeLess(heapNode[tempInd], heapNode[(tempInd-1)/2])) // this node is less than its parent so try to go up
    bubbleUp(tempInd);  
  else // this node is not less than its parent, so try to go down
    bubbleDown(tempInd);
}
Пример #2
0
/*Utliza a heap para fazer a ordenacao*/
void heapSort(Elem v[],int N){
	int i;
	for(i=N-1;i>0;i--) {
		swap(v,0,i);
		bubbleDown(v,i);
	}
}
Пример #3
0
int PQueue::deQueue()
{
	if(qsize == 0)
		return NULL;
	else {
		int min = queue[1];

		if(qsize>1) {
			int lastElem = queue[qsize];
			float lastW = weight[qsize];
			queue[1] = lastElem;
			weight[1] = lastW;
			
			qsize--;

			// mark
			//lastElem->pqIndex = 1;
			
			// heapify
			bubbleDown(1);

		} else {
			//queue.erase(queue.begin()+1);
			qsize = 0;
		}

		// mark
		//min->pqIndex = -1;

		return min;
	}
}
 // delete the key associated with index i
 void deleteKey(int i)   {
     int ind = index[i];
     swap(ind, N--);
     bubbleUp(ind);
     bubbleDown(ind);
     index[i] = -1;
 }
Пример #5
0
uint32_t heap_deleteMaxWithPriority(Heap *heap, uint32_t *priority) {
   Node max = heap->elements[0];
   if (priority) {
      *priority = max.priority;
   }

   // Put the last element at the top
   heap->elements[0] = heap->elements[heap->numElements - 1];
   heap->numElements--;

   // Bubble down
   bubbleDown(heap, 0);

   // Reallocate to a smaller array if necessary
   if (heap->numElements <= heap->size / 4) {

      int32_t newSize = MAX(MIN_HEAP_SIZE, heap->size / 2);
      heap->size = newSize;
#if DEBUG
      printf("\ndecreasing heap size to %u...\n\n", newSize);
#endif
      heap->elements = realloc(heap->elements, newSize * sizeof(Node));
   }

   return max.value;
}
Пример #6
0
  // preserve the heap invariant, by moving a violating
  // parent downward
  void binheap::bubbleDown(idx i, idx size) {
    idx left_i = left(i);
    idx right_i = right(i);
    if (right_i < size && heap[right_i] < heap[i]) {
      if (heap[left_i] < heap[right_i]) {
	std::swap(heap[left_i], heap[i]);
	bubbleDown(left_i, size); 
      } else {
	std::swap(heap[right_i], heap[i]);
	bubbleDown(right_i, size); 
      }
    } else if (left_i < size && heap[left_i] < heap[i]) {
      std::swap(heap[left_i], heap[i]);
      bubbleDown(left_i, size);
    }
  }
 // delete the minimal key and return its associated index
 // Warning: Don't try to read from this index after calling this function
 int deleteMin() {
     int min = heap[1];
     swap(1, N--);
     bubbleDown(1);
     index[min] = -1;
     heap[N+1] = -1;
     return min;
 }
Пример #8
0
/*Extrai o valor minimo da heap (primeiro)*/
int extractMin(Heap *h, Elem *x){
	if(h->used == 0) return 1;
	*x = h->values[0];
	h->used--;
	h->values[0] = h->values[h->used];
	bubbleDown(h->values,h->used);
	return 0;
}
Пример #9
0
string HeapPriorityQueue::dequeueMin() {
	if (isEmpty()) {
		error("HeapPriorityQueue is empty. No dequeuing.");
	}
	string returnStr = elems[1];
    elems[1] = elems[size()];
    logicalLength--;
    bubbleDown(1);
    return returnStr;
}
Пример #10
0
/* helper function: bubbleDown(int index)
 *
 * lexicographically repairs the heap
 * by swapping elements with their children
 */
void HeapPriorityQueue::bubbleDown(int index) {
    int childIndex1 = PARENT_CHILD_MULT*index;
    int childIndex2 = PARENT_CHILD_MULT*index + 1;
    bool has1Child = size() == childIndex1;
    bool has2Children = size() >= childIndex2;
    if (has2Children) {
        if (elems[childIndex1] < elems[childIndex2] &&
            elems[index] > elems[childIndex1]) {
            swap(index, childIndex1);
            bubbleDown(childIndex1);
        } else if (elems[index] > elems[childIndex2]) {
            swap(index, childIndex2);
            bubbleDown(childIndex2);
        }
    }else if (has1Child && 
              elems[index] > elems[childIndex1]) {
        swap(index, childIndex1);
    }
}
Пример #11
0
string HeapPriorityQueue::dequeueMin() {
    if (isEmpty()) error("The queue is empty.");
    
    string min = pQueue[1];
    pQueue[1] = pQueue[numAllocated];
    numAllocated--;
    bubbleDown(1);
    
    return min;
}
Пример #12
0
Node* Heap::removeMin() {
	Node *temp = arr[lastPosition];
	arr[lastPosition] = arr[0];
	arr[0] = temp;
	temp = arr[lastPosition];
	arr[lastPosition] = NULL;
	lastPosition--;
	bubbleDown(0);
	numItems--;
	return temp;
}
Пример #13
0
// pHeapBase -> (zero element is not used) and we cannot overflow.
// sorting from pHeapBase+1 to pHeapBase+heapSize;
void cPointSearchSimple_t::heapSort(__int64* pHeapBase, int32_t heapSize) {
    // construct the heap now.
    for (int32_t ii = heapSize / 2; ii > 0; ii--) {
        bubbleDown(pHeapBase, heapSize, ii);
    }

    __int64* pLast = pHeapBase + heapSize;
    __int64* pTop = pHeapBase + 1;
    for (int32_t ii = 1; ii < heapSize - 1; ii++) {
        __int64 vLast = *pLast;
        *pLast = *pTop;
        *pTop = vLast;
        bubbleDown(pHeapBase, heapSize - ii, 1);
        pLast--;
    }

    // for last one, we does not need bubble down.
    __int64 vLast = *pLast;
    *pLast = *pTop;
    *pTop = vLast;
}
Пример #14
0
// deletes this_node from the heap, and then repairs the heap
void deleteNodeFromHeap(node* this_node)
{ 
  this_node->inHeap = closed;
  tempInd = this_node->heapIndex;  
  this_node->heapIndex = -1;
  
  // if this was the last node on the heap, then we are done
  if(tempInd == indexOfLast)
  {
    indexOfLast--;
    
    if(indexOfLast == 0)
      parentOfLast = -1;
    else
      parentOfLast = (indexOfLast-1)/2;
    
    return;
  }
  else if(tempInd == 0) // if this is the top node, then just pop it
  {
    popHeap();
    return;
  }  
  
  // put last node from heap where this node used to be
  heapNode[tempInd] = heapNode[indexOfLast];
  heapNode[tempInd]->heapIndex = tempInd;
  
  // take care of heap values at old last node position
  heapNode[indexOfLast] = NULL;
  indexOfLast--;
  
  if(indexOfLast == 0)
    parentOfLast = -1;
  else
    parentOfLast = (indexOfLast-1)/2;
  
  // need to repair heap differently depending on the key vector of heapNode[tempInd]
  if(tempInd > parentOfLast) // then can't go down any more so only try to go up
  {
    bubbleUp(tempInd);
  }
  else if(nodeLess(heapNode[tempInd], heapNode[(tempInd-1)/2])) // this node is less than its parent so try to go up
  {
      bubbleUp(tempInd);
  }
  else // this node is not less than its parent, so try to go down
  {
    bubbleDown(tempInd); 
  }
}
Пример #15
0
void bubbleDown(POTNODE a, GRAPH G, POT P, int last)
{
    POTNODE child;
    if (MYDEBUG) printf("Entering bubbleDown.\n");
    child = 2*a;
    if (child < last &&
        priority(child+1, G, P) < priority(child, G, P))
        ++child;
    if (child <= last &&
        priority(a, G, P) > priority(child, G, P)) {
        swap(a, child, G, P);
        bubbleDown(child, G, P, last);
    }
    if (MYDEBUG) printf("Exiting bubbleDown.\n");
}
Пример #16
0
void Heap::bubbleDown(int pos) {
	int min = pos;
	if (getLeftIndex(pos) <= lastPosition && arr[getLeftIndex(pos)]->priority < arr[min]->priority) {
		min = getLeftIndex(pos);
	}
	if (getRightIndex(pos) <= lastPosition && arr[getRightIndex(pos)]->priority < arr[min]->priority) {
		min = getRightIndex(pos);
	}
	if (min != pos) {
		Node *temp = arr[pos];
		arr[pos] = arr[min];
		arr[min] = temp;
		bubbleDown(min);
	}
}
Пример #17
0
// removes the top valued node from the heap and returns a pointer to it
node* popHeap()
{
  node* oldTopNode = heapNode[0];
  heapNode[0] = heapNode[indexOfLast];
  heapNode[0]->heapIndex = 0;
  heapNode[indexOfLast] = NULL;
  indexOfLast--;

  if(indexOfLast == 0)
    parentOfLast = -1;
  else
    parentOfLast = (indexOfLast-1)/2;
  
  bubbleDown(0);
  oldTopNode->inHeap = closed;
  oldTopNode->heapIndex = -1;
  return oldTopNode;
}
Пример #18
0
void HeapPriorityQueue::bubbleDown(int parent) {
    int leftChild = 2 * parent;
    int rightChild = 2 * parent + 1;

    // If no children
    if (leftChild > numAllocated) return;

    // If parent less than equal both children no swap needed
    if (pQueue[parent] <= pQueue[leftChild]) {
        if (rightChild > numAllocated) return;
        if (pQueue[parent] <= pQueue[rightChild])  return;
    }

    int smaller = leftChild;
    if (pQueue[rightChild] < pQueue[leftChild]) smaller = rightChild;
    swap(pQueue[parent], pQueue[smaller]);
    bubbleDown(smaller);
}
Пример #19
0
T CPriorityHeap<T>::deleteKey(const T key)
{
  T res;

  for(typename vector< CPriority<T> >::iterator it = heap.begin(); it != heap.end(); ++it) {
    if (key == it->key)
        {
          T delKey = it->key;

          it->key       = heap.back().key;
          it->priority  = heap.back().priority;

          heap.pop_back();
          index--;

          bubbleDown(it - heap.begin());
          return delKey;
        }

  }

  return res;
}
Пример #20
0
void Dijkstra(GRAPH G, POT P, int *pLast)
{
    NODE u, v; /* v is the node we select to settle */
    LIST ps; /* ps runs down the list of successors of v;
              u is the successor pointed to by ps */
    if (MYDEBUG) printf("Entering Dijkstra.\n");
    initialize(G, P, pLast);
    while ((*pLast) > 1) {
        v = P[1];
        swap(1, *pLast, G, P);
        --(*pLast);
        bubbleDown(1, G, P, *pLast);
        ps = G[v].successors;
        while (ps != NULL) {
            u = ps->nodeName;
            if (G[u].distance > G[v].distance + ps->nodeLabel) {
                G[u].distance = G[v].distance + ps->nodeLabel;
                bubbleUp(G[u].toPOT, G, P);
            }
            ps = ps->next;
        }
    }
}
Пример #21
0
string HeapPriorityQueue::dequeueMin() {
    //If there are no elements, return error to the user.
    if(isEmpty()){
        error("There are no elements in the queue.");
    }
    
    //Obtain result, which is the root.
    string result = *root;
    
    //Swap the root and the last element added.
    string* end = elements[total + 1];
    string* front = elements[1];
    front = end;
    root = front;
    
    //Reorder elements so as to maintain the integrity of the tree.
    bubbleDown(root, elements);
    
    //Increment the total.
    total --;
    
    //Return the result.
	return result;
}
Пример #22
0
 void binheap::removeMin()
 {
   heap[0] = heap[heap.size() - 1];
   heap.pop_back();
   bubbleDown(0);
 }
Пример #23
0
 void binheap::bubbleDown(idx i) {
   bubbleDown(i, size());
 }
 // change the key associated with index i to the specified value
 void changeKey(int i, int key)  {
     keys[i] = key;
     bubbleUp(index[i]);
     bubbleDown(index[i]);
 }
 // increase the key associated with index i to the specified value
 void increaseKey(int i, int key)    {
     keys[i] = key;
     bubbleDown(index[i]);
 }