void insert_max_heap(element item, int *n) { if (HEAP_FULL(*n)) { fprintf(stderr, "The heap is full.\n"); exit(EXIT_FAILURE); } int i = ++(*n); // this is equal to i = *n + 1; (*n) += 1; while ((item.key > heap[i/2].key) && (i != 1)) { heap[i] = heap[i/2]; i /= 2; } heap[i] = item; }
int heap_insert (void *data, unsigned int key, struct heap_h *h) { int i; if (HEAP_FULL (h)) return -1; i = ++(h->cnt); while ((i != 1) && (key > (h->p + i/2)->key)) { memcpy (h->p + i, h->p + i/2, sizeof (struct heap_t)); i /= 2; } (h->p + i)->key = key; (h->p + i)->data = data; }