Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <climits>
  4. #include <fstream>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <iostream>
  8. #include<string.h>
  9.  
  10. using namespace std;
  11.  
  12. struct hufnod{
  13.     char c;
  14.     int frecv;
  15.     struct hufnod *right,*left;
  16. };
  17.  
  18. struct nod{
  19.     hufnod *p;
  20.     struct nod *next;
  21. };
  22.  
  23. // swap two integers
  24. void swap(int *x, int *y)
  25. {
  26. int temp = *x;
  27. *x = *y;
  28. *y = temp;
  29. }
  30. // Min-heap class
  31. class Min_Heap
  32. {
  33. int *heaparr; // pointer to array of elements in heap
  34. int capacity; // maximum capacity of min heap
  35. int heap_size; // current heap size
  36. public:
  37. Min_Heap(int cap){
  38. heap_size = 0;
  39. capacity = cap;
  40. heaparr = new int[capacity];
  41. }
  42. // to heapify a subtree with the root at given index
  43. void MinHeapify(int );
  44. int parent(int i) { return (i-1)/2; }
  45. // left child of node i
  46. int left(int i) { return (2*i + 1); }
  47. // right child of node i
  48. int right(int i) { return (2*i + 2); }
  49. // extract minimum element in the heap(root of the heap)
  50. int extractMin();
  51. // decrease key value to newKey at i
  52. void decreaseKey(int i, int newKey);
  53. // returns root of the min heap
  54. int getMin() { return heaparr[0]; }
  55. // Deletes a key at i
  56. void deleteKey(int i);
  57. // Inserts a new key 'key'
  58. void insertKey(int key);
  59. void displayHeap(int n){
  60. for(int i = 0;i<n;i++)
  61. cout<<heaparr[i]<<" ";
  62. cout<<endl;
  63.  
  64. }
  65. };
  66. // Inserts a new key 'key'
  67. void Min_Heap::insertKey(int key)
  68. {
  69. if (heap_size == capacity) {
  70. cout << "\nOverflow: Could not insertKey\n";
  71. return;
  72. }
  73. // First insert the new key at the end
  74. heap_size++;
  75. int i = heap_size - 1;
  76. heaparr[i] = key;
  77. // Fix the min heap property if it is violated
  78. while (i != 0 && heaparr[parent(i)] > heaparr[i])
  79. {
  80. swap(&heaparr[i], &heaparr[parent(i)]);
  81. i = parent(i);
  82. }
  83. }
  84. void Min_Heap::decreaseKey(int i, int newKey) {
  85. heaparr[i] = newKey;
  86. while (i != 0 && heaparr[parent(i)] > heaparr[i]) {
  87. swap(&heaparr[i], &heaparr[parent(i)]);
  88. i = parent(i);
  89. }
  90. }
  91. int Min_Heap::extractMin()
  92. {
  93. if (heap_size <= 0)
  94. return INT_MAX;
  95. if (heap_size == 1) {
  96. heap_size--;
  97. return heaparr[0];
  98. }
  99. // Store the minimum value,delete it from heap
  100. int root = heaparr[0];
  101. heaparr[0] = heaparr[heap_size-1];
  102. heap_size--;
  103. MinHeapify(0);
  104. return root;
  105. }
  106. void Min_Heap::deleteKey(int i)
  107. {
  108. decreaseKey(i, INT_MIN);
  109. extractMin();
  110. }
  111. void Min_Heap::MinHeapify(int i)
  112. {
  113. int l = left(i);
  114. int r = right(i);
  115. int min = i;
  116. if (l < heap_size && heaparr[l] < heaparr[i])
  117. min = l;
  118. if (r < heap_size && heaparr[r] < heaparr[min])
  119. min = r;
  120. if (min != i)
  121. {
  122.  
  123. swap(&heaparr[i], &heaparr[min]);
  124. MinHeapify(min);
  125. }
  126. }
  127. int main(){
  128.  
  129.     char text[]={" veni_vidi_vici"};
  130.  
  131.     int Frecv[256]={0},i=0,j=0;
  132.     int n=strlen(text);
  133.     while(i<n){
  134.         /*j=0;while(j<256){if(j==text[i])Frecv[j]+=1; j++;}*/
  135.         Frecv[text[i]] +=1;
  136.         i++;
  137.     }
  138.     struct nod *h=NULL,*q,*r;
  139.     cout<<"Lista (cod simbol, numar de aparitii): "<<endl;
  140.     j=0;
  141.     while(j<256){
  142.         if(Frecv[j]){
  143.             ///caracter ,cod caracter ,numar aparitii
  144.             cout<<(char)j<<"->"<<j<<"->"<<Frecv[j]<<endl;
  145.             if(!h){
  146.                 h=new struct nod;
  147.                 h->p=new struct hufnod;
  148.                 h->p->c=j;
  149.                 h->p->frecv=Frecv[j];
  150.                 h->p->left=h->p->right=NULL;
  151.                 h->next=NULL;
  152.             }else{
  153.                 r=new struct nod;
  154.                 r->p=new struct hufnod;
  155.                 r->p->c=j;
  156.                 r->p->frecv=Frecv[j];
  157.                 r->p->left=h->p->right=NULL;
  158.                 r->next=NULL;
  159.                 q=h;
  160.                 if(r->p->frecv < q->p->frecv){  /// daca se gaseste o frecventa mai mica
  161.                     r->next=q;
  162.                     h=r;
  163.                 }else{
  164.                     while(q->next){             /// inserare intre 2 elemente
  165.                         if(q->next->p->frecv > r->p->frecv)
  166.                             break;
  167.                         q=q->next;
  168.                     }
  169.                     r->next=q->next;
  170.                     q->next=r;
  171.                 }
  172.             }
  173.         }
  174.         j++;
  175.  
  176.     }
  177.  
  178.     struct nod *nou;
  179.  
  180.     do{
  181.         q=h;
  182.         struct hufnod *a=q->p;
  183.         struct hufnod *b=q->next->p;
  184.         h=q->next->next;
  185.         nou=new struct nod;
  186.         nou->next=NULL;
  187.         nou->p=new struct hufnod;
  188.        /// cout<<a->frecv<<" "<<b->frecv<<" ";
  189.         nou->p->frecv = a->frecv + b->frecv;
  190.        /// cout<<nou->p->frecv<<" ";
  191.         if(a->frecv == b->frecv){
  192.             nou->p->left=a;
  193.             nou->p->right=b;
  194.         }else{
  195.             if(a->frecv > b->frecv){
  196.                 nou->p->left=b;
  197.                 nou->p->right=a;
  198.             }else{
  199.                 nou->p->left=a;
  200.                 nou->p->right=b;
  201.             }
  202.         }
  203.         ///cout<<nou->p->left->frecv;
  204.         ///cout<<" "<<nou->p->right->frecv<<endl;;
  205.        q=h;
  206.        if(q){
  207.            if(nou->p->frecv <= q->p->frecv){
  208.                 nou->next=q;
  209.                 h=nou;
  210.            }else{
  211.                 while(q->next){
  212.                     if(q->next->p->frecv > nou->p->frecv)
  213.                         break;
  214.                     q=q->next;
  215.                 }
  216.                 nou->next=q->next;
  217.                 q->next=nou;
  218.                 ///cout<<nou->p->frecv<<" ";
  219.            }
  220.        }
  221.     /// cout<<nou->p->frecv<<"\n";
  222.     }while(nou->p->frecv!=n);
  223.  
  224.     ifstream f("fisier.txt");
  225. Min_Heap h1(51);
  226. for(i=0;i<50;i++)
  227. {f>>Frecv[j];
  228. h1.insertKey(Frecv[j]);
  229. }
  230. cout<<"Heap after insertion:";
  231. h1.displayHeap(12);
  232.  
  233.  
  234. int x,y;
  235. x=h1.extractMin();
  236. y=h1.extractMin();
  237.  
  238. cout<<"Afisare dupa 2 extrageri consecutive a min"<<endl;
  239. h1.displayHeap(10);
  240.  
  241.  
  242.  
  243.  
  244.  
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement