Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Src/FileCompression/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
aaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
aaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnaaaabbcddddeeeeffffggghhhiiijjklllmmnoopqqrrrssstttuuuvwwwxxyyzzz
thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog
aaabbbbcccccdddddeeeeeffffffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuu
huffmanencodingexampletextwithrandomlettersandvaryingfrequencies
aaaaaaaabbbbccccccdddddddeeeeeeeeeefffffffggghhhiiijkllllmmnnopprrrsstttt
fnaidfaoisdfhaoidfnasfnakdbfkasjdbjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldnajjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjasbdjasbdoabnsldna
100 changes: 100 additions & 0 deletions Src/FileCompression/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include<bits/stdc++.h>
using namespace std;

struct Node {
char ch;
int freq;
Node* left;
Node* right;

Node(char c, int f, Node* l = nullptr, Node* r = nullptr)
: ch(c), freq(f), left(l), right(r) {}
};
struct Compare {
bool operator()(Node* a, Node* b) {
return a->freq > b->freq;
}
};
unordered_map<char,string> encodings;

void prefix(Node* root,string s){
if(root->ch!='*'){
encodings[root->ch]=s;
return;
}
if(root->left !=NULL)prefix(root->left,s+'0');
if(root->right !=NULL)prefix(root->right, s+'1');
}

int main(){
ifstream file("input.txt");
if(!file.is_open()){
cout<<"file could not be opened"<<endl;
return 1;
}
unordered_map<char,int> hashmap;
char ch;
while(file.get(ch)){
hashmap[ch]++;
}
file.close();
priority_queue<Node*, vector<Node*>, Compare> pq;
string orignal="";


for(auto it=hashmap.begin();it!=hashmap.end();it++){
char c=it->first;
orignal+=c;
int f=it->second;
cout<<c<<" : "<<f<<endl;

Node* a = new Node(c, f, NULL, NULL);
pq.push(a);
}

while (pq.size() > 1) {
Node* left = pq.top(); pq.pop();
Node* right = pq.top(); pq.pop();

Node* parent = new Node('*', left->freq + right->freq, left, right);
pq.push(parent);
}
Node* root=pq.top();
prefix(root,"");

for(auto it=encodings.begin();it!=encodings.end();it++){
cout<< it->first<< " : "<<it->second<<endl;
}

string bitstream="";
for(char c:orignal){
bitstream+=encodings[c];
}
vector<unsigned char> bytes;
unsigned char current=0;
int count=0;

for(char bit: bitstream){
current=current>>1|bit-'0';
count++;

if(count==8){
bytes.push_back(current);
current=0;
count=0;
}
}
if(count > 0) {
current <<= (8 - count);
bytes.push_back(current);
}

ofstream out("output.bin",ios::binary);
for(unsigned char b:bytes){
out.write((char*)&b,1);

}
out.close();


}
Binary file added Src/FileCompression/main.exe
Binary file not shown.
Binary file added Src/FileCompression/output.bin
Binary file not shown.
Loading