哈夫曼编码链表+树实现
来源:互联网 发布:淘宝网咖啡杯 编辑:程序博客网 时间:2024/06/03 06:53
/**
输入格式是
输入每个字符的次数
然后依次输出每个字符的编码
3a 10b 5c 84a 1b 1c 1d 1
a:0b:10c:11a:00b:01c:10d:11
*/
#include<stdio.h>#include<stdlib.h>typedef struct tree{ char ch; int data; struct tree *left,*right,*next;} Tree,*PTree;int order[256];long long huf[256][2];void visit(PTree T,int x,long long hf){ if(T) { visit(T->left,x+1,hf<<1); visit(T->right,x+1,(hf<<1)+1); if(T->left==0&&T->right==0) { huf[T->ch][0]=x; huf[T->ch][1]=hf; } free(T); }}int main(){ int n,d,sl,i,j; char s[5],str[256]; while(~scanf("%d",&n)) { PTree temp,L=NULL,pre; sl = 0; while(n--) { scanf("%s%d",s,&d); str[sl++]=*s; if(L==NULL) { L=(PTree)malloc(sizeof(Tree)); L->ch = *s; L->data = d; L->next = L->left = L->right = NULL; } else { pre = temp = L; while(temp&&(temp->data<d||(temp->data==d&&temp->ch<*s))) { pre = temp; temp = temp->next; } if(temp==NULL) { pre->next=(PTree)malloc(sizeof(Tree)); pre->next->ch = *s; pre->next->data = d; pre->next->left = pre->next->right = NULL; pre->next->next = NULL; } else { if(pre==L&&(L->data>d||(L->data==d&&L->ch>*s))) { temp = L; L=(PTree)malloc(sizeof(Tree)); L->ch = *s; L->data = d; L->left = L->right = NULL; L->next = temp; } else if(pre->data>d||(pre->data==d&&pre->ch>*s)) { temp = pre->next; pre=(PTree)malloc(sizeof(Tree)); pre->ch = *s; pre->data = d; pre->left = pre->right = NULL; pre->next = temp; } else { temp = pre->next; pre->next=(PTree)malloc(sizeof(Tree)); pre->next->ch = *s; pre->next->data = d; pre->next->left = pre->next->right = NULL; pre->next->next = temp; } } } } PTree root=L; while(L&&L->next) { root = (PTree)malloc(sizeof(Tree)); root->left = L; root->right = L->next; root->data = L->data + L->next->data; root->ch = L->ch; L = L->next->next; pre = temp = L; while(temp&&(temp->data<root->data||(temp->data==root->data&&temp->ch<root->ch))) { pre = temp; temp = temp->next; } if(L==NULL) { L = root; break; } else if(temp==NULL) { pre->next=root; pre->next->next = NULL; } else { if(pre==L&&(L->data>root->data||(L->data==root->data&&L->ch>root->ch))) { temp = L; L = root; L->next = temp; } else if(pre->data>root->data||(pre->data==root->data&&pre->ch>root->ch)) { temp = pre->next; pre=root; pre->next = temp; } else { temp = pre->next; pre->next=root; pre->next->next = temp; } } root=L; } visit(root,0,0); for(i=0; i<sl; i++) { printf("%c:",str[i]); for(j=huf[str[i]][0]-1; j>=0; j--) { printf("%d",(huf[str[i]][1]>>j)&1); } printf("\n"); } } return 0;}
阅读全文
0 0
- 哈夫曼编码链表+树实现
- 二叉树应用之哈夫曼编码(二叉链表实现)
- 编码实现链表逆序
- C++实现哈夫曼编码--构建哈夫曼编码树
- 哈夫曼编码树的C++实现
- 范式哈夫曼编码实现
- 哈夫曼编码C++实现
- VB6实现哈夫曼编码
- 哈夫曼编码算法实现
- 哈夫曼编码的实现
- 哈夫曼编码解码实现
- c 实现哈夫曼编码
- python实现哈夫曼编码
- 哈夫曼编码实现
- 哈夫曼编码 C++实现
- 哈夫曼编码实现 C++
- C 实现 哈夫曼编码
- java实现哈夫曼编码
- Gensim使用小解
- 《Spring3实战》摘要(5-2)Spring中使用Hibernate
- Redis 的 Sentinel 文档
- theharvester
- springMVC接收json格式
- 哈夫曼编码链表+树实现
- Hibernate框架中的缓存技术详解
- HDOJ HDU 1015 Safecracker
- 物联网笔记-----------Django之template分页的实现
- 装饰器高级版
- 涂鸦两面的问题
- STM32 IAP在线升级详解
- 开始学习SRS的第一步
- springMVC配置说一二--------过滤器