/* delete min value. multi-pass */ void heap_delete_min(heap *hp) { heap_node *a,*b,*next_tmp,*new_root=NULL; heap_node * h = hp->root; /* until there is one node */ /* heap_node_meld in pairs, 1st and 2nd, 3rd and 4th, ...*/ while(h->first_child!=h->last_child) { a = h->first_child; while(a!=NULL) { next_tmp=NULL; b = a->next_sibling; heap_node_isolate(a); if (b) { next_tmp=b->next_sibling; heap_node_isolate(b); a = heap_node_meld(a,b); } h = heap_node_meld(h,a); a = next_tmp; } } if (h->first_child) { new_root = h->first_child; h->first_child->parent=NULL; } free(h); hp->root = new_root; }
/* decrease the value of the node */ void heap_decrease_key(heap *h, heap_node *node, int delta) { node->value -= delta; /* if the node is not at the root of the tree, remove it and merge it with the root */ if (node!=h->root) { /* remove the node tree from the siblings and parents*/ heap_node_isolate(node); /* merge with root*/ h->root = heap_node_meld(h->root,node); } }
/* decrease the value of the node */ void heap_decrease_key(heap_node *node, foi new_weight) { node->value.weight = new_weight; /* if the node is not at the root of the tree, remove it and merge it with the root */ if (node!=hp->root) { /* remove the node tree from the siblings and parents*/ heap_node_isolate(node); /* merge with root*/ hp->root = heap_node_meld(hp->root,node); } }
/* insert element to the heap */ heap_node * heap_insert(heap *h,int value) { heap_node *node = heap_node_new(value); h->root = heap_node_meld(h->root,node); return node; }
/* insert element to the heap */ heap_node * heap_insert(foi weight) { heap_node *node = heap_node_new(weight); hp->root = heap_node_meld(hp->root,node); return node; }