树——数据结构
来源:互联网 发布:哲学入门 知乎 编辑:程序博客网 时间:2024/05/15 23:53
二叉树的链式储存及其操作,Huffman树及Huffman编码;
#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct Node //二叉树的链式储存方式;{ char data; struct Node*LChild; struct Node*RChild;} BitNode,*BiTree;void Init(BiTree *b) //初始化;{ *b=(BiTree)malloc(sizeof(BitNode)); (*b)->LChild=NULL; (*b)->RChild=NULL;}void PreOrder(BiTree root) //先序遍历二叉树;{ if(root!=NULL) { printf("%c ",root->data); PreOrder(root->LChild); PreOrder(root->RChild); }}void InOrder(BiTree root) //中序遍历二叉树;{ if(root!=NULL) { InOrder(root->LChild); printf("%c ",root->data); InOrder(root->RChild); }}void PostOrder(BiTree root) //后序遍历二叉树;{ if(root!=NULL) { PostOrder(root->LChild); PostOrder(root->RChild); printf("%c ",root->data); }}void CreateBiTree(BiTree *bt) //建立二叉树;{ char ch; ch=getchar(); if(ch=='.') *bt=NULL; else { *bt=(BiTree)malloc(sizeof(BitNode)); (*bt)->data=ch; CreateBiTree(&((*bt)->LChild)); CreateBiTree(&((*bt)->RChild)); }}typedef struct{ BiTree elem[50]; int top;} SeqStack;void InitStack(SeqStack *S){ S->top=-1;}void Push(SeqStack *s,BiTree x){ if(s->top==49) return; s->top++; s->elem[s->top]=x;}void Pop(SeqStack *s,BiTree *x){ if(s->top==-1) return; else { *x=s->elem[s->top]; s->top--; }}void LayerOrder(BiTree bt) //广搜;{ SeqStack Q; InitStack(&Q); BiTree p; if(bt==NULL) return; Push(&Q,bt); while(Q.top!=-1) { Pop(&Q,&p); printf("%c ",p->data); if(p->LChild) Push(&Q,p->LChild); if(p->RChild) Push(&Q,p->RChild); }}typedef struct //Huffman树;{ int weight; int parent; int LChild; int RChild;} HTNode,HuffmanTree[100];void select(HuffmanTree ht,int n,int *s1,int *s2) //提取最小两个值;{ *s1=0; *s2=0; for(int i=1; i<=n; i++) { if(ht[i].parent==0) { if(ht[*s1].weight>ht[i].weight) *s1=i; if(ht[*s1].weight<ht[*s2].weight) { int t=*s1; *s1=*s2; *s2=t; } } }}void CrtHuffmanTree(HuffmanTree ht,int w[],int n) //建立Huffman树;{ for(int i=1; i<=n; i++) { ht[i].weight=w[i]; ht[i].LChild=ht[i].parent=ht[i].RChild=0; } int m=2*n-1; for(int i=n+1; i<=m; i++) ht[i].LChild=ht[i].parent=ht[i].RChild=ht[i].weight=0; for(int i=n+1; i<=m; i++) { int s1,s2; select(ht,i-1,&s1,&s2); ht[i].weight=ht[s1].weight+ht[s2].weight; ht[s1].parent=i; ht[s2].parent=i; ht[i].LChild=s1; ht[i].RChild=s2; }}typedef char * HuffmanCode[100];void CrtHuffmanCode(HuffmanTree ht,HuffmanCode hc,int n) //求Huffman编码;{ char *cd; int start,c,p; cd=(char*)malloc(n*sizeof(char)); cd[n-1]='\0'; for(int i=1; i<=n; i++) { start=n-1; c=i; p=ht[i].parent; while(p!=0) { --start; if(ht[p].LChild==c) cd[start]='0'; else cd[start]='1'; c=p; p=ht[p].parent; } hc[i]=(char*)malloc((n-start)*sizeof(char)); strcpy(hc[i],&cd[start]); } free(cd);}int LeafCount=0;void leaf(BiTree root) //求叶子节点;{ if(root!=NULL) { leaf(root->LChild); leaf(root->RChild); if(root->LChild==NULL&&root->RChild==NULL) LeafCount++; }}int depth=0;void PreTreeDepth(BiTree bt,int h) //求深度;{ if(bt!=NULL) { if(h>depth) depth=h; PreTreeDepth(bt->LChild,h+1); PreTreeDepth(bt->RChild,h+1); }}int main(){ BiTree b1; Init(&b1); CreateBiTree(&b1); PreOrder(b1); printf("\n"); InOrder(b1); printf("\n"); PostOrder(b1); printf("\n"); LeafCount=0; leaf(b1); printf("%d\n",LeafCount); PreTreeDepth(b1,0); printf("%d\n",depth); LayerOrder(b1); printf("\n"); HuffmanTree ht; int w[11]= {0,1,2,3,4,5,6,7,8,9,10}; ht[0].weight=0x3f3f3f3f; CrtHuffmanTree(ht,w,10); for(int i=11; i<=19; i++) printf("%d ",ht[i].weight); printf("\n"); HuffmanCode hc; CrtHuffmanCode(ht,hc,10); for(int i=1; i<=10; i++) printf("%s ",hc[i]); printf("\n"); return 0;}
阅读全文
0 0
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 数据结构——树
- 树——数据结构
- 数据结构——树
- 数据结构 —— 树
- 数据结构——树
- 数据结构——树
- trie树—数据结构
- 数据结构—树
- 数据结构—AVL树
- Hibernate HQL简单详解
- js---在前端开发中,如何通过正则表达式去除字符串string首尾的空格,JavaScript正则去除首尾空格
- Linux下文件系统的容量扩充
- 第九篇 linux文件与目录管理(下)
- LeetCod题目:Container With Most Water
- 树——数据结构
- 传统复制在线切换到GTID模式
- python升级2.7.13版本进入环境出现大量信息
- Caffe深度学习进阶之Cifar-10分类任务(下)
- Semi-Global Matching(SGM)算法原文理解
- iOS查看屏幕帧数工具--YYFPSLabel
- 洛谷 P3384 【模板】树链剖分
- 快速排序
- less-Namespaces and Accessors(名称空间和访问器)