Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class Node {
  10. private:
  11.     int key;
  12.  
  13. public:
  14.     Node(int key) {
  15.         this->key = key;
  16.     }
  17.  
  18.     int getKey() {
  19.         return this->key;
  20.     }
  21. };
  22.  
  23. class Heap {
  24. private:
  25.     vector<Node*> nodes;
  26.     int size;
  27.  
  28.     int compareTo(Node* a, Node* b) {
  29.         return a->getKey() - b->getKey();
  30.     }
  31.  
  32.     void upHeap(int index) {
  33.         if ((index - 1) / 2 >= 0 && compareTo(nodes[index], nodes[(index - 1) / 2]) < 0) {
  34.             Node* t = nodes[index];
  35.             nodes[index] = nodes[(index - 1) / 2];
  36.             nodes[(index - 1) / 2] = t;
  37.  
  38.             upHeap((index - 1) / 2);
  39.         }
  40.     }
  41.  
  42.     void downHeap(int index) {
  43.         int left = 2 * index + 1;
  44.         int right = 2 * index + 2;
  45.  
  46.         Node* t;
  47.         if (right < size) {
  48.             if (compareTo(nodes[index], nodes[left]) < 0 && compareTo(nodes[index], nodes[right]) < 0) {
  49.                 return;
  50.             }
  51.  
  52.             if (compareTo(nodes[left], nodes[right]) < 0) {
  53.                 t = nodes[left];
  54.                 nodes[left] = nodes[index];
  55.                 nodes[index] = t;
  56.  
  57.                 downHeap(left);
  58.             } else {
  59.                 t = nodes[right];
  60.                 nodes[right] = nodes[index];
  61.                 nodes[index] = t;
  62.  
  63.                 downHeap(right);
  64.             }
  65.         } else if (left < size) {
  66.             if (compareTo(nodes[index], nodes[left]) < 0) {
  67.                 return;
  68.             }
  69.  
  70.             t = nodes[index];
  71.             nodes[index] = nodes[left];
  72.             nodes[left] = t;
  73.  
  74.             downHeap(left);
  75.         }
  76.     }
  77.  
  78. public:
  79.  
  80.     Heap() {
  81.         this->size = 0;
  82.     }
  83.  
  84.     Heap(vector<int>& nodes) {
  85.         this->size = 0;
  86.  
  87.         for (auto & it : nodes) {
  88.             this->add(it);
  89.         }
  90.     }
  91.  
  92.     ~Heap() {
  93.         for (auto & it : nodes) {
  94.             delete it;
  95.         }
  96.     }
  97.  
  98.     void add(int val) {
  99.         nodes.push_back(new Node(val));
  100.         size++;
  101.  
  102.         upHeap(size - 1);
  103.     }
  104.  
  105.     Node* remove() {
  106.         if (size < 1) {
  107.             cout << "Kopiec pusty!" << endl << endl;
  108.             return NULL;
  109.         }
  110.  
  111.         Node* t = nodes[0];
  112.         nodes[0] = nodes[size - 1];
  113.         nodes[size - 1] = t;
  114.  
  115.         Node* root = nodes[size - 1];
  116.         nodes.pop_back();
  117.         size--;
  118.  
  119.         downHeap(0);
  120.  
  121.         return root;
  122.     }
  123.  
  124.     void clean() {
  125.         while (size > 0) {
  126.             remove();
  127.         }
  128.     }
  129.  
  130.     void display() {
  131.         cout << "Rozmiar kopca: " << size << endl;
  132.         for (auto & it : nodes) {
  133.             cout << it->getKey() << endl;
  134.         }
  135.  
  136.         cout << endl;
  137.     }
  138. };
  139.  
  140. int main() {
  141.  
  142.     srand((unsigned) time(NULL));
  143.  
  144.     const int MAX_ORDER = 7;
  145.     Heap* heap = new Heap();
  146.     for ( int o = 1; o <= MAX_ORDER ; o++)
  147.     {
  148.         const int n = pow (10 , o);
  149.  
  150.         clock_t t1 = clock ();
  151.         for ( int i = 0; i < n; i++) {
  152.             int randomValue = rand() % n;
  153.             heap->add(randomValue);
  154.         }
  155.         clock_t t2 = clock ();
  156.  
  157.         cout << "Czas wykonania (dodawanie): " << (double) (t2 - t1) / CLOCKS_PER_SEC << endl << endl;
  158.  
  159.         t1 = clock();
  160.         for ( int i = 0; i < n; i++) {
  161.             Node* polled = heap->remove();
  162.             cout << polled->getKey() << endl;
  163.             delete polled ;
  164.         }
  165.         t2 = clock();
  166.         cout << endl;
  167.  
  168.         cout << "Czas wykonania (usuwanie): " << (double) (t2 - t1) / CLOCKS_PER_SEC << endl << endl;
  169.     }
  170.  
  171.     delete heap;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement