#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Keytype;//
typedef struct node
{ Keytype score;//成绩设为关键字
    char name[10];//学生成绩
    char xuenum[12];//学号
    struct node *lchild,*rchild;
    }Stunode;
typedef Stunode * Stutree;
inserttree(Stutree *T,Stunode *S)//插入树的结点
{Stunode *f,*p=*T;
    while(p){
        f=p;
        p=((S->score)<=(p->score))?p->lchild:p->rchild;
    }
    p=(Stunode *)malloc(sizeof(Stunode));
    strcpy(p->name,S->name);
    strcpy(p->xuenum,S->xuenum);
    p->score=S->score;    
    p->lchild=NULL;
    p->rchild=NULL;
    if(*T==NULL)*T=p;
    else
    if(S->score<=f->score)f->lchild=p;
    else f->rchild=p;     
}
Stutree creatree()//建立二叉排序树
{  Stutree T=NULL;    
    Stunode *Stuinfo;
    char flag;
    flag='y';
    printf("输入学生信息:\n");
    printf("学生姓名 学号 学习成绩  \n");    
    while(flag=='y'||flag=='Y')
    {
    Stuinfo=(Stunode *)malloc(sizeof(Stunode));
    scanf("%s,%s,%d",Stuinfo->name,Stuinfo->xuenum,&Stuinfo->score);
    inserttree(&T,Stuinfo);
    printf("还有吗?y/n\n");
    getchar();
    flag=getchar();
    }
    return T;
    }
void Inorder(Stutree root)//中序遍历

    if(root!=NULL)
    {
    Inorder(root->lchild);
    printf("%s,%s,%d\n",root->name,root->xuenum,root->score);
    Inorder(root->rchild);
}
}
Stunode *searchtree(Stutree T,Keytype Stuscore)//查找学生信息
 {  if(T==NULL||Stuscore==T->score)
     return T;
   if(Stuscore<=T->score)
    return searchtree(T->lchild,Stuscore);
   if(Stuscore>T->score)
    return searchtree(T->rchild,Stuscore);
   }

main()
{
  Stutree root;
  int Stuscore;
  Stunode *student;
 printf("生成二叉树:\n");
     root=creatree();
 printf("中序遍历输出结果:\n");
 printf("学生姓名 学号 学习成绩:\n");
   Inorder(root);
 printf("输入你要查找的学生成绩:");
   scanf("%d",&Stuscore);
 printf("\n");
 student=searchtree(root,Stuscore);
 if(!student)
 printf("没有查到成绩为%d的学生",Stuscore);
 else{
     printf("成绩为%d的学生信息:\n",Stuscore);
    printf("%s,%s,%d\n",student->name,student->xuenum,student->score);  
   }
}