当前位置: 首页 > news >正文

哈夫曼树代码

#include stdio.h #include stdlib.h // 修正使用标准库头文件替换malloc.h #define MAX_CODE_LENGTH 100 // 新增定义最大编码长度 #define INT_MAX 9999 typedef struct HuffmanNode { char character; int parent; int direction; // 0-左子树/1-右子树 int weight; } HuffmanNode; // 初始化哈夫曼树 HuffmanNode* huffmanTreeInit(const char characters[], const int weights[], int num) { if (num 0) { printf(Error: Invalid number of characters!\n); return NULL; } const int totalNodes num * 2 - 1; HuffmanNode* tree (HuffmanNode*)malloc(sizeof(HuffmanNode) * totalNodes); if (!tree) { printf(Error: Memory allocation failed!\n); return NULL; } // 初始化叶子节点 for (int i 0; i num; i) { tree[i] (HuffmanNode){ .character characters[i], .weight weights[i], .parent -1, .direction -1 }; } // 初始化内部节点 for (int i num; i totalNodes; i) { tree[i] (HuffmanNode){ .character x, .weight -1, .parent -1, .direction -1 }; } return tree; } // 打印哈夫曼树 void huffmanTreePrint(const HuffmanNode* tree, int numLeaves) { const int totalNodes numLeaves * 2 - 1; printf(%-8s%-8s%-8s%-8s\n, Char, Parent, Dir, Weight); for (int i 0; i totalNodes; i) { printf(%-8c%-8d%-8d%-8d\n, tree[i].character, tree[i].parent, tree[i].direction, tree[i].weight); } } // 查找最小权重节点改进版 int findMinWeightIndex(const HuffmanNode* tree, int currentSize) { int minWeight INT_MAX; int minIndex -1; for (int i 0; i currentSize; i) { if (tree[i].parent -1 tree[i].weight minWeight tree[i].weight ! -1) { minWeight tree[i].weight; minIndex i; } } return minIndex; } // 构建哈夫曼树重构核心逻辑 void buildHuffmanTree(HuffmanNode* tree, int numLeaves) { if (!tree || numLeaves 1) return; int currentSize numLeaves; const int totalNodes numLeaves * 2 - 1; for (int pos numLeaves; pos totalNodes; pos) { // 查找两个最小节点 int min1 findMinWeightIndex(tree, currentSize); if (min1 -1) break; tree[min1].parent pos; tree[min1].direction 0; // 标记为左子树 int min2 findMinWeightIndex(tree, currentSize); if (min2 -1) break; tree[min2].parent pos; tree[min2].direction 1; // 标记为右子树 // 创建父节点 tree[pos].weight tree[min1].weight tree[min2].weight; tree[pos].parent -1; tree[pos].direction -1; currentSize; } } // 查找编码改进内存管理 void findEncoding(const HuffmanNode* tree, char target, int numLeaves) { int code[MAX_CODE_LENGTH] {0}; int codeLength 0; // 查找目标字符位置 int index -1; for (int i 0; i numLeaves; i) { if (tree[i].character target) { index i; break; } } if (index -1) { printf(Character %c not found!\n, target); return; } // 回溯生成编码 int current index; while (tree[current].parent ! -1) { code[codeLength] tree[current].direction; current tree[current].parent; } // 打印结果 printf(%c encoding: , target); for (int i codeLength-1; i 0; --i) { printf(%d, code[i]); } printf(\n); } // 测试用例改进变量管理 void huffmanTest() { const int numChars 5; const char chars[] {a, b, c, d, e}; const int weights[] {52, 8, 15, 23, 2}; printf(\n Huffman Tree Test \n); HuffmanNode* tree huffmanTreeInit(chars, weights, numChars); if (!tree) return; printf(\nInitial tree:\n); huffmanTreePrint(tree, numChars); buildHuffmanTree(tree, numChars); printf(\nAfter construction:\n); huffmanTreePrint(tree, numChars); printf(\nEncodings:\n); for (int i 0; i numChars; i) { findEncoding(tree, chars[i], numChars); } free(tree); } int main() { huffmanTest(); return 0; }通过画图更好理解代码
http://www.rkmt.cn/news/1395058.html

相关文章:

  • 2026年AI论文平台深度评测:6款工具全流程得分排名
  • 拉美海外仓实测评测:合规时效成本及平台适配全维度对比 - 互联网科技品牌测评
  • NVIDIA Profile Inspector完全指南:解锁显卡隐藏性能的7个关键技术步骤
  • Nacos 2.x 源码深度解析 (六):三级缓存体系 —— 降级兜底与故障自愈机制
  • 在Eclipse IDE中开始使用GitHub Copilot的ABAP支持
  • 【飞机】自主无人机飞行稳定和轨迹跟踪【含Matlab源码 15569期】
  • 用Python解码新年决心的时间序列规律
  • 从‘年龄与疾病’到数据分析入门:用OpenJudge题目教你玩转计数与百分比
  • 武汉名包回收哪家靠谱高价?正规透明、高价省心全攻略 - 奢侈品回收测评
  • LlamaParse:5步掌握AI文档解析与智能检索的终极指南
  • 微商云仓新零售商城开发介绍
  • 这份榜单够用!盘点2026年全网顶尖的的降AIGC平台
  • 1.58-bit的AI突围:面壁智能×华为昇腾如何改写大模型训练规则
  • 地平线最新提出HorizonDrive:自动驾驶世界模型新范式、实现分钟级自回归生成
  • Python平方运算的7种实现与工程选型指南
  • 深度拆解 OpenCoWork:一个本地多智能体桌面平台的架构设计与实现
  • CXL协议与GPU存储扩展技术解析
  • 在Windows、Linux和macOS上免费畅玩Switch游戏:Ryujinx模拟器完整指南
  • 掌握FanControl风扇曲线配置:三步告别电脑噪音与高温困扰
  • Python异常处理实战:从语法错误到生产级容错
  • WinThumbsPreloader:重新定义Windows资源管理效率的智能革命
  • QT开发小技巧:让你的QLineEdit提示文字更醒目(调整颜色、字体大小)并集成实用按钮
  • 告别TeamViewer!免费开源的VNC Viewer 6.20保姆级安装与连接教程
  • LinkSwift:让网盘下载变得轻松简单的八大网盘直链获取神器
  • 从重复劳动到智能助手:如何用Auto.js实现Android自动化革命
  • 观测 TaoToken 在多模型间自动路由的故障转移表现
  • 观测到接入 Taotoken 后代码助手响应延迟显著降低
  • 终极免费IDM激活完整指南:三步实现永久下载加速
  • 东芝IH电饭煲温度保险丝熔断自救指南:从故障诊断到元件替换全记录
  • WinThumbsPreloader-V2:Windows图片浏览的革命性加速方案,告别文件夹卡顿的终极工具