1 / 42

Trees and Codes

a. d. e. b. c. Trees and Codes. Binary Trees and Prefix Codes. Imagine that you have an alphabet of symbols e.g. A, B, …, Z, a, b, …, z, ‘ ’, ‘.’ We wish to represent a string of these symbols as a string of bits e.g. “This is a string of characters” becomes “01100111000101010111001”.

blaze
Download Presentation

Trees and Codes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. a d e b c Trees and Codes Trees

  2. Binary Trees and Prefix Codes • Imagine that you have an alphabet of symbols • e.g. A, B, …, Z, a, b, …, z, ‘ ’, ‘.’ • We wish to represent a string of these symbols as a string of bits • e.g. “This is a string of characters” becomes “01100111000101010111001” Trees

  3. Method 1: Use a fixed number for each symbol • Map A -> 1, B -> 2, … • “This is a string of characters” becomes a string of numbers • “20,34,35,…” • I required commas to separate the characters! • Use a fixed width, padded with leading 0’s instead • “020034035…” Trees

  4. Method 1: Fixed width • How wide do my characters need to be • Using a string of bits • How many bits are needed to represent the largest character? • ceil(log2(n)) bits • Use that many bits for each character • This is the system used within the computer with 8 bits for each ASCII code Trees

  5. Method II: Prefix codes • For each symbol, we’ll use a code with a special property • No code is the prefix of any other code • How does this work? • Decoding: • We read in the codes one bit at a time • When we have a code we recognise, it must be the end of a symbol • It cannot be part of a longer symbol because no code is the prefix of another code Trees

  6. Method II: Prefix codes • Example • A:00, B:010, C:011, D:10, E:11 • ADBECABADE • 00100101101100010001011 • A D B E C A B A D E • The string decodes Trees

  7. Making a Prefix Code • We want the code to be efficient • No strings longer than necessary • No wasted strings • A code is a set of strings of binary digits, such that no string corresponding to one symbol is the prefix of a string corresponding to another symbol • In a tree, leaf nodes have no children • No path from the root to a leaf is the prefix of a path from the root to another node Trees

  8. Binary Trees and Prefix Codes • Binary trees are in one to one correspondence with Prefix Codes • A:00, B:010, C:011, D:10, E:11 0 1 0 1 0 1 A D E 0 1 B C Trees

  9. Prefix Trees • Binary Trees • The left child corresponds to 0, the right to 1 • Each leaf contains a symbol • The code for a symbol corresponds to the path from the root to the leaf containing that symbol Trees

  10. Encoding and Decoding • Imagine the encoder and decoder running in parallel • Encoding • Start from the root • While you are not at the symbol’s leaf • If the symbol you wish to send is a left decendant, send 0 and move to your left child, else send 1 and move to your right child • Decoding • Start from the root • While you are not at a leaf • Read a bit. If it is 0 then move to your left chile, else move to your right child Trees

  11. Encoding:ACD Decoding: Encoding and Decoding: ACD A D E A D E B C B C Trees

  12. Encoding:ACD Decoding: Encoding and Decoding: ACD 0 A D E A D E B C B C Trees

  13. Encoding:ACD Decoding: Encoding and Decoding: ACD A D E A D E B C B C Trees

  14. Encoding:ACD Decoding: Encoding and Decoding: ACD 0 A D E A D E B C B C Trees

  15. Encoding:CD Decoding:A Encoding and Decoding: ACD A D E A D E B C B C Trees

  16. Encoding:CD Decoding:A Encoding and Decoding: ACD A D E A D E B C B C Trees

  17. Encoding:CD Decoding:A Encoding and Decoding: ACD 0 A D E A D E B C B C Trees

  18. Encoding:CD Decoding:A Encoding and Decoding: ACD A D E A D E B C B C Trees

  19. Encoding:CD Decoding:A Encoding and Decoding: ACD 1 A D E A D E B C B C Trees

  20. Encoding:CD Decoding:A Encoding and Decoding: ACD A D E A D E B C B C Trees

  21. Encoding:CD Decoding:A Encoding and Decoding: ACD 1 A D E A D E B C B C Trees

  22. Encoding:D Decoding:AC Encoding and Decoding: ACD A D E A D E B C B C Trees

  23. Encoding:D Decoding:AC Encoding and Decoding: ACD A D E A D E B C B C Trees

  24. Encoding:D Decoding:AC Encoding and Decoding: ACD 1 A D E A D E B C B C Trees

  25. Encoding:D Decoding:AC Encoding and Decoding: ACD A D E A D E B C B C Trees

  26. Encoding:D Decoding:AC Encoding and Decoding: ACD 1 A D E A D E B C B C Trees

  27. Encoding: Decoding:ACD Encoding and Decoding: ACD A D E A D E B C B C Trees

  28. Back to Method I: Balanced Tree • Method I was to used fixed length code words • Each path from the root to a leaf is the same length: a balanced tree • Balanced trees are good for worst case path length. Are they good for coding? • Yes, if you assume the worst case • But we can normally do better… Trees

  29. Statically optimal codes • Want common symbols to have short codes • This will make uncommon symbols have longer codes • In a tree with a fixed number of leave/symbols, moving one leaf/symbol closer to the root will move others further away Trees

  30. Huffman codes • From Shannon’s information theory, The optimal static code assigns -log2(p) bits to a symbol that occurs with probability p • It is possible to make a Huffman code tree with this property • Will look at this later in the course Trees

  31. Adaptive Codes • As long as the same change is made in both sending and receiving trees/codes, there is no reason why the tree/code must remain static • Send a character using the initial tree • Update the tree using that character • Can also be updated in the receiver as it already has the character • Send the next character Trees

  32. Encoding:ACD Decoding: Encoding and Decoding: ACD A D E A D E B C B C Trees

  33. Encoding:CD Decoding:A Encoding and Decoding: ACD 00 A D E A D E B C B C Trees

  34. Encoding:CD Decoding:A Encoding and Decoding: ACD Make same change in both trees: Rotate A’s parent A A B C D E B C D E Trees

  35. Encoding:CD Decoding:A Encoding and Decoding: ACD A A B C D E B C D E Trees

  36. Encoding:D Decoding:CA Encoding and Decoding: ACD 101 A A B C D E B C D E Trees

  37. Encoding:D Decoding:CA Encoding and Decoding: ACD A A B B C C D E D E Trees

  38. Encoding:D Decoding:CA Encoding and Decoding: ACD A A B B C C D E D E Trees

  39. Encoding: Decoding:CAD Encoding and Decoding: ACD A A 1110 B B C C D E D E Trees

  40. Splay Trees do Rotations after Every Operation • new operation: splay • splaying moves a node to the root using rotations • right rotation • makes the left child x of a node y into y’s parent; y becomes the right child of x • left rotation • makes the right child y of a node x into x’s parent; x becomes the left child of y y x a right rotation about y a left rotation about x y T1 x T3 x y T3 T1 T2 T2 y T1 x T3 T3 T2 T1 T2 (structure of tree above x is not modified) (structure of tree above y is not modified) Trees

  41. “x is aleft-left grandchild” means x is a left child of its parent, which is itself a left child of its parent • p is x’s parent; g is p’s parent Splaying: start with node x is x a left-left grandchild? is x the root? zig-zig yes stop right-rotate about g, right-rotate about p yes no is x a right-right grandchild? zig-zig is x a child of the root? no left-rotate about g, left-rotate about p yes is x a right-left grandchild? yes zig-zag is x the left child of the root? left-rotate about p, right-rotate about g no yes is x a left-right grandchild? zig zig zig-zag yes right-rotate about p, left-rotate about g right-rotate about the root left-rotate about the root yes Trees

  42. Visualizing the Splaying Cases zig-zag x z z z y y T4 T1 y T1 T2 T3 T4 T4 x T3 x T2 T3 T1 T2 zig-zig y zig x T4 x T1 x y w T2 y z T3 w T1 T2 T3 T4 T3 T4 T1 T2 Trees

More Related