Outline(web.eecs.umich.edu/~sugih/courses/eecs281/f11/lectures/... · 2011. 10. 1. ·...
Transcript of Outline(web.eecs.umich.edu/~sugih/courses/eecs281/f11/lectures/... · 2011. 10. 1. ·...
Data Structures and Algorithms
Discussion 4: Week of Sep 26, 2011
Outline(
• Heaps(• Huffman(Encoding(• AVL(Trees(
Heap%Review�• Heapify(
;(PercolateUp,(TrickleDown(;(O(logN)(Eme(for(both(cases(
• BuildHeap(;(O(N)(Eme,(proof(is(non;trivial(
• DequeueMax(;(Replace(root(with(last(node,(then(fix(down(;(O(logN)(Eme(
Max;Heapify(2)(
heap-size[A] = 10�
Building%a%heap((
Build;Max;Heap(A)(1(((heap;size[A](←(length[A](2%%%for(i(←(!length[A]/2"((downto(1(3(( (((((do(Max;Heapify(A,(i)(
Chapter 6 P.5
Building%a%heap�
Building%a%heap� Building%a%heap�
DequeueMax�21
15
14 10
20
2
Delete(21.(
15
14 10
20
2
Move(2(to(root.(
2
15
14 10
20
Not(max(heap!(
Trickle(down!(
20
15
14 10
2 Max(heap!(
DequeueMax�
20
15
14 10
2
Delete(20.(
15
14 10
2 Move(10(to(root.(
10
15
14
2
Not(max(heap!(
Trickle(down!(
15
10
14
2
DequeueMax�
Not(max(heap!(
Trickle(down!(
15
14
10
2
Max(heap!(
15
10
14
2
Huffman(Encoding(
• Compresses(data(to(use(less(space.(• Works(best(on(text(data,(not(binary(data.(
– Why?(
Huffman(Encoding(
• Compresses(data(to(use(less(space.(• Works(best(on(text(data,(not(binary(data.(
– Huffman(encoding(uses(a(character’s(frequency(in(order(to(encode(things(–(the(more(frequent(a(character,(the(be_er(the(compression.(
– Binary(data(looks(like:(páW.ÈDŽOŸuæ`ç\lß(– Text(data(is(relaEvely(uniform(and(predictable.(
Huffman(Encoding�
Given(the(text(ALIBABA(which(of(the(following(can(be(the(Huffman(prefix(code(for(compressing(this(text?(A.(A=001,(B=000,(I=01,(L=10.(B.(A=00,(B=01,(I=10,(L=11.(C.(A=0,(B=11,(I=001,(L=000.(D.(A=0,(B=01,(I=100,(L=110.(E.(A=1,(B=01,(I=001,(L=000.(
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=001,(B=000,(I=01,(L=10.((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=001,(B=000,(I=01,(L=10.(No.((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=00,(B=01,(I=10,(L=11.(((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=00,(B=01,(I=10,(L=11.(No.((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=0,(B=11,(I=001,(L=000.((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=0,(B=11,(I=001,(L=000.(Yes.((
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=0,(B=01,(I=100,(L=110(
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=0,(B=01,(I=100,(L=110.(Yes.(
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=1,(B=01,(I=001,(L=000.(
Huffman(Encoding(
• ALIBABA(– A:(3(– B:(2(– L(:(1(– I(:(1(
• A=1,(B=01,(I=001,(L=000.(Yes.(
AVL Trees�
• A self-balancing binary search tree • Balance Factor
- Height(left-sub-tree) – Height(right-sub-tree) • Balance factor of -1, 0, or 1 for each node • Rotations needed for maintain balance
- Single Rotation (LL or RR) - Double Rotation(LR or RL)�
Single Rotation�
Double Rotation�Example
! Is this AVL balanced?
! Why/why not? 14
10
6
18
Example ! What happens
when you add (one after the other): ! 4 ! 12 ! 20
14
10
6
18
AVL Trees ! Adding 4
14
10
6
18
Example ! Adding 4
14
10
6
18
4
Example ! Adding 4
! Not AVL balanced ! What do we do?
14
10
6
18
4
Example ! Adding 4
! Not AVL balanced ! What do we do? ! Right rotation
around 10
14
10
6
18
4
Example ! Adding 4
! Not AVL balanced ! What do we do? ! Right rotation
around 10
14
6
4
18
10
Example ! Adding 12
14
6
4
18
10
Example ! Adding 12
14
6
4
18
10
12
Example ! Adding 12
! Not AVL balanced ! What do we do?
14
6
4
18
10
12
Example ! Adding 12
! Not AVL balanced ! What do we do? ! Double rotation
14
6
4
18
10
12
Example ! Adding 12
! Not AVL balanced ! What do we do? ! Double rotation ! Left rotation
around 6
14
6
4
18
10
12
Example ! Adding 12
! Not AVL balanced ! What do we do? ! Double rotation ! Left rotation
around 6
14
10
6
18
12
4
Example ! Adding 12
! Not AVL balanced ! What do we do? ! Double rotation ! Left rotation
around 6 ! Right rotation
around 14
14
10
6
18
12
4
Example ! Adding 12
! Not AVL balanced ! What do we do? ! Double rotation ! Left rotation
around 6 ! Right rotation
around 14
10
6
4
14
12 18
Example ! Adding 20
10
6
4
14
12 18
Example ! Adding 20
10
6
4
14
12 18
20
Example ! Adding 20
! Still AVL Balanced 10
6
4
14
12 18
20