void Heap::insert(hItem item) { int index; index = item.j * pdata->rows + item.i; if(item.flag == BAND) { if(item.hpos == -1) // not yet in heap { // T update and heap insertion pdata->Tfield[index].T = item.T; size = size + 1; pdata->Tfield[index].hpos = size; heap[size] = &(pdata->Tfield[index]); upHeap(size); } else // already in heap { // only T update double oldT = heap[item.hpos]->T; heap[item.hpos]->T = item.T; if(oldT > item.T) upHeap(item.hpos); else downHeap(item.hpos); } } }
int main (int argc, char *argv[]){ int heapItems = 0; int heapSize = 16; int minHeap[heapSize]; int upHeap(int x){ //TODO build upHeap if(x == 1) { return 0; // if we are at the root, stop } if(minHeap[parent(x)] <= minHeap[x]) { return 0; //already satisfy heap-invariant for min-heap } // swap x and parent(x) int temp = minHeap[x]; minHeap[x] = minHeap[parent(x)]; minHeap[parent(x)] = temp; upHeap(parent(x)); return 0; } int add(int value){ if( heapItems == 0) { // We're empty minHeap[1] = value; ++heapItems; return 0; } if( (heapItems + 1) == heapSize ) { // We're full //TODO grow array } // default case, add to end and upheap ++heapItems; minHeap[heapItems] = value; upHeap(heapItems); return 0; } add(33); add(27); add(57); add(67); add(17); add(87); printf("["); int i = 1; for(; i <= heapItems; ++i) { printf("%d, ", minHeap[i]); } printf("]\n\n"); exit(0); }
void Heap :: update(double time, int Ind) { Keys [ T2H [ Ind ] ] = time; // Must do one upHead run because maybe this new, lower, time // is lower than those of the parents. By design of Fast-Marching // it should never be larger though. upHeap(T2H [ Ind ]); }
void PriorityQueue::insert(QueueElement QE) { if(!contains(QE)) { PQ.push_back(QE); upHeap(PQ, PQ.size() - 1); } else chgPriority(QE); }
void increaseKey(int old, int newkey, int *queue){ if(newkey < queue[old]){ printf("podaj większy klucz"); } else { queue[old] = newkey; upHeap(old, queue); } }
long PriorityQueue::insert (PQNode& item) { if (numItems == maxItems) return(1); pqList[++numItems] = item; upHeap(numItems); return(0); }
void upHeap(int array[],int n){ int tmp; if(array[n]>array[n/2] && n>1){ tmp=array[n]; array[n]=array[n/2]; array[n/2]=tmp; upHeap(array,n/2); } }
/* * Update priority for a task in the queue */ void LpelTaskqueueUpdatePriority(taskqueue_t *tq, lpel_task_t *t, double np){ int pos = searchItem(tq, t); assert(pos > 0); double p = t->sched_info.prior; t->sched_info.prior = np; if (np > p) upHeap(tq, pos); else if (np < p) downHeap(tq, pos); }
void upHeap(int i) { int x; if (i!=0 && (Heap[parent(i)] < Heap[i])) { x = Heap[parent(i)]; Heap[parent(i)] = Heap[i]; Heap[i] = x; printf("rodzic %d ", parent(i)); upHeap(parent(i)); } }
void PriorityQueue::change (long itemIndex, long newValue) { if (newValue > pqList[itemIndex].key) { pqList[itemIndex].key = newValue; downHeap(itemIndex); } else if (newValue < pqList[itemIndex].key) { pqList[itemIndex].key = newValue; upHeap(itemIndex); } }
int main() { wczytaj(); wypisz(size); kopcuj(size); wypisz(size); Heap[8]=100; upHeap(8); wypisz(size); return 0; }
/* * Add a task to the task queue */ void LpelTaskqueuePush( taskqueue_t *tq, lpel_task_t *t){ //allocate more memory if needed if (tq->count >= tq->alloc) { tq->alloc = tq->alloc + BLOCKSIZE; tq->heap = realloc(tq->heap, tq->alloc *sizeof(taskqueue_t*)); } tq->heap[tq->count] = t; upHeap(tq, tq->count); tq->count++; }
void PriorityQueue::chgPriority(QueueElement QE) { for(int i = 0; i < PQ.size(); i++) if(PQ[i].node == QE.node) { if(PQ[i].value > QE.value) { PQ[i].value = QE.value; upHeap(PQ, i); } else { PQ[i].value = QE.value; downHeap(PQ, i); } break; } }
void Heap :: insert(double time, int Ind) { // Reallocate more memory if necessary if ( heapCount == ( allocatedSize - 1 ) ) { Keys = ( double * ) realloc( Keys, ( allocatedSize + Initial_Heap_Alloc_Size ) * sizeof( double ) ); H2T = ( int * ) realloc( H2T, ( allocatedSize + Initial_Heap_Alloc_Size ) * sizeof( int ) ); } // Insert element at the end of the heap Keys [ heapCount ] = time; H2T [ heapCount ] = Ind; T2H [ Ind ] = heapCount; heapCount++; // Ensure the heap is maintained by moving the // new element as necessary upwards in the heap. upHeap(heapCount - 1); }
void insert(int element, int *queue){ queue[omega] = element; upHeap(omega, queue); omega++; // printf("q = %d o = %d\n", queue[omega-1], omega); }