-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibHeap1.c
More file actions
318 lines (279 loc) · 7.75 KB
/
Copy pathFibHeap1.c
File metadata and controls
318 lines (279 loc) · 7.75 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int num_of_nodes=0;//global variable that stores the number of nodes
int num_root_list=0;
struct node{
int key;
struct node* left;
struct node* right;
struct node* parent;
struct node* child;
int isMarked;
int rank;
int degree;
};
struct node* min=NULL;//pointer to min
struct node* create_node(int val)
{
struct node* Node=(struct node*)malloc(sizeof(struct node));
Node->key=val;//data value of the node
Node->isMarked=0;//if marked? 1 else 0
Node->degree=0;//no. of sibling nodes
Node->rank=0;//no. of child nodes
Node->left=NULL;//left pointer
Node->right=NULL;//right pointer
Node->parent=NULL;//parent pointer
Node->child=NULL;//child pointer
printf("\nNode with key %d created\n",Node->key);
return Node;//retruns the created node to the funtion defined
}
struct node* insert_node(int val)
{
struct node* Node=create_node(val);//creates a node and returns the created node to Node
if(num_of_nodes==0)
{
Node->right=Node;
Node->left=Node;
min=Node;
}
else
{
Node->left=min->left;
Node->right=min;
(min->left)->right=Node;
min->left=Node;
//Now check which has low key value and make it min
if(min->key>Node->key)
min=Node;
}
printf("\nNode with key %d is inserted into heap\n",Node->key);
num_of_nodes++;//Increase the total number of nodes
if(Node->parent==NULL) num_root_list+=1; //as the node has no parent, i.e it is root list
return Node;
}
struct node* insert_child(struct node* node, int val)
{
// if(node->rank<1)
// {
struct node* child_node;
child_node=create_node(val);//created a node of all NULLs and named child_node
child_node->left=child_node->right=child_node;//doubly linked list of lenght 1
child_node->parent=node;//Now child_node is the parent of child_node
node->rank+=1;//increment rank of the parent
// }
if(node->rank==1)//child pointer of the parent is to be pointed to its first child
node->child= child_node;
if(node->rank>1)//leaving this for now ????
{
//join this in the doubly linked list of the previous children at the left of child pointer
// child_node->left=node->child->left;
// node->child->left=child_node;
node->child->left->right=child_node;
child_node->right=node->child;
printf("Right of %d is : %d ",child_node->key,node->child->key);
}
printf("\nNow %d is child of %d \n",(child_node)->key,(node->key));
return child_node;
}
void print_rootList(struct node* node)//prints the root list (doubly linked list)
{
printf("\nThe root list of the heap is: \n");
struct node* ptr=node;
do
{
printf("-%d-",ptr->key);
ptr=ptr->right;
}while(ptr!=node);
printf("\n");
}
void print_siblings(struct node* node)
{
struct node* ptr=node;
do
{
printf("-%d-",ptr->key);
ptr=ptr->right;
}while(ptr!=node);
printf("\n");
}
void free_node(struct node* node)
{
node->left=NULL;
node->child=NULL;
node->right=NULL;
node->parent=NULL;
return;
}
void getMin(struct node* node)//get min node in the list of passed node
{
min=node;
struct node* ptr=node;
do
{
if ((min->key)>(ptr->key))
min=ptr;
ptr=ptr->right;
}
while(ptr!=node);
// return min;
}
int extract_min(struct node* node)
{
int current_min =node->key;
if(current_min==NULL) return 0;
min=node->right;
printf("temp min: %d\n",min->key);
struct node* ptr=node;
// node->left=node->right=NULL; Why is this not working here? why should I not free the node/old min which is passed to this function
printf("\nptr is pointing to %d\n",ptr->key);
// add all the children of min to root list
// ((ptr->child)->right)->left=ptr->left;
// ptr->left->right=ptr->child->right;
// printf("\nNow %d 's right sibling is %d\n",ptr->left->key,ptr->child->key);
// printf("\nptr is pointing to %d\n",ptr->key);
// ptr->child->right->left=ptr->left;
if(node->rank!=0)
{
ptr->left->right=ptr->child->right;
printf("\nNow %d is left of %d \n",ptr->left->key,ptr->child->right->key);
((ptr->child)->right)=ptr->right;
printf("\nNow %d 's right sibling is %d\n",ptr->child->key,ptr->right->key);
// printf("\nptr is pointing to %d\n",ptr->key);
}
else return -1;//The min doesnt have any children
// printf("Minimum is %d and is deleted",current_min);
getMin(min);//new min is stored now in 'min'
num_root_list=num_root_list-1+ptr->rank;
return current_min;
}
//void buildRankMatrix()
//{
// int i=0;
// int rankMat[num_root_list];
//
// for(i=0;i<num_root_list;i++)
// rankMat[i]=NULL;
//
// struct node* trav=min;//trav pointer
//
// while(trav->right!=min && i==trav->rank && rankMat[i]==NULL)
// {
// rankMat[i]=trav;
// trav=trav->right;
// }
//
// printf("Rank Matrix: ");
// for(i=0;i<num_root_list;i++)
// printf(" %d ",rankMat[i]);
//
//}
void joinAsChild(struct node* node, struct node* AtNode)
{
node->left->right=node->right;
node->right->left=node->left;
node->parent=AtNode;
if(AtNode->rank==0)
{
AtNode->child=node;
}
else
{
AtNode->child->left->right=node;
node->right=(AtNode->child);
}
num_root_list--;
}
int main()
{
struct node* root=insert_node(1);
root=insert_node(3);
root=insert_node(2);
root=insert_node(4);
// root=insert_node(5);
// root=insert_node(6);
print_rootList(min);
//printf("\nno. of nodes in root list: %d\n",num_root_list);
// printf("%d is left of %d", min->left->key,min->key);
insert_child(min,10);
insert_child(min,11);
//create a random heap
// struct node* child1=(struct node*)malloc(sizeof(struct node));
// struct node* child2=(struct node*)malloc(sizeof(struct node));
// child1->key=10;//data value of the node
// child1->isMarked=0;//if marked? 1 else 0
// child1->degree=0;//no. of sibling nodes
// child1->rank=0;//no. of child nodes
// child1->left=child2;//left pointer
// child1->right=child2;//right pointer
// child1->parent=min;//parent pointer
// child1->child=NULL;//child pointer
//
// min->child=child1;
//
// child2->key=11;//data value of the node
// child2->isMarked=0;//if marked? 1 else 0
// child2->degree=0;//no. of sibling nodes
// child2->rank=0;//no. of child nodes
// child2->left=child1;//left pointer
// child2->right=child1;//right pointer
// child2->parent=min;//parent pointer
// child2->child=NULL;//child pointer
// printf("children of min: %d and %d\n", child1->key,child2->key);
// print_siblings(min->child); //alling this function here has no problems
printf("\nExtract minimum: %d\n", extract_min(min));
// printf("\n%d\n",num_root_list);
print_rootList(min);
// get the new min
// printf("\nNew minimum: %d\n",min->key);
// Fill the rank matrix---------------------------------------
// buildRankMatrix();
int i=0;
struct node* rankMat[num_root_list];
for(i=0;i<num_root_list;i++)
rankMat[i]=i;
struct node* trav=min;//trav pointer
i=0;
// FillRankMat:
// consolidate
while(i<num_root_list)
{
if(i!=rankMat[i]->rank)
{
while(trav->right!=min && i==trav->rank)
{
if(rankMat[i]==i)
{
rankMat[i]=trav;
trav=trav->right;
i++;
}
else
{
if (trav->key > rankMat[i]->key)
{
//trav join as child at rankMat[i]
joinAsChild(trav,rankMat[i]);
trav=min;
}
else
{
//*rankMat[i] join as child at trav
joinAsChild(rankMat[i],trav);
trav=min;
}
}
}
}
i++;
}
printf("Rank Matrix: ");
for(i=0;i<num_root_list;i++)
printf(" %d ",rankMat[i]);
// rank matrix is filled--------------------------------------
//-------------------------END OF CODE ALMOST-------------------
// printf("\nPress any key to close!!!!!!!!!\n");
// getchar();
return 0;
}
//EOC