-->
当前位置:首页 > 题库 > 正文内容

程序填空题:二叉排序树查找的递归算法

Luz4年前 (2021-05-10)题库1936
本题要求实现二叉排序树的查找操作。
对于二叉排序树,如下图:

![二叉排序树.png](~/70d3f056-8d8c-4015-923e-51c694f1092e.png)

### 输入样例:
```
4
1 8 0 9
```
### 输出样例:

```out
1 is found
8 is found
0 is not found
9 is not found
```

其中BSTree结构定义如下:
```
typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
```
### 程序如下:

```
#include
#include
typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
BSTree CreateBST();/ *二叉排序树创建,由裁判实现,细节不表* /
BSTree SearchBST(BSTree T,ElemType e);
int main()
{
BSTree T,result;
ElemType n,e;
T = CreateBST();
scanf("%d",&n);
for(int i=0;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;
}
BSTree SearchBST(BSTree T,ElemType e)
{
if(@@[!T||T->data == e](2)) return T;
if(edata) return @@[ SearchBST(T->lchild,e) ](2);
else @@[ return SearchBST(T->rchild,e) ](2);
}
```







答案:
第1空:!T||T->data == e

第2空: SearchBST(T->lchild,e)

第3空: return SearchBST(T->rchild,e)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。