i_love_rao_khushboo

Creation, Insertion and Deletion af a Heap

Jun 1st, 2022 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. // Ref: https://www.udemy.com/course/datastructurescncpp/
  2. /********************************************************************************************************/
  3.  
  4. // In the below algorithm operations for binary max heap have been implemented, binary min heap can also
  5. // be created similarly.
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define ll long long
  11. #define ld long double
  12. #define ull unsigned long long
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define mp make_pair
  16. #define F first
  17. #define S second
  18. #define PI 3.1415926535897932384626
  19. #define sz(x) ((int)(x).size())
  20.  
  21. typedef pair<int, int> pii;
  22. typedef pair<ll, ll> pll;
  23. typedef vector<int> vi;
  24. typedef vector<ll> vll;
  25. typedef vector<ull> vull;
  26. typedef vector<bool> vb;
  27. typedef vector<char> vc;
  28. typedef vector<string> vs;
  29. typedef vector<pii> vpii;
  30. typedef vector<pll> vpll;
  31. typedef vector<vi> vvi;
  32. typedef vector<vll> vvll;
  33. typedef vector<vull> vvull;
  34. typedef vector<vb> vvb;
  35. typedef vector<vc> vvc;
  36. typedef vector<vs> vvs;
  37.  
  38. /************************************************** DEBUGGER *******************************************************************************************************/
  39.  
  40. #ifndef ONLINE_JUDGE
  41. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  42. #else
  43. #define debug(x)
  44. #endif
  45.  
  46. void _print(ll t) { cerr << t; }
  47. void _print(int t) { cerr << t; }
  48. void _print(string t) { cerr << t; }
  49. void _print(char t) { cerr << t; }
  50. void _print(ld t) { cerr << t; }
  51. void _print(double t) { cerr << t; }
  52. void _print(ull t) { cerr << t; }
  53.  
  54. template <class T, class V> void _print(pair <T, V> p);
  55. template <class T> void _print(vector <T> v);
  56. template <class T> void _print(vector <vector<T>> v);
  57. template <class T> void _print(set <T> v);
  58. template <class T, class V> void _print(map <T, V> v);
  59. template <class T> void _print(multiset <T> v);
  60. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  61. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  62. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  63. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  64. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  65. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  66.  
  67. /*******************************************************************************************************************************************************************/
  68.  
  69. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  70. int rng(int lim) {
  71.     uniform_int_distribution<int> uid(0,lim-1);
  72.     return uid(rang);
  73. }
  74.  
  75. const int INF = 0x3f3f3f3f;
  76. const int mod = 1e9+7;
  77.  
  78. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  79.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  80.                          
  81. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  82. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  83.  
  84. /******************************************************************************************************************************/
  85.  
  86. // it is the position where the last element in the heap is present
  87. int pos;
  88.  
  89. int delete_heap(vi &v) {
  90.     if(pos == -1) {
  91.         cout << "Heap empty! ";
  92.         return INT_MAX;
  93.     }
  94.    
  95.     int x = v[0];
  96.     v[0] = v[pos];
  97.     pos -= 1;
  98.    
  99.     // i => to store the index of parent
  100.     // j => to store the index of left child of i
  101.     int i = 0, j = 1;  
  102.    
  103.     // Nothing but an iterative implementation of the heapify() algorithm
  104.     while(j <= pos) {
  105.         // compare the left and right childs of i
  106.         if(j + 1 <= pos and v[j+1] > v[j]) j += 1;
  107.        
  108.         // compare the parent i, with the max(left child, right child)
  109.         if(v[j] > v[i]) {
  110.             swap(v[j], v[i]);
  111.             i = j;
  112.             j = 2 * i + 1;
  113.         }
  114.        
  115.         else break;
  116.     }
  117.    
  118.     return x;
  119. }
  120.  
  121. // insertion is being done in-place
  122. void insert_heap(vi &v, int n) {
  123.     int tmp = v[n], i = n;
  124.    
  125.     // the parent index can also be found by ceil(i/2) - 1
  126.     while(i > 0 and v[(i % 2 == 0) ? (i / 2 - 1) : (i / 2)] < tmp) {
  127.         v[i] = v[(i % 2 == 0) ? (i / 2 - 1) : (i / 2)];
  128.         i = (i % 2 == 0) ? (i / 2 - 1) : (i / 2);
  129.     }
  130.    
  131.     v[i] = tmp;
  132. }
  133.  
  134. void create_heap(vi &v) {
  135.     int n = sz(v);
  136.    
  137.     for(int i = 0; i < n; i++) {
  138.         insert_heap(v, i);
  139.     }
  140. }
  141.  
  142. void solve()
  143. {
  144.     int n; cin >> n;
  145.     vi v(n);
  146.     for(int i = 0; i < n; i++) cin >> v[i];
  147.    
  148.     pos = -1;
  149.     create_heap(v);
  150.     pos = n - 1;
  151.  
  152.     cout << delete_heap(v) << "\n";
  153.     cout << delete_heap(v) << "\n";
  154.     cout << delete_heap(v) << "\n";
  155.     cout << delete_heap(v) << "\n";
  156.     cout << delete_heap(v) << "\n";
  157.     cout << delete_heap(v) << "\n";
  158.     cout << delete_heap(v) << "\n";
  159. }
  160.  
  161. int main()
  162. {
  163.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  164.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  165.  
  166.     // #ifndef ONLINE_JUDGE
  167.     //     freopen("input.txt", "r", stdin);
  168.     //     freopen("output.txt", "w", stdout);
  169.     // #endif
  170.    
  171.     // #ifndef ONLINE_JUDGE
  172.     //      freopen("error.txt", "w", stderr);
  173.     // #endif
  174.    
  175.     int t = 1;
  176.     // int test = 1;
  177.     // cin >> t;
  178.     while(t--) {
  179.         // cout << "Case #" << test++ << ": ";
  180.         solve();
  181.     }
  182.  
  183.     return 0;
  184. }
Add Comment
Please, Sign In to add comment