Huffman Encoding.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Esqulax
    New Member
    • Mar 2008
    • 1

    Huffman Encoding.

    Hiya guys.
    im after a bit of guidance to be honest

    My task:
    Create a program that generates huffman codewords for a 16 symbol alphabet, the input should be the probabilities of each symbol occurring.
    Im gonna use 6 for ease of programming for now

    Im unsure where to go... Ive created a basic "take user input - assign to an array" program.(userin put[])

    Heres my thoughts.. copy userinput[] to huffmanarray[0,i] using a for loop, and bubble sort it
    then take huffmanarray[0,6] add [0,5] (result)
    then copy huffmanarray[0,i-2] to huffmanarray[1,i]
    The put (result) into huffmanarray[1,5] and bubblesort again

    rinse and repeat
    This will create a huffman coding table in the array
    Thats the only way i can think of doing it, but it seems terribly inefficient.

    Ive seen some object orientated approaches programming the trees, and nodes, but i dont really understand how they do it. And i don't really have any experience with graphical programming (applets n suchlike)

    (It is uni work, but as i say, I'm more after guidance as to how it can be done, id rather understand it and program it myself, rather that rip off someone else work)
    any help would be greatly appreciated
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Never ever sort anything yourself for Huffman encoding; use a SortedSet instead.
    The set should store nodes of the Huffman tree and every node in this tree can
    be ordered according to its frequency. If two frequencies are equal design a small
    artificial ordering to make the two nodes different.

    For the tree building just do this: get the two smallest nodes from the set, remove
    them from the set, combine them and add them back again to the set. Do this
    until there are less than two elements in the set.

    kind regards,

    Jos

    Comment

    Working...