Tree: Huffman Decoding

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