数据结构(哈夫曼树的建立代码实现)

#include<iostream>
#include<string>
#include<iomanip>
#include<Windows.h>
using namespace std;
typedef struct
{
    char data;
    int weight;
    int parent, lchild, rchild;
}HTNode, * HuffmanTree;
typedef char** HuffmanCode;
char Chdata[500];
void Select(HuffmanTree HT, int len, int& s1, int& s2)
{
    int i, min1 = 0x3f3f3f3f, min2 = 0x3f3f3f3f;//先赋予最大值
    for (i = 1; i <= len; i++)
    {
        if (HT[i].weight < min1 && HT[i].parent == 0)
        {
            min1 = HT[i].weight;
            s1 = i;
        }
    }
    int temp = HT[s1].weight;//将原值存放起来,然后先赋予最大值,防止s1被重复选择
    HT[s1].weight = 0x3f3f3f3f;
    for (i = 1; i <= len; i++)
    {
        if (HT[i].weight < min2 && HT[i].parent == 0)
        {
            min2 = HT[i].weight;
            s2 = i;
        }
    }
    HT[s1].weight = temp;//恢复原来的值
}

void CreatHuffmanTree(HuffmanTree& HT, int n)
{

    //构造赫夫曼树HT
    int m, s1, s2, i;
    if (n <= 1) return;
    m = 2 * n - 1;
    HT = new HTNode[m + 1];          //0号单元未用,所以需要动态分配m+1个单元,HT[m]表示根结点   
    for (i = 1; i <= m; ++i)            //将1~m号单元中的双亲、左孩子,右孩子的下标都初始化为0   
    {
        HT[i].parent = 0;  HT[i].lchild = 0;  HT[i].rchild = 0;
    }

    cout << "请输入待编码字符:" << endl;
    for (i = 0; i < n; i++)
    {
        cin >> HT[i].data;
        Chdata[i] = HT[i].data;
    }

    cout << "请依次输入叶子结点的权值:\n";
    for (i = 1; i <= n; ++i)            //输入前n个单元中叶子结点的权值  
        cin >> HT[i].weight;
    /*――――――――――初始化工作结束,下面开始创建赫夫曼树――――――――――*/
    for (i = n + 1; i <= m; ++i)
    {      //通过n-1次的选择、删除、合并来创建赫夫曼树
        Select(HT, i - 1, s1, s2);
        //在HT[k](1≤k≤i-1)中选择两个其双亲域为0且权值最小的结点,
        // 并返回它们在HT中的序号s1和s2
        HT[s1].parent = i;
        HT[s2].parent = i;
        //得到新结点i,从森林中删除s1,s2,将s1和s2的双亲域由0改为i
        HT[i].lchild = s1;
        HT[i].rchild = s2;                            //s1,s2分别作为i的左右孩子
        HT[i].weight = HT[s1].weight + HT[s2].weight;     //i 的权值为左右孩子权值之和
    }                                                //for
}                                                    // CreatHuffmanTree

void CreatHuffmanCode(HuffmanTree HT, HuffmanCode& HC, int n)
{
    //从叶子到根逆向求每个字符的赫夫曼编码,存储在编码表HC中
    int i, start, c, f;
    HC = new char* [n + 1];                         //分配n个字符编码的头指针矢量
    char* cd = new char[n];                            //分配临时存放编码的动态数组空间
    cd[n - 1] = '\0'; //编码结束符

    for (i = 1; i <= n; ++i)
    {                                                  //逐个字符求赫夫曼编码
        start = n - 1;                              //start开始时指向最后,即编码结束符位置
        c = i;
        f = HT[i].parent;                             //f指向结点c的双亲结点
        while (f != 0)
        {                                              //从叶子结点开始向上回溯,直到根结点
            --start;                                  //回溯一次start向前指一个位置
            if (HT[f].lchild == c)
                cd[start] = '0';                        //结点c是f的左孩子,则生成代码0
            else
                cd[start] = '1';                         //结点c是f的右孩子,则生成代码1
            c = f;
            f = HT[f].parent;                         //继续向上回溯
        
        }                                              //求出第i个字符的编码   
        
        HC[i] = new char[n - start];                     // 为第i 个字符编码分配空间
        strcpy(HC[i], &cd[start]);                    //将求得的编码从临时空间cd复制到HC的当前行中
    }
    free; cd;                                        //释放临时空间
}


void Print(HuffmanTree HT, int n, HuffmanCode HC)
{
    int m = 2 * n - 1;
    int temp=0,WPL=0,multiply=0;;
    cout << "in index weight parent lChild rChild code road" << endl;
    cout << left;    // 左对齐输出
    for (int i = 1; i <= m; ++i) {
        if (Chdata[i - 1]) {
            cout << setw(5) << HT[i - 1].data << " ";
        }
        else {
            cout << setw(5) << " " << " ";
        }
        cout << setw(5) << i << " ";
        cout << setw(5) << HT[i].weight << " ";
        cout << setw(5) << HT[i].parent << " ";
        cout << setw(6) << HT[i].lchild << " ";
        cout << setw(6) << HT[i].rchild << " ";
        if (Chdata[i - 1]) {
            for (; i <= sizeof(HC) + 1;)
            {
                cout << setw(5) << HC[i] << strlen(HC[i])<<endl;
                break;
            }
        }
        else { cout << setw(5) << " " << " " << endl; }
        
        multiply=HT[i].weight*strlen(HC[i]);
        cout<<"第"<<i<<"个结点的带权路径长度为:"<<multiply<<endl;

    }
}

void main()
{
    system("title 1121张三");
    system("date /T");                                                                    //显示当前日期
    system("TIME /T");
    system("color E4");
    HuffmanTree HT;
    HuffmanCode HC;
    int n;
    cout << "\t\t\t--------------------------------" << endl;
    cout << "\t\t\t$            哈夫曼树          $" << endl;
    cout << "\t\t\t--------------------------------" << endl;
    cout << "\t\t\t$学号:123456789   姓名:张三  $" << endl;
    cout << "\t\t\t--------------------------------" << endl;
    cout << "\t\t\t$            欢迎测试          $" << endl;
    cout << "\t\t\t--------------------------------" << endl;
    cout << "\t\t\t$------------测试开始----------$" << endl;
    cout << "\t\t\t--------------------------------" << endl;
    cout << "请输入叶子结点的个数:\n";
    cin >> n;
    CreatHuffmanTree(HT, n);
    cout << "哈夫曼树建立完毕!\n";
    CreatHuffmanCode(HT, HC, n);
    Print(HT, n, HC);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值