Huffman树编码 DFS实现

通过写这个编码和huffman树的构建,让我对huffman tree 有了更深的理解:

1:首先huffman是经过n-1次迭代之后生成的树,所以如果开始的节点有n个的话,那麽生成之后的的树的节点个数是2*n-1

2:编码的话可以用DFS处理;

3:树的节点新加了一个parents值,让我学到了一种新的写法。//在挑选最小值的时候需要

#include<iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
struct element
{
    char c=NULL;
    int weight;        // 权值域
    int lchild, rchild, parent;  // 该结点的左、右、双亲结点在数组中的下标
    string code;
};
// 选取权值最小的两个结点
void selectMin(element a[],int n, int &s1, int &s2)
{   int i,j;
    for ( i = 0; i < n; i++)
    {
        if (a[i].parent == -1)// 初始化s1,s1的双亲为-1
        {
            s1 = i;
            break;
        }
    }
    for (; i < n; i++)// s1为权值最小的下标
    {
        if (a[i].parent == -1 && a[s1].weight > a[i].weight)
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值