Exercise 2.70
Now that you want to decode it, you have to define the tree first.
(DefineTree(generate-huffman-tree '((A 2) ( NA ) ( BOOM 1) ( SHA 3) (GET 2) ( YIP 9) (JOB 2) (WAH 1))
And then just to encode the lyrics given in the question.
(define message-1 '(Get a job))(define message-2 '(Sha nana- na na na na na na))(define message-3 '(Wah Yip Yip Yip Yip Yip Yip Yip Yip Yip))(define message-4 '(Sha Boom)) (encode message-1 tree); Value: (1 1 0 0 1 1 1 1 0 1 1 1 1 1)(encode message-2 tree); Value: (1 1 1 0 0 0 0 0 0 0 0 0)(encode message-3 tree); Value: (1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0)(encode message-4 tree); Value: (1 1 1 0 1 1 0 1 1)
Because the problem also requires the calculation of the bits tree required for coding, we can use length to calculate.
(length (encode message-1 tree) ) (length (encode message-2 tree) ) ; Value:12 (length (encode message-3 tree) ) ; value:23 (length (encode message-4 tree) ) ; Value:9
So multiply these 4 numbers by the number of occurrences and then add the required number of bits, or 84.
If you want to use fixed length coding, the title of 8 characters because each to occupy 3 bits above, and the lyrics of a total of 36 characters, multiply it is to use a fixed length encoding minimum required bits number, also even if 108.
"SICP Exercise" 97 Exercise 2.70