#include<stdio.h>
#include<stdlib.h>
typedef char datatype;
typedef struct bintreenode
{
datatype info;
struct bintreenode *lchild;
struct bintreenode *rchild;
}bintreenode,*bintree;
bintreenode *createbintree()
{
char ch;
bintreenode *bt=NULL;
scanf("%c",&ch);
if(ch=='#')
bt=NULL;
else
{
bt=(bintreenode *)malloc(sizeof(struct bintreenode));
bt->info=ch;
bt->lchild=createbintree();
bt->rchild=createbintree();
}
return bt;
}
void preorder(bintree bt)
{
if(bt==NULL) return;
printf("%c",bt->info);
preorder(bt->lchild);
preorder(bt->rchild);
}
void inorder(bintree bt)
{
if(bt==NULL) return;
inorder(bt->lchild);
printf("%c",bt->info);
inorder(bt->rchild);
}
void postorder(bintree bt)
{
if(bt==NULL) return;
postorder(bt->lchild);
postorder(bt->rchild);
printf("%c",bt->info);
}
void countleaf(bintree bt,int &count)
{
if(bt)
{
if((!bt->lchild)&&(!bt->rchild))
{
printf("叶子节点为:\n%c\n",bt->info);
count++;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
int search(bintree bt,datatype x)
{
if(x=='#') return 0;
if(bt==NULL) return 0;
if(bt->info==x) return 1;
if(bt->lchild==NULL&&bt->rchild==NULL) return 0;
else return(search(bt->lchild,x)+search(bt->rchild,x));
}
int main()
{
bintree bt=NULL;
int count=0,post;
char data,n;
printf("请输入二叉树的先序序列,以#结束\n");
bt=createbintree();
printf("二叉树先序存储完成\n\n");
printf("该二叉树的先序遍历输出为\n");
preorder(bt);
printf("\n\n");
printf("该二叉树的中序遍历为\n");
inorder(bt);
printf("\n\n");
printf("该二叉树的后序遍历为\n\n");
postorder(bt);
printf("\n\n");
countleaf(bt,count);
printf("该二叉树的叶子节点个数为\n");
printf("%d",count);
printf("请输入你要查找的元素\n\n");
n=getchar(); //接受回车字符
scanf("%c",&data);
post=search(bt,data);
if(post)
{
printf("%d\n\n该值存在\n",post);
}
else
{
printf("%d\n\n该值不存在\n",post);
}
}
#include<stdlib.h>
typedef char datatype;
typedef struct bintreenode
{
datatype info;
struct bintreenode *lchild;
struct bintreenode *rchild;
}bintreenode,*bintree;
bintreenode *createbintree()
{
char ch;
bintreenode *bt=NULL;
scanf("%c",&ch);
if(ch=='#')
bt=NULL;
else
{
bt=(bintreenode *)malloc(sizeof(struct bintreenode));
bt->info=ch;
bt->lchild=createbintree();
bt->rchild=createbintree();
}
return bt;
}
void preorder(bintree bt)
{
if(bt==NULL) return;
printf("%c",bt->info);
preorder(bt->lchild);
preorder(bt->rchild);
}
void inorder(bintree bt)
{
if(bt==NULL) return;
inorder(bt->lchild);
printf("%c",bt->info);
inorder(bt->rchild);
}
void postorder(bintree bt)
{
if(bt==NULL) return;
postorder(bt->lchild);
postorder(bt->rchild);
printf("%c",bt->info);
}
void countleaf(bintree bt,int &count)
{
if(bt)
{
if((!bt->lchild)&&(!bt->rchild))
{
printf("叶子节点为:\n%c\n",bt->info);
count++;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
int search(bintree bt,datatype x)
{
if(x=='#') return 0;
if(bt==NULL) return 0;
if(bt->info==x) return 1;
if(bt->lchild==NULL&&bt->rchild==NULL) return 0;
else return(search(bt->lchild,x)+search(bt->rchild,x));
}
int main()
{
bintree bt=NULL;
int count=0,post;
char data,n;
printf("请输入二叉树的先序序列,以#结束\n");
bt=createbintree();
printf("二叉树先序存储完成\n\n");
printf("该二叉树的先序遍历输出为\n");
preorder(bt);
printf("\n\n");
printf("该二叉树的中序遍历为\n");
inorder(bt);
printf("\n\n");
printf("该二叉树的后序遍历为\n\n");
postorder(bt);
printf("\n\n");
countleaf(bt,count);
printf("该二叉树的叶子节点个数为\n");
printf("%d",count);
printf("请输入你要查找的元素\n\n");
n=getchar(); //接受回车字符
scanf("%c",&data);
post=search(bt,data);
if(post)
{
printf("%d\n\n该值存在\n",post);
}
else
{
printf("%d\n\n该值不存在\n",post);
}
}