数据结构中哈夫曼树的应用(C语言)

数据结构中哈夫曼树的应用(C语言)

#include#includetypedef int DataType;#define MaxValue 10000#define MaxBit 10#define MaxN 100#define N 100;int n1=0;char c[100];typedef struct Node { DataType data; struct Node *leftChild;struct Node *rightChild;}BiTreeNode;typedef struct{ int weight; int flag; int parent; int leftChild; int rightChild;}HaffNode;typedef struct{ int bit[MaxN]; int start; int weight;}Code;struct worder{ char words; /*字符*/}word[100];struct weighted{ int weighter; /*转换权值有利于文件的存储*/}weight[100] ;void Initiate(BiTreeNode **root) /*初始化二叉树*/{*root=(BiTreeNode * )malloc(sizeof(BiTreeNode)); (*root)->leftChild=NULL; (*root)->rightChild=NULL;}BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x) /*插入左子树*/{BiTreeNode *s,*t;if(curr==NULL) return NULL;t=curr->leftChild;s=(BiTreeNode *)malloc(sizeof(BiTreeNode));s->data=x;s->leftChild=t;s->rightChild=NULL;curr->leftChild=s;return curr->leftChild;}BiTreeNode *InsertRightNode(BiTreeNode *curr ,DataType x) /*插入右子树*/{BiTreeNode *s,*t;if(curr==NULL) return NULL;t=curr->rightChild;s=(BiTreeNode *)malloc(sizeof(BiTreeNode));s->data=x;s->rightChild=t;s->leftChild=NULL;curr->rightChild=s;return curr->rightChild;}void Haffman(int weigh[],int n,HaffNode haffTree[],int a[][3]) /*建立哈夫曼树*/{ int i,j,m1,m2,x1,x2; for(i=0;i<2*n-1;i++) { if(istart=n-1; cd->weight=haffTree[i].weight; child=i; parent=haffTree[child].parent; while(parent!=-1) { if(haffTree[parent].leftChild==child) cd->bit[cd->start]=0; else cd->bit[cd->start]=1; cd->start--; child=parent; parent=haffTree[child].parent; } for(j=cd->start+1;jbit[j]; haffCode[i].start=cd->start+1; haffCode[i].weight=cd->weight; }}void PrintBiTree(BiTreeNode *bt ,int n) /*将哈夫曼树转换成的二叉树进行打印*/{int i;if(bt==NULL)return;PrintBiTree(bt->rightChild,n+1);for(i=0;idata!=0&&bt->data<100){if(n>0){printf("---"); printf("%d\n\n",bt->data);}}PrintBiTree(bt->leftChild,n+1);}int search(int a[][3],int m) /*查找和a[][2]相等的权值*/{ int i=1; if(m==1) return 0; while(a[i][2]!=a[m][0]&&iMaxN) { printf("error!\n\n"); exit(0); }}void menu() /*界面*/{ printf("\n\n\n\t\t*************************************\n\n"); printf("\t\t\t1. To Code:\n\n"); /*编码*/printf("\t\t\t2. Decoding:\n\n"); /*译码*/printf("\t\t\t3. Output the huffman Tree:\n\n"); /*打印哈夫曼树*/printf("\t\t\t4. New data\n\n");printf("\t\t\t5. Quit up...\n\n");printf("\n\t\t************************************\n\n"); printf("Input you choice :\n");}void main(){ FILE *fp,*fp1,*fp2,*fp3,*fp4; int i,j; int b[100][3],m=100,n,w,k=0,weigh[100]; struct worder *d; struct weighted *p2; char h; BiTreeNode *root,*p; HaffNode *myHaffTree=(HaffNode *)malloc(sizeof(HaffNode)*(2*m+1)); Code *myHaffCode=(Code *)malloc(sizeof(Code)*m); Initiate(root); if(((fp1=fopen("hfmTree","rb"))==NULL)&&((fp=fopen("hfmTree-1","rb"))==NULL)) { loop: printf("how many number do you want input?\n"); scanf("%d",&n); if((fp=fopen("hfmTree-1","wb"))==NULL) { printf("Error,cannot open file\n" ); exit(0); } for(i=0;iweighter ; k++; } Haffman(weigh,n,myHaffTree,b); HaffmanCode(myHaffTree,n,myHaffCode); while(1) { do { clrscr(); menu(); scanf("%d",&w); }while(w!=1&&w!=2&&w!=3&&w!=4&&w!=5); switch(w) { case 1: clrscr(); printf("plesase input :\n"); scanf("%s",&c) ; if((fp2=fopen("ToBeTran","wb"))==NULL) { printf("Error,cannot open file\n" ); exit(0); } fprintf(fp2,"%s",c) ; fclose (fp2); code(myHaffCode,n) ; getch(); break; case 2: if((fp2=fopen("ToBeTran","rb"))==NULL) { printf("\n\n Please,increase records first~!! \n" ); return; } fscanf(fp2,"%s",&c); printf("The words:"); printf("%s",c); if((fp4=fopen("TextFile.","wb"))==NULL) { printf("Error,cannot open file\n" ); exit(0); } fprintf(fp4,"%s",c) ; fclose (fp4); getch(); break; case 3: clrscr(); printf("The huffman Tree:\n\n\n\n\n\n"); p=InsertLeftNode(root,b[n-1][2]); creat(p,n-1,b); PrintBiTree(root->leftChild,n); printf("\n"); getch(); clrscr(); break; case 4:goto loop; case 5:exit(0); } } }getch();}