斐波那契堆

2021-06-05 18:37:59 字數 2250 閱讀 2490

以下是實現的程式

肯定可以再優化的。。

//#include #include #include #include using namespace std;

class node

delete m_child;

m_child = null;

} }};

class fibonacciheap

node* insert(int key)

void insert(node *nd)//將結點nd插入到root list中

else

}void union(fibonacciheap &f)

insert(head);

head->m_parent = null;

if(m_min->m_key>head->m_key)

m_min = head;

f.m_min = null;//刪除f

} node *extractmin()//要在外面刪除min

child->m_parent = null;

insert(child);*/

/*node *tchild = child;

vectorchildren;

while (tchild!=null)

for (int i=0;im_right;

tchild->m_parent = null;

insert(tchild);

tchild = (next!=child) ?next:null;}}

if(minnode == minnode->m_right)

m_min = null;

else

minnode->m_child = null;

minnode->m_right = minnode;

minnode->m_left = minnode;

--m_numnode;

} return minnode;

} void consolidate()//;用陣列a來檢查root list上重複degree的結點;因為此時的minnode是上步中隨意指定的,所以不能拿minnode 作為錨點

link(y,curr);

a[d] = null;//curr的度數為degree+1,所以degree暫時沒有對應的根

++d;

}a[d] = curr;

} for (int i=0;im_right;

node *left = child->m_left;

right ->m_left = left;

left->m_right = right;

node *mchild = parent->m_child;

if(mchild==null)

else

child->m_mark = false;

++parent->m_degree; }

void decreasekey(node *x,int key)

x->m_key = key;

node *y = x->m_parent;

if (y!=null && x->m_key m_key)

}void cascadingcut(node *y)

cut(y,parent);

y = parent;

} }void cut(node *x,node *y)//x是y的孩子,將x上提為root

else

x->m_parent = null;

x->m_mark = false;

insert(x); }

void print() }

void print(int level,node *nd) }

~fibonacciheap()

delete m_min;

} }};int main()

; node *n5,*n10,*n16;

for (int i=0;i<17;++i)

/*node *t = f.extractmin();

cout//

// delete t;

// // t = f.extractmin();

//}//coutdelete t;

t = f.extractmin();

} return 0;

}

貌似有個**度數什麼的可以優化

要用prim檢測下

斐波那契堆

ifndef finbonacci heap h define finbonacci heap h include stdlib.h include math.h define error0 printf error at file s line d n file line 定義乙個求有符號的無窮大...

斐波那契堆

斐波那契堆同二項堆一樣,也是一種可合併堆。相對於二項堆,斐波那契堆的優勢在於如果不涉及刪除元素的操作,則它的平攤執行時間為o 1 但是由於其演算法過於複雜,因而實際上更多的是用二項堆。乙個斐波那契堆具有如下性質 堆有一組有序樹組成,但是堆中的樹不一定是二項樹 斐波那契堆中的樹之間是無序的 二項堆中的...

斐波那契堆

斐波那契堆的介紹 斐波那契堆是堆的一種,它和二項堆一樣,也是一種可合併堆,可用於實現合併優先佇列。而斐波那契堆比二項堆具有更好的平攤分析效能,它的合併操作的時間複雜度是o 1 與二項堆一樣,它也是由一組堆最小有序樹組成,並且是一種可合併堆。與二項堆不同的是,斐波那契堆中的樹不一定是二項樹 而且二項堆...