"SICP Exercise" 97 Exercise 2.70

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.