static void fibheap_consolidate(fibheap_t *fib) { int i, max = 0; fibnode_t *tmp, *node, *min, *stack[1 + 8 * sizeof(long)] = {NULL}; while ((min = fib->min) != NULL) { fib->min = fibnode_remove(min); for (i = min->degree; stack[i] != NULL; i++) { node = stack[i], stack[i] = NULL; if (min->key > node->key) tmp = min, min = node, node = tmp; fibheap_link(node, min); } stack[i] = min; if (i > max) max = i; } assert(max < 1 + 8 * sizeof(long)); for (i = 0; i <= max; i++) { if (stack[i]) __fibheap_insert(fib, stack[i]); } }
/* Remove NODE from the rootlist of HEAP. */ static void fibheap_rem_root (fibheap_t heap, fibnode_t node) { if (node->left == node) heap->root = NULL; else heap->root = fibnode_remove (node); }
/* Remove NODE from PARENT's child list. */ static void fibheap_cut (fibheap_t heap, fibnode_t node, fibnode_t parent) { fibnode_remove (node); parent->degree--; fibheap_ins_root (heap, node); node->parent = NULL; node->mark = 0; }
static void fibheap_cut(fibheap_t *fib, fibnode_t *node, fibnode_t *parent) { parent->degree--; parent->child = fibnode_remove(node); node->parent = NULL; node->mark = 0; fibnode_insert(fib->min, node); }
static fibnode_t *__fibheap_extract_min(fibheap_t *fib) { fibnode_t *n, *min = fib->min; // 将min的子节点合并到堆的根节点上 if (min->child) { min->child->parent = NULL; for (n = min->child->left; n != min->child; n = n->left) n->parent = NULL; fibnode_union(min, min->child); } fib->nodes--; fib->min = fibnode_remove(min); fibheap_consolidate(fib); FIBNODE_INIT(min); return min; }