Huffman编码与解码C++程序

//*******************************huffman.h**********************//*****************叶子结点为n的哈夫曼树共有2n-1个结点**********#ifndef HUFFMAN_H#define HUFFMAN_Hclass HuffmanNode {public: char info; //结点
摘要由CSDN通过智能技术生成
//*******************************huffman.h**********************
//*****************叶子结点为n的哈夫曼树共有2n-1个结点**********

#ifndef HUFFMAN_H
#define HUFFMAN_H

class HuffmanNode {
public:
    char info; //结点信息
    double weight; //结点权值
    int parent, lchild, rchild; //父亲结点,左右孩子结点
    HuffmanNode() {
        parent=lchild=rchild=-1;
    }
    HuffmanNode(const char &data, const double &wt, const int &pa=-1, const int &lch=-1, const int &rch=-1) {
        info=data;
        weight=wt;
        parent=pa;
        lchild=lch;
        rchild=rch;
    }
}; //class HuffmanNode end
        
class HuffmanTree {
public:
    HuffmanTree(const int &s=100) {
        maxSize=(s>100?s:100); 
        arrayTree=new HuffmanNode[maxSize];
        currentSize=0;
        codeArray=0;
    }
    ~HuffmanTree() {
        delete[] arrayTree;
        if (codeArray!=0)
            delete[] codeArray;
    }
    void run(const char*, const char*, const char*);

private:
    HuffmanNode *arrayTree; //哈夫曼结点数组
    int maxSize; //数组最大值
    int currentSize; //当前数组大小
    void insert(const char&, const double&); //插入结点
    void createHuffmanTree(); //创建哈夫曼树
    void createHuffmanCode(); //创建哈夫曼编码
    int findPosition(const char &) const; //返回字符在arrayTree[]中的位置
    int getLongestCodeLength() const; //返回编码系统中长度最大的编码的位置
    int isEqual(const char *s) const; //判断s是否存在于编码系统中,若存在则返回s在编码系统中的位置,否则返回-1
    void print(); //打印huffman编码
private:
    class Code { //HuffmanTree的私有类,编码类
    public:
        Code():length(10) { ptr=new char[length]; }
        ~Code() { delete
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是 Python 代码实现: ```python from heapq import heapify, heappush, heappop from collections import defaultdict from typing import Dict, List, Tuple def huffman_encode(s: str) -> Tuple[str, Dict[str, str]]: freq = defaultdict(int) for c in s: freq[c] += 1 # 构建 Huffman 树 heap = [[wt, [sym, ""]] for sym, wt in freq.items()] heapify(heap) while len(heap) > 1: lo = heappop(heap) hi = heappop(heap) for pair in lo[1:]: pair[1] = "0" + pair[1] for pair in hi[1:]: pair[1] = "1" + pair[1] heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:]) # 生成编码表 code = dict(heap[0][1:]) # 对字符串进行编码 encoded = "".join(code[ch] for ch in s) return encoded, code def huffman_decode(encoded: str, code: Dict[str, str]) -> str: # 反转编码表 decode = {v: k for k, v in code.items()} # 对编码字符串进行解码 current_code = "" decoded = "" for bit in encoded: current_code += bit if current_code in decode: decoded += decode[current_code] current_code = "" return decoded # 测试 s = "hello world" encoded, code = huffman_encode(s) print(f"Encoded: {encoded}") print(f"Code: {code}") decoded = huffman_decode(encoded, code) print(f"Decoded: {decoded}") ``` 输出: ``` Encoded: 1101100011000110101011110100111000110111010001101111100011001000 Code: {'h': '1100', 'e': '10', 'l': '011', 'o': '111', ' ': '00', 'w': '11010', 'r': '100', 'd': '010', 'l': '011'} Decoded: hello world ``` 注意,如果输入字符串中有重复的字符,需要将它们区分开来,否则解码时会出现歧义。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值