Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees1 Trees and Codes a bc de 000100111011 abcde.

Similar presentations


Presentation on theme: "Trees1 Trees and Codes a bc de 000100111011 abcde."— Presentation transcript:

1 Trees1 Trees and Codes a bc de 000100111011 abcde

2 Trees2 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”

3 Trees3 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…”

4 Trees4 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(log 2 (n)) bits Use that many bits for each character This is the system used within the computer with 8 bits for each ASCII code

5 Trees5 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

6 Trees6 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

7 Trees7 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

8 Trees8 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 A BC DE 0 0 0 0 1 1 1 1

9 Trees9 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

10 Trees10 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

11 Trees11 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

12 Trees12 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding: 0

13 Trees13 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

14 Trees14 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding: 0

15 Trees15 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

16 Trees16 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

17 Trees17 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 0

18 Trees18 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

19 Trees19 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 1

20 Trees20 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

21 Trees21 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 1

22 Trees22 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

23 Trees23 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

24 Trees24 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC 1

25 Trees25 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

26 Trees26 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC 1

27 Trees27 Encoding and Decoding: ACD A BC DEA BC DE Encoding:Decoding:ACD

28 Trees28 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…

29 Trees29 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

30 Trees30 Huffman codes From Shannon’s information theory, The optimal static code assigns -log 2 (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

31 Trees31 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

32 Trees32 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

33 Trees33 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 00

34 Trees34 Encoding and Decoding: ACD Encoding:CDDecoding:A A BCDE A BCDE Make same change in both trees: Rotate A’s parent

35 Trees35 Encoding and Decoding: ACD Encoding:CDDecoding:A A BCDE A BCDE

36 Trees36 Encoding and Decoding: ACD Encoding:DDecoding:CA A BCDE A BCDE 101

37 Trees37 Encoding and Decoding: ACD Encoding:DDecoding:CA A B C DE A B C DE

38 Trees38 Encoding and Decoding: ACD Encoding:DDecoding:CA A B C DE A B C DE

39 Trees39 Encoding and Decoding: ACD Encoding:Decoding:CAD A B C DE A B C DE 1110

40 Trees40 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 y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 left rotation makes the right child y of a node x into x’s parent; x becomes the left child of y y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 (structure of tree above y is not modified) (structure of tree above x is not modified) a right rotation about ya left rotation about x

41 Trees41 Splaying: is x the root? stop is x a child of the root? right-rotate about the root left-rotate about the root is x the left child of the root? is x a left-left grandchild? is x a left-right grandchild? is x a right-right grandchild? is x a right-left grandchild? right-rotate about g, right-rotate about p left-rotate about g, left-rotate about p left-rotate about p, right-rotate about g right-rotate about p, left-rotate about g start with node x “ x is a left-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 no yes no yes zig-zig zig-zag zig-zig zig

42 Trees42 Visualizing the Splaying Cases zig-zag y x T2T2 T3T3 T4T4 z T1T1 y x T2T2 T3T3 T4T4 z T1T1 y x T1T1 T2T2 T3T3 z T4T4 zig-zig y z T4T4 T3T3 T2T2 x T1T1 zig x w T1T1 T2T2 T3T3 y T4T4 y x T2T2 T3T3 T4T4 w T1T1


Download ppt "Trees1 Trees and Codes a bc de 000100111011 abcde."

Similar presentations


Ads by Google