6-1 二叉排序树查找操作 (6分)

该博客介绍了如何在C语言中实现二叉排序树的查找功能,提供了函数接口定义和示例代码,并展示了二叉排序树的示例结构。

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

本题要求实现二叉排序树的查找操作。

函数接口定义:


BSTree SearchBST(BSTree T,ElemType e);

其中BSTree结构定义如下:

typedef int ElemType;
typedef struct BSTNode
{
	ElemType data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct BSTNode
{
	ElemType data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
BSTree CreateBST();/* 二叉排序树创建,由裁判实现,细节不表 */
BSTree SearchBST(BSTree T,ElemType e);
void Inorder(BSTree T);/* 中序遍历,由裁判实现,细节不表 */

int main()
{
	BSTree T,result;
	ElemType n,e;
	T = CreateBST();
	printf("Inorder:");	Inorder(T);	printf("\n");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&e);
		result = SearchBST(T,e);
		if(result) printf("%d is found\n",result->data);
		else printf("%d is not found\n",e);
	}
	return 0;
}
/* 你的代码将被嵌在这里 */

对于创建好的二叉排序树,如下图:

输入样例:

4
1 8 0 9

输出样例:

Inorder: 1 2 3 4 5 6 7 8
1 is found
8 is found
0 is not found
9 is not found

下面是本题代码

BSTree SearchBST(BSTree T,ElemType e)

{
    if(!T || T->data == e)
        return T;
    else if(e < T->data)
        return SearchBST(T->lchild,e);
    else
        return SearchBST(T->rchild,e);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值