主题:帮我改一下程序吧,我写得太烂了
#include <stdio.h>
#include <malloc.h>
//下面是码表存储的函数
struct BinTree
{
int value1;
int value2;
struct BinTree *lchild;
struct BinTree *rchild;
};
struct BinTree *creat_table(int *lentab,int *codtab,int tabwidth,int tabheight)
{
int n=0;
int i,j;
int len,cod;
struct BinTree *p,*root;
for(j=0;j<tabheight;j++)
{
for(i=0;i<tabwidth;i++)
{
cod=codtab[i]; //读取一个码字
len=lentab[i]; //设置len为码字长度
//设置当前节点为根节点
if(n==0)
{
root=NULL; //p为新节点地址,但虚节点地址为NULL
root=(struct BinTree *)malloc(sizeof(struct BinTree)); //为根节点开辟一个单元
root->lchild=root->rchild=NULL;
}
p=root; //
while(len) //如果当前码字长度不为零
{
int bit;
bit=(cod>>(len-1))&0x01; //取码字的第len位至bit,具体做法是将当前码字
//右移(len-1)位,把第len位移到最低位,然后与
n=n+1;
//00……0001按位与就可以取到第len位了,此处
//if(n==1) root=p1;
//else
//{
if(bit==0)
{
if(p->lchild=NULL) //如果当前节点的左孩子不存在
{
p->lchild=(struct BinTree *)malloc(sizeof(struct BinTree)); //建立左孩子节点
p->value1=p->value2=-1; //设置左孩子节点的value1和value2,
}
p=p->lchild; //移动当前节点指针到左孩子节点
//p->lchild=p->rchild=NULL;
}
else
{
if(p->rchild=NULL) //如果当前节点的右孩子不存在
{
p->rchild=(struct BinTree *)malloc(sizeof(struct BinTree)); //建立右孩子节点
p->value1=p->value2=-1; //设置右孩子节点的value1和value2,
}
p=p->rchild; //移动当前节点指针到右孩子节点
//p->lchild=p->rchild=NULL;
}
len-=1;
//}
//len-=1;
}
p->value1=i;
p->value2=j;
}
}
return root;