天梯赛L3-016 二叉搜索树的结构 c++

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

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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,即"A和B是兄弟结点";
  • A is the parent of B,即"A是B的双亲结点";
  • A is the left child of B,即"A是B的左孩子";
  • A is the right child of B,即"A是B的右孩子";
  • A and B are on the same level,即"A和B在同一层上"。
    题目保证所有给定的整数都在整型范围内。

输出格式:
对每句陈述,如果正确则输出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

题目中给出N的范围不超过100,在N=100并且二叉搜索树形成一条链时(虽然数据没有这么干),需要的数组长度非常大。既然普通数组不行,那我们可以借助map把它当成超级数组来用。树中每个元素仅会出现一次,所以同样可以将每个数对应的数组下标存在map中。
对应题目中可能出现的6种命题,假设A、B下标为a、b

  • A为根: a = 1 a=1 a=1
  • A与B是兄弟: a ∣ 1 = b a|1=b a∣1=b 或者 b ∣ 1 = a b|1=a b∣1=a
  • A是B的双亲: ⌊ b / 2 ⌋ = a \lfloor b/2\rfloor=a b/2=a
  • A是B的左孩子:b*2=a
  • A是B的右孩子:b*2+1=a
  • A与B在同一层:存在非负整数 x 使得 2 x ≤ a 、 b < 2 x + 1 2^x ≤ a、b <2^{x+1} 2xab2x+1
#include <bits/stdc++.h>
using namespace std;
map<int,int> tr;
map<int,int> rt;
void insert(int u,int x)
{
    if(!tr.count(u)) tr[u]=x,rt[x]=u;
    else if(tr[u]>x) insert(u<<1,x);
    else insert(u<<1|1,x);
}
int check(int x)
{
    int res=rt[x];
    return res==0?-0x3f3f3f3f:res;
}
bool isRoot(int a)
{
    return a==1;
}
bool isSiblings(int a,int b)
{
    if(a+b<0) return false;
    return (a|1)==b||(b|1)==a;
}
bool isParent(int a,int b)
{
    if(a+b<0) return false;
    return (b>>1)==a;
}
bool left(int a,int b)
{
    if(a+b<0) return false;
    return (b<<1)==a;
}
bool right(int a,int b)
{
    if(a+b<0) return false;
    return (b<<1|1)==a;
}
bool level(int a,int b)
{
    if(a+b<0) return false;
    int x=0,y=0;
    while((2<<x) <= a) x++;
    while((2<<y) <= b) y++;
    return x==y;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) {
        int x;
        cin>>x;
        insert(1,x);
    }

    int t;
    cin>>t;
    while(t--) {
        int a,b;
        string str;
        bool flag;
        cin>>a>>str;
        if(str[0]=='a') {
            cin>>b>>str>>str;
            if(str[0]=='s') {
                flag=isSiblings(check(a),check(b));
            } else {
                flag=level(check(a),check(b));
                cin>>str>>str>>str;
            }
        } else {
            cin>>str>>str;
            if(str=="root") {
                flag=isRoot(check(a));
            } else if(str[0]=='p') {
                cin>>str>>b;
                flag=isParent(check(a),check(b));
            } else if(str[0]=='l') {
                cin>>str>>str>>b;
                flag=left(check(a),check(b));
            } else if(str[0]=='r') {
                cin>>str>>str>>b;
                flag=right(check(a),check(b));
            }
        }
        cout<<(flag?"Yes":"No")<<'\n';
    }
    return 0;
}
  • 18
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值