Trees and Codes
description
Transcript of Trees and Codes
Trees 1
Trees and Codes
a
b c
d e00 010 011 10 11
a b c d e
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 characterThis is the system used within the computer with 8 bits for each ASCII code
Trees 5
Method II: Prefix codesFor 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
ADBECABADE00100101101100010001011
A D B E C A B A D E
The string decodes
Trees 7
Making a Prefix CodeWe 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 symbolIn 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
A
B C
D E
0
0
0
0
1
11
1
Trees 9
Prefix Trees
Binary Trees The left child corresponds to 0, the
right to 1
Each leaf contains a symbolThe 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 and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:ACD Decoding:
Trees 12
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:ACD Decoding:
0
Trees 13
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:ACD Decoding:
Trees 14
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:ACD Decoding:
0
Trees 15
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
Trees 16
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
Trees 17
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
0
Trees 18
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
Trees 19
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
1
Trees 20
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
Trees 21
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
1
Trees 22
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:D Decoding:AC
Trees 23
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:D Decoding:AC
Trees 24
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:D Decoding:AC
1
Trees 25
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:D Decoding:AC
Trees 26
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:D Decoding:AC
1
Trees 27
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding: Decoding:ACD
Trees 28
Back to Method I: Balanced Tree
Method I was to used fixed length code wordsEach path from the root to a leaf is the same length: a balanced treeBalanced 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 codesThis 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 pIt 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 and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:ACD Decoding:
Trees 33
Encoding and Decoding: ACD
A
B C
D E A
B C
D E
Encoding:CD Decoding:A
00
Trees 34
Encoding and Decoding: ACD
Encoding:CD Decoding:A
A
B C D E
A
B C D E
Make same change in both trees:Rotate A’s parent
Trees 35
Encoding and Decoding: ACD
Encoding:CD Decoding:A
A
B C D E
A
B C D E
Trees 36
Encoding and Decoding: ACD
Encoding:D Decoding:CA
A
B C D E
A
B C D E
101
Trees 37
Encoding and Decoding: ACD
Encoding:D Decoding:CA
A
B
C
D E
A
B
C
D E
Trees 38
Encoding and Decoding: ACD
Encoding:D Decoding:CA
A
B
C
D E
A
B
C
D E
Trees 39
Encoding and Decoding: ACD
Encoding: Decoding:CAD
A
B
C
D E
A
B
C
D E
1110
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
y
x
T1 T2
T3
y
x
T1
T2T3
left rotation makes the right child y of a node x
into x’s parent; x becomes the left child of y
y
x
T1 T2
T3
y
x
T1
T2T3
(structure of tree above y is not modified)
(structure of tree above x is not modified)
a right rotation about y a left rotation about x
Trees 41
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
yes
yes
yes
yes
yes
no
no
yes zig-zig
zig-zag
zig-zag
zig-zig
zigzig
Trees 42
Visualizing the Splaying Cases
zig-zag
y
x
T2 T3
T4
z
T1
y
x
T2 T3 T4
z
T1
y
x
T1 T2
T3
z
T4
zig-zig
y
z
T4T3
T2
x
T1
zig
x
w
T1 T2
T3
y
T4
y
x
T2 T3 T4
w
T1