Fibonacci堆巩固功能

时间:2014-06-23 19:56:54

标签: c algorithm tree priority-queue fibonacci-heap

我试图为C程序实现Fibonacci堆数据结构,但我在Consolidate函数中遇到了问题。 为了检查每个根节点,我打破了双链表,但我无法确定如何修复"进程后的列表,并避免有一些节点指向NULL?

提前感谢!

这是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; i < 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;

for (i = 0; i < 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); 
}

}

我张贴图片以便更好地解释: 在步骤3中,节点7->左边应该指向23并且23->右边应该指向7,但两者都指向NULL ...我认为这是因为我打破了循环而不是在之后修复它联。

 //break loop
w = FH->min;
FH->min->left->right = NULL;
FH->min->left = NULL;

http://postimg.org/image/cv2mh6urj/

抱歉我的英语不好。

0 个答案:

没有答案