This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacciheap.h
147 lines (137 loc) · 1.95 KB
/
fibonacciheap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef fibonacciheap_h
#define fibonacciheap_h
#include<iostream>
using namespace std;
template<class T>
class Node
{
public:
T data;
Node<T> *left,*right;
Node<T>* parent,*child;
int order;
Node()
{
left = right = parent = child = NULL;
order=-1;
}
Node(T x)
{
left = right = parent = child = NULL;
order = 0;
data = x;
}
};
template<class T>
class fib_heap
{
Node<T>* min;
public:
fib_heap()
{
min=NULL;
}
int insert(T x)
{
Node<T>* temp=new Node<T>(x);
if(min==NULL)
{
min=temp;
min->left=min;
min->right=min;
}
else
{
temp->left=min->left;
temp->right=min;
min->left=temp;
(temp->left)->right=temp;
if(x<min->data)min=temp;
}
return 0;
}
int insert(Node<T>* temp)
{
if(min==NULL)
{
min=temp;
min->left=min;
min->right=min;
}
else
{
temp->left=min->left;
temp->right=min;
min->left=temp;
(temp->left)->right=temp;
if(temp->data<min->data)min=temp;
}
return 0;
}
int display()
{
Node<T>* temp=min;
if(min==NULL)return -1;
cout<<" "<<min->data<<endl;
temp=min->left;
while(temp!=min&& temp!=NULL)
{
cout<<" "<<temp->data<<endl;
temp=temp->left;
}
return 0;
}
Node<T>* link(Node<T>* x)
{
Node<T>* c=x->child;
if(c==NULL)
{
cout<<" NULL Child \n\n"<<endl;
return NULL;
}
else
{
Node<T>* p=c->right;
insert(c);
while(p!=c&& p!=NULL)
{
insert(p);
p=p->right;
}
}
}
int update_min()
{
Node<T>* p=min;
Node<T>* q=p;
p=min->right;
while(p!=NULL && p!=q )
{
if(p->data<min->data)
{
min=p;
}
p=p->right;
}
}
int del(Node<T>* temp)
{
Node<T>* x=temp;
(temp->left)->right=x->right;
(temp->right)->left=x->left;
delete temp;
}
T extract_min()
{
T temp=min->data;
Node<T>* t=min;
cout<<" "<<temp<<"\n\n";
if(min->child==NULL)cout<<" No Child \n\n";
link(min);
min->data = 99;
update_min();
del(t);
return temp;
}
};
#endif