Пример #1
0
static void heapify_down(key_t * vec, size_t pos,
                         size_t maxPos)
{
    if (LEFT(pos) > maxPos)
        return; // nu mai avem cu ce compara
    // avem stanga, verificam si pentru dreapta
    if (RIGHT(pos) <= maxPos) {
        // avem si fiu drept
        if (vec[pos].priority <= min(vec[LEFT(pos)],
            vec[RIGHT(pos)]).priority)
            return; // proprietate restabilita
        if (vec[LEFT(pos)].priority <=
            vec[RIGHT(pos)].priority) {
            swap(&vec[pos], &vec[LEFT(pos)]);
            heapify_down(vec, LEFT(pos), maxPos);
        } else {
            swap(&vec[pos], &vec[RIGHT(pos)]);
            heapify_down(vec, RIGHT(pos), maxPos);
        }
    } else {
        // avem doar fiu stang
        if (vec[pos].priority <=
            vec[LEFT(pos)].priority)
            return; // proprietate restabilita
        swap(&vec[pos], &vec[LEFT(pos)]);
        heapify_down(vec, LEFT(pos), maxPos);
    }
}
Пример #2
0
Файл: heap.c Проект: vdt/libcore
/* Complexity: O(n log n), worst-case if data is located
 * at the right-most leaf node on the lowest level of the
 * tree.
 */
int heap_remove(Heap *heap, const void *data)
{
    unsigned long i;

    assert(heap != NULL);

    if(heap_is_empty(heap)) {
        return -1;
    }

    for(i = 0; i < heap_size(heap); i++) {
        if(darray_index(heap->h, i) == data) {
            if(darray_swap(heap->h, i, darray_size(heap->h) - 1) < 0) {
                return -1;
            }

            /* Don't care about the return value */
            darray_remove(heap->h, darray_size(heap->h) - 1);
            heapify_down(heap, i);

            return 0;
        }
    }

    return -1;
}
 void build_max_heap_down(vector<T>& A) {
     auto unprocessed_size = A.size()/2;
     while(unprocessed_size > 0)
     {
         unprocessed_size--;
         heapify_down(A, unprocessed_size);
     }
 }
Пример #4
0
void* Heap_remove(Heap_T heap)
{
  assert(heap->index > 0);
  void* val = heap->array[0];
  heap->array[0] = heap->array[--heap->index];
  heapify_down(heap, 0);
  return val;
}
    void heapify_down(vector<T>& A, size_t i, size_t heap_size) {
        if(i >= A.size())
            return;

        auto max3 = compute_max_3(A, i, left(i), right(i), heap_size);
        if(i != max3)
        {
            swap(A[max3], A[i]);
            heapify_down(A, max3);
        }
    }
Пример #6
0
key_t pqueue_pop(pqueue_t * pqueue)
{
    /* interschimbam primul cu ultimul element
       din vector si mentinem proprietatea de
       heap dupa ce scoatem noul ultim element */
    key_t ret = *pqueue->vec;
    swap(pqueue->vec, pqueue->vec + pqueue->size - 1);
    --pqueue->size;
    if (pqueue->size)
        heapify_down(pqueue->vec, 0, pqueue->size - 1);
    return ret;
}
    void heap_sort(vector<T>& A) {
        build_max_heap_down(A);

        // [heap) [sorted)
        auto begin_sorted = A.size();
        while(begin_sorted > 0) {
            begin_sorted--;
            swap(A[begin_sorted], A[0]);
            auto heap_size = begin_sorted;
            heapify_down(A, 0, heap_size);
        }
    }
key_type pq_delete( implicit_heap *queue, implicit_node* node )
{
    key_type key = node->key;
    implicit_node *last_node = queue->nodes[queue->size - 1];
    push( queue, last_node->index, node->index );

    pq_free_node( queue->map, 0, node );
    queue->size--;

    if ( node != last_node )
        heapify_down( queue, last_node );

    return key;
}
Пример #9
0
Файл: world.c Проект: cmr/iiag
static void heapify_down(int i) {
	int has_l, has_r;
	action_node * n;
	action_node * l;
	action_node * r;

	if (i < world.acts_cnt) {
		n = world.acts + i;
		l = world.acts + (i + 1) * 2 - 1; // get the left child
		r = world.acts + (i + 1) * 2;     // get the right child

		has_l = l - world.acts < world.acts_cnt;
		has_r = r - world.acts < world.acts_cnt;

		if (has_l && (!has_r || compare(l, r)) && compare(l, n)) {
			swap(n, l);
			heapify_down(l - world.acts);
		} else if (has_r && compare(r, n)) {
			swap(n, r);
			heapify_down(r - world.acts);
		}
	}
}
Пример #10
0
Файл: heap.c Проект: vdt/libcore
/* Complexity: O(size(heap1) + 2 * size(heap2)) => O(n) */
int heap_merge(Heap *heap1, Heap* heap2)
{
    unsigned long i;

    assert(heap1 != NULL);
    assert(heap2 != NULL);

    if(heap_is_empty(heap1) && heap_is_empty(heap2)) {
        return -1;
    }

    /* O(size(heap2)) */
    if(darray_concat(heap1->h, heap2->h) < 0) {
        return -1;
    }

    /* O(size(heap1) + size(heap2)) */
    for(i = (darray_size(heap1->h) - 1) / 2; i > 0; i--) {
        heapify_down(heap1, i);
    }
    heapify_down(heap1, 0); /* Edge-case for loop 0 index */

    return 0;
}
Пример #11
0
Файл: world.c Проект: cmr/iiag
action * pop_action(void) {
	action * a;

	if (world.acts_cnt) {
		a = world.acts->act;
		assert(a != NULL);

		swap(world.acts, world.acts + world.acts_cnt - 1);
		world.acts_cnt--;
		heapify_down(0);
	} else {
		a = NULL;
	}

	return a;
}
Пример #12
0
void heapify_down(int arr[],int c)
{
	int largest=0,tmp;
	if(left(c) < n && arr[left(c)] < arr[right(c)])
		largest = right(c);
	else largest = left(c);
	
	if(arr[largest] > arr[c])
	{
		tmp = arr[largest];
		arr[largest] = arr[c];
		arr[c] = tmp;
		
		heapify_down(arr,largest);
	}
}
Пример #13
0
Файл: heap.c Проект: vdt/libcore
/* Complexity: O(log n) */
void* heap_pop(Heap *heap)
{
    void *ret = NULL;

    assert(heap != NULL);

    if(heap_is_empty(heap)) {
        return NULL;
    }

    if(darray_swap(heap->h, 0, darray_size(heap->h) - 1) < 0) {
        return NULL;
    }

    ret = darray_remove(heap->h, darray_size(heap->h) - 1);
    heapify_down(heap, 0);

    return ret;
}
Пример #14
0
int remove_top(int arr[],int n)
{
	int top = arr[0];
	arr[0] = arr[n];
	heapify_down(arr,0);
}