Advertisement
Guest User

code

a guest
May 24th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int freq[200];
  5. struct node
  6. {
  7. int fr;
  8. char c;
  9. char str[50]={NULL};
  10. struct node *lchild, *rchild;
  11. };
  12. int count=0;
  13. struct node *a[2700];
  14. int ll=0;
  15. char huff[27][1000];
  16. int min()
  17. {
  18. int b,d;
  19. b=9999999;
  20. for(int i=0;i<count;i++)
  21. {
  22. if(a[i]!=NULL&&a[i]->fr>0)
  23. {
  24. if(b>a[i]->fr)
  25. {
  26. b=a[i]->fr;
  27. d=i;
  28. }
  29. }
  30. }
  31. return d;
  32. }
  33. void huffman()
  34. {
  35. int p;
  36. struct node *x,*y,*temp;
  37. while(ll>1)
  38. {
  39. p=min();
  40. x=a[p];
  41. a[p]=NULL;
  42. p=min();
  43. y=a[p];
  44. a[p]=NULL;
  45. temp=(struct node*)malloc(sizeof(struct node));
  46. temp->lchild=x;
  47. temp->rchild=y;
  48. temp->fr=x->fr+y->fr;
  49. a[count++]=temp;
  50. ll--;
  51.  
  52. }
  53. }
  54. void recur(struct node *root,char stemp[50])
  55. {
  56.  
  57. strcpy(root->str,stemp);
  58. char a[50];
  59. strcpy(a,stemp);
  60. if(root->lchild==NULL&&root->rchild==NULL)
  61. {
  62. printf("%c %s\n",root->c,root->str);
  63. return;
  64. }
  65. strcat(a,"0");
  66. recur(root->lchild,a);
  67. strcpy(a,stemp);
  68. strcat(a,"1");
  69. recur(root->rchild,a);
  70.  
  71.  
  72. }
  73. void searc(struct node *root, char x )
  74. {
  75.  
  76. if(root->c==x)
  77. {
  78. printf("%s",root->str);
  79. return;
  80. }
  81. else if(root->lchild==NULL&&root->rchild==NULL)
  82. return;
  83. searc(root->lchild,x);
  84. searc(root->rchild,x);
  85.  
  86.  
  87. }
  88. int main()
  89.  
  90. {
  91. char ch;
  92. freopen("input.txt","r",stdin);
  93. freopen("out.txt","w",stdout);
  94. int total=0;
  95. while(scanf("%c", &ch)==1)
  96. {
  97. if(ch>='A'&&ch<='Z')
  98. freq[ch+32]++;
  99. else
  100. freq[ch]++;
  101. total++;
  102. }
  103. struct node *now;
  104. now=(struct node*)malloc(sizeof(struct node));
  105. now->c=' ';
  106. now->fr=freq[' '];
  107. now->lchild=NULL;
  108. now->rchild=NULL;
  109. a[count++]=now;
  110. printf(" : %d\n",freq[' ']);
  111. for(int j='a';j<='z';j++)
  112. {
  113. now=(struct node*)malloc(sizeof(struct node));
  114. now->c=j;
  115. now->fr=freq[j];
  116. now->lchild=NULL;
  117. now->rchild=NULL;
  118. a[count++]=now;
  119. printf("%c : %d\n",j,freq[j]);
  120. }
  121.  
  122.  
  123.  
  124. for(int k=0;k<count;k++)
  125. {
  126. if(a[k]!=NULL&&a[k]->fr>0)
  127. {
  128. ll++;
  129. }
  130. }
  131. huffman();
  132. char st[10]={NULL};
  133. recur(a[count-1],st);
  134. printf("%d %d\n",total,a[count-1]->fr);
  135. printf("huffman string:\n");
  136. freopen("input.txt","r",stdin);
  137. while(scanf("%c",&ch)==1)
  138. {
  139. searc(a[count-1],ch);
  140. }
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement