C语言数据结构作业之哈夫曼树

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std; 

#define MaxSize 1024  
#define OK 1
#define ERROR 0
typedef int Status;

typedef struct wordcnt{  
    char ch;
    int cnt = 0;
}Count;

typedef struct NumCount{  
    Count count[MaxSize];
    int length = 0;
}NumCount;

typedef struct HTree{  
    char data;
    int weight;
    int parent,lchild,rchild;
}HTNode,*HuffmanTree; 

typedef struct HCode{ 
    char data;
    char* str; 
}*HuffmanCode;


Status ReadData(char *source);  
Status WordCount(char *data,NumCount *paraCnt);  
Status Show(NumCount *paraCnt);    
Status CreateHuffmanTree(HuffmanTree &HT,int length,NumCount cntarray);   
Status select(HuffmanTree HT,int top,int *s1,int *s2);   
Status CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int length);  
Status Encode(char *data,HuffmanCode HC,int length);  
Status Decode(HuffmanTree HT,int length);   

int main(int argc, char** argv) {
    char data[MaxSize];  
    NumCount Cntarray;
    ReadData(data);   
    WordCount(data,&Cntarray);   
 
    HuffmanTree tree;
    CreateHuffmanTree(tree,Cntarray.length,Cntarray);  
    HuffmanCode code;  
    CreateHuffmanCode(tree,code,Cntarray.length);  
    Encode(data,code,Cntarray.length);  
    Decode(tree,Cntarray.length);  
    cout<<"Please view the generated TXT file to check the result"<<endl; 
    return 0;
}

Status ReadData(char *source)
{
     
    ifstream infile;
    infile.open("in.txt");
    cout<<"Reading..."<<endl;
    cout<<"the input file is:"<<endl;
    infile.getline(source,MaxSize);
    cout<<source<<endl;
    infile.close();
    cout<<endl;
    return OK;
}

Status WordCount(char *data,NumCount *paraCnt)
{
    int flag; 
    int len = strlen(data);
    for(int i = 0;i < len;++i)
    {
        flag = 0;
        for(int j = 0;j < paraCnt->length;++j)
        {
            if(paraCnt->count[j].ch == data[i]) // 若已有记录,直接++ 
            {
                ++paraCnt->count[j].cnt;
                flag = 1;
                break;
            }
            
        }
        if(!flag)  
        {
            paraCnt->count[paraCnt->length].ch = data[i];
            ++paraCnt->count[paraCnt->length].cnt;
            ++paraCnt->length;
        }
    }
    return OK;
}

Status Show(NumCount *paraCnt)
{
    cout<<"the length is "<<paraCnt->length<<endl;
    for(int i = 0;i < paraCnt->length;++i)
    {
        cout<<"The character "<<paraCnt->count[i].ch<<"  appears  "<<paraCnt->count[i].cnt<<endl;
    }
    cout<<endl;
    return OK;
}

Status CreateHuffmanTree(HuffmanTree &HT,int length,NumCount cntarray)
{
    if(length <= 1) return ERROR;
    int s1,s2;
    int m = length*2-1;  
    HT = new HTNode[m+1];
    for(int i = 1;i <= m;++i)  
    {
        HT[i].parent = 0;
        HT[i].lchild = 0;
        HT[i].rchild = 0;
    }
    
    for(int i = 1;i <= length;++i) 
    {
        HT[i].data = cntarray.count[i-1].ch;
        HT[i].weight = cntarray.count[i-1].cnt;
    }
    
    for(int i = length + 1;i <= m;++i)
    {
        select(HT,i-1,&s1,&s2);   
        HT[s1].parent = i;
        HT[s2].parent = i;
        HT[i].lchild = s1;
        HT[i].rchild = s2;
        HT[i].weight = HT[s1].weight + HT[s2].weight;   
    }
    return OK;
}

Status select(HuffmanTree HT,int top,int *s1,int *s2)
{
    int min = INT_MAX;
    for(int i = 1;i <= top;++i)   
    {
        if(HT[i].weight < min && HT[i].parent == 0)
        {
            min = HT[i].weight;
            *s1 = i;
        }
    }
    
    min = INT_MAX;
    for(int i = 1;i <= top;++i)   
    {
        if(HT[i].weight < min && i != *s1 && HT[i].parent == 0)
        {
            min = HT[i].weight;
            *s2 = i;
        }
    }
    return OK;    
}

Status CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int length)
{
    HC = new HCode[length+1];
    char *cd = new char[length];  
    cd[length-1] = '\0';   
    int c,f,start;
    for(int i = 1;i <= length;++i)
    {
        start = length-1;  // 
        c = i;
        f = HT[c].parent;
        while(f != 0)
        {
            --start;   
            if(HT[f].lchild == c)
                cd[start] = '0';
            else 
                cd[start] = '1';
            c = f;
            f = HT[c].parent;
        }
        HC[i].str = new char[length-start];  
        HC[i].data = HT[i].data;
        strcpy(HC[i].str,&cd[start]);   
    }
    delete cd;
}

Status Encode(char *data,HuffmanCode HC,int length)
{
    ofstream outfile;
    outfile.open("code.txt");
    for(int i = 0;i < strlen(data);++i)   
    {
        for(int j = 1;j <= length;++j)
        {
            if(data[i] == HC[j].data)
            {
                outfile<<HC[j].str;
            }
        }
    }
    outfile.close();
    cout<<"the code txt has been written"<<endl;
    cout<<endl;
    return OK;
}

Status Decode(HuffmanTree HT,int length)
{
    char codetxt[MaxSize*length];
    ifstream infile;
    infile.open("code.txt");
    infile.getline(codetxt,MaxSize*length);
    infile.close();
    
    ofstream outfile;
       outfile.open("out.txt");
    
    int root = 2*length-1;  // 从根节点开始遍历 
    for(int i = 0;i < strlen(codetxt);++i)
    {
        if(codetxt[i] == '0') root = HT[root].lchild;  //为0表示向左遍历 
        else if(codetxt[i] == '1') root = HT[root].rchild; //为1表示向右遍历 
        if(HT[root].lchild == 0 && HT[root].rchild == 0)  // 如果已经是叶子节点,输出到输出文件中,然后重新回到根节点 
        {
            outfile<<HT[root].data;
            root = 2*length-1;
        }
    }
    outfile.close();
    cout<<"the output txt has been written"<<endl;
    cout<<endl;
    return OK;
}
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/gz040725/article/details/138870346

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以通过结构体和指针来定义和操作哈夫曼树。 首先,定义一个结构体来表示节点的信息,如下所示: ``` struct HuffmanNode { int weight; // 权重值 int parent; // 父节点位置 int left; // 左子节点位置 int right; // 右子节点位置 }; ``` 然后,定义一个指向节点的指针数组来存储所有节点: ``` #define MAX_NODE_NUM 100 // 最大节点数 HuffmanNode huffTree[MAX_NODE_NUM]; ``` 在构建哈夫曼树的过程中,需要不断地取出队列中权重值最小的两个节点,合并成一个新的父节点,并将父节点加入队列。对于每个节点,需要记录其位置以及权重值,因此可以定义一个结构体来表示节点在队列中的信息,如下所示: ``` struct QueueNode { int pos; // 节点位置 int weight; // 权重值 }; ``` 然后,定义一个数组来存储所有节点的位置: ``` int queue[MAX_NODE_NUM]; ``` 在构建哈夫曼树的过程中,可以先将所有叶子节点加入队列中: ``` int n = 0; // 节点数 for (int i = 0; i < num; i++) { huffTree[i].weight = weights[i]; huffTree[i].parent = -1; // 初始时父节点为-1,表示该节点还没有父节点 huffTree[i].left = -1; // 初始时左子节点为-1,表示该节点是叶子节点 huffTree[i].right = -1; // 初始时右子节点为-1,表示该节点是叶子节点 queue[n++] = i; } ``` 接下来,不断取出队列中权重值最小的两个节点,合并成一个新的父节点,并将父节点加入队列,直到队列中只剩下一个节点: ``` while (n > 1) { int min1 = 0, min2 = 1; if (huffTree[queue[min1]].weight > huffTree[queue[min2]].weight) { swap(min1, min2); } for (int i = 2; i < n; i++) { if (huffTree[queue[i]].weight < huffTree[queue[min1]].weight) { min2 = min1; min1 = i; } else if (huffTree[queue[i]].weight < huffTree[queue[min2]].weight) { min2 = i; } } int pos1 = queue[min1]; int pos2 = queue[min2]; int parent = n; // 新的父节点的位置为n n++; // 更新节点数 huffTree[pos1].parent = parent; huffTree[pos2].parent = parent; huffTree[parent].weight = huffTree[pos1].weight + huffTree[pos2].weight; huffTree[parent].left = pos1; huffTree[parent].right = pos2; queue[min1] = parent; queue[min2] = queue[n-1]; } ``` 最后,可以通过遍历树来获取每个字符的编码。具体来说,可以从叶子节点开始,沿着父节点不断向上遍历,记录经过的路径(0表示向左,1表示向右),直到根节点。得到的路径即为该字符的哈夫曼编码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值