#include "stdio.h"
#include"stdlib.h"
#include
typedef struct BTREE {
char data;
struct BTREE *left;
struct BTREE *right;
} BTNode,*BTree;
BTree node;
void createBST(BTree *T, char ord);
BTree createBT()
{
BTree *T,t;
int tong,i;
t=(BTree)malloc(sizeof(BTNode));
T=&t;
(*T)->left=NULL;
(*T)->right=NULL;
char *s;
s=(char * )malloc(100*sizeof(char));
scanf("%s",s);
(*T)->data=*s;
tong=strlen(s);
for(i=1;i
s=(s+1);
createBST(T,*s);
}
return t;
}
void createBST(BTree *T, char ord)
{
BTree *S;
S=(BTree *)malloc(sizeof(BTree));
if(ord<((*T)->data))
{
if((*T)->left==NULL)
{
(*T)->left=(BTree)malloc(sizeof(BTNode));
(*T)->left->data=ord;
(*T)->left->left=NULL;
(*T)->left->right=NULL;
}
else
{
*S=(*T)->left;
createBST(S,ord);
}
}
else if(ord>((*T)->data))
{
if((*T)->right==NULL)
{
(*T)->right=(BTree)malloc(sizeof(BTNode));
(*T)->right->data=ord;
(*T)->right->left=NULL;
(*T)->right->right=NULL;
}
else
{
*S=(*T)->right;
createBST(S,ord);
}
}
}
BTree searchBST(BTree T, char key)
{
if(T==NULL) return NULL;
else if(key==T->data) return T;
else if(key
else return searchBST(T->right,key);
}
int insertBST(BTree *T, char e)
{
BTree p=new BTNode;
if(e==(*T)->data) return -1;
else if(e<(*T)->data)
{
if(!(*T)->left)
{
p->data=e;
p->left=NULL;
p->right=NULL;
(*T)->left=p;
}
else
insertBST(&((*T)->left),e);
}
else
{
if(!(*T)->right)
{
p->data=e;
p->left=NULL;
p->right=NULL;
(*T)->right=p;
}
else
insertBST(&((*T)->right),e);
}
return 0;
}
int deleteBST(BTree *T,char key)
{
if(!(*T))
{
return -1;
}
else if((*T)->data>key)
{
node=*T;
return deleteBST(&((*T)->left),key);
}
else if((*T)->data==key)
{
if(!((*T)->left))//单支
{
if((node)->left==(*T))
{
(node)->left=(*T)->right;
return 0;
}
else
{
(node)->right=(*T)->right;
return 0;
}
}
else if(!((*T)->right))//单支
{
if((node)->left==(*T))
{
(node)->left=(*T)->left;
return 0;
}
else
{
(node)->right=(*T)->left;
return 0;
}
}
else//有左右孩子的节点,不是单支
{
BTree p,ls=(*T)->left;
p=*T;
while(ls->right)
{
p=ls;
ls=ls->right;
}
(*T)->data=ls->data;
if(p!=(*T))
{
p->right=ls->left;
}
else
{
p->left=ls->left;
}
return 0;
}
}
else
{
node=*T;
return deleteBST(&(*T)->right,key);
}
}
void InOrder( BTree T)
{
if(T)
{
InOrder(T->left);
printf("%c",T->data);
InOrder(T->right);
}
}
int main()
{
BTree *R , root;
char key;
int i,k;
while(1)
{
int state=0;
scanf("%d",&k);
while(k<0 || k>6) scanf("%d",&k);
switch(k)
{
case 0:
root=createBT();
R=&root;
break;
case 1:
getchar();
scanf("%c",&key);
BTree p;
p=searchBST( *R , key );
if(p==NULL)
printf("未找到\n");
else
printf("找到了 %c\n",p->data);
break;
case 2:
getchar();
scanf("%c",&key);
int insert;
insert=insertBST( R , key );
if(insert==-1)
printf("已存在,插入失败。\n");
else
printf("插入成功。\n");
break;
case 3:
getchar();
scanf("%c",&key);
int dele;
dele=deleteBST( R , key );
if(dele==-1)
printf("不存在,删除失败。\n");
else
printf("删除成功。\n");
break;
case 4:
InOrder( *R );
printf("\n");
break;
case 5:
state=1;
break;
}
if(state==1) break;
}
return 0;
}
输入0建立(输入一个字符串回车结束)
输入1查找
输入2插入
输入3删除
输入4遍历输出二叉树
输入5退出
你可以自己在测试一下,可以过的。然后题目什么要求你自己修改就可以了