void heapify ( struct heap *heap, /* array of vals/tag to make into heap */ int index, /* root of subtree to heapify */ int nvals, /* number of values in array */ int *map /* maps from tag values to heap indices */ ) { double swap_val; /* temporary storage for swapping values */ double swap_tag; /* temporary storage for swapping values */ int l, r; /* indices of left & right children */ int largest; /* index of largest value */ l = left(index); r = right(index); if (l <= nvals && heap[l].val > heap[index].val) largest = l; else largest = index; if (r <= nvals && heap[r].val > heap[largest].val) largest = r; if (largest != index) { /* swap index with largest and recurse */ swap_val = heap[index].val; swap_tag = heap[index].tag; heap[index].val = heap[largest].val; heap[index].tag = heap[largest].tag; heap[largest].val = swap_val; heap[largest].tag = swap_tag; if (map != NULL) { /* update pointers to heap tags */ map[heap[index].tag] = index; map[heap[largest].tag] = largest; } heapify(heap, largest, nvals, map); } }
//////////////////////////////////////////////////////////////////// // Performs the heapsort //////////////////////////////////////////////////////////////////// int* HeapSort::sort() { cloneOver(); // reset resArr // construct a heap heapify(); // perform the sort int i, temp; for (i = size - 1; i > 0; i--) { // swap node 0 (root) with the node (i) swap(&resArr[0], &resArr[i]); // fix the heap up until immediately before node (i) fixHeap(0, i-1); } return resArr; }
/***************************************************************************** * heap_extract_max * * Remove and return the maximum element in the heap. *****************************************************************************/ heap_elt_t *heap_extract_max( heap_t *heap ) { heap_elt_t *max; if ( heap->size < 1 ) { return NULL; } max = heap->elements[ 0 ]; heap->elements[ 0 ] = heap->elements[ heap->size - 1 ]; heap->elements[ 0 ]->index = 0; heap->size--; heapify( heap, 0 ); return max; }
// Max heap void heapify(Array array, int idx, int max) { int left = 2 * idx + 1; int right = 2 * idx + 2; int largest; if (left < max && cmp(array[left], array[idx]) > 0) { largest = left; } else { largest = idx; } if (right < max && cmp(array[right], array[largest]) > 0) { largest = right; } if (largest != idx) { swap(array, idx, largest); heapify(array, largest, max); } }
int mainHeap(){ int i; PQ_ComplHeap * pq = initPQ(10 * sizeof(int)); LeftHeap * lpq1 = initLeftHeap(); LeftHeap * lpq2 = initLeftHeap(); LeftHeap * lpq3 = initLeftHeap(); heapify(20,pq); /* for(i = pq->size-1 ; i > 0 ; i--) delMax(pq); printVector(pq->vector); printVector(pq->vector); printf("\n"); heapSort(pq); printVector(pq->vector); printf("\n"); */ /* insertLeftHeap(17,lpq1); insertAsLC(13,lpq1->root); insertAsRC(12,lpq1->root); insertAsLC(6,lpq1->root->lc); insertLeftHeap(15,lpq2); insertAsLC(10,lpq2->root); insertAsRC(8,lpq2->root); travIn_R(lpq1->root); printf("\n"); travIn_R(lpq2->root); lpq3->root = mergeLeftHeap(lpq1->root,lpq2->root); printf("\n root : %d \n" , lpq3->root->data); travIn_R(lpq3->root); delMaxLeftHeap(lpq3); printf("\n root : %d \n" , lpq3->root->data); travIn_R(lpq3->root); */ for(i = 1 ; i < 20 ; i++) insertLeftHeap(i,lpq1); travIn_R(lpq1->root); return 0; }
void heapify(hp_s* hp,int idx) { int left = -1; int right = -1; int largest_idx = idx; //printf("Index sent is: %d \n",idx); //Mark invalid left node data as -1 //Find left node of the idx-left node is at 2i+1 //printf("Array size is %d: \n",hp->hsz); if((2*idx + 1) < hp->hlen) left = hp->arr[2*idx + 1]; //Find right node of idx-right node is at 2i if((2*idx + 2) < hp->hlen) right = hp->arr[2*idx + 2]; //Find largest node if(left!= -1 && right != -1) largest_idx = left>right?(2*idx + 1):(2*idx + 2); else if(left != -1) largest_idx = (2*idx + 1); else if (right != -1) largest_idx = (2*idx + 2); largest_idx = hp->arr[idx]>=hp->arr[largest_idx]?idx:largest_idx; //Organize heap acc to max heap case. if(largest_idx != idx) { int temp=0; temp = hp->arr[largest_idx]; hp->arr[largest_idx] = hp->arr[idx]; hp->arr[idx] = temp; //printf("Index targeted is: %d\n",largest_idx); heapify(hp,largest_idx); } }
void heapify(struct vertex **Q,int i,int size) { int l,r,small; struct vertex *temp; l=2*i; r=2*i+1; if(l<=size && Q[l-1]->d < Q[i-1]->d) small=l; else small=i; if(r<=size && Q[r-1]->d < Q[small-1]->d) small=r; if(i!=small) { temp=Q[small-1]; Q[small-1]=Q[i-1]; Q[i-1]=temp; heapify(Q,small,size); } }
void heapify(int varArr[],int current,int end) { int l=left_child(current),r=right_child(current); int largest; if (l<end&&varArr[current]<varArr[l]) largest=l; else largest=current; if (r<end&&varArr[largest]<varArr[r]) largest=r; if(largest!=current) { swap(&varArr[current],&varArr[largest]); heapify(varArr,largest,end); } }
template <typename RandomAccessIterator, typename Predicate = LessPred<RandomAccessIterator> > inline static void heapify(RandomAccessIterator first, RandomAccessIterator last, const RandomAccessIterator idx, const Predicate pred) { auto const left = first + 2 * std::distance(first, idx) + 1; auto const right = first + 2 * std::distance(first, idx) + 2; auto largest = idx; // largest or smallest if (left < last and pred(*idx, *left)) { largest = left; } if (right < last and pred(*largest, *right)) { largest = right; } if (largest != idx) { std::iter_swap(idx, largest); heapify(first, last, largest, pred); } }
/* Build a Min Heap given an array of numbers Instead of using insertNode() function n times for total complexity of O(nlogn), we can use the buildMinHeap() function to build the heap in O(n) time */ void buildMinHeap(minHeap *hp, int *arr, int size) { int i ; // Insertion into the heap without violating the shape property for(i = 0; i < size; i++) { if(hp->size) { hp->elem = realloc(hp->elem, (hp->size + 1) * sizeof(node)) ; } else { hp->elem = malloc(sizeof(node)) ; } node nd ; nd.data = arr[i] ; hp->elem[(hp->size)++] = nd ; } // Making sure that heap property is also satisfied for(i = (hp->size - 1) / 2; i >= 0; i--) { heapify(hp, i) ; } }
void heapify(collision_heap* heap, short int i){ short int l = heap_left(i), r = heap_right(i), largest; if(l <= heap->length && heap->heap[l].time < heap->heap[i].time){ largest = l; }else{ largest = i; } if(r <= heap->length && heap->heap[r].time < heap->heap[largest].time){ largest = r; } if(largest != i){ collision_data temp; temp = heap->heap[i]; heap->heap[i] = heap->heap[largest]; heap->heap[largest] = temp; heapify(heap, largest); } }
int pass(struct queue_ptr *q, int n, int time){ int i; if(q->count != 0){ for(i = 0; i<q->count; i++){ if(q->heap[i].t1>time+n) continue; /* else if(q->heap[i].t1>time){ q->heap[i].t2 = q->heap[i].t2 = q->heap[i].t2 - (n-(q->heap[i].t1-time)); if(q->heap[i].t2<0) q->heap[i].t2 = 0; } */else{ q->heap[i].t2 -=n; if(q->heap[i].t2<0) q->heap[i].t2 = 0; } //else q->heap[i].t2-=n; } } heapify(q, 0, q->count); time+=n; return time; }
void heapify(int k) { int largest = k; if (k*2 + 1 < size && heap[largest] < heap[k*2 + 1]) { largest = k*2 + 1; } if (k*2 + 2 < size && heap[largest] < heap[k*2 + 2]) { largest = k*2 + 2; } if (k != largest) { swap(k, largest); heapify(largest); } }
PriorityQueue::PriorityQueue(size_t sourceNodeID) { // Node at first position is reserved for implementation of binary heap node[0].nodeID = 0; node[0].cost = 0; heapSize = NODES; // All nodes except sourceNode would have cost of infinity with source node having value of 0 for(size_t i=1;i<=NODES;i++) { node[i].nodeID = i; indexMap[i-1] = i; if(sourceNodeID == i) node[i].cost = 0; else node[i].cost = INF; } // Heapify all elements for(size_t j=heapSize/2;j>=1;j--) heapify(j); }
void heapify(struct Heap* heap, int i) { int largest = i; int left = (i << 1) + 1; int right = (i + 1) << 1; if (left < heap->size && strcmp(heap->array+left*WSIZE, heap->array+largest*WSIZE)>0) largest = left; if (right < heap->size && strcmp(heap->array+right*WSIZE, heap->array+largest*WSIZE)>0) largest = right; if (largest != i) { swap(heap->array+largest*WSIZE, heap->array+i*WSIZE); heapify(heap, largest); } }
int heap_sort(int *a, int len) { int i; if(a == NULL) return ERROR; for(i = 0; i < len; i++) { //sink(a, i); heapify(a, i+1, i, a[i]); } for(i = len-1; i > 0; i--) { swap(&a[0], &a[i]); sink(a, --len); } return OK; }
void heapSort(int* A, size_t iRoot, size_t iEnd) { // Remove root and place it to the position of last elment // Move last elment to root. // Actually swap root with last elment till end reaches to start position. int rootElement = A[ iRoot ]; A[iRoot] = A[iEnd]; A[iEnd] = rootElement; // Root has been placed into sorted position, decrement last index iEnd = iEnd -1 ; // Call heapify function and heap short till iEnd reaches to root element if( iRoot < iEnd ) { heapify(A, iRoot, iEnd); //displayData(A, iRoot, iEnd ); // Sort Hipified Element heapSort(A, iRoot,iEnd); } }
/* add packet to the relevent flow */ void buffer_write(Packet* p, bool virtual_f) { bool insertP = FALSE; Flow* f = getFlow(p, &insertP, virtual_f); FHeap* heap = virtual_f ? virtual_flows : flows; if (!insertP) //if we didn't insert the packet { if (flow_isEmpty(f)){ // revived flow flow_enqueue(f, p); heapify(heap->data, heap->count); heap->weight += f->weight; } else { flow_enqueue(f, p); } } }
// Initializes the heap using the first length number of items in the array // values. struct heapStruct * initHeapfromArray(int* values, int length) { int i; struct heapStruct* h; h = (struct heapStruct*)(malloc(sizeof(struct heapStruct))); // We allocate one extra slot, since slot 0 stays unused. h->heaparray = (int*)malloc(sizeof(int)*(length+1)); // Just copy the values into our array. for (i=1; i<=length; i++) h->heaparray[i] = values[i-1]; // This is the number of values we copied. h->size = length; // This takes our random values and rearranges them into a heap. heapify(h); return h; }
void main() { clrscr(); int n, i, p, k; scanf("%d",&n); for(i=1;i<=n;H[i]=i,i++) scanf("%d",&A[i]); hn = n; heapify(); showHeap(n); scanf("%d",&p); for(i=1;i<=p;i++) { scanf("%d",&A[++n]); insert(); } showHeap(n); heapSort(); showHeap(n); }
minmaxheap::minmaxheap(std::string input_file) { for (int i = 0; i < 500; i++) { heap[i] = -1; // need to have a default value } std::ifstream infile; infile.open ("data.txt"); std::string line; while(std::getline(infile, line)){ std::stringstream lineStream(line); int value; while(lineStream >> value){ heap[num_nodes] = value; num_nodes++; } } infile.close(); for (int i = num_nodes-1; i >= 0; i--) { heapify(i); } }
/** * If item is already in the queue, sets its priority to the sum * of the old priority and the new one. If the item is not in the queue, * adds it to the queue. * * returns true if the item was already present */ bool insert_cumulative(T item, Priority priority) { // determine if the item is already in the queue typename index_map_type::const_iterator iter = index_map.find(item); if(iter != index_map.end()) { // already present // If it is already present update the priority size_t i = iter->second; heap[i].second = priority + heap[i].second; // If the priority went up move the priority until its greater // than its parent while ((i > 1) && (priority_at(parent(i)) <= priority)) { swap(i, parent(i)); i = parent(i); } // Trickle down if necessary heapify(i); // This should not be necessary return false; } else { // not already present so simply add push(item, priority); return true; } } // end of insert cumulative
/** * Fix heap * @param heap Pointer to a heap struct. * @param i Current index in heap array. */ static void heapify(heap_t * heap, int i) { int l = left(i); int r = right(i); if (r <= heap->size) { int largest; if (heap->a[i]->priority > heap->a[r]->priority) largest = l; else largest = r; if (heap->a[i]->priority < heap->a[largest]->priority) { swap(heap, i, largest); heapify(heap, largest); } } else if ((l == heap->size) && (heap->a[i]->priority < heap->a[l]->priority)) { swap(heap, i, l); } }
void heap::heapify(int x) { int largest = x; int l = largest*5+1; if(table[l]!=-1) { for(int j = largest*5 + 1;j<=largest*5 + 5;j++) { if(table[l]>=table[j] && table[j]!=-1 && table[l]!=-1) { l = j; } } //std::cout<<"smallest one for index "<<x<<" is "<<table[l]<<std::endl; if(table[l]<table[largest]) { swap(l,largest); heapify(l); } } }
static inline void astar_replace_route(struct astar_node *node, int x, int y, int cost, int goodness, struct astar_node *next, int location) { /* Replace the old route values with the new ones. */ node->x = x; node->y = y; node->cost = cost; node->goodness = goodness; node->next = next; node->order.key.i_key = location; /* If the old route was scheduled but not explored yet then fixup the * priority queue to reflect the new goodness of the route. */ if (node->scheduled) { heapify(schedule, 0); return; } /* Otherwise reschedule the route to be explored again. */ heap_insert(schedule, &node->goodness); node->scheduled = 1; }
// D HEAPSORT = ok void heapify(int i) { int L,r,max=0,aux=0; L = (2*i)+1; r = (2*i)+2; if((L<tam_heap) && (vetor[L]>vetor[i])) { max = L; } else max = i; if((r<tam_heap) && (vetor[r]>vetor[max])) max = r; if(max != i) { aux = vetor[i]; vetor[i] = vetor[max]; vetor[max] = aux; heapify(max); } }
static void save_plane(plane_heap_t *heap, lattice_t *lattice, uint32 which_z_block, uint32 which_lattice_xyz) { plane_t *h = heap->entries; if (heap->num_entries <= PLANE_HEAP_SIZE - 1) { plane_t *s = h + heap->num_entries++; s->plane = *lattice; s->which_z_block = which_z_block; s->which_lattice_xyz = which_lattice_xyz; if (heap->num_entries == PLANE_HEAP_SIZE) make_heap(h, PLANE_HEAP_SIZE); } else if (h->plane.score < lattice->score) { h->plane = *lattice; h->which_z_block = which_z_block; h->which_lattice_xyz = which_lattice_xyz; heapify(h, 0, PLANE_HEAP_SIZE); } }
void *bt_heap_cherrypick(struct ptr_heap *heap, void *p) { size_t pos, len = heap->len; for (pos = 0; pos < len; pos++) if (unlikely(heap->ptrs[pos] == p)) goto found; return NULL; found: if (unlikely(heap->len == 1)) { (void) heap_set_len(heap, 0); check_heap(heap); return heap->ptrs[0]; } /* Replace p with previous last entry and heapify. */ heap_set_len(heap, heap->len - 1); /* len changed. previous last entry is at heap->len */ heap->ptrs[pos] = heap->ptrs[heap->len]; heapify(heap, pos); return p; }
hp_s* build_hp(int *arr,int size) { hp_s* hp = (hp_s*)malloc(sizeof(hp_s)); hp->hsz = size; hp->hlen = 0; hp->arr = arr; hp->hlen = hp->hsz; int i =0; i = ((size-2)/2); for(i=i;i>=0;i--) { heapify(hp,i); //for(i=0;i<size;i++) // printf("%d ->",arr[i]); //printf("\n"); } return hp; }
/***************************************************************************** * heap_delete * * Delete element n from the heap. *****************************************************************************/ heap_elt_t *heap_delete( heap_t *heap, GLuint n ) { heap_elt_t *element; if ( ( heap->size < 1 ) || ( n >= heap->size ) ) { return NULL; } element = heap->elements[ n ]; heap->elements[ n ] = heap->elements[ heap->size - 1 ]; heap->elements[ n ]->index = n; heap->size--; heapify( heap, n ); return element; }