forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman decoding in C++
58 lines (56 loc) · 1.33 KB
/
Huffman decoding in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <queue>
using namespace std;
class node
{
public:
char data;
node *left, *right;
node(char c)
{
data = c;
left = right = NULL;
}
};
string decode_huff(node * root, string s) {
string temp="";
struct node*head=root;
for(int i=0;i<s.size();i++)
{
if(s[i]=='1')
{
cout<<"GOing to right"<<endl;
head=head->right;
}
else
{
cout<<"GOing to left"<<endl;
head=head->left;
}
if(head->left==NULL && head->right==NULL)
{
temp+=head->data;
cout<<temp<<endl;
head=root;
}
}
return temp;
}
int main()
{
node *root = new node('&');
root->left = new node('&');
root->left->left = new node('k');
root->left->right = new node('&');
root->left->right->left = new node('o');
root->left->right->right = new node('g');
root->right = new node('&');
root->right->left = new node('e');
root->right->right = new node('&');
root->right->right->right = new node('s');
root->right->right->left = new node('&');
root->right->right->left->left = new node('f');
root->right->right->left->right = new node('r');
cout<<decode_huff(root,"01110100011111000101101011101000111");
return 0;
}