数据结构第十周 :(带权路径长度 + 哈夫曼编码)

带权路径长度

【问题描述】 输入一串正整数,正整数之间用空格键分开,请建立一棵哈夫曼树,以输入的数字作为叶子节点,求这棵哈夫曼树的带权路径长度。

【输入形式】 首先输入正整数的个数n,然后是对应的n个正整数,正整数个数不超过10个

【输出形式】 输出创建的哈夫曼树的带权路径总长度

【样例输入】

5

4 5 6 7 8

【样例输出】

69

#include<iostream>
#define Leave 10 //叶子节点的数量
#define Node 2 * Leave - 1 //总结点的个数
using namespace std;

typedef struct //用静态三叉链表(数组)来实现哈夫曼的定义
{
    int weight;
    int parent;
    int LChild;
    int RChild;
}HtNode, HuffmanTree[Node + 1]; //零号节点不用

int sum = 0;

void SelectMin(HuffmanTree ht, int limit, int * s1, int * s2) //实参传的是地址,形参要用指针
{
    *s1 = *s2 = 0;
    int Min1, Min2;
    Min1 = Min2 = 99999;
    int i = 0;
    for(i = 1; i <= limit; i ++)
    {
        if(ht[i].parent != 0) continue;
        else
        {
            if(ht[i].weight < Min1)
            {
                Min2 = Min1; //将Min1 的值赋给Min1
                *s2 = *s1;
                Min1 = ht[i].weight; //将新找到的最小值赋给Min1
                *s1 = i;
            }
            else if(ht[i].weight < Min2)
            {
                Min2 = ht[i].weight;
                *s2 = i;
            }
        }
    }
}

void CrtHuffmanTree(HuffmanTree ht, int w[Leave], int n) //哈夫曼树的创建
{
    int i = 1; //初始化哈夫曼树
    for(i = 1; i <= n; i ++)
    {
        ht[i].weight = w[i];
        ht[i].parent = 0;
        ht[i].LChild = 0;
        ht[i].RChild = 0;
    }
    int m = 2 * n - 1;
    for(i = n + 1; i <= m; i ++)
    {
        ht[i].weight = 0;
        ht[i].parent = 0;
        ht[i].LChild = 0;
        ht[i].RChild = 0;    }

    for(i = n + 1; i <= m; i ++) //创建非叶节点,建哈夫曼树
    {
        int s1, s2;
        SelectMin(ht, i - 1, &s1, &s2); // 在ht[1]~ht[i - 1]内找两个parent为0且权重最小的节点复制给s1, s2
        ht[i].weight = ht[s1].weight + ht[s2].weight;
        ht[s1].parent = i;
        ht[s2].parent = i;
        ht[i].LChild = s1;
        ht[i].RChild = s2;
    }
}

void PreOrder(HuffmanTree ht, int pos, int h) //求叶子结点的高度并乘权重
{
    if(ht[pos].LChild != 0)
        PreOrder(ht, ht[pos].LChild, h + 1);
    if(ht[pos].LChild == 0 && ht[pos].RChild == 0)
    {
        sum += ht[pos].weight * h;
    }
    if(ht[pos].RChild != 0)
        PreOrder(ht, ht[pos].RChild, h + 1);
}
int main()
{
    int w[Leave];
    int count = 0;
    cin>>count;
    int i = 1; //从一号位置开始放
    int num = count;
    while(count --)
    {
        cin>>w[i ++];
    }
    HuffmanTree ht;
    CrtHuffmanTree(ht, w, num);
    PreOrder(ht,2 * num - 1, 0);
    cout<<sum;
    return 0;
}

哈夫曼编码

【问题描述】读入n个字符所对应的权值,自底向上构造一棵哈夫曼树,自顶向下生成每一个字符对应的哈夫曼编码,并依次输出。另,求解某字符串的哈夫曼编码,求解某01序列的译码。

【输入形式】输入的第一行包含一个正整数n,表示共有n个字符需要编码。其中n不超过100。第二行中有n个用空格隔开的正整数,分别表示n个字符的权值,依次按照abcd…的默认顺序给出。然后是某字符串和某01序列。

【输出形式】前n行,每行一个字符串,表示对应字符的哈夫曼编码。然后是某字符串的哈夫曼编码,某01序列的译码。

【注意】保证每次左子树比右子树的权值小;如出现相同权值的,则先出现的在左子树,即下标小的在左子树。

【样例输入】

8
5 29 7 8 14 23 3 11

aabchg

00011110111111001

【样例输出】

0001
10
1110
1111
110
01
0000
001

000100011011100010000

acdef

#include<iostream>
#define Leave 100
#define Node 2 * Leave - 1
using namespace std;

typedef struct //定义静态三叉链表来定义哈夫曼树
{
    int weight;
    int parent;
    int LChild;
    int RChild;
}HtNode, HuffmanTree[Node + 1]; //从一号位置开始存放,所以多一位空间

void Search(HuffmanTree ht, int limit, int *s1, int *s2) //s1,s2保存的是最小值的下标
{
    *s1 = *s2 = 0;
    int Min1, Min2;
    Min1 = Min2 = 9999;
    int i = 0;
    for(i = 1; i <= limit; i ++)
    {
        if(ht[i].parent != 0) continue;
        if(ht[i].weight < Min1)
        {
            Min2 = Min1;
            *s2 = *s1;
            Min1 = ht[i].weight;
            *s1 = i;
        }
        else if(ht[i].weight < Min2)
        {
            Min2 = ht[i].weight;
            *s2 = i;
        }
    }
}

void CrtHuffmanTree(HuffmanTree ht, int w[Leave], int leave)
{
    int i = 0;
    for(i = 1; i <= Leave; i ++)
    {
        ht[i].weight = w[i];
        ht[i].LChild = 0;
        ht[i].RChild = 0;
        ht[i].parent = 0;
    }

    int node = 2 * leave - 1;
    for(i = leave + 1; i <= node; i ++)
    {
        ht[i].weight = 0;
        ht[i].parent = 0;
        ht[i].LChild = 0;
        ht[i].RChild = 0;
    }

    for(i = leave + 1; i <= node; i ++)
    {
        int s1, s2;
        s1 = s2 = 0;
        Search(ht, i - 1, &s1, &s2); //是在创建好的哈夫曼数节点上找最小值,当前位置之前的节点全在选择范围
        ht[i].weight = ht[s1].weight + ht[s2].weight;
        ht[i].LChild = s1;
        ht[i].RChild = s2;
        ht[s1].parent = i;
        ht[s2].parent = i;
    }
}

void Encode(HuffmanTree ht, int limit, int ques)
{
    int num[100];
    int count = 0;
    int p = ht[ques].parent;
    while(p != 0)
    {
        if(ques == ht[p].LChild)
        {
            num[count ++] = 0;
        }
        else if(ques == ht[p].RChild)
        {
            num[count ++] = 1;
        }
        ques = p;
        p = ht[ques].parent;
    }
    while(count --)
    {
        cout<<num[count];
    }
}

void Decode(HuffmanTree ht, int limit)
{
    char code[100];
    cin>>code;
    int pos = 0;
    int next = limit;
    while(code[pos] != '\0')
    {
        if(code[pos] == '0')
            next = ht[next].LChild;
        else if(code[pos] == '1')
            next = ht[next].RChild;
        if(ht[next].LChild == 0 && ht[next].RChild == 0)
        {
            char c = 'a';
            c = c + next - 1;
            cout<<c;
            next = limit;
        }
        pos ++;
    }
}

int main()
{
    int leave;
    cin>>leave;
    int w[leave + 1];
    int i = 0;
    for(i = 1; i < leave + 1; i ++)
    {
        cin>>w[i]; //从一号位置开始存放各个字母的权重
    }
    HuffmanTree ht; // 定义一个结构体数组变量
    CrtHuffmanTree(ht, w, leave);
    for(i = 1; i <= leave; i ++)
    {
        Encode(ht, 2 * leave - 1, i);
        cout<<endl;
    }
    char s[20];
    cin>>s;
    for(i = 0; s[i] != '\0'; i ++)
    {
        int ques = s[i] - 97 + 1;
        Encode(ht, 2 * leave - 1, ques);
    }
    cout<<endl;
    Decode(ht, 2 * leave - 1);
    return 0;
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只可爱的小猴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值