构建赫夫曼树及求赫夫曼编码(从叶子到根逆向)

#include
#include
using namespace std;
typedef struct{
    int weight;
    int parent,lchild,rchild;
}HTNode,*HuffmanTree;
void Select(HuffmanTree HT,int len,int &s1,int &s2){
    int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;
    for(i=1;i<=len;i++){
        if(HT[i].weight>HT[i].weight;  
    for(i=n+1;i<=m;++i){
        Select(HT,i-1,s1,s2);
        HT[s1].parent=i;     
        HT[s2].parent=i;   
        HT[i].lchild=s1;   
        HT[i].rchild=s2 ;                            
        HT[i].weight=HT[s1].weight+HT[s2].weight;     
    }                                                
 }    
 typedef char **HuffmanCode;
 //赫夫曼编码 
 void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n){
    int i,start,c,f;
    HC=new char*[n+1];                                 
    char *cd=new char[n];                            
    cd[n-1]='\0';                                    
    for(i=1;i<=n;++i){                                                  
        start=n-1;                                  
        c=i; 
        f=HT[i].parent;                             
        while(f!=0){                                          
            --start;                              
            if(HT[f].lchild==c)  
                cd[start]='0';                        
            else 
                cd[start]='1';                     
            c=f; 
            f=HT[f].parent;                     
        }                                                 
        HC[i]=new char[n-start];                     
        strcpy(HC[i], &cd[start]);                   
    }
    delete cd;                                        
}                                                    
void show(HuffmanTree HT,HuffmanCode HC,int n){
    for(int i=1;i<=n;i++)
        cout<>n;
    CreatHuffmanTree(HT,n);
    HuffmanCode HC;
    CreatHuffmanCode(HT,HC,n);
    show(HT,HC,n);
}

构建赫夫曼树及求赫夫曼编码(从叶子到根逆向)_第1张图片

你可能感兴趣的:(数据结构,c++)