实践6.1

二叉排序树

源程序:

#include "stdio.h"
#include "stdlib.h"

typedef struct tnode
{
int id;
int score;
struct tnode *lchild,*rchild;
}stu;
void Ins_Student(stu **p,long id,int score)
{
stu *s;
if(*p ==NULL)
{
s=(stu *)malloc(sizeof(stu));
s->id=id;
s->score=score;
s->lchild=NULL;
s->rchild=NULL;
*p=s;
}
else if(score<( *p)->score)
Ins_Student(&(( *p)->lchild),id,score);
else
Ins_Student(&(( *p)->rchild),id,score);
}
stu *Create_Student()
{
int id,score;
stu *root;
root=NULL;
printf("请输入学号和成绩(用,隔开,用0结束:)");
printf(" -------------------------------- ");
printf("学号,成绩:");
scanf("%ld,%d",&id,&score);
while(score!=0)
{
Ins_Student(&root,id,score);
printf("学号,成绩:");
scanf("%ld,%d",&id,&score);
}
printf(" -------------------------------- ");
return root;
}
void In_Order(stu *bt)
{
if(bt!=NULL)
{
In_Order(bt->lchild);
printf("%ld,%d ",bt->id,bt->score);
In_Order(bt->rchild);
}
}
void main()
{
stu *root;
root=Create_Student();
printf("排序后的结果: ");
printf("学号,成绩: ");
In_Order(root);
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/13329348.html