So I made a program that should do Huffman encoding. When comparing my answers to the correct ones, not all of mine matched up.
I got
[("a","0"),("c","100"),("b","101"),("d","110"),("f","1110"),("e","1111")]
The correct answer is
[('a',"0"),('b',"101"),('c',"100"),('d',"111"),('e',"1101"),('f',"1100")]
Mine method however gives me me a slight change. On branch 30 I use a 0 to get to D instead of a 1.
So this makes me wonder, are both answers correct? After all they both have the same string lengths.
If I am wrong, can someone explain why?
In case anyone wants it, my code is written in Haskell below
mergHufffman::(String,Int) -> (String,Int) -> (String,Int)
mergHufffman x y = (fst x ++ fst y, snd x + snd y)
data HTree a = Leaf a | Branch (HTree a) (HTree a) deriving Show
treeHuff::[(String,Int)] -> HTree (String,Int)
treeHuff (x:[]) = Leaf x
treeHuff (x:y:[])
| snd x < snd y = Branch (Leaf x) (Leaf y)
| snd x > snd y = Branch (Leaf y) (Leaf x)
treeHuff (x:y:z:list)
| snd x > snd merged = Branch (Leaf x) (treeHuff $ sortFirst $ y:z:list)
| otherwise = Branch (treeHuff $ y:z:[]) (treeHuff $ sortFirst $ x:list)
where merged = mergHufffman y z
sortFirst::[(String,Int)]->[(String,Int)]
sortFirst freq = reverse $ sortBy (comparing snd) freq
readHuffTree :: HTree (String,Int)-> String -> [(String, String)]
readHuffTree (Branch x y) code = f1 ++ f2
where
f1 = readHuffTree x (code ++ "0")
f2 = readHuffTree y (code ++ "1")
readHuffTree (Leaf x) code = ((fst x, code):[])