Tree: Huffman Decoding

Sort by

recency

|

80 Discussions

|

  • + 0 comments
    void iter(node * root, std::string key, std::map<std::string, std::string> &dictionary){
    
    if(root == NULL){
        return;
    }
    
    if(root->data != '\0') {
        std::string s{root->data};
        dictionary[key] = s;
    }
    
    iter(root->left, key+"0", dictionary);
    iter(root->right, key+"1", dictionary);
    }
    
    void decode_huff(node * root, string s) {
    std::map<std::string, std::string> dictionary;   
    std::string binary = "";
    
    iter(root, binary, dictionary);
    
    binary="";
    
    for (char c : s){
        std::string str{c};
        binary = binary+str;
    
        if (dictionary.find(binary) != dictionary.end()) {
            std::cout <<dictionary[binary];
            binary="";
        }
    }
    }
    
  • + 0 comments
    void decode(String s, Node root) {
            String word = "";
            Node location = root;
            for(char i: s.toCharArray()){
           if(i == '0'){
            if (location.left.left ==null){
                word+=location.left.data;
                location = root;
            }else{
            location = location.left;
            }
           }
           else if(i=='1'){
            if (location.right.right ==null){
                word+=location.right.data;
                location = root;
            }else{
            location = location.right;
            }
           }
           } 
           System.out.print(word);
    
  • + 0 comments

    Python coders beware: when node.data does not contain a character, it is not an empty string or None. It is '\0'.

    This drove me nuts for 20 min until I read some discussions.

    I feel like this was a poor choice of "empty" string. Why not just set it to None or ""? I guess I learned something new about Python today but it had nothing to do with trees. Anyone disagree?

  • + 0 comments
    def decodeHuff(root, s):
    	#Enter Your Code Here
        s = list(s)
        current = root
        results = []
        
        def traverse(current, s):
            if len(s) > 0:
                if s[0] == "0" and current.left is not None:
                    s.pop(0)
                    traverse(current.left, s)
                elif s[0] == "1" and current.right is not None:
                    s.pop(0)
                    traverse(current.right, s)
            
            if current.left is None and current.right is None:
                results.append(current.data)
    
        while len(s) > 0:
            traverse(current, s)
        
        print("".join(results))
    
  • + 0 comments
    void PrintCode(node* node , string s , int index  , struct node* root)
    {
        if(node == nullptr)
        {
            return;
        }
         index = index+1;
        if(index >s.length())
        {
            return ;
        }
       
    
       // cout<<node->freq;
        //Leaf node reached
        if(node->left == nullptr && node->right == nullptr )
        { 
           cout<<node->data;
            node = root;
        }
        
        //Move left
        if(s[index] == '0')
        { 
            PrintCode(node->left , s, index , root);
    
        }else if(s[index] == '1')
        {
            PrintCode(node->right , s, index , root);
        }
       
    }
    
    
    void PrintLeafNode(node * node)
    {
        
        if(node == nullptr)
        {
            return;
        }
        if(node->left == nullptr && node->right == nullptr)
        {    
            cout<<node->data  << " "<< node->freq << endl;
        }
        PrintLeafNode(node->left);
        PrintLeafNode(node->right);
    }
    void decode_huff(node * root, string s) {
    
        //given is root node // we can traverse root node  based on the value s
       PrintCode(root,s,-1,root);
        
    }