c - Fibonacci heap consolidate function -
i'm trying implement fibonacci heap info construction c programme i've problem in consolidate function. check every root node i've break double linked list can't figure how "repair" list after process , avoid have node pointing null ?
thanks in advance!
here code of func:
void fhconsolidate(fiboheap *fh){ int register i; int d; int h = fh->nnodes + 1; fhnode *x, *y, *w, *next; fhnode **a = malloc(sizeof(long) * h); for(i=0; < h; i++){ a[i] = null; } //break loop w = fh->min; fh->min->left->right = null; fh->min->left = null; while(w != null){ x = w; next = w->right; w = w->right; d = x->rank; while(a[d] != null) { y = a[d]; if (y->key < x->key){ fhexchange(x, y); } fhlink(fh, x, y); a[d] = null; d++; } a[d] = x; } fh->min = null; (i = 0; < h; i++){ if (a[i] != null) { fhaddtorootlist(fh, a[i]); if (fh->min == null || a[i]->key < fh->min->key){ fh->min = a[i]; } } } }
void fhinsert(fiboheap *fh, fhnode *newnode){ if (fh->min != null){ newnode->left = fh->min->left; newnode->right = fh->min; fh->min->left->right = newnode; fh->min->left = newnode; if(newnode->key < fh->min->key){ fh->min = newnode; } } else{ fh->min = newnode; fh->min->left = fh->min; fh->min->right = fh->min; } fh->nnodes = fh->nnodes + 1; fh->ntrees = fh->ntrees + 1; }
void fhaddtorootlist(fiboheap *fh, fhnode *x){ if (x->mark == 1){ x->mark = 0; fh->nnodes = fh->nnodes - 1; fhinsert(fh, x); } }
i post image explain better: @ step 3 node 7->left should point 23 , 23->right should point 7, both point null...i think happens because break loop , don't repair after linking.
//break loop w = fh->min; fh->min->left->right = null; fh->min->left = null; http://postimg.org/image/cv2mh6urj/
sorry bad english.
c algorithm tree priority-queue fibonacci-heap
No comments:
Post a Comment