Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  Huffman coding
  4. //
  5. //  Created by Dawid on 17/12/2017.
  6. //  Copyright © 2017 Dawid. All rights reserved.
  7. //ALA MA KOTA, A KOT MA ALE.
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #define MAX (1000000+7)
  13. #define INF 1000000000;
  14. struct pair
  15. {
  16.     char first;
  17.     int second;
  18. };
  19.  
  20. struct pair wystapienia[130];
  21.  
  22. int iloscLiter=0;
  23. int dlugosc=0;
  24.  
  25. void dodaj( struct pair kopiec[iloscLiter], struct pair a)
  26. {
  27.     dlugosc++;
  28.     kopiec[dlugosc]=a;
  29.     struct pair tmp;
  30.     int index=dlugosc;
  31.     while(kopiec[index].second<kopiec[index-1].second && index>1)
  32.     {
  33.         tmp= kopiec[index];
  34.         kopiec[index]=kopiec[index-1];
  35.         kopiec[index-1]=tmp;
  36.         index--;
  37.     }
  38. }
  39.  
  40.  
  41. struct node
  42. {
  43.     int data;
  44.     struct node *left;
  45.     struct node *right;
  46. };
  47. typedef struct node Node;
  48.  
  49.  
  50. Node* newNode(int data)
  51. {
  52.     Node* node = (struct node*)malloc(sizeof(struct node));
  53.     node->data = data;
  54.     node->left = NULL;
  55.     node->right = NULL;
  56.     return(node);
  57. }
  58. void usunNode(Node *kopiec[iloscLiter])
  59. {
  60.     if(kopiec[dlugosc]!=NULL)
  61.     {
  62.         kopiec[1]=kopiec[dlugosc];
  63.         dlugosc--;
  64.         int i=1;
  65.         Node *tmp;
  66.         while(kopiec[i]!=NULL && i<=dlugosc && kopiec[i]->data>kopiec[i+1]->data)
  67.         {
  68.             tmp=kopiec[i];
  69.             kopiec[i]=kopiec[i+1];
  70.             kopiec[i+1]=tmp;
  71.             i++;
  72.         }
  73.     }
  74.    
  75. }
  76.  
  77. void dodajNode(Node *kopiec[iloscLiter], Node *a)
  78. {
  79.     dlugosc++;
  80.     kopiec[dlugosc]=a;
  81.     Node *tmp;
  82.     int index=dlugosc;
  83.    while(kopiec[index]!=NULL && index>1 && kopiec[index]->data<kopiec[index-1]->data)
  84.     {
  85.         tmp=kopiec[index];
  86.         kopiec[index]=kopiec[index-1];
  87.         kopiec[index-1]=tmp;
  88.         index--;
  89.     }
  90.    
  91. }
  92.  
  93. int sum;
  94. void DFS(Node *node)
  95. {
  96.     sum+=node->data;
  97.     if(node->left!=NULL)
  98.     {
  99.         DFS(node->left);
  100.     }
  101.     if(node->right!=NULL)
  102.     {
  103.         DFS(node->right);
  104.     }
  105. }
  106.  
  107. int main(void)
  108. {
  109.    
  110.     char tab[MAX];
  111.     fgets(tab,MAX,stdin);
  112.    
  113.     int i=0;
  114.     while(tab[i]!='\n')  //sprawdzam ile razy dany char wystapil w tekscie
  115.     {
  116.         int tmp=tab[i];
  117.         wystapienia[tmp].first=tab[i];
  118.         wystapienia[tmp].second++;
  119.         i++;
  120.     }
  121.    
  122.     for(int j=0;j<130;j++) //sprawdzam ile unikalnych liter wystapilo
  123.     {
  124.         if(wystapienia[j].second!=0) iloscLiter++;
  125.     }
  126.    
  127.    
  128.     struct pair WystapieniaZnakow[iloscLiter];  //tworze priority queue dla par t.ze <char, ilosc wystapienia>
  129.    
  130.     for(int j=0;j<130;j++) //wrzucam do powyzej utworzonej kolejki moje pary
  131.     {
  132.         if(wystapienia[j].second!=0 && wystapienia[j].first!='\n' && wystapienia[j].first!='\0')
  133.         {
  134.             //printf("dodaje: %c %d\n",wystapienia[j].first,wystapienia[j].second);
  135.             dodaj(WystapieniaZnakow, wystapienia[j]);
  136.         }
  137.     }
  138.  
  139.    
  140.     Node *nodes[iloscLiter];
  141.    
  142.     for(int k=1;k<=iloscLiter;k++) //Tworze nowa kolejke typu Node, zebym mogl tam wrzucac i wyrzucac rzeczy
  143.     {
  144.         nodes[k]=newNode(WystapieniaZnakow[k].second);
  145.     }
  146.     while(dlugosc>1) //dopoki wiecej niz 1 Node jest w kolejce lacze je w pary
  147.     {
  148.         Node*Leftson=nodes[1];
  149.         usunNode(nodes);
  150.         Node*RightSon=nodes[1];
  151.         usunNode(nodes);
  152.         Node *Parent=newNode(Leftson->data+RightSon->data);
  153.         Parent->left=Leftson;
  154.         Parent->right=RightSon;
  155.         dodajNode(nodes, Parent);
  156.     }
  157.     DFS(nodes[1]);
  158.     printf("ilosc: %d\n",iloscLiter);
  159.     if(iloscLiter==1)
  160.      printf("%d\n",sum);
  161.     else printf("%d\n",sum-nodes[1]->data);
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement