vadimk772336

Untitled

Oct 25th, 2021
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. using namespace std;
  5.  
  6. struct heap_elements;
  7.  
  8. struct list_segments {  //Структура отрезка - элемента списка отрезок
  9.     int l;
  10.     int r;
  11.     int status;         // Но лучше просто считать это элементов класса и делать del по указателю
  12.     int position; //Найдя по указателю отерзок поймём какой он в списке и вызовем del(pos)
  13.     struct list_segments *next;
  14.     struct list_segments *prev;
  15.     heap_elements *to_heap; //Указатель на себя в куче (элемент структуры heap_elements)
  16.    
  17. };
  18. struct heap_elements {
  19.     int l;
  20.     int r;
  21.     int position;
  22.     list_segments *to_list; //Указатель на себя в списке отрезков.
  23. };
  24.  
  25.  
  26. bool operator<(const heap_elements& a, const heap_elements& b)
  27. {
  28.     int len_a = a.r-a.l;
  29.     int len_b = b.r-b.l;
  30.    
  31.     if ( (a.r-a.l < b.r-b.l) )
  32.         return true;
  33.     if ( (a.r-a.l == b.r-b.l) & (a.l > b.l))
  34.         return true;
  35.  
  36.     return false;
  37. }
  38.  
  39.  
  40. class Heap
  41. {
  42.     static const int SIZE = 100; // максимальный размер кучи
  43.     struct heap_elements* h; // указатель на массив кучи
  44.     int HeapSize; // размер кучи
  45. public:
  46.     Heap(); // конструктор кучи
  47.     void addelem(heap_elements); // добавление элемента кучи
  48.     void outHeap(); // вывод элементов кучи в форме кучи
  49.     void out(); // вывод элементов кучи в форме массива
  50.     void siftup();
  51.     void siftdown(int);
  52.     void dalete(int);
  53. };
  54.  
  55. Heap::Heap()
  56. {
  57.     h = new heap_elements[SIZE];
  58.     HeapSize = 0;
  59. }
  60.  
  61. void Heap::siftup()
  62. {
  63.     int i, parent;
  64.     i = HeapSize - 1;
  65.     parent = (i - 1) / 2;
  66.     while (parent >= 0 && i > 0)
  67.     {
  68.         if (h[parent] < h[i])
  69.         {
  70.             heap_elements temp = h[i];
  71.             h[i] = h[parent];
  72.             h[parent] = temp;
  73.             h[i].position = i;
  74.             h[parent].position = parent;
  75.         }
  76.         i = parent;
  77.         parent = (i - 1) / 2;
  78.     }
  79. }
  80.  
  81. void Heap::addelem(heap_elements elem)
  82. {
  83.  
  84.     h[HeapSize] = elem;
  85.     //h[HeapSize].r = elem.r;
  86.     //h[HeapSize].to_list=elem.to_list;
  87.     h[HeapSize].position = HeapSize;
  88.     HeapSize++;
  89.     siftup();
  90.     /*
  91.   parent = (i-1)/2;
  92.   while(parent >= 0 && i > 0)  {
  93.     if(h[i] > h[parent]) {
  94.       int temp = h[i];
  95.       h[i] = h[parent];
  96.       h[parent] = temp;
  97.     }
  98.     i = parent;
  99.     parent = (i-1)/2;
  100.   }
  101.   */
  102. }
  103.  
  104. void Heap::siftdown(int position)
  105. {
  106.     int i, parent;
  107.     int max_child;
  108.     i = position;
  109.     int child1 = 2 * i + 1;
  110.     int child2 = 2 * i + 2;
  111.     if (h[child2] < h[child1])
  112.         max_child = child1;
  113.     else
  114.         max_child = child2;
  115.  
  116.     while (child1 < HeapSize)
  117.     {
  118.         if (child1 == HeapSize - 1)
  119.         {
  120.             max_child = child1;
  121.         }
  122.         else if (h[child2] < h[child1])
  123.             max_child = child1;
  124.         else
  125.             max_child = child2;
  126.  
  127.         if (h[i] < h[max_child])
  128.         {
  129.             heap_elements temp = h[i];
  130.             h[i] = h[max_child];
  131.             h[max_child] = temp;
  132.             h[i].position = i;
  133.             h[max_child].position = max_child;
  134.         }
  135.         i = max_child;
  136.         child1 = 2 * i + 1;
  137.         child2 = 2 * i + 2;
  138.     }
  139. }
  140.  
  141. void Heap::dalete (int position)
  142. {
  143.  
  144.     h[position] = h[HeapSize - 1];
  145.     h[HeapSize - 1].l = 0;
  146.     h[HeapSize - 1].r = 0;
  147.     HeapSize--;
  148.     siftdown(position);
  149. }
  150.  
  151. void Heap::out(void)
  152. {
  153.     for (int i = 0; i < HeapSize; i++)
  154.     {
  155.         cout << h[i].l << " "<<h[i].r<<" ";
  156.     }
  157.     cout << endl;
  158. }
  159.  
  160. int main()
  161. {
  162.     Heap heap;
  163.     //int k;
  164.     heap_elements vertex;
  165.     vertex.l = 1;
  166.     vertex.r = 7;
  167.     vertex.to_list = NULL;
  168.     heap.addelem(vertex);
  169.     vertex.l = 0;
  170.     vertex.r = 2;
  171.     vertex.to_list = NULL;
  172.     heap.addelem(vertex);
  173.     vertex.l = 4;
  174.     vertex.r = 6;
  175.     vertex.to_list = NULL;
  176.     heap.addelem(vertex);
  177.  
  178.     heap.out();
  179.     heap.dalete(0);
  180.     heap.out();
  181.     return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment