Skip to content

Commit 6cba089

Browse files
feat: implemented file compression via huffman encoding
1 parent 6998b97 commit 6cba089

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

Src/FileCompression/input.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
aaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
2+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
3+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
4+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
5+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
6+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
7+
aaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
8+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
9+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
10+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
11+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
12+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
13+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
14+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
15+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
16+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
17+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
18+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
19+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
20+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
21+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
22+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
23+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
24+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
25+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
26+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
27+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
28+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
29+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
30+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
31+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
32+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
33+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
34+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
35+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
36+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
37+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
38+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
39+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
40+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
41+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
42+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
43+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
44+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
45+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
46+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
47+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
48+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
49+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
50+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
51+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
52+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
53+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
54+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
55+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
56+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
57+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
58+
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
59+
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
60+
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
61+
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
62+
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnajjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldna

Src/FileCompression/main.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
char ch;
6+
int freq;
7+
Node* left;
8+
Node* right;
9+
10+
Node(char c, int f, Node* l = nullptr, Node* r = nullptr)
11+
: ch(c), freq(f), left(l), right(r) {}
12+
};
13+
struct Compare {
14+
bool operator()(Node* a, Node* b) {
15+
return a->freq > b->freq;
16+
}
17+
};
18+
unordered_map<char,string> encodings;
19+
20+
void prefix(Node* root,string s){
21+
if(root->ch!='*'){
22+
encodings[root->ch]=s;
23+
return;
24+
}
25+
if(root->left !=NULL)prefix(root->left,s+'0');
26+
if(root->right !=NULL)prefix(root->right, s+'1');
27+
}
28+
29+
int main(){
30+
ifstream file("input.txt");
31+
if(!file.is_open()){
32+
cout<<"file could not be opened"<<endl;
33+
return 1;
34+
}
35+
unordered_map<char,int> hashmap;
36+
char ch;
37+
while(file.get(ch)){
38+
hashmap[ch]++;
39+
}
40+
file.close();
41+
priority_queue<Node*, vector<Node*>, Compare> pq;
42+
string orignal="";
43+
44+
45+
for(auto it=hashmap.begin();it!=hashmap.end();it++){
46+
char c=it->first;
47+
orignal+=c;
48+
int f=it->second;
49+
cout<<c<<" : "<<f<<endl;
50+
51+
Node* a = new Node(c, f, NULL, NULL);
52+
pq.push(a);
53+
}
54+
55+
while (pq.size() > 1) {
56+
Node* left = pq.top(); pq.pop();
57+
Node* right = pq.top(); pq.pop();
58+
59+
Node* parent = new Node('*', left->freq + right->freq, left, right);
60+
pq.push(parent);
61+
}
62+
Node* root=pq.top();
63+
prefix(root,"");
64+
65+
for(auto it=encodings.begin();it!=encodings.end();it++){
66+
cout<< it->first<< " : "<<it->second<<endl;
67+
}
68+
69+
string bitstream="";
70+
for(char c:orignal){
71+
bitstream+=encodings[c];
72+
}
73+
vector<unsigned char> bytes;
74+
unsigned char current=0;
75+
int count=0;
76+
77+
for(char bit: bitstream){
78+
current=current>>1|bit-'0';
79+
count++;
80+
81+
if(count==8){
82+
bytes.push_back(current);
83+
current=0;
84+
count=0;
85+
}
86+
}
87+
if(count > 0) {
88+
current <<= (8 - count);
89+
bytes.push_back(current);
90+
}
91+
92+
ofstream out("output.bin",ios::binary);
93+
for(unsigned char b:bytes){
94+
out.write((char*)&b,1);
95+
96+
}
97+
out.close();
98+
99+
100+
}

Src/FileCompression/main.exe

272 KB
Binary file not shown.

Src/FileCompression/output.bin

20 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)