斐波那契堆

2021-06-16 21:14:39 字數 3883 閱讀 8233

#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__)

// 定義乙個求有符號的無窮大的巨集

#define sign_infinitely_great(t) ((0x1<<8*sizeof(t)-1)-1)

// 定義乙個求有符號的無窮小的巨集

#define sign_infinitely_small(t) (0x1<<(8*sizeof(t)-1))

// 定義int型的無窮大值

#define int_infinitely_greate sign_infinitely_great(int)

// 定義int型的無窮小值

#define int_infinitely_small sign_infinitely_small(int)

template struct fibo_node

; template class fibo_heap

fibo_heap *insert_node(fibo_node*nd);

fibo_heap *heap_union(fibo_heap* h1,fibo_heap *h2);

fibo_node*extract_min();

fibo_heap *decrease(fibo_node*nd,key key);

fibo_heap *delete_node(fibo_node* nd);

};

template fibo_heap::fibo_heap()

template fibo_heap::~fibo_heap()

;templatefibo_heap*fibo_heap::insert_node(fibo_node*nd)

nd->deg = 0 ;

nd->chd = null;

nd->mrk = false;

nd->pnt = null;

if (_minh == null)

else

_nh++;

} return this;

} template fibo_heap*fibo_heap::heap_union(fibo_heap*h1,fibo_heap*h2)

tmp = h2->_minh;

tmpi = h2->_nh;

h2->_minh = null;

h2->_nh = 0;

this->_minh = tmp;

this->_nh = tmpi;

return this;

} else if (h2->_minh == null)

h1->_minh->l->l = h2->_minh->l;

h2->_minh->l->r = h1->_minh->l;

h2->_minh->l = h1->_minh;

h1->_minh->r = h2->_minh;

h1->_nh += h2->_nh;

if (h1->_minh->key > h2->_minh->key)

tmp = h1->_minh;

tmpi = h1->_nh;

h1->_minh = h2->_minh = null;

h1->_nh = h2->_nh = 0;

this->_minh = tmp;

this->_nh = tmpi;

return this;

} templatefibo_node*fibo_heap::extract_min()

fibo_node*c = m->chd;

if (c != null)

while(c);

} if (m->l == m)

else

_nh--;

return m;

} //如何exchange,交換了兩個結點位置(保持結點指標與結點資料關聯性不變以便於外部呼叫)以及呼叫他們的指標(用於交換)

template void fibo_heap::exchange(fibo_node**n1 , fibo_node**n2)

fibo_node*_n1 = *n1;

fibo_node*_n2 = *n2;

if (_n1 == _n2)return;

fibo_node*t;

t = _n2->chd;

_n2->chd = _n1->chd;

_n1->chd = t;

t = _n2->pnt;

_n2->pnt = _n1->pnt;

_n1->pnt = t;

t = _n1->l;

_n1->l = _n2->l;

_n2->l->r = _n1;

_n2->l = t;

t->r = _n2;

t = _n1->r;

_n1->r = _n2->r;

_n2->r->l = _n1;

_n2->r = t;

t->l = _n2;

t = *n1;

*n1 = *n2;

*n2 = t;

} template void fibo_heap::consolidate()

else

n->mrk = false;

} arr[m->deg] = null;

m->deg++;

} arr[m->deg] = m;

if (m == e || m== m->l)break;//邊界需要改善

m = m->l;

} while (1);

m = null;

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

} }

_minh = m;

} templatevoid fibo_heap::cut(fibo_node*n , fibo_node*p)

else

p->deg--;

n->pnt = null;

n->l = _minh->l;

n->r = _minh;

_minh->l->r = n;

_minh->l = n;

n->mrk = false;

} template void fibo_heap::cascading_cut(fibo_node*n)

else

} } template fibo_heap*fibo_heap::decrease(fibo_node*nd,key key)

fibo_node*p = nd->pnt;

nd->key = key;

if (p !=null && nd->key < p->key)

if (nd->key < _minh->key)

return this;

} template fibo_heap*fibo_heap::delete_node(fibo_node* nd)

#endif

斐波那契堆

以下是實現的程式 肯定可以再優化的。include include include include using namespace std class node delete m child m child null class fibonacciheap node insert int key v...

斐波那契堆

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

斐波那契堆

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