展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > 如何使用Python和C语言实现哈夫曼编码?

如何使用Python和C语言实现哈夫曼编码?

1.C语言实现1.1代码说明a创建双向链表:在创建哈夫曼树的过程中,需要不断对结点进行更改和删除,所以选用双向链表的结构更容易'''C#include#include#include//哈夫曼树结构体,数据域存储字符及其权重typedefstructnode{charc;intweight;structnode*lchild,*rchild;}Huffman,*Tree;//双向链表结构体,数据域存储哈夫曼树结点typedefstructlist{Treeroot;structli

本文目录

    1.C语言实现

    1.1代码说明

    a  创建双向链表:

    在创建哈夫曼树的过程中,需要不断对结点进行更改和删除,所以选用双向链表的结构更容易

    '''C
    #include 
    #include 
    #include 
     
     
    //哈夫曼树结构体,数据域存储字符及其权重
    typedef struct node
    {
        char c;
        int weight;
        struct node *lchild, *rchild;
    }Huffman, *Tree;
     
     
    //双向链表结构体,数据域存储哈夫曼树结点
    typedef struct list
    {
        Tree root;
        struct list *pre;
        struct list *next;
    }List, *pList;
     
     
    //创建双向链表,返回头结点指针
    pList creatList()
    {
        pList head = (pList)malloc(sizeof(List));
     
        pList temp1 = head;
        pList temp2 = (pList)malloc(sizeof(List));
        temp1->pre = NULL;
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'a';
        temp1->root->weight = 22;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
        
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp2 = (pList)malloc(sizeof(List));
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'b';
        temp1->root->weight = 5;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
        
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp2 = (pList)malloc(sizeof(List));
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'c';
        temp1->root->weight = 38;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp2 = (pList)malloc(sizeof(List));
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'd';
        temp1->root->weight = 9;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp2 = (pList)malloc(sizeof(List));
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'e';
        temp1->root->weight = 44;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp2 = (pList)malloc(sizeof(List));
        temp1->next = temp2;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'f';
        temp1->root->weight = 12;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
     
        temp2->pre = temp1;
        temp1 = temp2;
        temp1->next = NULL;
        temp1->root = (Tree)malloc(sizeof(Huffman));
        temp1->root->c = 'g';
        temp1->root->weight = 65;
        temp1->root->lchild = NULL;
        temp1->root->rchild = NULL;
     
        return head;                          
    }

    b创建栈结构:

    解码过程需要用到两个栈,一个用来存放树结点,一个用来存放码0和1

    '''C
    #define STACK_INIT_SIZE 100   //栈初始开辟空间大小
    #define STACK_INCREMENT 10    //栈追加空间大小
     
    //字符栈结构体,存放编码'0'和'1'
    typedef struct {
        char *base;
        char *top;
        int size;
    }charStack;
     
     
    //栈初始化
    charStack charStackInit()
    {
        charStack s;
        s.base = (char *)malloc(sizeof(char)*STACK_INIT_SIZE);
        s.top = s.base;
        s.size = STACK_INIT_SIZE;
        return s;
    }
     
    //入栈
    void charPush(charStack *s, char e)
    {
        if(s->top - s->base >= s->size)
        {
            s->size += STACK_INCREMENT;
            s->base = realloc(s->base, sizeof(char)*s->size);
        }
        *s->top = e;
        s->top++;
    }
     
    //出栈
    char charPop(charStack *s)
    {
        if(s->top != s->base)
        {
            s->top--;
            return *s->top;
        }
        return -1;
    }
     
    //得到栈顶元素,但不出栈
    char charGetTop(charStack *s)
    {
        s->top--;
        char temp = *s->top;
        s->top++;
        return temp;
    }
     
    //栈结构体,存放哈夫曼树结点
    typedef struct 
    {
        Huffman *base;
        Huffman *top;
        int size;
    }BiStack;
     
    //栈初始化
    BiStack stackInit()
    {
        BiStack s;
        s.base = (Huffman *)malloc(sizeof(Huffman)*STACK_INIT_SIZE);
        s.top = s.base;
        s.size =STACK_INIT_SIZE;
        return s;
    }
     
    //入栈
    void push(BiStack *s, Huffman e)
    {
        if(s->top - s->base >= s->size)
        {
            s->size += STACK_INCREMENT;
            s->base = (Huffman *)realloc(s->base, sizeof(Huffman)*s->size);
        }
        *s->top = e;
        s->top++;
    }
     
    //出栈
    Huffman pop(BiStack *s)
    {
        Huffman temp;
        s->top--;
        temp = *s->top;
        return temp;
    }
     
    //得到栈顶元素,但不出栈
    Huffman getTop(BiStack s)
    {
        Huffman temp;
        s.top--;
        temp = *s.top;
        return temp;
    }
     
    char stack[7][10];             //记录a~g的编码
    //遍历栈,得到字符c的编码
    void traverseStack(charStack s, char c)
    {
        int index = c - 'a'; 
        int i = 0;
        while(s.base != s.top)
        {
            stack[index][i] = *s.base;
            i++;
            s.base++;
        }
    }

    c 创建哈夫曼树:

    '''C
    //通过双向链表创建哈夫曼树,返回根结点指针
    Tree creatHuffman(pList head)
    {
        pList list1 = NULL;
        pList list2 = NULL;
        pList index = NULL;
        Tree root = NULL;
        while(head->next != NULL)   //链表只剩一个结点时循环结束,此结点数据域即为哈夫曼树的根结点
        {
            list1 = head;
            list2 = head->next;
            index = list2->next;
            root = (Tree)malloc(sizeof(Huffman));
            while(index != NULL)    //找到链表中权重最小的两个结点list1,list2
            {
                if(list1->root->weight > index->root->weight || list2->root->weight > index->root->weight)
                {
                    if(list1->root->weight > list2->root->weight) list1 = index;
                    else list2 = index;
                }
                index = index->next;
            }
            //list1和list2设为新结点的左右孩子
            if(list2->root->weight > list1->root->weight)
            {
                root->lchild = list1->root;
                root->rchild = list2->root;
            }
            else
            {
                root->lchild = list2->root;
                root->rchild = list1->root;
            }
            //新结点字符统一设为空格,权重设为list1与list2权重之和
            root->c = ' ';
            root->weight = list1->root->weight + list2->root->weight;
            //list1数据域替换成新结点,并删除list2
            list1->root = root;
            list2->pre->next = list2->next;
            if(list2->next != NULL)
                list2->next->pre = list2->pre;    
        }
        return head->root;
    }

    d编码:

    '''C
    char stack[7][10];             //记录a~g的编码
    //遍历栈,得到字符c的编码
    void traverseStack(charStack s, char c)
    {
        int index = c - 'a'; 
        int i = 0;
        while(s.base != s.top)
        {
            stack[index][i] = *s.base;
            i++;
            s.base++;
        }
    }
     
     
    //通过哈夫曼树编码
    void encodeHuffman(Tree T)
    {  
        BiStack bs = stackInit();
        charStack cs = charStackInit();
        Huffman root = *T;  
        Tree temp = NULL;
        push(&bs, root);      //根结点入栈
        while(bs.top != bs.base)      //栈空表示遍历结束
        {
            root = getTop(bs);
            temp = root.lchild;       //先访问左孩子
            while(temp != NULL)       //左孩子不为空
            {
                //将结点左孩子设为空,代表已访问其左孩子
                root.lchild = NULL;
                pop(&bs);            
                push(&bs, root);
                //左孩子入栈
                root = *temp;
                temp = root.lchild;
                push(&bs, root);
                //'0'入字符栈
                charPush(&cs, '0');
            }
            temp = root.rchild;     //后访问右孩子     
            while(temp == NULL)     //右孩子为空,代表左右孩子均已访问,结点可以出栈 
            {
                //结点出栈
                root = pop(&bs);
                //寻到叶子结点,可以得到结点中字符的编码
                if(root.c != ' ')
                    traverseStack(cs, root.c);
                charPop(&cs);       //字符栈出栈
                if(bs.top == bs.base) break;    //根结点出栈,遍历结束
                //查看上一级结点是否访问完左右孩子  
                root = getTop(bs);
                temp = root.rchild;           
            }
            if(bs.top != bs.base)
            {
                //将结点右孩子设为空,代表已访问其右孩子
                root.rchild = NULL;       
                pop(&bs);
                push(&bs, root);
                //右孩子入栈
                root = *temp;      
                push(&bs, root);
                //'1'入字符栈
                charPush(&cs, '1');
            }    
        }
    }

    e解码:

    '''C
    char decode[100];   //记录解码得到的字符串
    //通过哈夫曼树解码
    void decodeHuffman(Tree T, char *code)
    {
        int cnt = 0;
        Tree root;
        while(*code != '\0')                  //01编码字符串读完,解码结束
        {
            root = T;
            while(root->lchild != NULL)       //找到叶子结点
            {
                if(*code != '\0')
                {
                    if(*code == '0')
                        root = root->lchild;
                    else
                        root = root->rchild;
                    code++;
                }
                else break;
            }
            decode[cnt] = root->c;             //叶子结点存放的字符即为解码得到的字符
            cnt++;
        }
    }

    f主函数:

    '''C
    void main()
    {
        pList pl = creatList();
        printf("字符的权重如下\n");
        for(pList l = pl; l->next != NULL; l = l->next)
            printf("字符%c的权重是 %d\n", l->root->c, l->root->weight);
        Tree T = creatHuffman(pl);
        encodeHuffman(T);
        printf("\n\n字符编码结果如下\n");
        for(int i = 0; i < 7; i++)
            printf("%c : %s\n", i+'a', stack[i]);
        char code[100];
        printf("\n\n请输入编码:\n");
        scanf("%s", code);
        printf("解码结果如下:\n");
        decodeHuffman(T, code);
        printf("%s\n", decode);
        printf("\n\n");
        system("date /T");
        system("TIME /T");
        system("pause");
        exit(0); 
    }

    1.2运行结果

    如何使用Python和C语言实现哈夫曼编码?

    2.Python实现

    2.1代码说明

    a创建哈夫曼树:

    #coding=gbk
     
    import datetime
    import time
    from pip._vendor.distlib.compat import raw_input
     
    #哈夫曼树结点类
    class Huffman:
        def __init__(self, c, weight):
            self.c = c
            self.weight = weight
            self.lchild = None
            self.rchild = None
        
        #创建结点左右孩子    
        def creat(self, lchild, rchild):
            self.lchild = lchild
            self.rchild = rchild
     
    #创建列表        
    def creatList():
        list = []
        list.append(Huffman('a', 22))
        list.append(Huffman('b', 5))
        list.append(Huffman('c', 38))
        list.append(Huffman('d', 9))
        list.append(Huffman('e', 44))
        list.append(Huffman('f', 12))
        list.append(Huffman('g', 65))
        return list
     
    #通过列表创建哈夫曼树,返回树的根结点
    def creatHuffman(list):
        while len(list) > 1:               #列表只剩一个结点时循环结束,此结点即为哈夫曼树的根结点
            i = 0
            j = 1
            k = 2
            while k < len(list):           #找到列表中权重最小的两个结点list1,list2          
                if list[i].weight > list[k].weight or list[j].weight > list[k].weight:
                    if list[i].weight > list[j].weight:
                        i = k
                    else:
                        j = k
                k += 1       
            root = Huffman(' ', list[i].weight + list[j].weight) #新结点字符统一设为空格,权重设为list1与list2权重之和   
            if list[i].weight < list[j].weight:                  #list1和list2设为新结点的左右孩子
                root.creat(list[i], list[j])
            else:
                root.creat(list[j], list[i])
            #list1数据域替换成新结点,并删除list2
            list[i] = root
            list.remove(list[j])
        return list[0]

    b编码:

    #通过哈夫曼树编码
    def encodeHuffman(T):
        code = [[], [], [], [], [], [], []]
        #列表实现栈结构
        treeStack = []
        codeStack = []
        treeStack.append(T)
        while treeStack != []:        #栈空代表遍历结束
            root = treeStack[-1]
            temp = root.lchild
            while temp != None:
                #将结点左孩子设为空,代表已访问其左孩子
                root.lchild = None        
                #左孩子入栈          
                treeStack.append(temp)         
                root = temp
                temp = root.lchild
                #0入编码栈
                codeStack.append(0)
            temp = root.rchild            #后访问右孩子
            while temp == None:           #右孩子为空,代表左右孩子均已访问,结点可以出栈
                root = treeStack.pop()           #结点出栈
                #寻到叶子结点,可以得到结点中字符的编码
                if root.c != ' ':
                    codeTemp = codeStack.copy()
                    code[ord(root.c) - 97] = codeTemp     
                if treeStack == []:    #根结点出栈,遍历结束
                    break
                codeStack.pop()        #编码栈出栈
                #查看上一级结点是否访问完左右孩子
                root = treeStack[-1]
                temp = root.rchild
            if treeStack != []:
                treeStack.append(temp)     #右孩子入栈
                root.rchild = None         #将结点右孩子设为空,代表已访问其右孩子
                codeStack.append(1)        #1入编码栈
        return code

    c解码:

    #通过哈夫曼树解码
    def decodeHuffman(T, strCode):
        decode = []
        index = 0
        while index < len(strCode):        #01编码字符串读完,解码结束
            root = T
            while root.lchild != None:     #找到叶子结点
                if index < len(strCode):
                    if strCode[index] == '0':
                        root = root.lchild
                    else:
                        root = root.rchild
                    index += 1
                else:
                    break
            decode.append(root.c)           #叶子结点存放的字符即为解码得到的字符
        return decode

    d主函数:

    if __name__ == '__main__':
        list = creatList()
        print("字符的权重如下")
        for i in range(len(list)):
            print("字符{}的权重为: {}".format(chr(i+97), list[i].weight))
        T = creatHuffman(list)
        code = encodeHuffman(T)
        print("\n字符编码结果如下")
        for i in range(len(code)):
            print(chr(i+97), end=' : ')
            for j in range(len(code[i])):
                print(code[i][j], end='')
            print("")
        strCode = input("\n请输入编码:\n")
        #哈夫曼树在编码时被破坏,必须重建哈夫曼树
        list = creatList()
        T = creatHuffman(list)
        decode = decodeHuffman(T, strCode)
        print("解码结果如下:")
        for i in range(len(decode)):
            print(decode[i], end='')
        print("\n\n")
        datetime = datetime.datetime.now()
        print(datetime.strftime("%Y-%m-%d\n%H:%M:%S"))
        input("Press Enter to exit…")

    2.2运行结果

    如何使用Python和C语言实现哈夫曼编码?

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 Python C语言
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    Aperty macOS版
    Aperty macOS版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirServer Windows版
    AirServer Windows版

    AirServer Windows版可把电脑变成AirPlay、Google Cast和Miracast无线投屏接收端,适合会议演示、课堂教学及家庭大屏分享。发送设备通常无需另装客户端,并可同时接收和智能排列多个镜像会话。