From 8e119dad6d8210683068cfe11c5f6953a666e3cc Mon Sep 17 00:00:00 2001 From: Nitinpal07 <32491448+Nitinpal07@users.noreply.github.com> Date: Mon, 2 Nov 2020 00:46:56 +0530 Subject: [PATCH 1/2] Create HuffmanEncoding.cpp C++ implementation of HuffmanEncoding --- HuffmanEncoding.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 HuffmanEncoding.cpp diff --git a/HuffmanEncoding.cpp b/HuffmanEncoding.cpp new file mode 100644 index 0000000..82378fb --- /dev/null +++ b/HuffmanEncoding.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +class Node{ + public: + char c; + int val; + Node* left; + Node* right; + Node(char c,int val){ + this->c = c; + this->val = val; + left=right=NULL; + } +}; +class mycomp +{ public: + bool operator() (Node* a, Node* b) + { + return a->val>b->val; + } +}; + +Node* huffmancode(vector> a){ + + priority_queue,mycomp> q; + for(int i=0;i1){ + Node* top1 = q.top(); + q.pop(); + Node* top2 = q.top(); + q.pop(); + Node* newnode = new Node('#',top1->val+top2->val); + newnode->left = top1; + newnode->right = top2; + q.push(newnode); + } + Node* root = q.top(); + q.pop(); + return root; +} + +void encode(Node* root,string s,map &m){ + if(root==NULL){ + return; + } + if(root->left==NULL && root->right==NULL){ + m[root->c] = s; + } + encode(root->left,s+"0",m); + encode(root->right,s+"1",m); +} +int main(){ + int n; + cin>>n; + vector> a(n); + for(int i=0;i>x>>y; + a[i] = {x,y}; + } + Node* root = huffmancode(a); + map m; + string s = ""; + encode(root,s,m); + for(auto i:m){ + cout<"< Date: Mon, 2 Nov 2020 01:11:41 +0530 Subject: [PATCH 2/2] Create HuffmanEncoding_Decoding.cpp --- HuffmanEncoding_Decoding.cpp | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 HuffmanEncoding_Decoding.cpp diff --git a/HuffmanEncoding_Decoding.cpp b/HuffmanEncoding_Decoding.cpp new file mode 100644 index 0000000..ead9d78 --- /dev/null +++ b/HuffmanEncoding_Decoding.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; +class Node{ + public: + char c; + int val; + Node* left; + Node* right; + Node(char c,int val){ + this->c = c; + this->val = val; + left=right=NULL; + } +}; +class mycomp +{ public: + bool operator() (Node* a, Node* b) + { + return a->val>b->val; + } +}; + +Node* huffmancode(vector> a){ + + priority_queue,mycomp> q; + for(int i=0;i1){ + Node* top1 = q.top(); + q.pop(); + Node* top2 = q.top(); + q.pop(); + Node* newnode = new Node('#',top1->val+top2->val); + newnode->left = top1; + newnode->right = top2; + q.push(newnode); + } + Node* root = q.top(); + q.pop(); + return root; +} + +void encode(Node* root,string s,map &m){ + if(root==NULL){ + return; + } + if(root->left==NULL && root->right==NULL){ + m[root->c] = s; + } + encode(root->left,s+"0",m); + encode(root->right,s+"1",m); +} +int main(){ + string s; + cin>>s; + + int n = s.size(); + map m1; + for(int i=0;i> a; + for(auto i:m1){ + a.push_back({i.first,i.second}); + } + Node* root = huffmancode(a); + map m; + string t = ""; + encode(root,t,m); + map decode; + for(auto i:m){ + decode[i.second] = i.first; + cout<"<>temp; + string run = ""; + string ans=""; + for(int i=0;i