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

description

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

函数接口定义:

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;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入第一行以-1结束的整数,为二叉排序树中元素初始序列,第二行为一个整数n,代表要查询的元素个数,第三行为n个整数,代表要查询的元素值。
例如创建如下二叉排序树,输入为:

二叉排序树.png

4 3 5 1 2 7 6 8 -1
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

solution

BSTree SearchBST(BSTree T,ElemType e){
    if(!T || e == T->data) return T;
    else if(e < T-> data)SearchBST(T->lchild, e);
    else SearchBST(T->rchild, e);
} 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Status InsertBST(BSTree &T,ElemType e); //实现树的节点的插入 Status PreOrderTraverse(BSTree T); //实现树的递归前序遍历 Status InOrderTraverse(BSTree T); //实现树的递归序遍历 Status PostOrderTraverse(BSTree T); //实现树的递归后序遍历 Status AllOrderTraverse(BSTree T); //实现三种递归遍历的打印 Status NonPreOrder(BSTree T,Stack S); //实现树的非递归前序遍历 Status NonInOder(BSTree T,Stack S); //实现树的非递归序遍历 Status NonPostOrder(BSTree T,Stack S); //实现树的非递归后序遍历 Status NonAllOrder(BSTree T,Stack S); //实现三种非递归遍历的打印 Status LevelTraverse(BSTree T,Queue Q); //实现二叉树的层次遍历 Status PostsSearch(BSTree T,ElemType e);//实现二叉树给定关键字的查找 Status SwapSubtree(BSTree T); //实现结点左右子树的交换 int TreeDepth(BSTree T); //实现二叉树深度的求值 int TotalNodeNum(BSTree T); //实现二叉树总结点数的求值 int LeafNodeNum(BSTree T); //实现二叉树叶子结点数的求值 Status DeleteBST(BSTree &T,ElemType e); //实现树的节点的删除 int TreeHeight(BSTree T); //实现树的高度的求值 int Max(int a,int b); //实现两个数求最大值 Position MinElemSearch(BSTree T); //实现最小元素查找 BSTree LeftRotate(BSTree g); //实现二叉树一次右旋转操作 BSTree RightRotate(BSTree g); //实现二叉树一次左旋转操作 BSTree L_RRotate(BSTree g); //实现一次先左旋转再右旋转操作 BSTree R_LRotate(BSTree g); //实现一次先右旋转再左旋转操作 Status CreatStack(Stack &S); //实现栈的建立 Status CreatQueue(Queue &Q); //实现队列的建立

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值