哈夫曼树遍历求WPL和哈夫曼编码C语言--For初学者

这篇文章使用纯c来写的,实现了生成哈夫曼树、求WPL和生成哈夫曼编码的应用,思路是,先定义一个结构体如下

typedef struct node
{
    int weight;
    struct node *lchild,*rchild;
    struct node *next;
}HuffNode;

先生成单链表(用next链接),还存储输入的权值(weight),单链表无头结点(因为最后要转化成二叉树,所以不用头结点),而且每一次节点的插入都使单链表保持递增的状态(为生成哈夫曼树做准备)。
计算WPL用到简单的递归,如下

int CalcWPL(HuffNode *huff,int wpl)
{
    HuffNode *p = huff;
    if(!p)
        return 0;
    else
    {
        if(!p->rchild&&!p->rchild)
            return p -> weight*wpl;
        else
            return CalcWPL(p->lchild,wpl+1) + CalcWPL(p->rchild,wpl+1);
    }
}

在计算WPL的代码上,稍作改动,就可以生成哈夫曼编码,如下

void HuffCode(HuffNode *huff,int len)
{
    HuffNode *p = huff;
    static int a[MAX];
    int i;
    if(p)
    {
        if(!p->lchild && !p->rchild)
        {
            printf("%d:",p->weight);

            for(i = 0;i< len;i++)
                printf("%d",a[i]);
            printf("\n");
        }
        else
        {
            a[len] = 0;
            HuffCode(p->lchild,len+1);
            a[len] = 1;
            HuffCode(p->rchild,len+1);
        }
    }
}

左孩子(lchild)和右孩子(rchild)是为生成二叉树准备的。
无头有序单链表的生成(就是链接权值),代码如下

void CreatUpLink(HuffNode **huff,int n)
{
    HuffNode *pre,*p ,*s,*head = (*huff);
    int x;
    scanf("%d",&x);
    s = malloc(sizeof(HuffNode));
    s -> lchild = NULL;
    s -> rchild = NULL;
    s -> weight = x;
    s -> next = NULL;
    head = s;
    n--;
    while(n--)
    {
        pre = p = head;
        scanf("%d",&x);
        s = malloc(sizeof(HuffNode));
        s -> lchild = NULL;
        s -> rchild = NULL;
        s -> weight = x;
        if(p->weight > x)
        {
            s -> next = p;
            head = s;
        }
        else
        {
            while(p&&p->weight < x)
                {
                    pre = p;
                    p = p->next;
                }
                s -> next = pre->next;
                pre -> next = s;
        }
    }
    (*huff) = head;
}

生成单链表是生成二叉树的基础,代码如下

void HuffTree(HuffNode **huff)
{
    HuffNode *pn1 ,*pn2 ,*p = *huff,*s = NULL,*q = NULL,*pre = NULL,*pr = NULL;
    if(!p->next)
        return ;
    while(p)
    {
        pn1 = p ;
        pn2 = pn1 -> next;
        pre = q = pn2 -> next ;
        s = malloc(sizeof(HuffNode));
        s -> lchild = pn1;
        s -> rchild = pn2;
        s -> weight = pn1 -> weight + pn2 -> weight;
        s ->next = NULL;
      if(!pn2 -> next)
      {
          s -> next = NULL;
          pn2 -> next = s;
          (*huff) = s;
          return;
      }
        if(pn2 -> next -> weight > s->weight)
        {
            s -> next = pn2 -> next;
            pn2 ->next = s;
        }
        else
        {
            while(q && q->weight < s -> weight)
                {
                    pre = q;
                    q = q->next;
                }
                s -> next = pre->next;
                pre -> next = s;
        }
        pr = pn2;
        p = pn2 -> next;
    }
    (*huff) = pr;
}

以上是重要的模块,主函数没有写,读者可以自行编写,我提供的代码仅供参考。
下面是全部的代码,编辑环境是Code::Blocks。

#include <stdio.h>
#include <stdlib.h>

#define MAX 20
//201612-4
/* * * * * * * * * * * * * * * *
//测试数据
4
1 3 5 7
29

8
7 19 2 6 32 3 21 10
261
* * * * * * * * * * * * * * * */
typedef struct node
{
    int weight;
    struct node *lchild,*rchild;
    struct node *next;
}HuffNode;


void HuffTree(HuffNode **);
void CreatUpLink(HuffNode **,int);
void InitLink(HuffNode**);
void PrintNode(HuffNode**);
void PrePrint(HuffNode *);
int CalcWPL(HuffNode *,int);
void HuffCode(HuffNode *,int);
int main()
{
    int wpl = 0;
    HuffNode *huff;
    int n;
    scanf("%d",&n);
    CreatUpLink(&huff,n);
    PrintNode(&huff);
    HuffTree(&huff);
    printf("\nPreorder :\n");
    PrePrint(huff);
    wpl = CalcWPL(huff,0);
    printf("\n\nWPL = %d\n",wpl);
    printf("\nHuffCode:\n");
    HuffCode(huff,0);
    return 0;
}
int CalcWPL(HuffNode *huff,int wpl)
{
    HuffNode *p = huff;
    if(!p)
        return 0;
    else
    {
        if(!p->rchild&&!p->rchild)
            return p -> weight*wpl;
        else
            return CalcWPL(p->lchild,wpl+1) + CalcWPL(p->rchild,wpl+1);
    }
}
void HuffCode(HuffNode *huff,int len)
{
    HuffNode *p = huff;
    static int a[MAX];
    int i;
    if(p)
    {
        if(!p->lchild && !p->rchild)
        {
            printf("%d:",p->weight);

            for(i = 0;i< len;i++)
                printf("%d",a[i]);
            printf("\n");
        }
        else
        {
            a[len] = 0;
            HuffCode(p->lchild,len+1);
            a[len] = 1;
            HuffCode(p->rchild,len+1);
        }
    }
}
void PrePrint(HuffNode *huff)
{
    HuffNode *p = huff;
    if(p)
    {
        printf("%d\t",p->weight);
        PrePrint(p->lchild);
        PrePrint(p->rchild);
    }
}
void PrintNode(HuffNode **huff)
{
    HuffNode *p = (*huff);
    while(p)
    {
        printf("%d\t",p->weight);
        p = p->next;
    }
    putchar('\n');
}
void InitLink(HuffNode **huff)
{
    (*huff) = NULL;
}
void CreatUpLink(HuffNode **huff,int n)
{
    HuffNode *pre,*p ,*s,*head = (*huff);
    int x;
    scanf("%d",&x);
    s = malloc(sizeof(HuffNode));
    s -> lchild = NULL;
    s -> rchild = NULL;
    s -> weight = x;
    s -> next = NULL;
    head = s;
    n--;
    while(n--)
    {
        pre = p = head;
        scanf("%d",&x);
        s = malloc(sizeof(HuffNode));
        s -> lchild = NULL;
        s -> rchild = NULL;
        s -> weight = x;
        if(p->weight > x)
        {
            s -> next = p;
            head = s;
        }
        else
        {
            while(p&&p->weight < x)
                {
                    pre = p;
                    p = p->next;
                }
                s -> next = pre->next;
                pre -> next = s;
        }
    }
    (*huff) = head;
}
void HuffTree(HuffNode **huff)
{
    HuffNode *pn1 ,*pn2 ,*p = *huff,*s = NULL,*q = NULL,*pre = NULL,*pr = NULL;
    if(!p->next)
        return ;
    while(p)
    {
        pn1 = p ;
        pn2 = pn1 -> next;
        pre = q = pn2 -> next ;
        s = malloc(sizeof(HuffNode));
        s -> lchild = pn1;
        s -> rchild = pn2;
        s -> weight = pn1 -> weight + pn2 -> weight;
        s ->next = NULL;
      if(!pn2 -> next)
      {
          s -> next = NULL;
          pn2 -> next = s;
          (*huff) = s;
          return;
      }
        if(pn2 -> next -> weight > s->weight)
        {
            s -> next = pn2 -> next;
            pn2 ->next = s;
        }
        else
        {
            while(q && q->weight < s -> weight)
                {
                    pre = q;
                    q = q->next;
                }
                s -> next = pre->next;
                pre -> next = s;
        }
        pr = pn2;
        p = pn2 -> next;
    }
    (*huff) = pr;
}

运行结果图不附上了,读者可自行运算,本文只是用C语言来提供一种方法。
作者能力有限,难免有bug,欢迎大神评论指教。

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是一种带权路径长度最短的,通常用于数据压缩。其构造方法如下: 1. 将所有权值看作一个森林,每个森林都是一个节点,节点的权值为森林中所有节点的权值之和。 2. 从森林中选取两个权值最小的节点组成一棵新,其权值为这两个节点的权值之和。 3. 将新插入森林中,并从森林中删除这两个节点。 4. 重复步骤2和步骤3,直到森林中只剩下一个,即为哈。 先序遍可以按照以下方式实现: 1. 先输出当前节点的权值。 2. 如果当前节点有左子,则递归遍左子。 3. 如果当前节点有右子,则递归遍右子。 计算WPL(带权路径长度)的值可以按照以下方式实现: 1. 对于每个叶子节点,将其权值乘以其到根节点的路径长度,得到该叶子节点的带权路径长度。 2. 将所有叶子节点的带权路径长度相加,得到WPL的值。 以下是打印哈并释放哈的C语言代码: ``` #include <stdio.h> #include <stdlib.h> // 哈结点 typedef struct HuffmanNode { int weight; // 权值 struct HuffmanNode *leftChild; // 左孩子 struct HuffmanNode *rightChild; // 右孩子 } HuffmanNode, *HuffmanTree; // 构建哈 void createHuffmanTree(HuffmanTree *tree, int weights[], int n) { if (n <= 0) { *tree = NULL; return; } // 创建森林 HuffmanNode **forest = (HuffmanNode **)malloc(sizeof(HuffmanNode *) * n); for (int i = 0; i < n; i++) { forest[i] = (HuffmanNode *)malloc(sizeof(HuffmanNode)); forest[i]->weight = weights[i]; forest[i]->leftChild = NULL; forest[i]->rightChild = NULL; } while (n > 1) { // 找到最小的两个结点 int min1 = 0, min2 = 1; if (forest[min1]->weight > forest[min2]->weight) { min1 = 1; min2 = 0; } for (int i = 2; i < n; i++) { if (forest[i]->weight < forest[min1]->weight) { min2 = min1; min1 = i; } else if (forest[i]->weight < forest[min2]->weight) { min2 = i; } } // 合并两个结点 HuffmanNode *newNode = (HuffmanNode *)malloc(sizeof(HuffmanNode)); newNode->weight = forest[min1]->weight + forest[min2]->weight; newNode->leftChild = forest[min1]; newNode->rightChild = forest[min2]; // 将新结点插入森林 forest[min1] = newNode; for (int i = min2; i < n - 1; i++) { forest[i] = forest[i + 1]; } n--; } *tree = forest; free(forest); } // 先序遍 void preorderTraversal(HuffmanTree tree) { if (tree != NULL) { printf("%d ", tree->weight); preorderTraversal(tree->leftChild); preorderTraversal(tree->rightChild); } } // 计算WPL的值 int getWPL(HuffmanTree tree, int depth) { if (tree == NULL) { return 0; } else if (tree->leftChild == NULL && tree->rightChild == NULL) { return tree->weight * depth; } else { return getWPL(tree->leftChild, depth + 1) + getWPL(tree->rightChild, depth + 1); } } // 打印哈 void printHuffmanTree(HuffmanTree tree, int depth) { if (tree != NULL) { for (int i = 0; i < depth; i++) { printf(" "); } printf("%d\n", tree->weight); printHuffmanTree(tree->leftChild, depth + 1); printHuffmanTree(tree->rightChild, depth + 1); } } // 释放哈 void destroyHuffmanTree(HuffmanTree tree) { if (tree != NULL) { destroyHuffmanTree(tree->leftChild); destroyHuffmanTree(tree->rightChild); free(tree); } } int main() { int weights[] = {3, 4, 5, 6, 7, 8}; int n = sizeof(weights) / sizeof(int); HuffmanTree tree; createHuffmanTree(&tree, weights, n); printf("先序遍:"); preorderTraversal(tree); printf("\n"); int wpl = getWPL(tree, 0); printf("WPL的值:%d\n", wpl); printf("打印哈:\n"); printHuffmanTree(tree, 0); destroyHuffmanTree(tree); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值