reset password
Author Message
Anon
Posts: 134
Posted 16:36 Nov 13, 2013 |

Can anybody explain or talk about "val frenchCode?" I'm really lost as to what it does or what it is. Thanks

Mike
Posts: 11
Posted 16:48 Nov 13, 2013 |

It is a code tree like the one we built for "to boldly go." The numbers are the weights. You'll notice that and "Leaf" has only a single char while a "Fork" has a List of chars. frenchCode is a representation of Huffman compression done on a random wikipedia article. This representation is manually constructed, while the one we worked on in class recursively did all the work for us. 

Mike
Posts: 11
Posted 17:11 Nov 13, 2013 |

If we were to build the "to boldly go" tree manually, this is what it would look like. I've stuck /*comments*/ in the outer Fork to emphasize the parameters expected.

 

val toBoldlyGo: CodeTree = Fork(/*left*/Fork(Fork(Leaf('b',1),Leaf('d',1),List('b','d'),2),Leaf('o',3),List('b','d','o'),5),/*right*/Fork(Fork(Fork(Leaf('g',1),Leaf('t',1),List('g','t'),2),Leaf('y',1),List('g','t','y'),3),Leaf('l',2),List('g','t','y','l'),5),/*chars*/List('b','d','o','g','t','y','l'),/*weight*/10)
  

rabbott
Posts: 1649
Posted 18:29 Nov 14, 2013 |

Vou can put

val toBoldyGo: CodeTree = <as in the previous message>

into Huffman.scala and run this in the worksheet:

  decodeAsString(toBoldlyGo, encode(toBoldlyGo)("toboldlygo".toLowerCase.toList))  //> res0: String = toboldlygo

This shows that "toboldygo" was first encoded using that CodeTree and then decoded back to the original string.

It also works with other arrangements of the same letters.

decodeAsString(toBoldlyGo, encode(toBoldlyGo)("gottogo".toLowerCase.toList))  //> res1: String = gottogo

You might also try adding " " to the CodeTree so that you can encode and decode, for example, "go dolly"