Tree: Huffman Decoding

  • + 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';
    }