Python实现哈夫曼编码(Huffman code)

如题,通过python实现哈夫曼编码,代码如下:
哈夫曼编码的思想为:在节点中每次找出两个出线频次最低的组合在一起,当迭代到最后只剩下一个节点时,该节点就为根节点,所有节点构成了huffman树,通过对树的左右孩子的路径设置为0,1,实现每一个字符的huffman编码。

import random


# 统计字符出现频次,然后每次找出两个频次最低的,合为一个节点,最后只有一个节点时,就构成了树。然后对树进行编码,并进行输出即可

# 定义哈夫曼树节点的类:
class huffmannode(object):
    def __init__(self):
        self.parent = 0
        self.weight = 0
        self.lchild = 0
        self.rchild = 0


# 原始字符集
str_ori_list = []
for i in range(0, 100):
    str_ori_list.append(random.randint(0, 10))
# 统计字符出现频次
fre_list = {}

def fre_str(str_list):
    str_unique_list = list(set(str_list))
    fre_list = dict.fromkeys(str_unique_list, 0)
    for s in str_list:
        fre_list[s] = fre_list[s] + 1
    return fre_list


# a = fre_str(str_ori_list)
# print(a)

# 在huffman树节点中选择权重最小的两个节点
def selectNode(huffmantree):
    s1 = -1
    s2 = -1
    min1 = -1
    min2 = -1
    for node in huffmantree:
        if node.parent == 0 and node.weight > 0:
            if s1 < 0 or min1 > node.weight:
                s2 = s1
                min2 = min1
                s1 = huffmantree.index(node)
                min1 = node.weight
            elif s2 < 0 or min2 > node.weight:
                s2 = huffmantree.index(node)
                min2 = node.weight

    return s1, s2


# 哈夫曼编码
def huffman_code(fre_str_dict):
    # 每个节点的权重赋值
    n = len(fre_str_dict)
    m = 2 * len(fre_str_dict) - 1
    huffman_tree = []
    for i in fre_str_dict:
        temp_huffmancode = huffmannode()
        temp_huffmancode.weight = fre_str_dict[i]
        huffman_tree.append(temp_huffmancode)
    # 选择权重最小的两个节点,建立树结构
    for i in range(n, m):
        # 在huffman树节点中选择权重最小的两个节点
        s1, s2 = selectNode(huffman_tree)
        huffman_tree[s1].parent = huffman_tree[s2].parent = i
        temp_huffmancode1 = huffmannode()
        temp_huffmancode1.lchild = s1
        temp_huffmancode1.rchild = s2
        temp_huffmancode1.weight = huffman_tree[s1].weight + huffman_tree[s2].weight
        huffman_tree.append(temp_huffmancode1)
    # 根据构建好的huffman树进行编码
    str_list = list(set(str_ori_list))
    codeing_dict = dict.fromkeys(str_list, None)
    print(codeing_dict)
    for i in range(0, n):
        temp_list = []
        node = i
        parent = huffman_tree[i].parent
        while parent != 0:
            if huffman_tree[parent].lchild == node:
                temp_list.append(0)
            else:
                temp_list.append(1)
            node = parent
            parent = huffman_tree[node].parent
        codeing_dict[str_list[i]] = list(reversed(temp_list))

    return codeing_dict

#调用统计频数函数,返回字典:{key(字符):value(频数)}
unique_dict = fre_str(str_ori_list)
#构建huffman树,并返回字符编码
code_list = huffman_code(unique_dict)


  • 7
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python实现哈夫曼编码的示例代码: ```python import heapq from collections import defaultdict # 定义节点类 class Node: def __init__(self, char, freq): self.char = char self.freq = freq self.left = None self.right = None # 定义小于运算符,用于堆排序 def __lt__(self, other): return self.freq < other.freq # 构建哈夫曼树 def build_huffman_tree(freq_dict): heap = [] for char, freq in freq_dict.items(): heapq.heappush(heap, Node(char, freq)) while len(heap) > 1: node1 = heapq.heappop(heap) node2 = heapq.heappop(heap) merged_node = Node(None, node1.freq + node2.freq) merged_node.left = node1 merged_node.right = node2 heapq.heappush(heap, merged_node) return heap[0] # 递归遍历哈夫曼树,生成编码字典 def traverse_huffman_tree(node, current_code, encoding_dict): if node is None: return if node.char is not None: encoding_dict[node.char] = current_code return traverse_huffman_tree(node.left, current_code + "0", encoding_dict) traverse_huffman_tree(node.right, current_code + "1", encoding_dict) # 对文本进行编码 def encode_text(text, encoding_dict): encoded_text = "" for char in text: encoded_text += encoding_dict[char] return encoded_text # 对编码后的文本进行解码 def decode_text(encoded_text, decoding_dict): current_code = "" decoded_text = "" for bit in encoded_text: current_code += bit if current_code in decoding_dict: decoded_text += decoding_dict[current_code] current_code = "" return decoded_text # 统计字符出现频率 def count_freq(text): freq_dict = defaultdict(int) for char in text: freq_dict[char] += 1 return freq_dict # 示例 text = "hello world" freq_dict = count_freq(text) huffman_tree = build_huffman_tree(freq_dict) encoding_dict = {} traverse_huffman_tree(huffman_tree, "", encoding_dict) encoded_text = encode_text(text, encoding_dict) decoding_dict = {v: k for k, v in encoding_dict.items()} decoded_text = decode_text(encoded_text, decoding_dict) print("编码字典:", encoding_dict) print("编码后的文本:", encoded_text) print("解码后的文本:", decoded_text) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值