PTA 数据结构考题 - 二叉排序树

建立一个二叉排序树,根据给定值对其实施查找。

二叉排序树的二叉链表存储表示:

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

函数接口定义:

下面给出了 二叉排序树创建和搜索 函数的大部分内容,但缺少了一部分(以下划线____标识出来的部分)。

请先将以下代码中画横线的部分补充完整,然后将完整的函数BSTInsert,BSTCreate,BSTSearch提交系统,完成题目要求的功能。

void BSTInsert( BSTree &T, BSTree s)
{
    if(T==NULL)
        T=s;
    else if(s->data<T->data)
        BSTInsert( ____ ,s);
    else 
        BSTInsert( ____ ,s);
}

void BSTCreate(BSTree  &T)
{   
    ElemType x;  BSTree  s;
    T=NULL;
    cin>>x;
    while (x!=-1)
    {  
        s=new BSTNode;
        s->data=x;
        s->lchild=s->rchild=NULL;
        BSTInsert( ____ ,  ____ );
        cin>>x;
    }
}

BSTree BSTSearch(BSTree T, ElemType k)
{  
    if(!T || ____ )
        return ____ ;
    if(k<T->data)
        return  BSTSearch( ____ ,k);
    else
        return  BSTSearch ( ____ ,k);
}

该函数中的参数说明:

ElemType k 要搜索的值

顺序表中第一个数据元素存储在 T.R[1]

测试主程序样例:

int main ()
{    BSTree T,p; int x;
    BSTCreate(T);
    cin>>x;
    p=BSTSearch(T,x);
    if(p!=NULL)
    {  cout<<"have found!";
       cout<<" lchild:";
       if(p->lchild) cout<<p->lchild->data;
       else cout<<"NULL";
       cout<<" rchild:";
       if(p->rchild) cout<<p->rchild->data;
       else cout<<"NULL";
    }
    else
       cout<<"NOT FOUND!";
    return 0;
}

输入格式:

第一行输入二叉排序树中结点的值,以-1结束。用逐个插入的方式创建二叉排序树。

第二行输入一个要查找的值。

输出格式:

找到,输出have found!。接着空一格,输出该结点左孩子值,后再空一格,输出该结点右孩子的值。如果孩子为空,对应位置输出NULL

如果没有找到,输出NOT FOUND!

输入样例:

10 18 3 8 20 2 7 -1
3

输出样例:

have found! lchild:2 rchild:8

输入样例2:

10 18 3 8 20 2 7 -1
8

输出样例2:

have found! lchild:7 rchild:NULL

输入样例3:

10 18 3 8 20 2 7 -1
5

输出样例3:

NOT FOUND!

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

 实现代码:

#include<iostream>
using namespace std;

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

int main()
{
    BSTree T, p; int x;
    BSTCreate(T);
    cin >> x;
    p = BSTSearch(T, x);
    if (p != NULL)
    {
        cout << "have found!";
        cout << " lchild:";
        if (p->lchild) cout << p->lchild->data;
        else cout << "NULL";
        cout << " rchild:";
        if (p->rchild) cout << p->rchild->data;
        else cout << "NULL";
    }
    else
        cout << "NOT FOUND!";
    return 0;
}

void BSTInsert( BSTree &T, BSTree s)
{
    if(T==NULL)
        T=s;
    else if(s->data<T->data)
        BSTInsert( T->lchild ,s);
    else 
        BSTInsert( T->rchild,s);
}

void BSTCreate(BSTree  &T)
{   
    ElemType x;  BSTree  s;
    T=NULL;
    cin>>x;
    while (x!=-1)
    {  
        s=new BSTNode;
        s->data=x;
        s->lchild=s->rchild=NULL;
        BSTInsert( T, s );
        cin>>x;
    }
}

BSTree BSTSearch(BSTree T, ElemType k)
{  
    if(!T || T->data==k)
        return T;
    if(k<T->data)
        return  BSTSearch( T->lchild ,k);
    else
        return  BSTSearch ( T->rchild ,k);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值