/* 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; }
/* delete min value. classic two-pass */ void heap_delete_min(heap *hp) { heap_node *a,*b,*next_tmp,*new_root=NULL,*h; heap_combine(hp); h = hp->root; /* two-pass merge */ /* heap_node_link in pairs, 1st and 2nd, 3rd and 4th, ...*/ 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_link(a,b); } h = heap_node_link(h,a); a = next_tmp; } /* attach all to the oldest one */ a = h->first_child; while(a!=NULL) { b = a->next_sibling; if (!b) break; heap_node_isolate(a); heap_node_isolate(b); a = heap_node_link(a,b); h = heap_node_link(h,a); } if (h->first_child) { new_root = h->first_child; h->first_child->parent=NULL; } free(h); hp->n_elements--; hp->root = new_root; hp->minimum= new_root; }
/* decrease the value of the node */ void heap_decrease_key(heap *h, heap_node *node, int delta) { heap_node *node_first_child; node->value -= delta; if (node->value < h->minimum->value) { h->minimum = node; } /* left most sub child takes place of node */ if (node!=h->root) { node_first_child = node->first_child; if (node_first_child) { heap_node_isolate(node_first_child); node->first_child->parent = node->parent; node->first_child->next_sibling = node->next_sibling; node->first_child->previous_sibling = node->previous_sibling; if (node->next_sibling) { node->next_sibling->previous_sibling = node_first_child; } else { node->parent->last_child = node_first_child; } if (node->previous_sibling) { node->previous_sibling->next_sibling = node_first_child; } else { node->parent->first_child = node_first_child; } } else { heap_node_isolate(node); } } /* add the rest of node's subtrees to the pool as standalone trees */ while (node->first_child) { h->pool[h->pool_size++] = node->first_child; heap_node_isolate(node->first_child); } if (h->pool_size>=log2(h->n_elements)) heap_combine(h); }
/* 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); } }