Tree: Huffman Decoding

Sort by

recency

|

537 Discussions

|

  • + 0 comments

    Simple Java Solutions :

    void decode(String s, Node root) {
            String str = "";
            Node head = root;
            for(int i = 0;i<s.length();i++) {
                if(s.charAt(i)=='0') {
                    head = head.left;
                }
                if(s.charAt(i)=='1') {
                    head = head.right;
                }
                 str+=head.data;
                if(head.left==null && head.right==null) {
    
                head = root;
            }
        }
        System.out.println(str.replace("\0", ""));  
    }
    
  • + 1 comment

    This test is broken for java and I just earned points for printing what I am reading from stdin ....

  • + 0 comments
    void decode_huff(node * root, string s) {
        node* curr = root;
        string print;
        while(!s.empty())
        {
            if(curr->data != '\0')
            {
                print += curr->data;
                curr = root;
                continue;
            }
            if(s.front() == '0')
            {
                curr = curr->left;
            }
            else
            {
                curr = curr->right;
            }
            s.erase(0, 1);
        }
        print += curr->data;
        cout << print;
    }
    
  • + 0 comments
    // C++ version
    string ss;
    int idx;
    int len;
    
    void CheckNode(node * root) {
        
        if (root->data > 0) {
            std::cout << root->data;
            return;
        }
            
        if (ss[idx++] == '0') {
            CheckNode(root->left);
        }
        else {
            CheckNode(root->right);
        }
        
    }
    
    void decode_huff(node * root, string s) {
        if (root == nullptr)
            return;
        
        ss = s;
        len = ss.length();
        idx = 0;
        
        
        while (idx < len) {
            CheckNode(root);     
        }
        std::cout << '\n';
    }
    
  • + 0 comments

    Security is essential for my industry, and asp.net development outsourcing provide the peace of mind I need. I work with sensitive information daily, and knowing my applications are secure has been invaluable. It’s a relief to have a platform that takes data protection so seriously.