Advertisement
Guest User

Untitled

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