在Fibonacci堆中实现Consolidate

时间:2013-03-03 11:33:08

标签: algorithm tree fibonacci-heap

来自 Introduction to Algorithms 的伪代码声明:

for each node w in the root list of H
  link trees of the same degree

但是如何有效地为每个根节点部分实现?在整合过程中,原始根链接到相同程度的其他根,这使得很难仅通过根节点的循环列表。我如何判断是否检查了每个根节点?

1 个答案:

答案 0 :(得分:0)

您可以这样做的一个简单方法是使用三个步骤:

  1. 打破循环链接,使列表现在只是一个普通的双向链表。
  2. 迭代双向链表并处理每棵树。这很棘手,因为正如您所提到的,每个节点上的前向和下一个指针在迭代期间可能会发生变化。
  3. 关闭循环。
  4. 以下是您执行每个步骤的方法:

    打破循环链接:

    rootList->prev->next = NULL;
    rootList->prev = NULL;
    

    迭代双重链表。

    Node* current = rootList;
    while (current != NULL) {
        /* Cache the next node to visit so that even if the list changes, we can still
         * remember where to go next.
         */
        Node* next = current->next;
    
        /* ... main Fibonacci heap logic ... */
    
        current = next;
    }
    

    修复双向链表:

    Node* curr = rootList;
    if (curr != NULL) { // If list is empty, no processing necessary.
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = rootList;
        rootList->prev = curr;
    }
    

    希望这有帮助!