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
题解:主要就是给出来的点可能不在树上,用map标记一下即可,其他的直接模拟即可~~

#include<bits/stdc++.h>
using namespace std;
string str1,str2,str3,str4,str5,str6;
int n,x,y,q;
map<int,int>mp;
struct node
{
    node *l,*r;
    int data;
};
node *Insert(node *root,int x)
{
    if(root==NULL)
    {
        root=new node;
        root->data=x;
        root->l=root->r=NULL;
        return root;
    }
    else
    {
        if(x<root->data)
            root->l=Insert(root->l,x);
        else if(x>root->data)
            root->r=Insert(root->r,x);
    }
    return root;
}

int Find_F(node *root,int x,int y)//x是否是y的父亲
{
    if(root)
    {
        if(root->data==x)
        {
            if(root->l&&root->l->data==y)
            {
                return 1;
            }
            if(root->r&&root->r->data==y)
            {
                return 1;
            }
        }
        if(x<root->data)//为了保证结果的正确性,要正确利用二叉搜索的性质进行return,以下同理
            return Find_F(root->l,x,y);
        if(x>root->data)
            return Find_F(root->r,x,y);
    }
    return 0;
}
int Find_L(node *root,int x,int y)//x是否是y的左孩子
{
    if(root)
    {
        if(root->data==y)
        {
            if(root->l&&root->l->data==x)
            {
                return 1;
            }
        }
        if(y<root->data)
            return Find_L(root->l,x,y);
        if(y>root->data)
            return Find_L(root->r,x,y);
    }
    return 0;
}
int Find_R(node *root,int x,int y)//x是否是y的右孩子
{
    if(root)
    {
        if(root->data==y)
        {
            if(root->r&&root->r->data==x)
            {
                return 1;
            }
        }
        if(y<root->data)
            return Find_R(root->l,x,y);
        if(y>root->data)
            return Find_R(root->r,x,y);
    }
    return 0;
}
int Find_C(node *root,int x,int y)//x和y是否为兄弟节点
{
    if(root)
    {
        if(root->l&&root->r)
        {
            if(root->l->data==x&&root->r->data==y)
            {
                return 1;
            }
            if(root->r->data==x&&root->l->data==y)
            {
                return 1;
            }
        }
        return max(Find_C(root->l,x,y),Find_C(root->r,x,y));//不管左右,只要找到即可
    }
    return 0;
}
int Find_Level(node *root,int x,int step)//x的深度或高度
{
    if(root)
    {
        if(root->data==x)
            return step;
        if(x<root->data)
            return Find_Level(root->l,x,step+1);
        if(x>root->data)
            return Find_Level(root->r,x,step+1);
    }
    return 0;
}

int main()
{
    cin>>n;
    node *root=NULL;
    for(int i=1; i<=n; i++)
    {
        int r;
        cin>>r;
        mp[r]++;//将其标记,作为判断条件
        root=Insert(root,r);
    }
    cin>>q;
    while(q--)
    {
        cin>>x;
        cin>>str1;
        if(str1=="is")
        {
            cin>>str2>>str3;
            if(str3=="root")
            {
                if(root->data==x)
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
            else if(str3=="parent")
            {
                cin>>str4>>y;
                int r=Find_F(root,x,y);//x是否是y的father
                if(r&&mp[x]&&mp[y])
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
            else if(str3=="left")
            {
                cin>>str4>>str5>>y;
                int r=Find_L(root,x,y);//x是否是y的左孩子
                if(r&&mp[x]&&mp[y])
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
            else if(str3=="right")
            {
                cin>>str4>>str5>>y;
                int r=Find_R(root,x,y);//x是否是y的右孩子
                if(r&&mp[x]&&mp[y])
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
        }
        else
        {
            cin>>y>>str2>>str3;//是否为兄弟节点
            if(str3=="siblings")
            {
                int r=Find_C(root,x,y);
                if(r&&mp[x]&&mp[y])
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
            else
            {
                cin>>str4>>str5>>str6;//最后一种情况,看是否在同一层上
                int Level_x=Find_Level(root,x,1);
                int Level_y=Find_Level(root,y,1);
                if((Level_x==Level_y)&&Level_x&&mp[x]&&mp[y])
                    cout<<"Yes"<<endl;
                else
                    cout<<"No"<<endl;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cherish-lin/p/10599660.html