Skip to content

Commit 83e98ab

Browse files
committed
temp save
1 parent bc61895 commit 83e98ab

File tree

1 file changed

+85
-72
lines changed

1 file changed

+85
-72
lines changed

include/fib-heap.h

Lines changed: 85 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,83 +24,96 @@
2424
#include "double_linked_list.h"
2525
namespace alg {
2626
template<typename _Key,typename _Val>
27-
class FibHeap {
28-
public:
29-
typedef _Key key_type;
30-
typedef _Val value_type;
31-
typedef struct node_t {
32-
int32_t degree;
33-
node_t * parent;
34-
node_t * child;
35-
bool mark;
36-
key_type key;
37-
value_type value;
38-
struct list_head node;// list data struct
39-
} *Node;
40-
private:
41-
FibHeap(const FibHeap &);
42-
FibHeap& operator=(const FibHeap&);
43-
private:
44-
int32_t n;
45-
Node min;
46-
struct list_head rootlist;
47-
public:
48-
FibHeap():n(0),min(0){
49-
INIT_LIST_HEAD(&rootlist);
50-
}
27+
class FibHeap {
28+
public:
29+
typedef _Key key_type;
30+
typedef _Val value_type;
31+
typedef struct node_t {
32+
int32_t degree;
33+
node_t * parent;
34+
node_t * child;
35+
bool mark;
36+
key_type key;
37+
value_type value;
38+
struct list_head node;// list data struct
39+
} *Node;
40+
private:
41+
FibHeap(const FibHeap &);
42+
FibHeap& operator=(const FibHeap&);
43+
private:
44+
int32_t n;
45+
Node min;
46+
struct list_head rootlist;
47+
public:
48+
FibHeap():n(0),min(0){
49+
INIT_LIST_HEAD(&rootlist);
50+
}
51+
52+
/**
53+
* insert a value into the Fibonacci Heap
54+
*/
55+
void Insert(key_type key, value_type value) {
56+
Node x = new node_t;
57+
x->degree = 0;
58+
x->p = NULL;
59+
x->child = NULL;
60+
x->mark = false;
61+
if (min == NULL) {
62+
min = x;
63+
list_add(&x->node, &rootlist);
64+
} else {
65+
list_add(&x->node, &rootlist);
66+
if (x->key < min->key) {
67+
min = x;
68+
}
69+
}
70+
n = n+1;
71+
}
5172

52-
/**
53-
* insert a value into the Fibonacci Heap
54-
*/
55-
void Insert(key_type key, value_type value) {
56-
Node x = new node_t;
57-
x->degree = 0;
58-
x->p = NULL;
59-
x->child = NULL;
60-
x->mark = false;
61-
if (min == NULL) {
62-
min = x;
63-
list_add(&x->node, &rootlist);
64-
} else {
65-
list_add(&x->node, &rootlist);
66-
if (x->key < min->key) {
67-
min = x;
73+
/**
74+
* Union 2 Fibonacci-Heap
75+
*/
76+
static FibHeap* Union(FibHeap *H1, FibHeap *H2) {
77+
FibHeap * H = new FibHeap();
78+
H->min = H1->min;
79+
H->rootlist = H1->rootlist;
80+
list_splice(&H->rootlist, &H1->rootlist);// concat 2 root-list
81+
list_splice(&H->rootlist, &H2->rootlist);
82+
if (H1->min == NULL || (H2.min != NULL && H2->min.key < H1->min.key)) {
83+
H->min = H2->min;
84+
}
85+
H->n = H1->n + H2->n;
86+
return H;
6887
}
69-
}
70-
n = n+1;
71-
}
7288

73-
/**
74-
* Union 2 Fibonacci-Heap
75-
*/
76-
static FibHeap* Union(FibHeap *H1, FibHeap *H2) {
77-
FibHeap * H = new FibHeap();
78-
H->min = H1->min;
79-
H->rootlist = H1->rootlist;
80-
list_splice(&H->rootlist, &H1->rootlist);// concat 2 root-list
81-
list_splice(&H->rootlist, &H2->rootlist);
82-
if (H1->min == NULL || (H2.min != NULL && H2->min.key < H1->min.key)) {
83-
H->min = H2->min;
84-
}
85-
H->n = H1->n + H2->n;
86-
return H;
87-
}
89+
/**
90+
* Extract Min Element
91+
*/
92+
Node * ExtractMin() {
93+
Node z = min;
94+
if (z != NULL) {
95+
Node n, ns;
96+
// for each child x of z, add x to the root list of H
97+
list_for_each_entry_safe(n,ns, z->child, node){
98+
list_add(&n->node, &rootlist);
99+
n->parent = NULL;
100+
}
101+
102+
// remove z from the root list of H
103+
list_del(&z->node, &rootlist);
104+
if (z == z->next) {// the only node on the root list
105+
min = NULL;
106+
} else {
107+
min = z->right;
108+
Consolidate();
109+
}
110+
n = n + 1;
111+
}
112+
}
88113

89-
/**
90-
* Extract Min Element
91-
*/
92-
Node * ExtractMin() {
93-
Node z = min;
94-
if (z != NULL) {
95-
Node n, ns;
96-
// for each child x of z, add x to the root list of H
97-
list_for_each_entry_safe(n,ns, z->child, node){
98-
list_add(&n->node, &rootlist);
99-
n->parent = NULL;
114+
void Consolidate() {
100115
}
101-
}
102-
}
103-
};
116+
};
104117
}
105118

106119
#endif //

0 commit comments

Comments
 (0)