Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. // A Tree node
  8. struct Node
  9. {
  10. char ch;
  11. int freq;
  12. Node *left, *right;
  13. };
  14.  
  15. // Function to allocate a new tree node
  16. Node* getNode(char ch, int freq, Node* left, Node* right)
  17. {
  18. Node* node = new Node();
  19.  
  20. node->ch = ch;
  21. node->freq = freq;
  22. node->left = left;
  23. node->right = right;
  24.  
  25. return node;
  26. }
  27.  
  28. // Comparison object to be used to order the heap
  29. struct comp
  30. {
  31. bool operator()(Node* l, Node* r)
  32. {
  33. // highest priority item has lowest frequency
  34. return l->freq > r->freq;
  35. }
  36. };
  37.  
  38. // traverse the Huffman Tree and store Huffman Codes
  39. // in a map.
  40. void encode(Node* root, string str,
  41. unordered_map<char, string> &huffmanCode)
  42. {
  43. if (root == nullptr)
  44. return;
  45.  
  46. // found a leaf node
  47. if (!root->left && !root->right) {
  48. huffmanCode[root->ch] = str;
  49. }
  50.  
  51. encode(root->left, str + "0", huffmanCode);
  52. encode(root->right, str + "1", huffmanCode);
  53. }
  54.  
  55. // traverse the Huffman Tree and decode the encoded string
  56. void decode(Node* root, int &index, string str)
  57. {
  58. if (root == nullptr) {
  59. return;
  60. }
  61.  
  62. // found a leaf node
  63. if (!root->left && !root->right)
  64. {
  65. cout << root->ch;
  66. return;
  67. }
  68.  
  69. index++;
  70.  
  71. if (str[index] =='0')
  72. decode(root->left, index, str);
  73. else
  74. decode(root->right, index, str);
  75. }
  76.  
  77. // Builds Huffman Tree and decode given input text
  78. void buildHuffmanTree(string text)
  79. {
  80. // count frequency of appearance of each character
  81. // and store it in a map
  82. unordered_map<char, int> freq;
  83. for (char ch: text) {
  84. freq[ch]++;
  85. }
  86.  
  87. // Create a priority queue to store live nodes of
  88. // Huffman tree;
  89. priority_queue<Node*, vector<Node*>, comp> pq;
  90.  
  91. // Create a leaf node for each character and add it
  92. // to the priority queue.
  93. for (auto pair: freq) {
  94. pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
  95. }
  96.  
  97. // do till there is more than one node in the queue
  98. while (pq.size() != 1)
  99. {
  100. // Remove the two nodes of highest priority
  101. // (lowest frequency) from the queue
  102. Node *left = pq.top(); pq.pop();
  103. Node *right = pq.top(); pq.pop();
  104.  
  105. // Create a new internal node with these two nodes
  106. // as children and with frequency equal to the sum
  107. // of the two nodes' frequencies. Add the new node
  108. // to the priority queue.
  109. int sum = left->freq + right->freq;
  110. pq.push(getNode('\0', sum, left, right));
  111. }
  112.  
  113. // root stores pointer to root of Huffman Tree
  114. Node* root = pq.top();
  115.  
  116. // traverse the Huffman Tree and store Huffman Codes
  117. // in a map. Also prints them
  118. unordered_map<char, string> huffmanCode;
  119. encode(root, "", huffmanCode);
  120.  
  121. cout << "Huffman Codes are :\n" << '\n';
  122. for (auto pair: huffmanCode) {
  123. cout << pair.first << " " << pair.second << '\n';
  124. }
  125.  
  126. cout << "\nOriginal string was :\n" << text << '\n';
  127.  
  128. // print encoded string
  129. string str = "";
  130. for (char ch: text) {
  131. str += huffmanCode[ch];
  132. }
  133.  
  134. cout << "\nEncoded string is :\n" << str << '\n';
  135.  
  136. // traverse the Huffman Tree again and this time
  137. // decode the encoded string
  138. int index = -1;
  139. cout << "\nDecoded string is: \n";
  140. while (index < (int)str.size() - 2) {
  141. decode(root, index, str);
  142. }
  143. }
  144.  
  145. // Huffman coding algorithm
  146. int main()
  147. {
  148. string text = "Huffman coding is a data compression algorithm.";
  149.  
  150. buildHuffmanTree(text);
  151.  
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement