Huffman Decoding | Practice | GeeksforGeeks

Given a encoded binary string and a Huffman MinHeap tree, your task is to complete the function decodeHuffmanData(), which decodes the binary encoded string and return the original string. 
Note: Each node of the min heap contains 2 data members, a character and an integer to denote its frequency. The character '$' is the special character used for internal nodes whose min heap node only need a integer field.

Example 1:
Input :
binaryString = 
0000000000001100101010101011111111010101010
Min Heap Tree =  
                $(20)
              /      \
            /          \
         $(8)            \
       /     \             \
    $(3)      \            $(12)
    /  \       \           /    \
B(1)    D(2)    E(5)    C(6)    A(6)

Output: AAAAAABCCCCCCDDEEEEE

Explanation:
The following chart can be made from the 
given min heap tree.
character    frequency    code
    A             6        00                 
    B             1        110
    C             6        01
    D             2        111    
    E             5        10

Example 2:
Input :
binaryString =
01110100011111000101101011101000111
Min Heap Tree =  
                         $(13)
                      /        \
                    /            \
                  /                \
               $(5)                  \
             /      \                  \
            /        \                   \
         $(3)         \                  $(8)
        /    \         \                /    \
     $(2)     \         \            $(4)     \
    /   \      \         \          /   \      \
f(1)    o(1)    r(1)    g(2)    k(2)    s(2)    e(4)

Output: geeksforgeeks

Explanation:
The following chart can be made from the 
given min heap tree.
character    frequency    code
    f             1        0000                 
    o             1        0001
    r             1        001
    g             2        01    
    k             2        100
    s             2        101
    e             4        11

Your Task:  
You dont need to read input or print anything. Complete the function decodeHuffmanData() which takes the root of the Huffman min heap tree and the encoded Binary String as input parameters and returns the decoded string.

Expected Time Complexity: O(N log N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 10^3
0
pritamtech19983 weeks ago
string decodeHuffmanData(struct MinHeapNode* root, string binaryString){   string s = "";   MinHeapNode *t = root;   int i=0;   while(i < binaryString.size()+1){       if(t->data == '$'){           if(binaryString[i] == '0')               t = t->left;           else               t = t->right;           i++;       }else{           s += t->data;           t = root;       }   }   return s;}
+2
vinamrajha2 months ago
string decodeHuffmanData(struct MinHeapNode* root, string binaryString)
{
   // Code here
   string res ="";
   MinHeapNode *curr= root;
   for(int i =0; i<binaryString.length(); ++i){
       if(binaryString[i]=='0') curr = curr->left;
       else curr =curr->right;
       if(curr->data!='$'){
           res+=curr->data;
           curr = root;
       }
   }
   return res;
}
0
siddhant072 months ago
//Using unordered map and preOrderTransversal of binary tree, O(N) solution
void preOrderTransversal(MinHeapNode* root, unordered_map<string, char> &mp, string curr){
    if(root == NULL){
        return;
    }
    if(root->data != '$' && root->right == NULL && root->left == NULL){
        mp[curr] = root->data;
        return; 
    }
    preOrderTransversal(root->left, mp, curr + '0');
    preOrderTransversal(root->right, mp, curr + '1');
    return;
}

string decodeHuffmanData(struct MinHeapNode* root, string binaryString)
{
    unordered_map<string, char> mp;
    preOrderTransversal(root, mp, "");
    string curr = "";
    string ans = "";
    for(int i = 0; i < binaryString.size(); i++){
        curr += binaryString[i];
        if(mp.count(curr)){
            ans += mp[curr];
            curr = "";
        }
    }
    return ans;
}
 
+1
devkantgorai13 months ago
Total Time Taken:
1.2/2.7
 
class Decode{   //Function to return the decoded string.   static String decodeHuffmanData(MinHeapNode root, String binaryString)   {       // I am really proud to solve this question with 100% accuracy- ALways believed in MYSELF.       MinHeapNode curr = root;       String finalStr = "";       for(int i =0;i<binaryString.length();i++){           if(binaryString.charAt(i) == '1')               curr = curr.right;           else if(binaryString.charAt(i) == '0')               curr = curr.left;                      if(curr.right == null && curr.left == null){               finalStr+=curr.data;               curr = root;           }       }       return finalStr;   }}
0
lindan1233 months ago
string decodeHuffmanData(struct MinHeapNode* root, string bs)
{
    if(bs.length()==0)
    {
        return "";
    }
    
    string s="";
    MinHeapNode* temp = root;
    for(int i=0;i<bs.length();i++)
    {
        if(temp->left==NULL and temp->right==NULL)
        {
            s = s+temp->data;
            temp = root;
        }
        if(bs[i]=='0')
        {
            temp = temp->left;
        }
        
        else
        {
            temp = temp->right;
        }
    }
    s = s+temp->data;
    
    return s;
}
Time Taken : 0.0
Cpp
0
himanshujain457
This comment was deleted.
0
codertheta13 months ago
    static String decodeHuffmanData(MinHeapNode root, String binaryString)
    {  
        int i = 0;
        String decoded = "";
        while(i < binaryString.length()){
            MinHeapNode curr = root;
            
            while(curr.left != null && curr.right != null){
                if(binaryString.charAt(i++) == '0')
                curr = curr.left;
                else
                curr = curr.right;
            }
            decoded += String.valueOf(curr.data);
        }
        return decoded;
    }
0
venkatreddysuddamalli4 months ago
def decodeHuffmanData(root, binaryString):      temp=root                        #create temporary pointer   result=[]      for char in binaryString:       if char is '0':                   #traverse to left           temp=temp.left       else:           temp=temp.right       #traverse to right               if temp.left is None and temp.right is None:     #check for leaf node              result.append(temp.data)           temp=root                                                 return "".join(result)
-1
nirajshinde6194 months ago
Python 12 line code
def decodeHuffmanData(root, binaryString):      temp=root                        #create temporary pointer   result=[]      for char in binaryString:       if char is '0':                   #traverse to left           temp=temp.left       else:           temp=temp.right       #traverse to right               if temp.left is None and temp.right is None:     #check for leaf node              result.append(temp.data)           temp=root                                                 return "".join(result) 
0
shubhamkumar22734 months ago
string decodeHuffmanData(struct MinHeapNode* root, string binaryString)
{
         MinHeapNode*temp=root;
          string A="";
          
          for(int i=0;i<binaryString.length();i++){
              if(binaryString[i]=='0'){
                  temp=temp->left;
              }else if(binaryString[i]=='1'){
                  temp=temp->right;
              }
              if(temp->data=='$')
              continue;
              else{
                 A=A+temp->data;
                 temp=root;
                 
              }
              
              
              
          }
          return A;
}
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.