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

本文介绍如何通过给定的一系列互不相等的整数构建二叉搜索树,并验证关于树结构的陈述句是否正确。利用unordered_map记录节点深度及父节点信息,通过sscanf函数解析字符串中的数字。

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

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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

测试点:

思路: 

用unorder_map存每个节点的深度(自顶向下)和每个节点的父亲节点。

使用string自带的find函数,查找字符串中是否含有某个字符串,若没有返回值为stirng::npos

使用sscanf函数直接提取字符串中的数字。。。(这个函数是真的好用!)。

 

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node *l,*r;
};
struct point{
	int fa,d=-1;
};
unordered_map<int,point> q;
struct node* bt(struct node *p,int x,int fa,int level)
{
    if(p==NULL)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=x;
        p->l=p->r=NULL;
        if(fa!=-1)
        {
            q[x].fa=fa;
            q[x].d=level;
        }
    }
    else
    {
        if(p->data>x)
        {
            p->l=bt(p->l,x,p->data,level+1);
        }
        else if(p->data<x)
        {
            p->r=bt(p->r,x,p->data,level+1);
        }
    }
    return p;
};
void test(struct node*p)
{
    if(p)
    {
        test(p->l);
        cout<<p->data<<" ";
        test(p->r);
    }
}
int main()
{
    int n,i,x,m,y;
    struct node*root=NULL;
    cin>>n;
    int r;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&x);
        if(i==1)
            r=x;
        root=bt(root,x,-1,0);
    }
    q[r].fa=-1;
    q[r].d=0;
    cin>>m;
    cin.get();
    string s;
    int f=0;
    while(m--)
    {
        y=-1;
        f=0;
        getline(cin,s);
        if(s.find("root")!=string::npos)
        {
            sscanf(s.c_str(),"%d is the root",&x);
            if(x==r)
            {
                f=1;
            }
        }
        else if(s.find("siblings")!=string::npos)
        {
            sscanf(s.c_str(),"%d and %d are siblings",&x,&y);
            if(q[x].fa==q[y].fa)
            {
                f=1;
            }
        }
        else if(s.find("level")!=string::npos)
        {
            sscanf(s.c_str(),"%d and %d are on the same level",&x,&y);
            if(q[x].d==q[y].d)
            {
                f=1;
            }
        }
        else if(s.find("parent")!=string::npos)
        {
            sscanf(s.c_str(),"%d is the parent of %d",&x,&y);
            if(q[y].fa==x)
            {
                f=1;
            }

        }
        else if(s.find("left")!=string::npos)
        {
            sscanf(s.c_str(),"%d is the left child of %d",&x,&y);
            if(q[x].fa==y&&x<y)
            {
                f=1;
            }
        }
        else if(s.find("right")!=string::npos)
        {
            sscanf(s.c_str(),"%d is the right child of %d",&x,&y);
            if(q[x].fa==y&&x>y)
            {
                f=1;
            }
        }
        if(q[x].d==-1||q[y].d==-1&&y!=-1)f=0;
        if(f) cout<<"Yes\n";
        else cout<<"No\n";

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值