|
| 1 | +#include<stdio.h> |
| 2 | +#include<malloc.h> |
| 3 | + |
| 4 | +struct node { |
| 5 | + int data; |
| 6 | + int degree; |
| 7 | + struct node* parent; |
| 8 | + struct node* child; |
| 9 | + struct node* sibling; |
| 10 | +}; |
| 11 | + |
| 12 | +struct node* MAKE_bin_HEAP(); |
| 13 | +int bin_LINK(struct node*, struct node*); |
| 14 | +struct node* CREATE_NODE(int); |
| 15 | +struct node* bin_HEAP_UNION(struct node*, struct node*); |
| 16 | +struct node* bin_HEAP_INSERT(struct node*, struct node*); |
| 17 | +struct node* bin_HEAP_MERGE(struct node*, struct node*); |
| 18 | +struct node* bin_HEAP_EXTRACT_MIN(struct node*); |
| 19 | +int REVERT_LIST(struct node*); |
| 20 | +int DISPLAY(struct node*); |
| 21 | +struct node* FIND_NODE(struct node*, int); |
| 22 | +int bin_HEAP_DECREASE_KEY(struct node*, int, int); |
| 23 | +int bin_HEAP_DELETE(struct node*, int); |
| 24 | + |
| 25 | +int count = 1; |
| 26 | + |
| 27 | +struct node* MAKE_bin_HEAP() { |
| 28 | + struct node* np; |
| 29 | + np = NULL; |
| 30 | + return np; |
| 31 | +} |
| 32 | + |
| 33 | +struct node *H = NULL; |
| 34 | +struct node *Hr = NULL; |
| 35 | + |
| 36 | +//to make y as a child of z |
| 37 | +int bin_LINK(struct node* y, struct node* z) { |
| 38 | + y->parent = z; |
| 39 | + y->sibling = z->child; |
| 40 | + z->child = y; |
| 41 | + z->degree = z->degree + 1; |
| 42 | +} |
| 43 | + |
| 44 | +struct node* CREATE_NODE(int k) { |
| 45 | + struct node* p;//new node; |
| 46 | + p = (struct node*) malloc(sizeof(struct node)); |
| 47 | + p->data = k; |
| 48 | + return p; |
| 49 | +} |
| 50 | + |
| 51 | +struct node* bin_HEAP_UNION(struct node* H1, struct node* H2) { |
| 52 | + struct node* prev_x; |
| 53 | + struct node* next_x; |
| 54 | + struct node* x; |
| 55 | + struct node* H = MAKE_bin_HEAP(); |
| 56 | + H = bin_HEAP_MERGE(H1, H2); |
| 57 | + if (H == NULL) |
| 58 | + return H; |
| 59 | + prev_x = NULL; |
| 60 | + x = H; |
| 61 | + next_x = x->sibling; |
| 62 | + while (next_x != NULL) { |
| 63 | + if ((x->degree != next_x->degree) || ((next_x->sibling != NULL) |
| 64 | + && (next_x->sibling)->degree == x->degree)) { |
| 65 | + prev_x = x; |
| 66 | + x = next_x; |
| 67 | + } else { |
| 68 | + if (x->data <= next_x->data) { |
| 69 | + x->sibling = next_x->sibling; |
| 70 | + bin_LINK(next_x, x); |
| 71 | + } else { |
| 72 | + if (prev_x == NULL) |
| 73 | + H = next_x; |
| 74 | + else |
| 75 | + prev_x->sibling = next_x; |
| 76 | + bin_LINK(x, next_x); |
| 77 | + x = next_x; |
| 78 | + } |
| 79 | + } |
| 80 | + next_x = x->sibling; |
| 81 | + } |
| 82 | + return H; |
| 83 | +} |
| 84 | + |
| 85 | +struct node* bin_HEAP_INSERT(struct node* H, struct node* x) { |
| 86 | + struct node* H1 = MAKE_bin_HEAP(); |
| 87 | + x->parent = NULL; |
| 88 | + x->child = NULL; |
| 89 | + x->sibling = NULL; |
| 90 | + x->degree = 0; |
| 91 | + H1 = x; |
| 92 | + H = bin_HEAP_UNION(H, H1); |
| 93 | + return H; |
| 94 | +} |
| 95 | + |
| 96 | +struct node* bin_HEAP_MERGE(struct node* H1, struct node* H2) { |
| 97 | + struct node* H = MAKE_bin_HEAP(); |
| 98 | + struct node* y; |
| 99 | + struct node* z; |
| 100 | + struct node* a; |
| 101 | + struct node* b; |
| 102 | + y = H1; |
| 103 | + z = H2; |
| 104 | + if (y != NULL) { |
| 105 | + if (z != NULL && y->degree <= z->degree) |
| 106 | + H = y; |
| 107 | + else if (z != NULL && y->degree > z->degree) |
| 108 | + /* need some modifications here;the first and the else conditions can be merged together!!!! */ |
| 109 | + H = z; |
| 110 | + else |
| 111 | + H = y; |
| 112 | + } else |
| 113 | + H = z; |
| 114 | + while (y != NULL && z != NULL) { |
| 115 | + if (y->degree < z->degree) { |
| 116 | + y = y->sibling; |
| 117 | + } else if (y->degree == z->degree) { |
| 118 | + a = y->sibling; |
| 119 | + y->sibling = z; |
| 120 | + y = a; |
| 121 | + } else { |
| 122 | + b = z->sibling; |
| 123 | + z->sibling = y; |
| 124 | + z = b; |
| 125 | + } |
| 126 | + } |
| 127 | + return H; |
| 128 | +} |
| 129 | + |
| 130 | +int DISPLAY(struct node* H) { |
| 131 | + struct node* p; |
| 132 | + if (H == NULL) { |
| 133 | + printf("\nHEAP EMPTY"); |
| 134 | + return 0; |
| 135 | + } |
| 136 | + printf("\nTHE ROOT NODES ARE:-\n"); |
| 137 | + p = H; |
| 138 | + while (p != NULL) { |
| 139 | + printf("%d", p->data); |
| 140 | + if (p->sibling != NULL) |
| 141 | + printf("-->"); |
| 142 | + p = p->sibling; |
| 143 | + } |
| 144 | + printf("\n"); |
| 145 | +} |
| 146 | + |
| 147 | +struct node* bin_HEAP_EXTRACT_MIN(struct node* H1) { |
| 148 | + int min; |
| 149 | + struct node* t = NULL; |
| 150 | + struct node* x = H1; |
| 151 | + struct node *Hr; |
| 152 | + struct node* p; |
| 153 | + Hr = NULL; |
| 154 | + if (x == NULL) { |
| 155 | + printf("\nNOTHING TO EXTRACT"); |
| 156 | + return x; |
| 157 | + } |
| 158 | + // int min=x->n; |
| 159 | + p = x; |
| 160 | + while (p->sibling != NULL) { |
| 161 | + if ((p->sibling)->data < min) { |
| 162 | + min = (p->sibling)->data; |
| 163 | + t = p; |
| 164 | + x = p->sibling; |
| 165 | + } |
| 166 | + p = p->sibling; |
| 167 | + } |
| 168 | + if (t == NULL && x->sibling == NULL) |
| 169 | + H1 = NULL; |
| 170 | + else if (t == NULL) |
| 171 | + H1 = x->sibling; |
| 172 | + else if (t->sibling == NULL) |
| 173 | + t = NULL; |
| 174 | + else |
| 175 | + t->sibling = x->sibling; |
| 176 | + if (x->child != NULL) { |
| 177 | + REVERT_LIST(x->child); |
| 178 | + (x->child)->sibling = NULL; |
| 179 | + } |
| 180 | + H = bin_HEAP_UNION(H1, Hr); |
| 181 | + return x; |
| 182 | +} |
| 183 | + |
| 184 | +int REVERT_LIST(struct node* y) { |
| 185 | + if (y->sibling != NULL) { |
| 186 | + REVERT_LIST(y->sibling); |
| 187 | + (y->sibling)->sibling = y; |
| 188 | + } else { |
| 189 | + Hr = y; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +struct node* FIND_NODE(struct node* H, int k) { |
| 194 | + struct node* x = H; |
| 195 | + struct node* p = NULL; |
| 196 | + if (x->data == k) { |
| 197 | + p = x; |
| 198 | + return p; |
| 199 | + } |
| 200 | + if (x->child != NULL && p == NULL) { |
| 201 | + p = FIND_NODE(x->child, k); |
| 202 | + } |
| 203 | + |
| 204 | + if (x->sibling != NULL && p == NULL) { |
| 205 | + p = FIND_NODE(x->sibling, k); |
| 206 | + } |
| 207 | + return p; |
| 208 | +} |
| 209 | + |
| 210 | +int bin_HEAP_DECREASE_KEY(struct node* H, int i, int k) { |
| 211 | + int temp; |
| 212 | + struct node* p; |
| 213 | + struct node* y; |
| 214 | + struct node* z; |
| 215 | + p = FIND_NODE(H, i); |
| 216 | + if (p == NULL) { |
| 217 | + printf("\nINVALID CHOICE OF KEY TO BE REDUCED"); |
| 218 | + return 0; |
| 219 | + } |
| 220 | + if (k > p->data) { |
| 221 | + printf("\nSORY!THE NEW KEY IS GREATER THAN CURRENT ONE"); |
| 222 | + return 0; |
| 223 | + } |
| 224 | + p->data = k; |
| 225 | + y = p; |
| 226 | + z = p->parent; |
| 227 | + while (z != NULL && y->data < z->data) { |
| 228 | + temp = y->data; |
| 229 | + y->data = z->data; |
| 230 | + z->data = temp; |
| 231 | + y = z; |
| 232 | + z = z->parent; |
| 233 | + } |
| 234 | + printf("\nKEY REDUCED SUCCESSFULLY!"); |
| 235 | +} |
| 236 | + |
| 237 | +int bin_HEAP_DELETE(struct node* H, int k) { |
| 238 | + struct node* np; |
| 239 | + if (H == NULL) { |
| 240 | + printf("\nHEAP EMPTY"); |
| 241 | + return 0; |
| 242 | + } |
| 243 | + |
| 244 | + bin_HEAP_DECREASE_KEY(H, k, -1000); |
| 245 | + np = bin_HEAP_EXTRACT_MIN(H); |
| 246 | + if (np != NULL) |
| 247 | + printf("\nNODE DELETED SUCCESSFULLY"); |
| 248 | +} |
| 249 | + |
| 250 | +int main() { |
| 251 | + int i, data, m, l; |
| 252 | + struct node* p; |
| 253 | + struct node* np; |
| 254 | + char ch; |
| 255 | + printf("\nENTER THE NUMBER OF ELEMENTS:"); |
| 256 | + scanf("%d", &data); |
| 257 | + printf("\nENTER THE ELEMENTS:\n"); |
| 258 | + for (i = 1; i <= data; i++) { |
| 259 | + scanf("%d", &m); |
| 260 | + np = CREATE_NODE(m); |
| 261 | + H = bin_HEAP_INSERT(H, np); |
| 262 | + } |
| 263 | + DISPLAY(H); |
| 264 | + do { |
| 265 | + printf("\nMENU:-\n"); |
| 266 | + printf( |
| 267 | + "\n1)INSERT AN ELEMENT\n2)EXTRACT THE MINIMUM KEY NODE\n3)DECREASE A NODE KEY\n 4)DELETE A NODE\n5)QUIT\n"); |
| 268 | + scanf("%d", &l); |
| 269 | + switch (l) { |
| 270 | + case 1: |
| 271 | + do { |
| 272 | + printf("\nENTER THE ELEMENT TO BE INSERTED:"); |
| 273 | + scanf("%d", &m); |
| 274 | + p = CREATE_NODE(m); |
| 275 | + H = bin_HEAP_INSERT(H, p); |
| 276 | + printf("\nNOW THE HEAP IS:\n"); |
| 277 | + DISPLAY(H); |
| 278 | + printf("\nINSERT MORE(y/Y)= \n"); |
| 279 | + fflush(stdin); |
| 280 | + scanf("%c", &ch); |
| 281 | + } while (ch == 'Y' || ch == 'y'); |
| 282 | + break; |
| 283 | + case 2: |
| 284 | + do { |
| 285 | + printf("\nEXTRACTING THE MINIMUM KEY NODE"); |
| 286 | + p = bin_HEAP_EXTRACT_MIN(H); |
| 287 | + if (p != NULL) |
| 288 | + printf("\nTHE EXTRACTED NODE IS %d", p->data); |
| 289 | + printf("\nNOW THE HEAP IS:\n"); |
| 290 | + DISPLAY(H); |
| 291 | + printf("\nEXTRACT MORE(y/Y)\n"); |
| 292 | + fflush(stdin); |
| 293 | + scanf("%c", &ch); |
| 294 | + } while (ch == 'Y' || ch == 'y'); |
| 295 | + break; |
| 296 | + case 3: |
| 297 | + do { |
| 298 | + printf("\nENTER THE KEY OF THE NODE TO BE DECREASED:"); |
| 299 | + scanf("%d", &m); |
| 300 | + printf("\nENTER THE NEW KEY : "); |
| 301 | + scanf("%d", &l); |
| 302 | + bin_HEAP_DECREASE_KEY(H, m, l); |
| 303 | + printf("\nNOW THE HEAP IS:\n"); |
| 304 | + DISPLAY(H); |
| 305 | + printf("\nDECREASE MORE(y/Y)\n"); |
| 306 | + fflush(stdin); |
| 307 | + scanf("%c", &ch); |
| 308 | + } while (ch == 'Y' || ch == 'y'); |
| 309 | + break; |
| 310 | + case 4: |
| 311 | + do { |
| 312 | + printf("\nENTER THE KEY TO BE DELETED: "); |
| 313 | + scanf("%d", &m); |
| 314 | + bin_HEAP_DELETE(H, m); |
| 315 | + printf("\nDELETE MORE(y/Y)\n"); |
| 316 | + fflush(stdin); |
| 317 | + scanf("%c", &ch); |
| 318 | + } while (ch == 'y' || ch == 'Y'); |
| 319 | + break; |
| 320 | + case 5: |
| 321 | + printf("\nTHANK U SIR\n"); |
| 322 | + break; |
| 323 | + default: |
| 324 | + printf("\nINVALID ENTRY...TRY AGAIN....\n"); |
| 325 | + } |
| 326 | + } while (l != 5); |
| 327 | +} |
0 commit comments