Exemple #1
0
//Starts at the top of the tree and ensures the tree continues to be a min heap fixing when needed
void Heap::fix_down(const int& t) {
	int left_index = t * 2;
	int right_index = (t * 2) + 1;

	// if two children
	if (right_index <= position) {
		// check for the smallest of the children and treat like one child but recurse
		if (tree[left_index]->weight < tree[right_index]->weight) {
			if (tree[left_index]->weight < tree[t]->weight) {
				swap(left_index, t);
				fix_down(left_index);
			}
		}
		else {
			if (tree[right_index]->weight < tree[t]->weight) {
				swap(right_index, t);
				fix_down(right_index);
			}
		}
	}
	// one  child
	else if (left_index <= position) {
		if (tree[left_index]->weight < tree[t]->weight)
			swap(left_index, t);
	}
}
Exemple #2
0
Item pq_delmax()
{
    assert(N > 0);

    exch(pq[1], pq[N]);
    fix_down(pq, 1, N - 1);
    return pq[N--];
}
Exemple #3
0
//Takes a node out of the top of the tree
HNode* Heap::dequeue() {
	count--;
	assert(position);

	HNode* ret = tree[1];
	swap(1, position--);
	fix_down(1);
	return ret;
}
NO *remover_heap(HEAP_ESTATICA *heap) {
  if (!vazia_heap(heap)) {
    NO *no = heap->vetor[0];
    heap->vetor[0] = heap->vetor[heap->fim];
    heap->fim--;
    fix_down(heap);
    return no;
  }
  return NULL;
}