Huffman Code Decoding Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • recordlovelife
    New Member
    • Sep 2007
    • 31

    Huffman Code Decoding Problem

    So i have written a code to encode a string of a select amount of letters into huffman code.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    string Huffman(char letter);
    string Huffman_word(string word);
    string Huffman_code(int code);
    
    string word;
    
    
    int main()
    {
    
    string word;
    cout<<"Please Enter Your String: ";
    getline(cin,word);
    cout<<Huffman_word(word);
    
    
    
    }
    
    string Huffman_word(string word)
    {//This function takes a word and returns its Huffman encoding
    string Huffman_code;
    int n = word.length();
    for(int i = 0; i<n; i++)
    {
    Huffman_code.append(Huffman(word[i]));
    }
    return Huffman_code;
    }
    
    ////////////DECODE///////////////
    
    
    
    
    
    
    
    
    
    ////////////ENCODE///////////////
    string Huffman(char letter)
    {//this function takes a letter and produces it's corresponding Huffman encoding
    switch(letter){
    
    
    case 'a':
    case 'A':
    return "00";
    break;
    case 'e':
    case 'E':
    return "000";
    break;
    case 'f':
    case 'F':
    return "1101";
    break;
    case 'h':
    case 'H':
    return "1011";
    break;
    case 'i':
    case 'I':
    return "1000";
    break;
    case 'm':
    case 'M':
    return "0111";
    break;
    case 'n':
    case 'N':
    return "0010";
    break;
    case 's':
    case 'S':
    return "1011";
    break;
    case 't':
    case 'T':
    return "0110";
    break;
    case 'l':
    case 'L':
    return "11001";
    break;
    case 'o':
    case 'O':
    return "00110";
    break;
    case 'p':
    case 'P':
    return "10011";
    break;
    case 'r':
    case 'R':
    return "11000";
    break;
    case 'u':
    case 'U':
    return "00111";
    break;
    case 'x':
    case 'X':
    return "10010";
    break;
    case ' ':
    return "111";
    break;
    
    
    }
    }

    that works fine, but now i want to decode it, but I don't know how to.

    here's what I want to do in psuedo code.

    look at huffman code
    take first number
    while(there is still more huffman code to examine){

    (if value were looking at in the huffman code == any of the huffman letter codes){
    add the letter to a string variable
    move on to the next number in the huffman code
    }
    else{
    the current huffman code value you are looking at should have the next number in the code added to it
    }
    }

    return theNowDecode code.

    But I have no clue how to go about this. Please give me a push.

    thanks
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by recordlovelife
    that works fine, but now i want to decode it, but I don't know how to.

    here's what I want to do in psuedo code.

    look at huffman code
    take first number
    while(there is still more huffman code to examine){

    (if value were looking at in the huffman code == any of the huffman letter codes){
    add the letter to a string variable
    move on to the next number in the huffman code
    }
    else{
    the current huffman code value you are looking at should have the next number in the code added to it
    }
    }

    return theNowDecode code.

    But I have no clue how to go about this. Please give me a push.

    thanks
    Well, first I would get the string into your program. Are you going to have this as a function that's passed a string, or will this open a file?

    Once you get the string, you know that either the first three characters will be matched, and if not, then the first four will be. So read in three, see if they match anything, and if not, read in one more, see if that matches anything.

    Does that make sense? What are you having trouble conceptualizing ?

    Comment

    • recordlovelife
      New Member
      • Sep 2007
      • 31

      #3
      That does make senses actually. My problem is I keep getting error messages based on what way I go about it.

      I will be going the funtion route with this.
      Like so...

      string huffmanDecode(s tring huffmanCode)

      Now here is the idea of what I want
      (Pardon the crappy code, im typing this from my phone)

      Code:
      huffmanDecode(string huffmanCode){
      Int i=0;
      While (i<huffmanCode.strlen();){
      
      How do I check a certain part of a string? Do I use substr? Or strstr? 
      
      Then take that part of the string and run it into a funtion which will have the list of the decodes, but how should I do this? Is it best to use a bunch of if/elses or switches, or another im not realizing?
      
      Then when you find a match, add its decoded value to a new string, to be returned at the end of this function.
      
      Now start from where you left of in the huffmanCode and go back to the top of the loop and start again.
      }
      
      Retun huffmanDecodedValue;
      }

      So I think conceptually I got it, but syntactically I gotta lot of work to do.


      All help would be great.
      Thanks

      Comment

      • Barok
        New Member
        • Sep 2007
        • 10

        #4
        case 'a':
        case 'A':
        return "00";
        break;
        case 'e':
        case 'E':
        return "000";
        break;
        case 'h':
        case 'H':
        return "1011";
        break;
        case 's':
        case 'S':
        return "1011";
        break;
        i guess this is not correct.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by Barok
          i guess this is not correct.
          Nice catch, Barok, I'm guessing the OP just mis-typed.

          Comment

          • recordlovelife
            New Member
            • Sep 2007
            • 31

            #6
            OP? alright now im just lost, the encoding step was working fine. And i understand to decode i'd have to reverse the encode list, so it check for encode matches and gives a decode return.

            Comment

            Working...