Tree - Huffman Decoding - Lab 7 FODSA Question - Contests - HackerRank
Tree - Huffman Decoding - Lab 7 FODSA Question - Contests - HackerRank
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent
characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to
a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only
the leaves will contain a letter and its frequency count. All other nodes will contain a null instead of a character, and the count of the
frequency of all of it and its descendant characters.
For instance, consider the string ABRACADABRA. There are a total of characters in the string. This number should match the
count in the ultimately determined root of the tree. Our frequencies are and . The two
smallest frequencies are for and , both equal to , so we'll create a tree with them. The root node will contain the sum of the
counts of its descendants, in this case . The left node will be the first character encountered, , and the right will contain
. Next we have items with a character count of : the tree we just created, the character and the character . The tree came
first, so it will go on the left of our new root node. will go on the right. Repeat until the tree is complete, then fill in the 's and 's
for the edges. The finished graph looks like:
Input characters are only present in the leaves. Internal nodes have a character value of ϕ (NULL). We can determine that our values
for characters are:
A - 0
B - 111
C - 1100
D - 1101
R - 10
A B R A C A D A B R A
0 111 10 0 1100 0 1101 0 111 10 0
or
01111001100011010111100
To avoid ambiguity, Huffman encoding is a prefix free encoding technique. No codeword appears as a prefix of any other codeword.
To decode the encoded string, follow the zeros and ones to a leaf and return the character there.
https://www.hackerrank.com/contests/lab-7-fodsa/challenges/tree-huffman-decoding/copy-from/1360617058 1/5
6/30/24, 10:05 PM Tree: Huffman Decoding | Lab 7 FODSA Question | Contests | HackerRank
You are given pointer to the root of the Huffman tree and a binary coded string to decode. You need to print the decoded string.
Function Description
Complete the function decode_huff in the editor below. It must return the decoded string.
There is one line of input containing the plain string, . Background code creates the Huffman tree then passes the head node and
the encoded string to the function.
Constraints
Output Format
Sample Input
s="1001011"
Sample Output
ABACA
Explanation
S="1001011"
Processing the string from left to right.
S[0]='1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the
decoded string.
We move back to the root.
S[3] = '1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the
decoded string.
We move back to the root.
S[6] = '1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the
decoded string.
We move back to the root.
https://www.hackerrank.com/contests/lab-7-fodsa/challenges/tree-huffman-decoding/copy-from/1360617058 2/5
6/30/24, 10:05 PM Tree: Huffman Decoding | Lab 7 FODSA Question | Contests | HackerRank
Submissions: 54
Max Score: 100
C++ ⚙
1 //
2 // main.cpp
3 // Huffman
4 //
5 // Created by Vatsal Chanana
6
7 ▾#include<bits/stdc++.h>
8 using namespace std;
9
10 ▾typedef struct node {
11 int freq;
12 char data;
13 node * left;
14 node * right;
15 } node;
16
17 ▾struct deref:public binary_function<node*, node*, bool> {
18 ▾ bool operator()(const node * a, const node * b)const {
19 return a->freq > b->freq;
20 }
21 };
22
23 typedef priority_queue<node *, vector<node*>, deref> spq;
24
25 ▾node * huffman_hidden(string s) {
26
27 spq pq;
28 vector<int>count(256,0);
29
30 ▾ for(int i = 0; i < s.length(); i++ ) {
31 ▾ count[s[i]]++;
32 }
33
34 ▾ for(int i=0; i < 256; i++) {
35
36 node * n_node = new node;
37 n_node->left = NULL;
38 n_node->right = NULL;
39 n_node->data = (char)i;
40 ▾ n_node->freq = count[i];
41
42 ▾ if( count[i] != 0 )
43 pq.push(n_node);
44
45 }
46
47 ▾ while( pq.size() != 1 ) {
48
49 node * left = pq.top();
50 pq.pop();
51 node * right = pq.top();
52 pq.pop();
53 node * comb = new node;
54 comb->freq = left->freq + right->freq;
55 comb->data = '\0';
56 comb->left = left;
57 comb->right = right;
https://www.hackerrank.com/contests/lab-7-fodsa/challenges/tree-huffman-decoding/copy-from/1360617058 3/5
6/30/24, 10:05 PM Tree: Huffman Decoding | Lab 7 FODSA Question | Contests | HackerRank
58 pq.push(comb);
59
60 }
61
62 return pq.top();
63
64 }
65
66 ▾void print_codes_hidden(node * root, string code, map<char, string>&mp) {
67
68 if(root == NULL)
69 return;
70
71 ▾ if(root->data != '\0') {
72 ▾ mp[root->data] = code;
73 }
74
75 print_codes_hidden(root->left, code+'0', mp);
76 print_codes_hidden(root->right, code+'1', mp);
77
78 }
79 ▾/*
80 The structure of the node is
81
82 typedef struct node {
83
84 int freq;
85 char data;
86 node * left;
87 node * right;
88
89 } node;
90
91 */
92
93
94 ▾void decode_huff(node * root, string s) {
95
96 node *treeroot = root;
97 // char *result = ( char*)malloc(sizeof(char)*len);
98 int i=0,k=0;
99 ▾ while ( s[i]!='\0'){
100 // printf(" s[%d]%c\n", i, s[i]);
101
102 ▾ if (s[i]== '1'){
103
104 root=root->right;
105 // printf(" i was here");
106 ▾ if (root->data != '\0'){
107 // printf(" i was here");
108 printf("%c", root->data);
109 root = treeroot;
110 }
111 i++;
112 }
113 ▾ else{
114
115 root=root->left;
116 ▾ if ( root->data!='\0'){
117 printf("%c", root->data);
118 root = treeroot;
119
120 }
121 i++;
122 }
123 }
124 }
Line: 48 Col: 1
https://www.hackerrank.com/contests/lab-7-fodsa/challenges/tree-huffman-decoding/copy-from/1360617058 4/5
6/30/24, 10:05 PM Tree: Huffman Decoding | Lab 7 FODSA Question | Contests | HackerRank
Upload Code as File Test against custom input Run Code Submit Code
Interview Prep | Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy |
https://www.hackerrank.com/contests/lab-7-fodsa/challenges/tree-huffman-decoding/copy-from/1360617058 5/5