**二叉树相关函数**

二叉树相关函数

int main()//主函数
{
while(~scanf("%s",st))
{
cnt=-1;
struct node *root;
root=creat();
zhongxu(root);//中序遍历
printf("\n");
houxu(root); //后序遍历
printf("\n");
cengxu(root);//层序遍历
printf("\n");
yezi(root);//统计叶子数
printf("%d\n",am);
level(root); //输出二叉树的叶子节点(按从上到下从左到右的顺序)
printf("\n");

}
return 0;

}


char st[51]; 根据输入的字符序列建立二叉树
int cnt;
struct node *creat()
{
struct node *root;
if(st[++cnt]==’,’)
{
root=NULL;
}
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=st[cnt];
root->l=creat();
root->r=creat();
}
return root;
}


void zhongxu(struct node *root) //中序遍历
{
if(root)
{
zhongxu(root->l);
printf("%c",root->data);
zhongxu(root->r);
}
}


void houxu(struct node *root) //后序遍历
{
if(root)
{
houxu(root->l);
houxu(root->r);
printf("%c",root->data);
}
}


void cengxu(struct node *root)//层序遍历//一层一层的输出
{
struct node *p[1000];
int r=0,f=0;
if(root)
{
p[r++]=root;
}
while(r>f)
{
root=p[f++];
printf("%c",root->data);
if(root->left) p[r++]=root->left;
if(root->right) p[r++]=root->right;
}
}


void yezi(struct node *root)//统计叶子数
{
if(root)
{
if(root->leftNULL&&root->rightNULL)
{
am++;
}
yezi(root->left);
yezi(root->right);
}
}


void level(struct node *root)//输出二叉树的叶子节点(按从上到下从左到右的顺序)
{
int in,out;
in=0;out=0;
struct node *p[60];
p[in++]=root;
while(in>out)
{
if(p[out])
{
if(p[out]->leftNULL&&p[out]->rightNULL)
{
printf("%c",p[out]->data);
}
p[in++]=p[out]->left;
p[in++]=p[out]->right;
}
out++;
}
}


int main()
{
int n;
char str1[1100],str2[1100];
scanf("%s",str1);
scanf("%s",str2);
n=strlen(str1);
creat(n,str1,str2);
printf("\n");
return 0;
}


struct node *creat(int n,char *str1,char *str2)//二叉树的重建与后续遍历输出// /已知先序中序输出后序的主函数

{
struct node *root;
int i;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=str1[0];//找到根节点,根节点为str1(先序序列)的第一个
for(i=0;i<n;i++)//找到str2(中序序列)的根节点的位置
{
if(str2[i]==str1[0])
break;
}
root->left=creat(i,str1+1,str2);//(左子树的长度,左子树在str1中开始位置的地址,左子树在str2中开始位置的地址)
root->right=creat(n-i-1,str1+i+1,str2+i+1);//(右子树的长度,右子树在str1中开始位置的地址,右子树在str2中开始位置的地址)
printf("%c",root->data);//后序遍历输出
return root;
};


struct node *creat(int n,char *a,char *b) //已知中序后序进行先序遍历
{
struct node *root;
int i;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=b[n-1];
printf("%c",root->data);
for(i=0;i<n;i++)
{
if(a[i]==b[n-1])
break;
}
root->left=creat(i,a,b);
root->right=creat(n-i-1,a+i+1,b+i);
return root;
};


int main()
{
int n;
int t;
scanf("%d",&t);
struct node *root;
char str1[1100],str2[1100];
while(t–)
{
scanf("%s",str1);
scanf("%s",str2);
n=strlen(str1);
root=creat(n,str1,str2);
printf("\n");
cengci(root);//层次遍历
printf("\n");
c=deep(root); //还原二叉树,就是求该二叉树的高度
printf("%d\n",c);
h=height(root); //求二叉树的深度
printf("%d\n",h);
}

return 0;

}

void cengci(struct node *root)//层次遍历
{
struct node *p[1000];
int r=0,f=0;
if(root)
{
p[r++]=root;
}
while(r>f)
{
root=p[f++];
printf("%c",root->data);
if(root->left) p[r++]=root->left;
if(root->right) p[r++]=root->right;
}
}

int deep(struct node *root)//还原二叉树,就是求该二叉树的高度
{
if(root==NULL)
{
return 0;
}
else
{
return max(deep(root->left),deep(root->right))+1;
}
}

int max(int a,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}

int height(struct node *root)//求二叉树的深度
{
int d=0;
if(root)
{
int n=height(root->left);
int m=height(root->right);
d=(m>n)?(m+1):(n+1);
}
return d;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值