主题:[原创]求二叉树图形输出的算法
刚开始学数据结构。编了很久才把Huffman编码器编出来。
老师要求用图形把最优二叉树输出来。
对于C的图形驱动什么的不是很懂。还有图形输出最优二叉树的算法自己想了很久都没想出来。
麻烦大家一点时间,帮忙提示个图形输出二叉树的算法。谢谢了!
下面是我Huffman编码器的C语言代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 10000
typedef struct
{
char node;
int weight,parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void HuffmanCoding(HTNode *HT,int x,int y)
{
char c;
int i,s1,s2;
for(i=x+1;i<=y;i++)
{
s1=select(HT,i-1);
HT[s1].parent=i;
s2=select(HT,i-1);
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
}
int select(HTNode *HT,int x)
{
int s1,i,min;
min=maxsize;
for(i=1;i<=x;i++)
{
if(HT[i].parent==0&&HT[i].weight<min)
{
s1=i;
min=HT[i].weight;
}
}
return s1;
}
void creatcode(HTNode *HT,HuffmanCode HC,int n)
{
int p,i,e,f;
char *cd,*temp;
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
p=n-1;
for(e=i,f=HT[i].parent;f!=0;e=f,f=HT[f].parent)
{
if(HT[f].lchild==e) cd[--p]='0';
else cd[--p]='1';
}
HC[i]=(char *)malloc((n-p)*sizeof(char));
temp=&cd[p];
strcpy(HC[i],temp);
}
for(i=1;i<=n;i++)
{
printf("HT[%2d]:%c %s\n",i,HT[i].node,HC[i]);
}
free(cd);
}
void wpl(HTNode *HT,int n)
{
int i,wpl;
for(i=1,wpl=0;i<=n;i++)
wpl=wpl+HT[i].weight;
printf("the total wpl is:%d\n",wpl);
}
void displayHuffmanTree(HuffmanTree HT,int n)
{
int i,m;
m=2*n-1;
for(i=1;i<=m;i++)
printf("HT[%2d] %c %3d %3d %3d %3d\n",i,HT[i].node,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
}
void main()
{
HTNode *HT;
HuffmanCode HC;
char c;
int i,n,m,wei,n1,m1;
scanf("%d",&n);
m=2*n-1;
getchar();
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
for(i=1;i<=n;++i)
{
scanf("%c%d",&c,&wei);
getchar();
HT[i].node=c;
HT[i].weight=wei;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(i=n+1;i<=m;i++)
{
HT[i].node='0';
HT[i].weight=0;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
HuffmanCoding(HT,n,m);
displayHuffmanTree(HT,n);
creatcode(HT,HC,n);
wpl(HT,n);
}
老师要求用图形把最优二叉树输出来。
对于C的图形驱动什么的不是很懂。还有图形输出最优二叉树的算法自己想了很久都没想出来。
麻烦大家一点时间,帮忙提示个图形输出二叉树的算法。谢谢了!
下面是我Huffman编码器的C语言代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 10000
typedef struct
{
char node;
int weight,parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void HuffmanCoding(HTNode *HT,int x,int y)
{
char c;
int i,s1,s2;
for(i=x+1;i<=y;i++)
{
s1=select(HT,i-1);
HT[s1].parent=i;
s2=select(HT,i-1);
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
}
int select(HTNode *HT,int x)
{
int s1,i,min;
min=maxsize;
for(i=1;i<=x;i++)
{
if(HT[i].parent==0&&HT[i].weight<min)
{
s1=i;
min=HT[i].weight;
}
}
return s1;
}
void creatcode(HTNode *HT,HuffmanCode HC,int n)
{
int p,i,e,f;
char *cd,*temp;
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
p=n-1;
for(e=i,f=HT[i].parent;f!=0;e=f,f=HT[f].parent)
{
if(HT[f].lchild==e) cd[--p]='0';
else cd[--p]='1';
}
HC[i]=(char *)malloc((n-p)*sizeof(char));
temp=&cd[p];
strcpy(HC[i],temp);
}
for(i=1;i<=n;i++)
{
printf("HT[%2d]:%c %s\n",i,HT[i].node,HC[i]);
}
free(cd);
}
void wpl(HTNode *HT,int n)
{
int i,wpl;
for(i=1,wpl=0;i<=n;i++)
wpl=wpl+HT[i].weight;
printf("the total wpl is:%d\n",wpl);
}
void displayHuffmanTree(HuffmanTree HT,int n)
{
int i,m;
m=2*n-1;
for(i=1;i<=m;i++)
printf("HT[%2d] %c %3d %3d %3d %3d\n",i,HT[i].node,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
}
void main()
{
HTNode *HT;
HuffmanCode HC;
char c;
int i,n,m,wei,n1,m1;
scanf("%d",&n);
m=2*n-1;
getchar();
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
for(i=1;i<=n;++i)
{
scanf("%c%d",&c,&wei);
getchar();
HT[i].node=c;
HT[i].weight=wei;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(i=n+1;i<=m;i++)
{
HT[i].node='0';
HT[i].weight=0;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
HuffmanCoding(HT,n,m);
displayHuffmanTree(HT,n);
creatcode(HT,HC,n);
wpl(HT,n);
}