Advertisement
Guest User

Untitled

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