zoukankan      html  css  js  c++  java
  • L3-016 二叉搜索树的结构 (30 分) 二叉树

      

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

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

    输入格式:

    输入在第一行给出一个正整数N(≤),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤),随后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

    模拟搜索二叉树即可
    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);i--)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define LL long long
    #define pb push_back
    #define fi first
    #define REP(i,N)  for(int i=0;i<(N);i++)
    #define CLR(A,v)  memset(A,v,sizeof A)
    ///////////////////////////////////
    #define inf 0x3f3f3f3f
    #define N 10000
    map<int,int>mp;
    int a[N];
    int n;
    void build(void )
    {
        CLR(a,-1);
        rep(i,1,n)
        {
            int x;
            RI(x);
            int id=1;
            while(1)
            {
                if(a[id]==-1)
                {
                    a[id]=x;
                    mp[x]=id;
                    break;
                }
                else if(x>a[id])
                {
                   id=id*2+1;
                }
                else id*=2;
            }
        }
    }
    int deep(int x)
    {
        int d=1;
        int L=1,R=1;
        if(x==1)return 1;
        while(1)
        {
            if(x>=L&&x<=R)
                return d;
    
            L*=2;
            R=L*2-1;
            d++;
        }
    }
    
    int main()
    {
        RI(n);
        build();
        int q;
        RI(q);
        string str;
        while(q--)
        {
            int a;
            RI(a);
            cin>>str;
            if(str=="is")
            {
                cin>>str;
                cin>>str;
                if(str=="root")
                {
                    if(mp[a]==1)
                        puts("Yes");
                    else puts("No");
    
                }
                else if(str=="parent")
                {
                    cin>>str;
                    int b;RI(b);
                    if(mp[b]/2==mp[a])
                         puts("Yes");
                    else puts("No");
                }
                else if(str=="left")
                {
                    cin>>str;cin>>str;
                    int b;RI(b);
                    if(mp[b]*2==mp[a])
                         puts("Yes");
                    else puts("No");
                }
                else if(str=="right")
                {
                    cin>>str;
                    cin>>str;
                    int b;RI(b);
                    if(mp[b]*2+1==mp[a])
                         puts("Yes");
                    else puts("No");
                }
            }
            else if(str=="and")
            {
                int b;RI(b);
                cin>>str;
                cin>>str;
                if(str=="siblings")
                {
                    if( abs(mp[b]-mp[a])==1  )
                         puts("Yes");
                    else puts("No");
                }
                else if(str=="on")
                {
                    cin>>str>>str>>str;
                    if(mp[a]==0||mp[b]==0)//数据有一些问题  会出现提问的数字不在树里
                        puts("No");
                    else if(deep(mp[a])==deep(mp[b]))
                        puts("Yes");
                    else puts("No");
                }
            }
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    github替代品
    【推荐】通用全面的APP测试用例设计
    Jmeter 性能测试,完整入门篇
    2020年,有哪些特别好用的 app 测试工具?
    如何做好Web端性能测试?
    如何做Web服务的性能测试?
    Office 各版本批量授权(VOL)和激活方法
    django中引用bootstrap的几种方式
    list类型数据执行效率测试
    Linux安装python3环境
  • 原文地址:https://www.cnblogs.com/bxd123/p/10610017.html
Copyright © 2011-2022 走看看