Advertisement
asrori

binary heap

May 30th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int array[100], n;
  4.  
  5. void insert(int num, int location);
  6. void hapus(int num);
  7. void display();
  8.  
  9. int main() {
  10.     int choice, num, root = 0;
  11.     int quit = 0;
  12.     n = 0;
  13.     while(!quit) {
  14.         printf("1.Insert the element \n");
  15.         printf("2.Delete the element \n");
  16.         printf("3.Display all elements \n");
  17.         printf("4.Quit \n");
  18.         printf("Enter your choice : ");
  19.         scanf("%d", &choice);
  20.         switch(choice) {
  21.             case 1:
  22.                 printf("Enter the element to be inserted to the list : ");
  23.                 scanf("%d", &num);
  24.                 insert(num, n);
  25.                 n = n + 1;
  26.                 break;
  27.             case 2:
  28.                 hapus(array[0]);
  29.                 break;
  30.             case 3:
  31.                 display();
  32.                 break;
  33.             case 4:
  34.                 quit = 1;
  35.                 break;
  36.             default:
  37.                 printf("Invalid choice \n");
  38.         }
  39.     }
  40. }
  41.  
  42. void display() {
  43.     int i;
  44.     if (n == 0) {
  45.         printf("Heap is empty \n");
  46.         return;
  47.     }
  48.     for (i = 0; i < n; i++)
  49.         printf("%d ", array[i]);
  50.     printf("\n");
  51. }
  52.  
  53. void insert(int num, int location) {
  54.     int parentnode;
  55.     while (location > 0) {
  56.         parentnode = (location - 1)/2;
  57.         if (num <= array[parentnode]) {
  58.             array[location] = num;
  59.             return;
  60.         }
  61.         array[location] = array[parentnode];
  62.         location = parentnode;
  63.     }
  64.     array[0] = num;
  65. }
  66.  
  67. void hapus(int num) {
  68.     int left, right, i, temp, parentnode;
  69.  
  70.     for (i = 0; i < num; i++) {
  71.         if (num == array[i])
  72.             break;
  73.     }
  74.     if (num != array[i]) {
  75.         printf("%d not found in heap list\n", num);
  76.         return;
  77.     }
  78.     array[i] = array[n - 1];
  79.     n = n - 1;
  80.     parentnode =(i - 1) / 2;
  81.     if (array[i] > array[parentnode]) {
  82.         insert(array[i], i);
  83.         return;
  84.     }
  85.     left = 2 * i + 1;
  86.     right = 2 * i + 2;
  87.     while (right < n) {
  88.         if (array[i] >= array[left] && array[i] >= array[right])
  89.             return;
  90.         if (array[right] <= array[left]) {
  91.             temp = array[i];
  92.             array[i] = array[left];
  93.             array[left] = temp;
  94.             i = left;
  95.         } else {
  96.             temp = array[i];
  97.             array[i] = array[right];
  98.             array[right] = temp;
  99.             i = right;
  100.         }
  101.         left = 2 * i + 1;
  102.         right = 2 * i + 2;
  103.     }
  104.     if (left == n - 1 && array[i])    {
  105.         temp = array[i];
  106.         array[i] = array[left];
  107.         array[left] = temp;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement