Advertisement
klebermo

huffman_main_cpp

May 28th, 2022
1,023
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. #include <list>
  8. using namespace std;
  9.  
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. #include <string>
  14. using namespace std;
  15.  
  16. class HuffmanNode {
  17. public:
  18.   char data = 0;
  19.   int frequency = 0;
  20.   HuffmanNode * left = nullptr;
  21.   HuffmanNode * right = nullptr;
  22.  
  23.   bool operator==(HuffmanNode other) { return this->data == other.data; }
  24.   bool operator==(char data) { return this->data == data; }
  25.   bool operator!=(HuffmanNode other) { return this->data != other.data; }
  26.   bool operator!=(char data) { return this->data != data; }
  27.  
  28.   bool operator<(HuffmanNode other) { return frequency < other.frequency; }
  29.   bool operator<=(HuffmanNode other) { return frequency <= other.frequency; }
  30.   bool operator>(HuffmanNode other) { return frequency > other.frequency; }
  31.   bool operator>=(HuffmanNode other) { return frequency >= other.frequency; }
  32.  
  33.   HuffmanNode operator++() { struct HuffmanNode temp; temp.frequency = frequency++; return temp; }
  34.   HuffmanNode operator++(int) { struct HuffmanNode temp; temp.frequency = ++frequency; return temp; }
  35.   HuffmanNode operator--() { struct HuffmanNode temp; temp.frequency = frequency--; return temp; }
  36.   HuffmanNode operator--(int) { struct HuffmanNode temp; temp.frequency = --frequency; return temp; }
  37.  
  38.   friend std::ostream& operator<<(std::ostream& output, const HuffmanNode& e) { output << "'" << e.data << "'(" << e.frequency << ")"; return output; };
  39. };
  40.  
  41. void inOrder(HuffmanNode * node) {
  42.   inOrder(node->left);
  43.   cout << *node;
  44.   inOrder(node->right);
  45. }
  46.  
  47. void encode(string filename) {
  48.   list<HuffmanNode> priorityQueue;
  49.  
  50.   fstream input;
  51.   input.open(filename, ios_base::in);
  52.  
  53.   if (input.is_open()) {
  54.     char c;
  55.     while (!input.eof()) {
  56.       input.get(c);
  57.  
  58.       HuffmanNode node;
  59.       node.data = c;
  60.       node.frequency = 1;
  61.  
  62.       HuffmanNode aux = *find(priorityQueue.begin(), priorityQueue.end(), node);
  63.       if(aux != *priorityQueue.end()) {
  64.         priorityQueue.remove(aux);
  65.         aux++;
  66.         priorityQueue.push_back(aux);
  67.       } else {
  68.         priorityQueue.push_back(node);
  69.       }
  70.     }
  71.   }
  72.  
  73.   input.close();
  74.  
  75.   priorityQueue.sort();
  76.  
  77.   while(priorityQueue.size() > 1) {
  78.     HuffmanNode left = priorityQueue.front();
  79.     priorityQueue.pop_front();
  80.  
  81.     HuffmanNode right = priorityQueue.front();
  82.     priorityQueue.pop_front();
  83.  
  84.     HuffmanNode z;
  85.     z.data = 0;
  86.     z.frequency = left.frequency + right.frequency;
  87.     z.left = &left;
  88.     z.right = &right;
  89.     priorityQueue.push_back(z);
  90.  
  91.     cout << "size: " << priorityQueue.size() << " -> " << z << ", left: " << left << ", right: " << right << endl;
  92.   }
  93.  
  94.   HuffmanNode root = *priorityQueue.begin();
  95.   cout << "{ ";
  96.   inOrder(&root);
  97.   cout << "}" << endl;
  98. }
  99.  
  100. void decode(string filename) {
  101.   //
  102. }
  103.  
  104. int main(int argc, char ** argv) {
  105.   if (argc < 3) {
  106.     cout << "Usage: huffman (encode|decode) <file_name>" << endl;
  107.     return 0;
  108.   }
  109.  
  110.   string action(argv[1]), file_name(argv[2]);
  111.  
  112.   if(action == "encode") encode(file_name);
  113.  
  114.   if(action == "decode") decode(file_name);
  115.  
  116.   return 0;
  117. }
  118.  
Advertisement
RAW Paste Data Copied
Advertisement