7-2 二叉搜索树的结构(30 分)

二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。

输入格式:

输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:

  • A is the root,即"A是树的根";
  • A and B are siblings,即"AB是兄弟结点";
  • A is the parent of B,即"AB的双亲结点";
  • A is the left child of B,即"AB的左孩子";
  • A is the right child of B,即"AB的右孩子";
  • A and B are on the same level,即"AB在同一层上"。

题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。

输入样例:

5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3

输出样例:

Yes
Yes
Yes
Yes
Yes
No
No
No

这个是26分的代码 ,少了一个判断 ,输出的点必须在给定的数里:最后给出满分代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 220;
int BST[maxn];
int n,m;
typedef struct TNode
{
    int data;
    struct TNode *left,*right;
}TNode,*BiTree;
void buildtree(BiTree &T,int x)//建树
{
    if(T==NULL)
    {
        T=(BiTree)malloc(sizeof(BiTree));
        T->data=x;
        T->left=NULL;
        T->right=NULL;
    }
    else if(x<T->data)
    {
        buildtree(T->left,x) ;
    }
    else if(x>T->data)
    {
        buildtree(T->right,x);
    }
}
bool Search_BST(BiTree &T,int x)
{
    if(T==NULL||T->data==x) return true;
    if(T->data>x) return Search_BST(T->left,x);
    else return Search_BST(T->right,x);
}
int Get_value(BiTree &T)
{
    if(T==NULL) return -1;
    return T->data;
}
bool Judge_Parent(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==x)
    {
        if(Get_value(T->left)==y||Get_value(T->right)==y)
            return true;
    }
    return (Judge_Parent(T->left,x,y)||Judge_Parent(T->right,x,y));
}
bool  Judge_LChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->left)==x)
            return true;
    }
    return (Judge_LChild(T->left,x,y)||Judge_LChild(T->right,x,y));
}
bool Judge_RChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->right)==x)
            return true;
    }
    return (Judge_RChild(T->left,x,y)||Judge_RChild(T->right,x,y));
}
void Height(BiTree& T,int e,int h,int &c)
{
    if(T==NULL) return;
    if(T->data==e) c=h;
    Height(T->left,e,h+1,c);
    Height(T->right,e,h+1,c);
}
bool Judge_Same(BiTree T,int x,int y)
{
    int f1 , f2;
    Height(T,x,0,f1);
    Height(T,y,0,f2);
    if(f1==f2) return true;
    return false;
}
bool Judge_Bother(BiTree T,int x,int y)
{
    if(T==NULL) return false;
    if(T->left!=NULL&&T->right!=NULL)
    {
        if(T->left->data==x&&T->right->data==y) return true;
        if(T->left->data==x&&T->right->data==y) return true;
    }
    Judge_Bother(T->left,x,y);
    Judge_Bother(T->right,x,y);
}
int main()
{
    scanf("%d",&n);
    BiTree T=NULL;
    for(int i=0;i<n;i++) scanf("%d",&BST[i]);
    for(int i=0;i<n;i++) buildtree(T,BST[i]);
    scanf("%d",&m);
    int _a,_b,_c;
    string a,b,c;
    while(m--)
    {
        scanf("%d",&_a);
        cin>>a;
        if(a=="is")
        {
            cin>>a>>b;
            if(b=="parent")
            {
                cin>>c>>_b;
                if(Judge_Parent(T,_a,_b))
                    printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="left")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_LChild(T,_a,_b))
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="right")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_RChild(T,_a,_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="root")
            {
                if(T!=NULL&&T->data==_a)
                    printf("Yes\n");
                else
                    printf("No\n");
                continue;
            }
        }
        else if(a=="and")
        {
            cin>>_b;
            cin>>b>>c;
            if(c=="siblings")
            {
                if(Judge_Bother(T,_a,_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else
            {
                getline(cin,b);
                if(Judge_Same(T,_a,_b)) // 层数相同
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
        }
    }
}

  加了判断的满分代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 220;
int BST[maxn];
int n,m;
typedef struct TNode
{
    int data;
    struct TNode *left,*right;
}TNode,*BiTree;
void buildtree(BiTree &T,int x)//建树
{
    if(T==NULL)
    {
        T=(BiTree)malloc(sizeof(BiTree));
        T->data=x;
        T->left=NULL;
        T->right=NULL;
    }
    else if(x<T->data)
    {
        buildtree(T->left,x) ;
    }
    else if(x>T->data)
    {
        buildtree(T->right,x);
    }
}
bool Search_BST(BiTree &T,int x)
{//查找关键字k并返回其指针
    if(T==NULL||T->data==x) return true;
    if(T->data>x) return Search_BST(T->left,x);
    else return Search_BST(T->right,x);
}
int Get_value(BiTree &T)
{
    if(T==NULL) return -1;
    return T->data;
}
bool Judge_Parent(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==x)
    {
        if(Get_value(T->left)==y||Get_value(T->right)==y)
            return true;
    }
    return (Judge_Parent(T->left,x,y)||Judge_Parent(T->right,x,y));
}
bool  Judge_LChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->left)==x)
            return true;
    }
    return (Judge_LChild(T->left,x,y)||Judge_LChild(T->right,x,y));
}
bool Judge_RChild(BiTree T,int x,int y)
{
    if(T==NULL)
    {
        return false;
    }
    if(T->data==y)
    {
        if(Get_value(T->right)==x)
            return true;
    }
    return (Judge_RChild(T->left,x,y)||Judge_RChild(T->right,x,y));
}
void Height(BiTree& T,int e,int h,int &c)
{
    if(T==NULL) return;
    if(T->data==e) c=h;
    Height(T->left,e,h+1,c);
    Height(T->right,e,h+1,c);
}
bool Judge_Same(BiTree T,int x,int y)
{
    int f1 , f2;
    Height(T,x,0,f1);
    Height(T,y,0,f2);
    if(f1==f2) return true;
    return false;
}
bool Judge_Bother(BiTree T,int x,int y)
{
    if(T==NULL) return false;
    if(T->left!=NULL&&T->right!=NULL)
    {
        if(T->left->data==x&&T->right->data==y) return true;
        if(T->left->data==x&&T->right->data==y) return true;
    }
    Judge_Bother(T->left,x,y);
    Judge_Bother(T->right,x,y);
}
bool finds(int x)
{
    for(int i=0;i<n;i++)
        if(x==BST[i]) return true;
    return false;
}
int main()
{
    scanf("%d",&n);
    BiTree T=NULL;
    for(int i=0;i<n;i++) scanf("%d",&BST[i]);
    for(int i=0;i<n;i++) buildtree(T,BST[i]);
    scanf("%d",&m);
    int _a,_b,_c;
    string a,b,c;
    while(m--)
    {
        scanf("%d",&_a);
        cin>>a;
        if(a=="is")
        {
            cin>>a>>b;
            if(b=="parent")
            {
                cin>>c>>_b;
                if(Judge_Parent(T,_a,_b)&&finds(_a)&&finds(_b))
                    printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="left")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_LChild(T,_a,_b)&&finds(_a)&&finds(_b))
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="right")
            {
                cin>>b>>c;
                cin>>_b;
                if(Judge_RChild(T,_a,_b)&&finds(_a)&&finds(_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else if(b=="root")
            {
                if(T!=NULL&&T->data==_a&&finds(_a))
                    printf("Yes\n");
                else
                    printf("No\n");
                continue;
            }
        }
        else if(a=="and")
        {
            cin>>_b;
            cin>>b>>c;
            if(c=="siblings")
            {
                if(Judge_Bother(T,_a,_b)&&finds(_a)&&finds(_b))
                     printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
            else
            {
                getline(cin,b);
                if(Judge_Same(T,_a,_b)&&finds(_a)&&finds(_b)) // 层数相同
                 printf("Yes\n");
                else
                printf("No\n");
                continue;
            }
        }
    }
}

  

PTA 7-2 二叉搜索树的结构(30 分)的更多相关文章

  1. 天梯赛练习 L3-016 二叉搜索树的结构 (30分)

    题目分析: 用数型结构先建树,一边输入一边建立,根节点的下标为1,所以左孩子为root*2,右孩子为root*2+1,输入的时候可用cin输入字符串也可用scanf不会超时,判断是否在同一层可以判断两 ...

  2. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历   二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...

  3. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...

  4. 【开200数组解决二叉搜索树的建立、遍历】PAT-L3-016. 二叉搜索树的结构——不用链表来搞定二叉搜索树

    L3-016. 二叉搜索树的结构 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它 ...

  5. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  6. L3-1 二叉搜索树的结构 (30 分)

    讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...

  7. L3-016 二叉搜索树的结构 (30 分) 二叉树

    二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...

  8. L3-016. 二叉搜索树的结构

    二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...

  9. L3-016 二叉搜索树的结构 (30 分)

    二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...

随机推荐

  1. Redis的key和value大小限制

    Redis的key和value大小限制        今天研究了下将java bean序列化到redis中存储起来,突然脑袋灵光一闪,对象大小会不会超过redis限制?不管怎么着,还是搞清楚一下比较好 ...

  2. 20165305 苏振龙《Java程序设计》第一周学习总结

    20165305 <Java程序设计>第一周学习总结 教材学习内容总结 我重点学习了jdk安装后面的第一章内容,算是更进一步的的了解了一些Java方面的基础知识. Java平台概论 Jav ...

  3. Jmeter下进行ip伪造

    转至https://blog.csdn.net/xingchao416/article/details/53506051 1.首先获取一些闲置的ip地址,且必须为固定地址,不能是自动获取的地址,方法: ...

  4. hive 实现一个字段多行转一行 和 一行转多行

    1.多行转一行 多行转一行可以通过concat_ws(',',collect_set(col_name)) as col_new的方式实现,可以参考:https://www.cnblogs.com/s ...

  5. Linux Centos下查看cpu、磁盘、内存使用情况,关闭MySQL日志

    Linux Centos下查看cpu.磁盘.内存使用情况,关闭MySQL日志 lsblk 查看分区和磁盘df -h 查看空间使用情况fdisk -l 分区工具查看分区信息cfdisk /dev/sda ...

  6. localstorage跨域解决方案

    localstorage也存在 跨域的问题, [解决思路如下] 在A域和B域下引入C域,所有的读写都由C域来完成,本地数据存在C域下; 因此 A哉和B域的页面必定要引入C域的页面; 当然C域最好是主域 ...

  7. highchart应用示例1--2个不同类型变量2个y轴

    1.ajax调用接口和处理数据 function getCityData() { var date1 = $('#datetimepicker1').val(); var date2 = $('#da ...

  8. [转载]asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别

    在asp.net中经常出现包含这种形式<%%>的html代码,总的来说包含下面这样几种格式: 一. <%%> 这种格式实际上就是和asp的用法一样的,只是asp中里面是vbsc ...

  9. 数据库中的undo日志、redo日志

    MySQL中有六种日志文件,分别是:重做日志(redo log).回滚日志(undo log).二进制日志(binlog).错误日志(errorlog).慢查询日志(slow query log).一 ...

  10. 关于java 操作linux命令的 一些相关

    ThreadPoolExecutor pool= new ThreadPoolExecutor(5,10, 3, TimeUnit.SECONDS, new LinkedBlockingQueue&l ...