赫夫曼树编码的表示与实现--自己写数据结构

头文件huffman.h

#ifndef _HUFFMAN_H_
#define _HUFFMAN_H_

#define MAX_WEIGHT 10000

typedef struct _HTNode
{
    int weight;
    int parent,lchild,rchild;
    char data;
}HTNode,*pHTNode;

typedef char** huffmancode;

void select_min_weight(HTNode* btree,int mn,int* s1,int* s2);
//void read_huffman_code(huffmancode code,HTNode** tree,int n);
void creat_huffman_tree(huffmancode code,HTNode** tree,char* pdata,int* pweight,int n);

#endif


相关数据结构huffman.c

/***************************
时间:2014.12.17
作者:XIAO_PING_PING
编译环境:DEV-C++ 4.9.9.2 
内容:赫夫曼树编码的表示与实现 
功能: 学习写数据结构 

****************************/
#include <string.h>
#include <stdlib.h>

#include "huffman.h" 

void select_min_weight(HTNode* btree,int mn,int* s1,int* s2)
{
    int mweight1,mweight2,i;     
    mweight1 = MAX_WEIGHT; 
    for(i = 0;i < mn;i++)
    {
         if(((btree+i)->weight < mweight1)&&(0 == (btree+i)->parent))  
         {
             *s1 = i;
             mweight1 = (btree+i)->weight;
    //         *s1 = (btree+i)->weight;
     //        nmin = i;                      
         }           
    }
    
    mweight2 = MAX_WEIGHT;
    for(i = 0;i < mn;i++)
    {
         if(((btree+i)->weight < mweight2)&&(0 == (btree+i)->parent)&&(*s1 != i))  
         {
             *s2 = i;
             mweight2 = (btree+i)->weight;                    
         }           
    }
}
/*
void read_huffman_code(huffmancode code,HTNode** tree,int n)
{
     int i;
     char *chs;
     int start;
     int c,f;
     
     code = (huffmancode)malloc(n * sizeof(char *));
     chs = (char *)malloc(n * sizeof(char *));
     chs[n-1] = '\0';
     
     for(i = 0;i < n;i++)
     {
         start = n - 1;
         for(c = i,f = *tree[i].parent;f != 0;c = f,f = *tree[f].parent)
         {
             if(*tree[f].lchild == c) 
             {
                 chs[--start] = '0';
             }
             else
             {
                 chs[--start] = '1';    
             }   
         }
         code[i] = (char *)malloc((n - start)*sizeof(char));
         strcpy(code[i],&chs[start]);
         
         printf("%c: %s",*tree[i].data,code[i]);
     }
     free(cd);
}
*/
void creat_huffman_tree(huffmancode code,HTNode** tree,char* pdata,int* pweight,int n)
{
     int i,m;
     int *order1,*order2;
     pHTNode p;
     order1 = (int *)malloc(sizeof(int));
     order2 = (int *)malloc(sizeof(int));
     
     m = n*2 - 1;
     *tree = (HTNode *)malloc(m * sizeof(HTNode));
     p = *tree;
     for(i = 0;i < n;i++,p++)
     {
         p->weight  = *(pweight+i);
         p->parent  = 0;
         p->lchild = 0;
         p->rchild  = 0;
         p->data    = *(pdata+i);    
     }
     for(i = n;i < m;i++,p++)
     {
          p->parent = 0;     
     }
     
     for(i = n;i < m;i++)
     {
         select_min_weight(*tree,i,order1,order2);
         (*tree + i)->weight = (*tree + *order1)->weight + (*tree + *order2)->weight;
         (*tree + i)->lchild = *order1;
         (*tree + i)->rchild = *order2; 
         (*tree + *order1)->parent = i;
         (*tree + *order2)->parent = i;
         
         printf ("order1.weight and order2.weight in round %d: %d, %d\n",\
                 i, (*tree + *order1)->weight, (*tree + *order2)->weight);  
                 /* 用于测试 */
         printf ("\n");
     }
     
     //int i;
     char *chs;
     int start;
     int c,f;
     
     code = (huffmancode)malloc(n * sizeof(char *));
     chs = (char *)malloc(n * sizeof(char *));
     chs[n-1] = '\0';
     
     for(i = 0;i < n;i++)
     {
         start = n - 1;
         for(c = i,f = (*tree + i)->parent;f != 0;c = f,f = (*tree + f)->parent)
         {
             if((*tree + f)->lchild == c) 
             {
                 chs[--start] = '0';
             }
             else
             {
                 chs[--start] = '1';    
             }   
         }
         code[i] = (char *)malloc((n - start)*sizeof(char));
         strcpy(code[i],&chs[start]);
         
         printf("%c : %s\n",(*tree + i + 1)->data,code[i]);
     }
     free(chs);
}


测试文件test.c如下

#include <string.h>
#include <stdlib.h>

#include "huffman.h"

int main()
{
    int N,i;
    //char nCR = 0;
    int* weights;
    char* datas;
    pHTNode phash;
    
    huffmancode pcode;
    printf("n = "); 
    scanf("%d",&N);
    datas = (char *)malloc((N + 2) * sizeof(char));
    weights = (int *)malloc(N * sizeof(int));
    
    printf("请输入编码字符:");
    for(i = 0;i < (N+2);i++)
    {      
        scanf("%c",datas+i);  //'#结束'
        //scanf("%c",&nCR); 
        //while('\n' == nCR);
        //nCR = 0;     
    }
    printf("请输入对应权值:\n");
    for(i = 0;i < N;i++)
    {      
        scanf("%d",weights+i);      
    }
    creat_huffman_tree(pcode,&phash,datas,weights,N);
    
    getch();
    return 0;       
}


运行结果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值