Advertisement
wurdalak007

HUFFMAN 2.0

Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <string>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct list {
  10. int repeat = 0;
  11. char symb = NULL;
  12. string code;
  13. list* left = nullptr;
  14. list* right = nullptr;
  15. };
  16.  
  17.  
  18. bool operator <( list first, list second ){
  19. return first.repeat > second.repeat;
  20. }
  21.  
  22.  
  23.  
  24. void init_arr( array<list, 257>& ASCII ) {
  25. for( int i = 0; i < 257; i++){
  26. ASCII[i].repeat = 0;
  27. ASCII[i].symb =(char)(i+1);
  28. }
  29. }
  30.  
  31. bool read( array<list, 257>& ASCII, string& input ) {
  32. char c = getchar_unlocked();
  33. if( c != EOF ) {
  34. input += c;
  35. ASCII[(int)c - 1].repeat++;
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41.  
  42. void generateCode( list*& root ) {//TO DO
  43. if( root == nullptr ) {
  44. return;
  45. }
  46. if( root->left != nullptr ) {
  47. root->left->code = root->code + '0';
  48. }
  49. if( root->right != nullptr ) {
  50. root->right->code = root->code + '1';
  51. }
  52.  
  53. generateCode(root->left);
  54. generateCode(root->right);
  55. }
  56.  
  57. void translate( list* root, vector<list*>& shedule ) {
  58.  
  59. if( root->right == nullptr && root->left == nullptr ) {
  60. shedule.push_back(root);
  61. return;
  62. } else {
  63. translate(root->left, shedule);
  64. translate(root->right, shedule);
  65. }
  66. }
  67.  
  68. string find_code( vector<list*> shed, char p) {
  69. for( int i = 0; i < shed.size(); i++){
  70. if( shed[i]->symb == p) {
  71. return shed[i]->code;
  72. }
  73. }
  74. return NULL;
  75. }
  76.  
  77. string into_bin ( char p ) {
  78. string binary;
  79. int p_num = (int)p;
  80. int i = 0;
  81.  
  82. while( p_num > 0 ) {
  83. p_num /= 2;
  84. i++;
  85. }
  86. p_num = (int)p;
  87. i--;
  88. for( ; i >= 0; --i) {
  89. if( 0 == ((p_num>>i)&0x1) ) {
  90. binary += '0';
  91. } else {
  92. binary += '1';
  93. }
  94. }
  95.  
  96. return binary;
  97.  
  98. }
  99.  
  100. void crypt_tree( vector<list*>& shedule, string& crypt ) {
  101.  
  102. for( int i = 0; i < shedule.size(); i++ ) {
  103. crypt += into_bin(shedule[i]->symb);
  104. crypt += ' ';
  105. crypt += shedule[i]->code;
  106. crypt += ' ';
  107. }
  108.  
  109. }
  110.  
  111. void Encode( array<list, 257>& ASCII, string& input, string& output, list*& out ) {
  112. vector<list*> shedule;
  113. priority_queue<list> queue;
  114. for( int i = 0; i < 257; i++ ) {
  115. if( ASCII[i].repeat != 0 ) {
  116. queue.push(ASCII[i]);
  117. }
  118. }
  119.  
  120.  
  121.  
  122. while( queue.size() >= 2) {
  123. list* tmp1 = new list(queue.top());
  124. queue.pop();
  125. list* tmp2 = new list( queue.top());
  126. queue.pop();
  127.  
  128. list* newtmp = new list;
  129. newtmp->left = tmp1;
  130. newtmp->right = tmp2;
  131. newtmp->repeat = tmp1->repeat + tmp2->repeat;
  132. queue.push(*(newtmp));
  133. }
  134.  
  135. list* root = new list( queue.top() );
  136. root->code = '1';
  137. root->left->code = "0";
  138. root->right->code = "1";
  139. generateCode(root->right);
  140. generateCode(root->left);
  141. translate( root, shedule );
  142.  
  143. crypt_tree(shedule, output);
  144.  
  145. for( int i = 0; i < input.length(); i++ ){
  146. output += find_code(shedule, input[i]);
  147. }
  148. out = root;
  149.  
  150. }
  151.  
  152. list* decode_tree ( string output, int& position ) {
  153. list* root = new list;
  154. list* tmp = root;
  155. int symb_num = 0;
  156. int j = 0;
  157. int i = 0;
  158.  
  159. while( i < output.length() ) {
  160. for(; output[i] != ' '; i++ ) {
  161. j++;
  162. symb_num = symb_num*2 + (output[i] - '0' )*2;
  163. if( symb_num > 256 ) {
  164. position = i - j + 1;
  165. return root;
  166. }
  167. }
  168. symb_num /= 2;
  169. j = 0;
  170. i++;
  171. while( output[i] != ' ') {
  172. if( output[i] == '1' ) {
  173. if( tmp->right == nullptr) {
  174. list* right = new list;
  175. tmp->right = right;
  176. right->code = tmp->code + '1';
  177. tmp = right;
  178. i++;
  179. } else {
  180. tmp = tmp->right;
  181. i++;
  182. }
  183. } else {
  184. if( tmp->left == nullptr ) {
  185. list* left = new list;
  186. tmp->left = left;
  187. left->code = tmp->code + '0';
  188. tmp = left;
  189. i++;
  190. } else {
  191. tmp = tmp->left;
  192. i++;
  193. }
  194. }
  195. }
  196. tmp->symb = (char)symb_num;
  197. tmp = root;
  198. i++;
  199. symb_num = 0;
  200. }
  201. cout <<" ";
  202. return root;
  203. }
  204.  
  205. void Decode( string& output) {
  206. int position = 0;
  207. list* root = decode_tree( output, position );
  208. list* tmp = new list;
  209. tmp = root;
  210.  
  211. for( int i = position; i < output.length(); i++ ) {
  212. if( output[i] == '0' ) {
  213. if( tmp->left == nullptr ) {
  214. cout << tmp->symb;
  215. tmp = root;
  216. }
  217. tmp = tmp->left;
  218. } else {
  219. if( tmp->right == nullptr) {
  220. cout << tmp->symb;
  221. tmp = root;
  222.  
  223. }
  224. tmp = tmp->right;
  225. }
  226. }
  227. }
  228.  
  229. int main() {
  230. string input;
  231. string output;
  232. array<list, 257> ASCII;
  233. init_arr( ASCII );
  234. list* out = new list;
  235.  
  236. while( read(ASCII, input) ) {
  237. };
  238.  
  239. Encode( ASCII, input, output, out );
  240.  
  241. cout<<output<<endl;
  242.  
  243. Decode(output);
  244.  
  245. return 0;
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement