Advertisement
catalin_stefann11

HEAP

Jul 1st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5.  
  6. using namespace std;
  7.  
  8. struct HeapItem
  9. {
  10.     char* profession;
  11.     float salary;
  12. };
  13.  
  14. struct Heap
  15. {
  16.     HeapItem** elements;
  17.     //dimensiunea maxima a masivului
  18.     int heapSize;
  19.     //numarul curent de elemente din vector
  20.     int noElements;
  21. };
  22.  
  23. HeapItem* createElement(char*, float);
  24. void initHeap(Heap&, int);
  25. void enqueue(Heap&, HeapItem*);
  26. void main()
  27. {
  28.     FILE* pFile = fopen("Date.txt", "r");
  29.     Heap heap;
  30.     initHeap(heap, 10);
  31.     if (pFile)
  32.     {
  33.         char buffer[100]; float salary;
  34.         fscanf(pFile, "%s", buffer);
  35.         while (!feof(pFile))
  36.         {
  37.             fscanf(pFile, "%f", &salary);
  38.  
  39.             //1. create an element of type List*
  40.             HeapItem* newElement = createElement(buffer, salary);
  41.             //2. insert the newly created element
  42.             enqueue(heap, newElement);
  43.             fscanf(pFile, "%s", buffer);
  44.         }
  45.         fclose(pFile);
  46.     }
  47.     for (int i = 0; i < heap.noElements; i++)
  48.     {
  49.         printf("Index: %d - %f\n",
  50.             i, heap.elements[i]->salary);
  51.     }
  52. }
  53.  
  54.  
  55.  
  56. void ReheapUp(Heap heap, int first, int back)
  57. {
  58.     HeapItem* parent;
  59.     int indexParent;
  60.     HeapItem* tmp;
  61.     if (back > first)
  62.     {
  63.         //rehipificare
  64.         indexParent = (back - 1) / 2;
  65.         parent = heap.elements[indexParent];
  66.         if (heap.elements[back]->salary >
  67.             parent->salary)
  68.         {
  69.             //interschimb
  70.             tmp = heap.elements[indexParent];
  71.             heap.elements[indexParent] = heap.elements[back];
  72.             heap.elements[back] = tmp;
  73.             ReheapUp(heap, 0, indexParent);
  74.         }
  75.     }
  76.     //altfel iesire din recursivitate
  77. }
  78. void ReheapDown(Heap heap, int front)
  79. {
  80.     int leftIndex = front * 2 + 1;
  81.     int rightIndex = front * 2 + 2;
  82.     int maxIndex;
  83.     HeapItem* max = NULL;
  84.     HeapItem* parent = heap.elements[front];
  85.  
  86.     if (leftIndex <= heap.noElements - 1)
  87.     {
  88.         if (leftIndex == heap.noElements - 1)
  89.             max = heap.elements[leftIndex];
  90.         else
  91.         {
  92.             if (heap.elements[leftIndex]->salary >
  93.                 heap.elements[rightIndex]->salary)
  94.             {
  95.                 max = heap.elements[leftIndex];
  96.                 maxIndex = leftIndex;
  97.             }
  98.             else
  99.             {
  100.                 max = heap.elements[rightIndex];
  101.                 maxIndex = rightIndex;
  102.             }
  103.         }
  104.         if (max->salary > heap.elements[front]->salary)
  105.         {
  106.             heap.elements[front] = max;
  107.             heap.elements[rightIndex] = parent;
  108.             ReheapDown(heap, maxIndex);
  109.         }
  110.     }
  111. }
  112.  
  113. HeapItem* dequeue(Heap& heap)
  114. {
  115.     HeapItem* result = NULL;
  116.     if (heap.noElements != 0)
  117.     {
  118.         result = heap.elements[0];
  119.         heap.noElements--;
  120.         heap.elements[0] = heap.elements[heap.noElements];
  121.         ReheapDown(heap, 0);
  122.     }
  123.     return result;
  124. }
  125.  
  126. void enqueue(Heap& heap, HeapItem* item)
  127. {
  128.     if (heap.noElements < heap.heapSize)
  129.     {
  130.         heap.elements[heap.noElements] = item;
  131.         ReheapUp(heap, 0, heap.noElements);
  132.         heap.noElements++;
  133.     }
  134. }
  135.  
  136. void initHeap(Heap& heap, int size)
  137. {
  138.     heap.elements = (HeapItem**)malloc(sizeof(HeapItem*)* size);
  139.     heap.heapSize = size;
  140.     heap.noElements = 0;
  141.     memset(heap.elements, NULL, sizeof(HeapItem*)*size);
  142. }
  143.  
  144. HeapItem* createElement(char* buffer, float salary)
  145. {
  146.     //1.allocate memory for the new element
  147.     HeapItem* newElement = (HeapItem*)malloc(sizeof(HeapItem));
  148.     //2.initialize it with the params
  149.     //(the connection attributes should remain NULL)
  150.     newElement->salary = salary;
  151.     newElement->profession = (char*)malloc(strlen(buffer) + 1);
  152.     strcpy(newElement->profession, buffer);
  153.     //3.return the new element
  154.     return newElement;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement