Huffman Decoding-1 | Practice | GeeksforGeeks

Given a string S, implement Huffman Encoding and Decoding.
Example 1:
Input : abc
Output : abc
Example 2: 
Input : geeksforgeeks
Output : geeksforgeeks
 
Your task: 
You don't need to read input or print anything. Your task is to complete the function decode_file(), which takes root of the tree formed while encoding and the encoded string as the input parameters and returns the decoded string.
 
Constraints:
2<=S<=1000
 
0
himanshujain4574 months ago
can anyone tell me what's wrong with this code??
map<string,char>mp;void solve(struct MinHeapNode* root,string s){   if(!root)   return;   if(root->data!='$')   {       mp[s]=root->data;       return;   }   solve(root->left,s+'0');   solve(root->right,s+'1');}string decode_file(struct MinHeapNode* root, string s){   string ans="";   solve(root,"");   string temp="";   for(int i=0;i<s.length();i++)   {       temp+=s[i];       if(mp.find(temp)!=mp.end())       {           ans+=mp[temp];           temp="";       }   }   return ans;   }
0
singhapoorv824 months ago
Total Time Taken:
0.7/1.9
 
class GfG {   // your required to complete this function   // this function should return the encoded string   // see the main function for the minHeapNode reference   String decodeString(minHeapNode root, String encodedStr){       if(encodedStr.length()==0) return "";       String s = "";       minHeapNode temp = root;       for(int i = 0 ; i<encodedStr.length() ; i++){           if(temp.right==null && temp.left==null){               s = s + temp.data;               temp = root;           }           if(encodedStr.charAt(i)=='0'){               temp = temp.left;           }else{               temp = temp.right;           }       }       s = s+temp.data;       return s;   }}
 
0
singhapoorv82
This comment was deleted.
0
dipeshmishra6265 months ago
write examples correctly..
input string should consists of only binary data. btw here is the java solution :-- 
 
int n=encodedStr.length();       int i=0;       String msg="";       while(i<n)       {           minHeapNode temp=root;           while(temp.left!=null && temp.right!=null)           {               if(encodedStr.charAt(i)=='0')               {                   temp=temp.left;               }               else{                   temp=temp.right;               }               i++;           }           msg+=temp.data;       }              return msg;
0
prajapatmanoj6097 months ago
string decode_file(struct MinHeapNode* root, string s){//add code here.   MinHeapNode *curr = root;   int n = s.size();   string ans;   for (int i = 0; i < n; i++)   {       if (s[i] == '0')       curr = curr->left;       else       curr = curr->right;       if (curr->left == NULL && curr->right == NULL)       {           ans += curr->data;           curr = root;       }          }   return ans;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.