数据结构实验之二叉树六:哈夫曼编码
来源:互联网 发布:淘宝卖什么虚拟物品好 编辑:程序博客网 时间:2024/05/22 08:13
Problem Description
字符的编码方式有多种,除了大家熟悉的ASCII编码,哈夫曼编码(Huffman Coding)也是一种编码方式,它是可变字长编码。该方法完全依据字符出现概率来构造出平均长度最短的编码,称之为最优编码。哈夫曼编码常被用于数据文件压缩中,其压缩率通常在20%~90%之间。你的任务是对从键盘输入的一个字符串求出它的ASCII编码长度和哈夫曼编码长度的比值。
Input
输入数据有多组,每组数据一行,表示要编码的字符串。
Output
对应字符的ASCII编码长度la,huffman编码长度lh和la/lh的值(保留一位小数),数据之间以空格间隔。
Example Input
AAAAABCDTHE_CAT_IN_THE_HAT
Example Output
64 13 4.9144 51 2.8
#include <stdio.h>#include <stdlib.h>#include <string.h>#define SUM 10typedef int Elemtype;typedef struct BiTnode{ Elemtype data; struct BiTnode *lchild, *rchild, *next, *parent;} BiTnode, *BiTree;typedef struct{ BiTree front; BiTree rear; int len;} Queue;Queue q;int wl;void Sort(int a[], int n);void Init();void EnQueue(int x);BiTree OutQueue();void Insert(BiTree root);BiTree Create();int Path(BiTree p);void WPL(BiTree p);int main(){ int n, i, length; char s[10010]; while(~scanf("%s", s)) { n = 0; wl = 0; int la; int b[150] = {0}; int a[150]; length = strlen(s); la = 8 * length; for(i = 0; i < length; i++) b[s[i]]++; for(i = 0; i < 150; i++) if(b[i] != 0) a[n++] = b[i]; Sort(a, n); Init(); for(i = 0;i < n; i++) EnQueue(a[i]); BiTree root; root = Create(); WPL(root); printf("%d %d %.1lf\n", la, wl, 1.0*la/wl); } return 0;}void Sort(int a[], int n){ int i, j; for(i = 0; i < n-1; i++) for(j = 0; j < n - i - 1; j++) { if(a[j] > a[j+1]) { int t = a[j]; a[j] = a[j+1]; a[j+1] = t; } }}void Init(){ q.front = (BiTree)malloc(sizeof(BiTnode)); q.front -> next = NULL; q.rear = q.front; q.len = 0;}void EnQueue(int x){ BiTree p; p = (BiTree)malloc(sizeof(BiTnode)); p -> next = NULL; p -> rchild = NULL; p -> lchild = NULL; p -> parent = NULL; p -> data = x; q.rear -> next = p; q.rear = p; q.len++;}BiTree OutQueue(){ if(q.len == 1) { BiTree p; p = q.front -> next; q.front = NULL; q.rear = q.front; q.len--; return p; } else { BiTree p, t; t = p = q.front -> next; p = p -> next; q.front -> next = p; q.len--; return t; }}void Insert(BiTree root){ BiTree p, t; p = q.front -> next; t = q.front; while(root -> data > p -> data) { p = p -> next; t = t -> next; if(!p) break; } if(p) { root -> next = p; t -> next = root; } else { q.rear -> next = root; q.rear = root; } q.len++;}BiTree Create(){ if(q.len > 1) { BiTree root, t1, t2; root=(BiTree)malloc(sizeof(BiTnode)); root -> next = NULL; root -> parent = NULL; t1 = OutQueue(); t2 = OutQueue(); root -> lchild = t1; root -> rchild = t2; root -> data = t1 -> data + t2 -> data; t1 -> parent = root; t2 -> parent = root; if(q.len == 0) return root; else { Insert(root); Create(); } }}int Path(BiTree p){ int l = 0; int x = p -> data; while(p -> parent) { p = p -> parent; l++; } return l * x;}void WPL(BiTree p){ if(!p -> lchild && !p -> rchild) { int e = Path(p); wl += e; } else { if(p -> lchild) WPL(p -> lchild); if(p -> rchild) WPL(p -> rchild); }}
阅读全文
0 0
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- sdutoj 3345 数据结构实验之二叉树六:哈夫曼编码
- SDUT3345数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- SDUT 3345 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- SDUTACM 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- 数据结构实验之二叉树六:哈夫曼编码
- vue.js精讲02
- 和好几天又开战!AMD显卡老大跳槽,要帮英特尔进军GPU
- Lab 5:面对面需求获取与原型设计
- 提高效率,eclipse上你可能不知道的技巧
- 医疗行业多层级复杂网络环境下的消息传输(远程会诊)架构与实现
- 数据结构实验之二叉树六:哈夫曼编码
- 上传
- 嵌入式 双向链表实现视频预录(hi3518c)
- Java并发编程:volatile关键字解析
- NOIP2016 换教室
- 千万级数据库分页优化
- ${param.id}的使用
- 多线程的安全隐患
- 《JAVA与模式》之观察者模式