Huffman树
来源:互联网 发布:数据库 锁定客户群 编辑:程序博客网 时间:2024/05/20 01:35
#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef struct node{ int data; struct node *lchild,*rchild,*next;}hufnode;typedef hufnode *linkhuf;linkhuf insert(linkhuf root,linkhuf s){ linkhuf p1,p2;//p1查找位置的前驱,p2查找插入位置 if(root==NULL)root=s;//空树 else { p1=NULL; p2=root; while(p2&&p2->data<s->data) { p1=p2; p2=p2->next; } s->next=p2; if(p1==NULL)root=s; else p1->next=s; } return root;}//创建哈夫曼树void creathuffman(linkhuf *root){ linkhuf s,r1,rr; while(*root&&(*root)->next) {//每次从链表上摘下两个节点作为新生成的左右子树 r1=*root; rr=(*root)->next; *root=rr->next; s=(linkhuf)malloc(sizeof(hufnode)); s->next=NULL; s->data=r1->data+rr->data; s->lchild=r1; s->rchild=rr; r1->next=rr->next=NULL; *root=insert(*root,s); }}//创建链表linkhuf creat(){ int x; linkhuf t=NULL,p,pre; scanf("%d",&x); if(x>0){ p=t=(hufnode*)malloc(sizeof(hufnode)); p->data=x; t->next=t->lchild=t->rchild=NULL; } while(scanf("%d",&x)!=EOF,x>0) { pre=(hufnode*)malloc(sizeof(hufnode)); pre->next=pre->lchild=pre->rchild=NULL; pre->data=x; p->next=pre; p=pre; } return t;}void inorder(linkhuf t){ if(t) { printf("%d\n",t->data); inorder(t->lchild); inorder(t->rchild); }}int main(){ linkhuf t=creat(); creathuffman(&t); inorder(t);}
0 0
- Huffman树&&Huffman编码
- Huffman树
- Huffman 树
- huffman树
- huffman 树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- Huffman树
- huffman 树
- Huffman树
- Huffman树
- Huffman树
- 自学成才网站
- Android shape
- 思科CCNA第三学期第六章答案
- 数组指针malloc空间问题
- HDU 1517 A Multiplication Game 巴什博弈
- Huffman树
- thinkphp对事务回滚的案例
- Java 泛型详解
- 【转载】 java多线程总结
- Awk 入门介绍
- 自学JAVA之Cannot make a static reference to the non-static method分析
- poj训练计划
- slf4j如何打印java异常堆栈信息throwable对象
- Java 正则表达式中量词的贪婪型,勉强型和占有型讲解