设二叉树的每个结点包含一个整型编程,建立二叉树的二叉链式存储结构,实现以下功能:
(1)求二叉树的结点数和叶子数
(2)层次遍历二叉树
(3)先、中、后序遍历二叉树
(4)求二叉树的深度
(5)求二叉树中以元素值为x的结点为根的子树的深度
代码如下:
//结点-整型键值-链式存储
//求二叉树的结点数和叶子树
//层次遍历二叉树
//对二叉树进行先序、中序、后序遍历
//求二叉树的深度
//求二叉树中以元素值为x的结点为根的子树的深度
#include "stdio.h"
#include "stdlib.h"
#define max 20
typedef int elemtype;
struct bnodept{//二叉树
elemtype data;
struct bnodept *lchild,*rchild;
};
typedef struct bnodept *bitreptr;
typedef struct{//队列
bitreptr v[max];
int front,rear;
}squeuetp;
void Init_squeuetp(squeuetp *lq){//初始化队列
lq->front=0;
lq->rear=0;
}
void in_squeuetp(squeuetp *lq,bitreptr root){//入队
if((lq->rear+1)%max==lq->front) printf("队列已满!\n");
else{
lq->rear=(lq->rear+1)%max;
lq->v[lq->rear]=root;
}
}
bitreptr out_squeuetp(squeuetp *lq){ //出队
if(lq->front==lq->rear) {printf("队列为空!\n");return NULL;}
else{
lq->front=(lq->front+1)%max;
return lq->v[lq->front];
}
}
int empty_squeuetp(squeuetp *lq){ //判断队列是否为空
if(lq->front==lq->rear) return 0;//队空
else return 1;
}
bitreptr crt_bt_pre(){ //先序建立二叉树
elemtype x;
bitreptr bt;
scanf("%d ",&x);
if(x==0) return NULL;//0是输入结束标志
else{
bt=(bitreptr) malloc(sizeof(struct bnodept));
bt->data=x;
bt->lchild=crt_bt_pre();
bt->rchild=crt_bt_pre();
return bt;
}
}
int countknot(bitreptr root){ //求二叉树的结点数(root为根)
static int i=0;//
if(root){
i++;
countknot(root->lchild);
countknot(root->rchild);
}
return i;
}
int countleaf(bitreptr root){ //求二叉树的叶子数(root为根)
int j=0;
if(!root) j=0;
else if((root->lchild==NULL)&&(root->rchild==NULL)) j=1;
else{
j=countleaf(root->lchild)+countleaf(root->rchild);
}
return j;
}
void level(bitreptr root){ //层次遍历二叉树
bitreptr t;
squeuetp lq;
Init_squeuetp(&lq);
in_squeuetp(&lq,root);//入队
while(empty_squeuetp(&lq)){
t=out_squeuetp(&lq);
printf("%d ",t->data);
if(t->lchild!=NULL)
in_squeuetp(&lq,t->lchild);
if(t->rchild!=NULL)
in_squeuetp(&lq,t->rchild);
}
}
void preorder(bitreptr root){ //先序遍历二叉树
if(root){
printf("%d ",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(bitreptr root){ //中序遍历二叉树
if(root){
inorder(root->lchild);
printf("%d ",root->data);
inorder(root->rchild);
}
}
void postorder(bitreptr root){ //后序建立二叉树
if(root){
postorder(root->lchild);
postorder(root->rchild);
printf("%d ",root->data);
}
}
int deepth(bitreptr root){ //求二叉树的深度
//非递归算法
//递归算法
int l,r;
if(!root) return 0;
else{
l=deepth(root->lchild);
r=deepth(root->rchild);
return l>r?(l+1):(r+1);
}
}
void deeptnappiont(bitreptr root,elemtype x){ //求二叉树中以元素值为x的结点为根的子树的深度
bitreptr t;
squeuetp lq;
Init_squeuetp(&lq);
in_squeuetp(&lq,root);//入队
while(empty_squeuetp(&lq)){
t=out_squeuetp(&lq);
if(x==t->data) break;
if(t->lchild!=NULL)
in_squeuetp(&lq,t->lchild);
if(t->rchild!=NULL)
in_squeuetp(&lq,t->rchild);
}
printf("以元素值为%d的结点为根的子树的深度为:%d\n",x,deepth(t));
}
int main(){
elemtype x;
bitreptr bt;
bt=crt_bt_pre();
printf("该二叉树的结点数为:%d\n",countknot(bt));
printf("该二叉树的叶子数为:%d\n",countleaf(bt));
printf("\n");
printf("先序遍历:");
preorder(bt);//先序遍历
printf("\n");
printf("中序遍历:");
inorder(bt);//中序遍历
printf("\n");
printf("后序遍历:");
postorder(bt);//后序遍历
printf("\n");
printf("层次遍历:");
level(bt);//层次遍历
printf("\n");
//deepth(bt);//求二叉树的深度
printf("二叉树的深度为:%d",deepth(bt));
printf("\n");
fflush(stdin);
printf("请输入以哪个元素值为根结点的子树的深度:\n");
scanf("%d",&x);
deeptnappiont(bt,x);//求二叉树中以元素值为x的结点为根的子树的深度
return 0;
}
运行示例:
如下图所示的二叉树:
以数字0作为结束的标志,如下图:
按上图先序输入以建立二叉树,输入后运行结果如下:
注意:1、键值为整型键值,所以输入应为整型元素;
2、输入0为结束标志;