Advertisement
Guest User

Kopiec

a guest
Nov 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. //ALGO2 IS1 211A LAB04
  2. //Jakub Ogryzek
  3. //oj44443@zut.edu.pl
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <time.h>
  8. #include <iomanip>
  9. #include <string>
  10. #include <cstdlib>
  11. #include <memory>
  12. #include <string>
  13. #include <random>
  14.  
  15. using namespace std;
  16.  
  17. template <typename T>
  18. class Heap {
  19. public:
  20.     T *data = new T[capacity];
  21.     int capacity;
  22.     int size;
  23.     Heap()
  24.     {
  25.         size = 0;
  26.         capacity = 1;
  27.         data = new T[capacity];
  28.     }
  29.     ~Heap()
  30.     {
  31.         delete[] data;
  32.         data = nullptr;
  33.         size = 0;
  34.         capacity = 0;
  35.     }
  36.  
  37.     int Push(int size)
  38.     {
  39.         if (size == 0)
  40.             return 0;
  41.         int parent_index = parent(size);
  42.         if (parent_index == -1000)
  43.             return 0;
  44.         if (data[size] <= data[parent_index])
  45.             return 0;
  46.         else {
  47.             swap(data[size], data[parent_index]);
  48.             Push(parent_index);
  49.         }
  50.     }
  51.  
  52.     void AddNewElement(const T&input)
  53.     {
  54.         size = size + 1;
  55.         if (size <= capacity) {
  56.             data[size - 1] = input;
  57.         }
  58.         if (size > capacity)
  59.         {
  60.             capacity = capacity * 2;
  61.             T *temp = new T[capacity];
  62.             for (int i = 0; i < size - 1; i++)
  63.             {
  64.                 temp[i] = move(data[i]);
  65.             }
  66.             temp[size - 1] = input;
  67.             delete[] data;
  68.             data = temp;
  69.         }
  70.         Push(size - 1);
  71.     }
  72.  
  73.     void DeleteRoot(int index)
  74.     {
  75.         data[0] = data[index];
  76.         //data[index] = NULL;
  77.         DeleteRootRek(0);
  78.         size = size - 1;
  79.     }
  80.  
  81.     int DeleteRootRek(int index)
  82.     {
  83.         int left = left_son(index);
  84.         int right = right_son(index);
  85.         if (data[index] > data[left] && data[index] > data[right])
  86.             //wezel ma wieksza wartosc od obydwu dzieci
  87.             return 0;
  88.         if (data[index] > data[left] && data[index] < data[right]) {
  89.             //idziemy w prawo
  90.             swap(data[index], data[right]);
  91.             DeleteRootRek(right);
  92.         }
  93.         if (data[index] < data[left] && data[index] > data[right]) {
  94.             //idziemy w lewo
  95.             swap(data[index], data[left]);
  96.             DeleteRootRek(left);
  97.         }
  98.         if (data[index] < data[left] && data[index] < data[right]) {
  99.             //obydwa dzieci maja wieksza wartosc i idziemy tam gdzie wartosc jest wieksza
  100.             if (data[left] > data[right]) {
  101.                 swap(data[index], data[left]);
  102.                 DeleteRootRek(left);
  103.             }
  104.             if (data[left] < data[right]) {
  105.                 swap(data[index], data[right]);
  106.                 DeleteRootRek(right);
  107.             }
  108.         }
  109.     }
  110.  
  111.     void swap(T&x, T&y)
  112.     {
  113.         T temp;
  114.         temp = x;
  115.         x = y;
  116.         y = temp;
  117.     }
  118.  
  119.     int parent(int index)
  120.     {
  121.         if (index > 0)
  122.             return (index - 1) / 2;
  123.         else {
  124.             cout << "Rodzic tego elementu nie istnieje" << endl;
  125.             return -1000;
  126.         }
  127.     }
  128.     int left_son(int index)
  129.     {
  130.         if (2 * index + 1 < size)
  131.             return 2 * index + 1;
  132.         else {
  133.             cout << "Ten element nie ma lewego syna" << endl;
  134.             return -1000;
  135.         }
  136.     }
  137.     int right_son(int index)
  138.     {
  139.         if (2 * index + 2 < size)
  140.             return 2 * index + 2;
  141.         else {
  142.             cout << "Ten element nie ma prawego syna" << endl;
  143.             return -1000;
  144.         }
  145.     }
  146.  
  147.     void ToString()
  148.     {
  149.         cout << "Kopiec:" << endl;
  150.         cout << "Liczba elementow: " << size << endl;
  151.         int dzielenie = 1;
  152.         for (int i = 0; i < size; i++)
  153.         {
  154.             cout << data[i] << ", ";
  155.         }
  156.     }
  157.  
  158.     void ClearHeap()
  159.     {
  160.         cout << "Kopiec zostal wyczyszczony" << endl;
  161.         for (int i = 0; i < size; i++)
  162.         {
  163.             data[i].~T();
  164.         }
  165.         size = 0;
  166.         capacity = 1;
  167.     }
  168. };
  169.  
  170. struct Struktura
  171. {
  172.     int pole1;
  173.     Struktura()
  174.     {
  175.         const int MAX_ORDER = 8;
  176.         const int n = pow(10, MAX_ORDER);
  177.         std::random_device rd;
  178.         std::default_random_engine generator(rd());
  179.         std::uniform_int_distribution<long long> zakres(1, n);
  180.         pole1 = zakres(generator);
  181.     }
  182.     Struktura(int a)
  183.     {
  184.         pole1 = a;
  185.     }
  186.     bool operator ==(const Struktura&n) const
  187.     {
  188.         return pole1 == n.pole1;
  189.     }
  190.     bool operator >(const Struktura&n) const
  191.     {
  192.         return pole1 > n.pole1;
  193.     }
  194.     bool operator <(const Struktura&n) const
  195.     {
  196.         return pole1 < n.pole1;
  197.     }
  198.     bool operator <=(const Struktura&n) const
  199.     {
  200.         return pole1 <= n.pole1;
  201.     }
  202.     bool operator >=(const Struktura&n) const
  203.     {
  204.         return pole1 >= n.pole1;
  205.     }
  206. };
  207.  
  208. ostream& operator <<(ostream&stream, const Struktura&m)
  209. {
  210.     return (stream << m.pole1); //<< m.pole2);
  211. }
  212.  
  213. int main()
  214. {
  215.     const int MAX_ORDER = 7;
  216.     Heap<Struktura>*binary_heap = new Heap<Struktura>();
  217.     //for (int o = 1; o <= MAX_ORDER; o++)
  218.     //{
  219.         const int n = pow(10, 1);
  220.         clock_t t1 = clock();
  221.         for (int i = 0; i < n; i++)
  222.         {
  223.             binary_heap->AddNewElement(Struktura{});
  224.         }
  225.         clock_t t2 = clock();
  226.         cout << "Czas wykonania operacji dodawania " << n << " elementow to: " << (double)(t2 - t1) / CLOCKS_PER_SEC << " sekundy" << endl;
  227.     //}
  228.     system("pause");
  229.     //binary_heap->DeleteRoot(binary_heap->size - 1);
  230.     binary_heap->ClearHeap();
  231.     delete binary_heap;
  232.  
  233.  
  234.  
  235.  
  236.  
  237.     /*Heap<int>* heap = new Heap<int>();
  238.     heap->AddNewElement(1); //0
  239.     heap->AddNewElement(2); //1
  240.     heap->AddNewElement(3); //2
  241.     heap->AddNewElement(4); //3
  242.     heap->AddNewElement(5); //4
  243.     heap->AddNewElement(6); //5
  244.     heap->AddNewElement(7); //6
  245.     heap->AddNewElement(8);
  246.     heap->AddNewElement(9);
  247.     heap->AddNewElement(10);
  248.     heap->AddNewElement(11);
  249.     heap->AddNewElement(12);
  250.     heap->AddNewElement(13);
  251.     heap->AddNewElement(14);
  252.     heap->AddNewElement(15);
  253.     //heap->DeleteRoot(heap->size - 1);
  254.     heap->ToString();
  255.     //heap->ClearHeap();
  256.     delete heap;*/
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement