#include
#include
typedef struct node *tree_pointer;
struct node{
char ch;
tree_pointer left_child,right_child;
};
tree_pointer root=NULL;
tree_pointer create(tree_pointer ptr)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
ptr=NULL;
else{
ptr=(tree_pointer)malloc(sizeof(node));
ptr->ch=ch;
ptr->left_child=create(ptr->left_child);
ptr->right_child=create(ptr->right_child);
}
return ptr;
}
void inorder(tree_pointer ptr)
{
if(ptr){
inorder(ptr->left_child);
printf("%c",ptr->ch);
inorder(ptr->right_child);
}
}
int search(tree_pointer ptr,char ch,int h)
{
int flag=0;
if(!ptr)
return 0;
if(ptr->ch==ch)
return h;
else{
flag=search(ptr->left_child,ch,h+1);
if(!flag)
flag=search(ptr->right_child,ch,h+1);
return flag;
}
}
void main()
{
char ch='A';
/*printf("输入所要查找的元素:\n");
scanf("%c",&ch);*/
printf("构建一个二叉树:\n");
root=create(root);
if(!search(root,ch,1))
printf("所要查找的元素%c不在二叉树中\n",ch);
else
printf("所要查找的元素%c在二叉树的第%d层\n",ch,search(root,ch,1));
printf("中序遍历二叉树:\n");
inorder(root);
}