实验九 哈夫曼编码

实验九 哈夫曼编码

一、【实验目的】
1、理解哈夫曼树的基本概念

 

2、掌握哈夫曼树的构造及数据结构设计

 

3、掌握哈夫曼编码问题设计和实现

 

二、【实验内容】
1、假设用于通信的电文仅由8个字母 {a, b, c, d, e, f, g, h} 构成,它们在电文中出现的概率分别为{ 0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10 },试为这8个字母设计哈夫曼编码。

 

提示:包含两个过程:1)构建哈夫曼树,(2)输出编码。

 

 

三、【实验源代码】

#include<stdio.h>

#include<stdlib.h>

#include<limits.h>

#define MaxNode 100

typedef struct {

    int start;

    char bit[MaxNode];//

}HuffCode;

typedef struct huff {

    double ww;

    int lchild, rchild, parent;

    HuffCode c;

}Huffmannode;

typedef struct hu {

    int root;

    Huffmannode tree[MaxNode];

}Hufftree;

char change(char x)

{

    if (x == '1')

         return '0';

    return '1';

}

char exchange(double w)//对应关系

{

    if (w == 0.07)

         return 'a';

    else if (w == 0.19)

         return 'b';

    else if (w == 0.02)

         return 'c';

    else if (w == 0.06)

         return 'd';

    else if (w == 0.32)

         return 'e';

    else if (w == 0.03)

         return 'f';

    else if (w == 0.21)

         return 'g';

    else if (w == 0.1)

         return 'h';

    else

         return ' ';

}

Hufftree* create(int m, double* w)

{

    int i, j, x1, x2;

    double m1, m2;

    Hufftree* pht;

    pht = (Hufftree*)malloc(sizeof(Hufftree));

    if (pht == NULL)

    {

         printf("out of space!");

         return 0;

    }

    for (i = 0; i < 2 * m - 1; i++)//置初态

    {

         pht->tree[i].lchild = -1;

         pht->tree[i].rchild = -1;

         pht->tree[i].parent = -1;

         if (i < m)

             pht->tree[i].ww = w[i];

         else

             pht->tree[i].ww = -1;

    }

    for (i = 0; i < m - 1; i++)//构造一个内部节点

    {

         m1 = m2 = INT_MAX;

         x1 = x2 = -1;

         for (j = 0; j < m + i; j++)//找出两个权值最小的无父节点

             if (pht->tree[j].ww < m1 && pht->tree[j].parent == -1)

             {

                  m2 = m1;

                  x2 = x1;

                  x1 = j;

                  m1 = pht->tree[j].ww;

             }

             else if (pht->tree[j].ww < m2 && pht->tree[j].parent == -1)

             {

                  m2 = pht->tree[j].ww;

                  x2 = j;

             }

         pht->tree[x1].parent = m + i;

         pht->tree[x2].parent = m + i;

         pht->tree[m + i].ww = m1 + m2;

         pht->tree[m + i].lchild = x1;

         pht->tree[m + i].rchild = x2;

    }

    pht->root = m + i - 1;

    return pht;

}

void Huffcode(Hufftree* tree, int m)

{

    int i ;

    int child, parent;

    for (i = 0; i <  m ; i++)//对叶子结点编码

    {

         tree->tree[i].c.start = m - 1;

         child = i;

         parent = tree->tree[i].parent;

         while (parent != -1)//从下遍历到根结点

         {

             if (tree->tree[parent].lchild == child)

                  tree->tree[i].c.bit[tree->tree[i].c.start] = '0';

             else

                  tree->tree[i].c.bit[tree->tree[i].c.start] = '1';

             child = parent;

             parent = tree->tree[parent].parent;

             tree->tree[i].c.start--;

         }

    }

    return;

}

int main()

{

    Hufftree* mytree;

    char ch[8] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

    int j, i, m = 8;

    char x;

    double w[8] = { 0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10 };

    mytree = create(m, w);

    Huffcode(mytree, m);

    for (i = 0; i <  m ; i++)

    {

         x = change(mytree->tree[i].c.bit[mytree->tree[i].c.start + 1]);//不知道为什么每一个编码的首字符刚好相反,我也没有找出哪里有问题

         printf("%c的霍夫曼编码为:%c", exchange(mytree->tree[i].ww),x);

         for (j = mytree->tree[i].c.start+2; j <= m-1; j++)

             printf("%c", mytree->tree[i].c.bit[j]);

         printf("\n");

    }

}

#include<stdio.h>

#include<stdlib.h>

#include<limits.h>

#define MaxNode 100

typedef struct {

    int start;

    char bit[MaxNode];//

}HuffCode;

typedef struct huff {

    double ww;

    int lchild, rchild, parent;

    HuffCode c;

}Huffmannode;

typedef struct hu {

    int root;

    Huffmannode tree[MaxNode];

}Hufftree;

char change(char x)

{

    if (x == '1')

         return '0';

    return '1';

}

char exchange(double w)//对应关系

{

    if (w == 0.07)

         return 'a';

    else if (w == 0.19)

         return 'b';

    else if (w == 0.02)

         return 'c';

    else if (w == 0.06)

         return 'd';

    else if (w == 0.32)

         return 'e';

    else if (w == 0.03)

         return 'f';

    else if (w == 0.21)

         return 'g';

    else if (w == 0.1)

         return 'h';

    else

         return ' ';

}

Hufftree* create(int m, double* w)

{

    int i, j, x1, x2;

    double m1, m2;

    Hufftree* pht;

    pht = (Hufftree*)malloc(sizeof(Hufftree));

    if (pht == NULL)

    {

         printf("out of space!");

         return 0;

    }

    for (i = 0; i < 2 * m - 1; i++)//置初态

    {

         pht->tree[i].lchild = -1;

         pht->tree[i].rchild = -1;

         pht->tree[i].parent = -1;

         if (i < m)

             pht->tree[i].ww = w[i];

         else

             pht->tree[i].ww = -1;

    }

    for (i = 0; i < m - 1; i++)//构造一个内部节点

    {

         m1 = m2 = INT_MAX;

         x1 = x2 = -1;

         for (j = 0; j < m + i; j++)//找出两个权值最小的无父节点

             if (pht->tree[j].ww < m1 && pht->tree[j].parent == -1)

             {

                  m2 = m1;

                  x2 = x1;

                  x1 = j;

                  m1 = pht->tree[j].ww;

             }

             else if (pht->tree[j].ww < m2 && pht->tree[j].parent == -1)

             {

                  m2 = pht->tree[j].ww;

                  x2 = j;

             }

         pht->tree[x1].parent = m + i;

         pht->tree[x2].parent = m + i;

         pht->tree[m + i].ww = m1 + m2;

         pht->tree[m + i].lchild = x1;

         pht->tree[m + i].rchild = x2;

    }

    pht->root = m + i - 1;

    return pht;

}

void Huffcode(Hufftree* tree, int m)

{

    int i ;

    int child, parent;

    for (i = 0; i <  m ; i++)//对叶子结点编码

    {

         tree->tree[i].c.start = m - 1;

         child = i;

         parent = tree->tree[i].parent;

         while (parent != -1)//从下遍历到根结点

         {

             if (tree->tree[parent].lchild == child)

                  tree->tree[i].c.bit[tree->tree[i].c.start] = '0';

             else

                  tree->tree[i].c.bit[tree->tree[i].c.start] = '1';

             child = parent;

             parent = tree->tree[parent].parent;

             tree->tree[i].c.start--;

         }

    }

    return;

}

int main()

{

    Hufftree* mytree;

    char ch[8] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

    int j, i, m = 8;

    char x;

    double w[8] = { 0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10 };

    mytree = create(m, w);

    Huffcode(mytree, m);

    for (i = 0; i <  m ; i++)

    {

         x = change(mytree->tree[i].c.bit[mytree->tree[i].c.start + 1]);//不知道为什么每一个编码的首字符刚好相反,我也没有找出哪里有问题

         printf("%c的霍夫曼编码为:%c", exchange(mytree->tree[i].ww),x);

         for (j = mytree->tree[i].c.start+2; j <= m-1; j++)

             printf("%c", mytree->tree[i].c.bit[j]);

         printf("\n");

    }

}

 

 

四、【实验结果】

 

 

五、【实验心得】

我理解到哈夫曼树是一种最优二叉树,假设叶子结点个数为m,那么树的结点就会有2*m-个,哈夫曼树用于最优解编码。可以通过数组来存储,因为已知结点个数,由于哈夫曼树的构造是由下往上的,所以结点的结构需要额外parent来指向父母结点。哈夫曼编码就是将频率多的数的编码短一点,频率少的编码长一点,从而来减少总码长。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值