菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
14
0

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

原创
05/13 14:22
阅读数 51695

二叉搜索树的结构(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


题目分析:
数据结构的一个题,因为数据结构老师要求用课本上的语法,故写的比较麻烦,但比较清晰易懂(基本上使用的都是C语言的语法)。
二叉搜索树的结点定义分别有节点值,左右孩子指针,父节点指针,
本题由输入的结点依次插入二叉排序树,然后根据输入的几组语句,判断两个结点的关系;
输入的待判断语句的处理,然后保存有用的值须想明白;
坑点:输入的语句的结点值可能不在已构建好的树中,须判断一下。

测试点:
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF=0x3f3f3f3f;
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode
{
    ElementType Data;
    Position parent;
    BinTree Left;
    BinTree Right;
};

void InsertTree(BinTree &T,int k)
{
    if(!T)
    {
        T=(BinTree)malloc(sizeof(TNode));
        T->Data=k;
        T->Left=T->Right=T->parent=NULL;
        //printf("%d\n",T->Data);
    }
    else
    {
        //printf("1111111111111111\n");
        Position p=T;
        Position pre=NULL;
        while(p)
        {
            pre=p;
            if(p->Data<k)
                p=p->Right;
            else
                p=p->Left;
        }
        p=(Position)malloc(sizeof(TNode));
        p->Data=k;
        p->Left=p->Right=NULL;
        p->parent=pre;
        if(pre->Data>k)
            pre->Left=p;
        else if(pre->Data<k)
            pre->Right=p;
        else
            return ;
    }
}

Position Search(BinTree T,int k)
{
    Position p=T;
    while(p)
    {
        if(p->Data>k)
            p=p->Left;
        else if(p->Data<k)
            p=p->Right;
        else
            return p;
    }
    return NULL;
}
int GetNumber(BinTree T,int k)
{
    int sum=1;
    Position p=T;
    while(p)
    {
        if(p->Data>k)
            p=p->Left;
        else if(p->Data<k)
            p=p->Right;
        else
            return sum;
        sum++;
    }
    return INF;
}
int main()
{
    BinTree T=NULL;
    int n;
    scanf("%d",&n);
    int v;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&v);
        InsertTree(T,v);
    }
    int m;
    scanf("%d",&m);
    getchar();
    char s[110];
    int v1,v2;
    for(int i=0; i<m; i++)
    {
        scanf("%d %s",&v1,s);
        if(!strcmp(s,"is"))
        {
            scanf("%s",s);
            scanf("%s",s);

            if(!strcmp(s,"root"))
            {
                if(T&&T->Data==v1)
                    printf("Yes\n");
                else
                    printf("No\n");
                continue;
            }
            else if(!strcmp(s,"left"))
            {
                Position p1=Search(T,v1);
                scanf("%s",s);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->Left==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"right"))
            {
                Position p1=Search(T,v1);
                scanf("%s",s);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->Right==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"parent"))
            {
                Position p1=Search(T,v1);
                scanf("%s %d",s,&v2);
                Position p2=Search(T,v2);
                if(p2&&p1&&p2->parent==p1)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
        else if(!strcmp(s,"and"))
        {
            scanf("%d %s",&v2,s);
            scanf("%s",s);
            if(!strcmp(s,"siblings"))
            {
                Position p1=Search(T,v1);
                Position p2=Search(T,v2);
                if(p1&&p2&&p1->parent==p2->parent)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else if(!strcmp(s,"on"))
            {
                scanf("%s",s);
                scanf("%s",s);
                scanf("%s",s);
                int n1=GetNumber(T,v1);
                //printf("%d\n",n1);
                int n2=GetNumber(T,v2);
                if(n1==n2&&n1!=INF)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
    }
    return 0;
}
/*

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

 

坚持不懈地努力才能成为大神!

发表评论

0/200
14 点赞
0 评论
收藏
为你推荐 换一批