赫夫曼树及其应用(代码补充,选做)

/*
	哈夫曼编码(代码补充) 
*/
#include 
#include 
#include 
typedef struct{
	int weight;
	int parent, lchild, rchild;
}HTNode, *HuffmanTree;	//动态数组,元素为HTNode 
typedef char** HuffmanCode;	//编码表,每一个元素都是一个编码,即char*方式存储的字符串 

void Init_weight(int **w, int n){
	while(!(*w=(int*)malloc((n+1)*sizeof(int))));
}

void Create_Weight(int n, int *w){
	int i, data;
	for(i = 1; i<=n; i++){
		scanf("%d", &data);
		w[i] = data;
	}
}	
	
	//初始化哈夫曼树 
void Init_HuffmanTree(HuffmanTree *HT,int *w, int n){
	int i;
	int m = 2*n-1;
	HTNode *p;
	while(!((*HT)=(HTNode*)malloc(sizeof(HTNode)*(m+1))));
	for(p = (*HT)+1, i=1; i<=n; i++, p++){
		p->weight = w[i];
		p->parent = 0;
		p->rchild = 0;
		p->lchild = 0;
	}
	for(; i<=m; i++, p++){
		p->lchild = 0;
		p->parent = 0;
		p->rchild = 0;
		p->weight = 0;
	}
}

//利用选择排序(循环一次得到最小的两个值) 
//在HT中选择parent为0,且权重最小的两个节点,返回他们的数组下标分别为s1和s2 
void Select(HuffmanTree HT, int i, int *s1, int *s2){
	//**************begin********************
	int j=0;
    int  min,min1,min2;
    for(j = 1; j <= i; j++){
        //如果此结点的父亲没有,那么把结点号赋值给 min,跳出循环
            if(HT[j].parent == 0){
                min1 = j;
                break;
            }
        }
    for (j = 1;  j<=i ; j++) {
        if(HT[j].parent==0){
            if(HT[j].weightmin2){
        min=min1;
        min1=min2;
        min2=min;
    }
    *s1 = min1;
    *s2 = min2;
    
    
  	//****************end******************
}

	//创建哈夫曼树
void Create_HuffmanTree(HuffmanTree HT,int n){
	//**************begin********************
	int s1=1,s2=1;
    int m = 2 * n - 1;
    for (int j = n+1; j <=m ; ++j) {
        Select(HT,j-1,&s1,&s2);
        //printf("%d %d\n",s1,s2);
        HT[s1].parent=j;
        HT[s2].parent=j;
        HT[j].lchild=s2;
        HT[j].rchild=s1;
        HT[j].weight=HT[s1].weight+HT[s2].weight;
    }
    
    
  	//****************end******************
}
	
	//初始化哈夫曼编码表 
void Init_HuffmanCode(HuffmanCode *HC,int n){
	while(!((*HC) = (HuffmanCode)malloc((n+1)*sizeof(char*))));
}
	
	//求哈夫曼编码
void GetHuffmanCode(HuffmanCode HC, HuffmanTree HT, int n){
	//**************begin********************
	char *s;
    int start;
    int i, c, p;
    s = (char *)malloc(n * sizeof(char));
    s[n - 1] = '\0';
    for (i = 1; i <= n;i++)
    {
        start = n - 1;
        c = i;
        p = HT[i].parent;
        while(p!=0)
        {
            --start;
            if(HT[p].rchild==c)
                s[start] = '0';
            if(HT[p].lchild==c)
                s[start] = '1';
            c = p;
            p = HT[p].parent;
        }
        HC[i] = (char *)malloc((n - start) * sizeof(char));
        strcpy(HC[i], &s[start]);
    }
    free(s);
    
    
  	//****************end******************
}
	
	//打印哈夫曼编码表
void PrintCode(HuffmanCode HC, int n){
	for(int i=1; i<=n; i++){
		printf("%s\n", HC[i]);
	} 
} 

void Destory(HuffmanTree *HT, HuffmanCode *HC){
	free(*HC);
	free(*HT);
}
int main(){
	HuffmanTree HT;
	HuffmanCode HC;
	int *w, n; 
	
	scanf("%d", &n);
	
	//初始化权重数组 
	Init_weight(&w, n);
	
	
	//创建权值数组
	//n表示权值个数 
	Create_Weight(n, w);	
	
	
	//初始化哈夫曼树 
	Init_HuffmanTree(&HT, w, n);
 
	//创建哈夫曼树
	Create_HuffmanTree(HT, n);
	
	//初始化哈夫曼编码表 
	Init_HuffmanCode(&HC, n);
	
	//求哈夫曼编码
	GetHuffmanCode(HC, HT, n); 
	
	//打印哈夫曼编码表
	PrintCode(HC, n); 
	
	//释放空间
	Destory(&HT, &HC);
	 
	
	return 0;
} 

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