huffmanTree build and huffman Coding

来源:互联网 发布:海纳百川软件安卓版 编辑:程序博客网 时间:2024/05/29 08:20
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)thenORDER 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.


 

原创粉丝点击