|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <unordered_map> |
| 4 | +#include <string> |
| 5 | +#include <queue> |
| 6 | +using namespace std; |
| 7 | +struct Node{ |
| 8 | + char symbol; |
| 9 | + unsigned freq; |
| 10 | + Node *left; |
| 11 | + Node *right; |
| 12 | + |
| 13 | + Node(char symbol, unsigned freq) : symbol(symbol), freq(freq), left(nullptr), right(nullptr) {} |
| 14 | +}; |
| 15 | + |
| 16 | +struct comp{ |
| 17 | + bool operator()(Node *l, Node *r){ |
| 18 | + return l->freq > r->freq; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +void printCodes(Node *root, string str, unordered_map<char, string> &huffmanCodes){ |
| 23 | + if (!root){ |
| 24 | + return; |
| 25 | + } |
| 26 | + printCodes(root->left, str + "0", huffmanCodes); |
| 27 | + printCodes(root->right, str + "1", huffmanCodes); |
| 28 | + if (!root->left && !root->right){ |
| 29 | + huffmanCodes[root->symbol] = str; |
| 30 | + } |
| 31 | +} |
| 32 | +unordered_map<char, string> generateHuffmanCodes(istream &input) { |
| 33 | + unordered_map<char, unsigned> freq; |
| 34 | + string line; |
| 35 | + while (getline(input, line)) { |
| 36 | + for (char c : line) { |
| 37 | + if (isalpha(c) || isspace(c)) { |
| 38 | + freq[toupper(c)]++; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + priority_queue<Node *, vector<Node *>, comp> pq; |
| 44 | + for (const auto &pair : freq) { |
| 45 | + pq.push(new Node(pair.first, pair.second)); |
| 46 | + } |
| 47 | + while (pq.size() > 1) { |
| 48 | + Node *left = pq.top(); |
| 49 | + pq.pop(); |
| 50 | + Node *right = pq.top(); |
| 51 | + pq.pop(); |
| 52 | + |
| 53 | + Node *mergedNode = new Node('$', left->freq + right->freq); |
| 54 | + mergedNode->left = left; |
| 55 | + mergedNode->right = right; |
| 56 | + |
| 57 | + pq.push(mergedNode); |
| 58 | + } |
| 59 | + Node *root = pq.top(); |
| 60 | + unordered_map<char, string> huffmanCodes; |
| 61 | + queue<pair<Node *, string>> q; |
| 62 | + q.push({root, ""}); |
| 63 | + while (!q.empty()) { |
| 64 | + Node *current = q.front().first; |
| 65 | + string currentCode = q.front().second; |
| 66 | + q.pop(); |
| 67 | + if (current->left) { |
| 68 | + q.push({current->left, currentCode + "0"}); |
| 69 | + } |
| 70 | + if (current->right) { |
| 71 | + q.push({current->right, currentCode + "1"}); |
| 72 | + } |
| 73 | + if (!current->left && !current->right) { |
| 74 | + huffmanCodes[current->symbol] = currentCode; |
| 75 | + } |
| 76 | + } |
| 77 | + delete root; |
| 78 | + return huffmanCodes; |
| 79 | +} |
| 80 | +int main(){ |
| 81 | + string file; |
| 82 | + cout<<"Enter file name: ";//Your input text file goes here |
| 83 | + getline(cin, file); |
| 84 | + ifstream input (file); |
| 85 | + string ofile; |
| 86 | + cout<<"Enter your output file name, where your codes will be saved: ";//Your output text file goes here |
| 87 | + getline(cin, ofile); |
| 88 | + ofstream outputFile(ofile); |
| 89 | + unordered_map<char, string> huffmanCodes = generateHuffmanCodes(input); |
| 90 | + for (const auto &pair : huffmanCodes) { |
| 91 | + cout << pair.first << " " << pair.second << "\n"; |
| 92 | + outputFile << pair.first << " " << pair.second << "\n"; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
0 commit comments