//Bubbles down after removing the root from the heap void bubble_down(int i) { int j = 2*i+1; if(end_of_queue <= 2*i) { //Has no children return; } else if (end_of_queue == 2*i+1) { //Has no right child j = 2*i; } else if( (customer_queue[2*i].priority > customer_queue[2*i+1].priority) || (customer_queue[2*i].priority == customer_queue[2*i+1].priority && customer_queue[2*i].a_time < customer_queue[2*i+1].a_time) || (customer_queue[2*i].priority == customer_queue[2*i+1].priority && customer_queue[2*i].a_time == customer_queue[2*i+1].a_time && customer_queue[2*i].s_time < customer_queue[2*i+1].s_time) || (customer_queue[2*i].priority == customer_queue[2*i+1].priority && customer_queue[2*i].a_time == customer_queue[2*i+1].a_time && customer_queue[2*i].s_time == customer_queue[2*i+1].s_time && customer_queue[2*i].arrival_no < customer_queue[2*i+1].arrival_no) ) { //Left child has higher priority than the right child j = 2*i; } //Compare child to parent if( (customer_queue[j].priority > customer_queue[i].priority) || (customer_queue[j].priority == customer_queue[i].priority && customer_queue[j].a_time < customer_queue[i].a_time) || (customer_queue[j].priority == customer_queue[i].priority && customer_queue[j].a_time == customer_queue[i].a_time && customer_queue[j].s_time < customer_queue[i].s_time) || (customer_queue[j].priority == customer_queue[i].priority && customer_queue[j].a_time == customer_queue[i].a_time && customer_queue[j].s_time == customer_queue[i].s_time && customer_queue[j].arrival_no < customer_queue[i].arrival_no) ) { //Max child is greater than what we are swapping in //Swap struct customer_struct temp; temp = customer_queue[i]; customer_queue[i] = customer_queue[j]; customer_queue[j] = temp; bubble_down(j); } }
void heapify(int *heap){ int last = (maxlines - 1)/2; int i; for (i = last; i >= 0; --i) { bubble_down(i, heap, maxlines-1); } }
int pop(struct heap* h) { int head = h->arr[1]; h->arr[1] = h->arr[h->n]; --h->n; bubble_down(h, 1); return head; }
/* Restore heap order property after a deletion */ void bubble_down(struct heap *heap, int position) { /* base case */ if ((position >= heap->end) || (left(position) > heap->end && right(position) > heap->end) ) { return; } /* 1 child case */ else if (left(position) <= heap->end && right(position) > heap->end) { if (strlen(heap->data[position]) > strlen(heap->data[left(position)]) ) /* violation of heap property */ { char *temp_child = heap->data[left(position)]; heap->data[left(position)] = heap->data[position]; heap->data[position] = temp_child; } return; } else /* normal case, both children non-void */ { if (strlen(heap->data[position]) > strlen(heap->data[left(position)]) || strlen(heap->data[position]) > strlen(heap->data[right(position)]) ) /* violation of heap property */ { int childIndex = (strlen(heap->data[left(position)]) < strlen(heap->data[right(position)]) ? left(position) : right(position)); char *child = heap->data[childIndex]; heap->data[childIndex] = heap->data[position]; heap->data[position] = child; bubble_down(heap, childIndex); /* update position to reflect swapped node's location */ } } }
/* assumes there is at least one item in the queue */ static void delete_min(pq q) { q->heap[0] = q->heap[--q->used]; q->heap[0]->heap_index = 0; if (q->used) bubble_down(q, 0); }
int delete_min(int *array, int *heap_size){ int ret = array[0]; *heap_size = *heap_size - 1; array[0] = array[*heap_size]; bubble_down(array, heap_size, 0); return ret; }
//Removes the root from the heap void remove_from_heap() { if (end_of_queue == 1) { return; } customer_queue[1] = customer_queue[end_of_queue-1]; end_of_queue--; bubble_down(1); }
void spdylay_pq_pop(spdylay_pq *pq) { if(pq->length > 0) { pq->q[0] = pq->q[pq->length-1]; --pq->length; bubble_down(pq, 0); } }
int pop_min (int *head, size_t *heap_size) { int ret = head[0]; head[0] = head[*heap_size]; head[*heap_size] = INT_MAX; (*heap_size)--; bubble_down(head, 0); return ret; }
void pq_insert_all (priority_queue *q, int s[], int n) { int i; q->n = n; for (i = 1; i <= n; i++) q->q[i] = s[i-1]; for (i = q->n/2; i>=1; i--) bubble_down(q, i); }
/** reheap re-enforces the heap property for a node that was modified * externally. Logarithmic performance for a node anywhere in the tree. */ void reheap(T node) { size_t heap_idx = get_heap_index(node); bubble_down(bubble_up(heap_idx)); #if CHECK_HEAP_CONSISTENCY validate_heap_state(); #endif }
void nghttp2_pq_pop(nghttp2_pq *pq) { if(pq->length > 0) { pq->q[0] = pq->q[pq->length-1]; --pq->length; bubble_down(pq, 0); } }
int *heap_pop(struct heap *h) { int *retval = h->data[0]; h->data[0] = h->data[h->bottom - 1]; h->bottom--; bubble_down(h, 0); return retval; }
T Heap<T, Compare>::pop() { PREC_EMPTY(); T element = BaseList<T>::at(0); BaseList<T>::destroy(0); BaseList<T>::move(0, BaseList<T>::size() - 1); BaseList<T>::prepare_size(BaseList<T>::size() - 1); bubble_down(0); return element; }
PriorityQueueElement priority_queue_extract(HeapPriorityQueue *queue) { // TODO assert priority_queue_size() > 0 PriorityQueueElement result = queue->elements[1]; queue->elements[1] = queue->elements[queue->pos - 1]; queue->pos--; bubble_down(queue, 1); return result; }
int remove_min(int *heap, int size) { int ret = heap[0], tmp, done = 0, left, right, i; SWAP(heap[0], heap[size-1], tmp); bubble_down(heap, size-1, 0); return ret; }
Data queue_pop(Queue *q) { Item top = q->array[1]; q->nextIndex--; // if there are more item in the queue swap this with the last item and bubble it down if(q->nextIndex>1) { swap(q, 1, q->nextIndex); bubble_down(q, 1); } return top.data; }
void priority_queue_delete(HeapPriorityQueue *queue, void *data) { int i; for (i = 0; i < queue->pos; ++i) { // Find the position. if (queue->elements[i].data != data) continue; queue->elements[i] = queue->elements[queue->pos - 1]; queue->pos--; bubble_down(queue, i); } }
struct nn_entry * nn_heap_pop(struct nn_heap *h) { struct nn_entry *e = NULL; if (h->next > ROOT_IDX) { e = h->entries[ROOT_IDX]; h->entries[ROOT_IDX] = h->entries[--h->next]; bubble_down(h, ROOT_IDX); } return e; }
// takes index of heap element and recursively swaps it down the tree to proper position void priority_queue::bubble_down(int index) { int child1 = index*2; int child2 = index*2 + 1; // done if at bottom or less or equal to both children if(index*2 > heap_size || (min_heap[index].value <= min_heap[child1].value && min_heap[index].value <= min_heap[child2].value)) { return; } // swap node with smaller child, and continue if(min_heap[child1].value < min_heap[child2].value) { swap(index, child1); bubble_down(child1); } else { swap(index, child2); bubble_down(child2); } }
void heapsort(int *heap){ heapify(heap); int temp; int i; for (i = maxlines-1; i >= 0; --i) { temp = heap[i]; heap[i] = heap[0]; heap[0] = temp; bubble_down(0, heap, i); } }
void heapify(struct heap* h) { // Build a heap from an unsorted h->arr // n must be set properly. // Bottom-up implementation int idx = h->n/2; while(idx > 1) { bubble_down(h, idx); --idx; } }
/* Delete the minimum item in the heap, then correct order and shape properties */ char* delete_min(struct heap *heap) { char *min; if (heap->end == 0) /* heap is empty, so don't try remove anything! */ return NULL; min = heap->data[1]; heap->data[1] = heap->data[heap->end]; /* move last item to top */ heap->data[heap->end] = NULL; /* remove previous reference */ --heap->end; bubble_down(heap, 1); /* verify/correct heap order property */ return min; }
void bubble_down(struct heap* h, int idx) { int left_child = idx * 2; int right_child = idx * 2 + 1; int max = idx; if(left_child <= h->n && h->arr[left_child] < h->arr[max]) max = left_child; if(right_child <= h->n && h->arr[right_child] < h->arr[max]) max = right_child; if(max != idx) { swap(&h->arr[max], &h->arr[idx]); bubble_down(h, max); } }
int pq_extract_min (priority_queue *q) { if (q->n <= 0) { printf("Warning: empty priority queue.\n"); return -1; } int min = q->q[1]; q->q[1] = q->q[q->n]; q->n--; bubble_down(q,1); return min; }
static void bubble_down(pq q, unsigned int k) { int l, r, s; l = CHILD_LEFT(k); r = CHILD_RIGHT(k); s = k; if (l < q->used && cmp(q, l, k) < 0) s = l; if (r < q->used && cmp(q, r, s) < 0) s = r; if (s == k) return; /* already satisfies the heap property */ swap(q, k, s); bubble_down(q, s); }
void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) { size_t i; int rv = 0; if(pq->length == 0) { return; } for(i = 0; i < pq->length; ++i) { rv |= (*fun)(pq->q[i], arg); } if(rv) { for(i = pq->length; i > 0; --i) { bubble_down(pq, i - 1); } } }
void bubble_down(struct heap *h, int index) { int smallest = index; int left = LEFT(index); int right = RIGHT(index); if (left <= h->bottom && h->data[left] <= h->data[index]) { smallest = left; } if (right <= h->bottom && h->data[right] <= h->data[left]) { smallest = right; } if (smallest != index) { swap(h, index, smallest); bubble_down(h, smallest); } }
T pop() { T res = top(); m_size--; clear_heap_index(res); if (!empty()) { // take the bottom element, stick it at the root and let it // bubble down to its right place. m_data[NODE_TYPE_ROOT] = m_data[m_size]; bubble_down(NODE_TYPE_ROOT); } #if CHECK_HEAP_CONSISTENCY validate_heap_state(); #endif return res; }
static void bubble_down(nghttp2_pq *pq, size_t index) { size_t lchild = index*2+1; size_t minindex = index; size_t i, j; for(i = 0; i < 2; ++i) { j = lchild+i; if(j >= pq->length) { break; } if(pq->compar(pq->q[minindex], pq->q[j]) > 0) { minindex = j; } } if(minindex != index) { swap(pq, index, minindex); bubble_down(pq, minindex); } }