码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-28 10:30:09      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

头文件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;       
}


运行结果如下:

技术分享

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

标签:

原文地址:http://blog.csdn.net/xiao_ping_ping/article/details/42211683

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!