查找二叉排序树中最大的键值(c代码)

该博客展示了用C语言实现二叉树的创建和查找功能的代码。定义了二叉树结构体,包含创建二叉树和查找二叉树节点的函数,在主函数中通过用户输入字母创建二叉树,并进行查找操作,最后提示结束程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>

#include<conio.h>
struct tree{
char key;
struct tree *Lchild,*Rchild;
};
struct tree *create_btree(struct tree *t,struct tree *r,char key)
{
if (r ==0 )
{
r=new (struct tree);
if ( r == 0)
{
printf("Out of memory/n"); return 0 ;
}
r->Lchild= 0; r->Rchild=0; r->key=key;
if (t)
{
if(key<t->key) t->Lchild=r;
else
t->Rchild=r;
}
else
{
r->Rchild=0; r->Lchild = 0;
}
return r;
}
if (key < r->key)
create_btree(r,r->Lchild,key);
if(key>=r->key)
create_btree(r,r->Rchild,key);
return t;
};
struct tree *search_btree(struct tree *t,char key1)
{ struct tree *p;
p=t;
if (!p)
{ printf("Empty btree/n"); return p; }
while(p->Rchild) {p=p->Rchild;};
printf("Successful search/n key1=%c/n",p->key);
return p;
};
void main()
{
char s[100], c, e,q;
struct tree *t=0, *p;
printf("Input a letter for Creating the Binary_Tree ( Directly press <Enter> to stop ):/n");
while (*s){
printf("/nInput a letter: ");

e=getch(); /*#include<conio.h>*/
putch(e); /*#include<conio.h>*/
if(e==13) break;
if (!t)
t=create_btree(t,t,e);
else
create_btree(t,t,e);
};
search_btree(t,0);
printf("/n");
printf("结束请按q!");
if(getchar()=='q') printf("再见");
else {while(1);};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值