huffmanTree build and huffman Coding

huffMan Tree:
Imagine that here is one article , which contains 27 'A' , 8 'B' ...and 5 'F' :
A 27 , B 8 , C15, D15, E30 ,F5


So now build a Huffman tree
(1)take first TWO numbers , (2)construct one tree ,left value is smaller than right side(3) then insert the ROOT into the sequence, (3)then ORDER the numbers .


1> order the numbers .
F5 , B8, C15, D15, A27, E30


2>


P13 ,C15 ,D15 ,A27 ,E30
/ \
F5 B8


3>


P28 D15 A27 E30

/ \

P13 C15

/ \

F5 B8



4>
D15 A27 P28 E30
/ \
P13 C15
/ \
F5 B8


5>


P42 P28 E30
/ \ / \
D15 A27 P13 C15
/ \
F5 B8


6> P28 E30 P42
/ \ / \
P13 C15 D15 A27
/ \
F5 B8


7>
P58
/ \ P42
P28 E30 / \
/ \ D15 A27
P13 C15
/ \
F5 B8


8> P100
/ \
/ \
P42 P58
/ \ / \
D15 A27 P28 E30
/ \
P13 C15
/ \
F5 B8




now let's say Left EDGE is 0 , right EDGE is 1
so for gets the huffman codes :


A:01
B:1001
C:101
D:00
E:11
F:1000


so the codes should be
01100110100111000


then analysis the sequence :
1>take each 0/1 , start from root search node ,till can not reach
2>remove the 0/1 already taken
3>loop



then will get huffman tree.




你可能感兴趣的:(huffmanTree build and huffman Coding)