Tree: Huffman Decoding

  • + 0 comments

    much simpler than I was anticipating :D

    C++14 solution:

    void decode_huff(node * root, string s) {
        string result;
        node* ptr = root;
        for (int i = 0; i < s.size(); ++i) {
            switch (s[i]) {
                case '0': ptr = ptr->left; break;
                case '1': ptr = ptr->right; break;
            }
            if (!ptr->left && !ptr->right) {
                result += ptr->data;
                ptr = root;
            }
        }
        cout << result << endl;
    }