上机题五对应程序-二叉排序树
上机题五对应程序
#include "stdio.h" #include "stdlib.h" typedef struct Bsnode {
char word[10];
struct Bsnode *Lchild,*Rchild;
}BSN,*BSP;
void inorder(BSP T); BSP BSTinsert(BSP T,BSP S)//二叉排序树插入算法 {
BSP q,p;
p=(BSP)malloc(sizeof(BSN));
q=(BSP)malloc(sizeof(BSN));
if(T==NULL)
return(S);
else
{
p=T;q=NULL;
while(p!=NULL)
{
q=p;
if(S->word[0]==p->word[0])
{
free(S);
return(T);
}
if(S->word[0]
word[0])
p=p->Lchild;
else
p=p->Rchild;
}
if(S->word[0]word[0])
q->Lchild=S;
else
q->Rchild=S;
return(T);}
}
BSP createBst()
{
BSP T,S;
char word[10];
int i;
T=NULL;
scanf("%s",word);
while(word[0]!='#')//设#为结束符
{
S=(BSP)malloc(sizeof(BSN));
S->Lchild=S->Rchild=NULL;
for(i=0;i<10;i++)
{
S->word[i]=word[i];
}
T=BSTinsert(T,S);
scanf("%s",word);
}
return(T);
}
void inorder(BSP KT) {
if(KT)
{
inorder(KT->Lchild);
printf("%s ",KT->word);
inorder(KT->Rchild);
}
}
void main()
{
printf("习题五:设英文
句子
关于阅读的唯美句子关于古风的唯美句子执行力的经典句子鼓励人努力奋斗的句子用沉默代替一切的句子
:“everyone round you can hear you when you speak.”试编写
完成下面任务的程序\n");
printf("(1)依次读入句中各单词,构造一棵二叉排序树\n");
printf("(2)按LDR遍历此二叉排序树。\n");
printf(" LDR: can everyone hear round speak when you(有序)\n");
printf("\n");
BSP BT;
BT=createBst();
printf("LDR:");
inorder(BT);
printf("\n");
}