import heapq
import os

# 节点类，用于构建霍夫曼树
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 get_char_frequency(filename):
    """
    统计文件中每个字符的频率
    :param filename: 输入文件名
    :return: 字符频率字典
    """
    char_frequency = {}
    with open(filename, 'r', encoding='utf-8') as file:
        for line in file:
            for char in line:
                if char in char_frequency:
                    char_frequency[char] += 1
                else:
                    char_frequency[char] = 1
    return char_frequency

# 构建霍夫曼树
def build_huffman_tree(char_frequency):
    """
    构建霍夫曼树
    :param char_frequency: 字符频率字典
    :return: 霍夫曼树根节点
    """
    priority_queue = []
    for char, freq in char_frequency.items():
        node = Node(char, freq)
        heapq.heappush(priority_queue, node)

    while len(priority_queue) > 1:
        left_node = heapq.heappop(priority_queue)
        right_node = heapq.heappop(priority_queue)
        parent_node = Node(None, left_node.freq + right_node.freq)
        parent_node.left = left_node
        parent_node.right = right_node
        heapq.heappush(priority_queue, parent_node)

    return priority_queue[0]

# 生成字符编码表
def generate_huffman_codes(root):
    """
    生成字符的霍夫曼编码表
    :param root: 霍夫曼树根节点
    :return: 字符的霍夫曼编码表
    """
    huffman_codes = {}
    current_code = ""

    def traverse(node, code):
        nonlocal current_code
        if node.char is not None:
            huffman_codes[node.char] = code
        else:
            traverse(node.left, code + "0")
            traverse(node.right, code + "1")

    traverse(root, current_code)
    return huffman_codes

# 将文件内容编码为霍夫曼编码
def encode_file(input_file, output_file, huffman_codes):
    """
    将文件内容编码为霍夫曼编码，并写入输出文件
    :param input_file: 输入文件名
    :param output_file: 输出文件名
    :param huffman_codes: 字符的霍夫曼编码表
    """
    with open(input_file, 'r', encoding='utf-8') as input_f, open(output_file, 'wb') as output_f:
        bit_string = ""
        for line in input_f:
            for char in line:
                bit_string += huffman_codes[char]

        padding_length = 8 - len(bit_string) % 8
        bit_string += "0" * padding_length  # 添加填充位

        # 将二进制字符串转换为字节，并写入输出文件
        for i in range(0, len(bit_string), 8):
            byte = bit_string[i:i+8]
            output_f.write(bytes([int(byte, 2)]))

        # 写入填充长度
        output_f.write(bytes([padding_length]))

        # 获取压缩后的文件大小
        output_size = os.path.getsize(output_file)
        print(f"压缩后的文件大小：{output_size}字节")

# 压缩文件
def compress_file(input_file, output_file):
    """
    压缩文件
    :param input_file: 输入文件名
    :param output_file: 输出文件名
    """
    char_frequency = get_char_frequency(input_file)
    huffman_tree_root = build_huffman_tree(char_frequency)
    huffman_codes = generate_huffman_codes(huffman_tree_root)
    encode_file(input_file, output_file, huffman_codes)

# 示例用法
input_file = 'input.txt'
output_file = 'output.txt'
compress_file(input_file,output_file)

# 获取输入文件大小
input_size = os.path.getsize(input_file)
print(f"输入文件大小：{input_size}字节")帮我把这段python文件翻译成C语言代码