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

题目解答:



#include <bits/stdc++.h>

using namespace std;

typedef struct BSTNode

{

    int data;

    struct BSTNode *lchild, *rchild;

} BSTNode, *BSTree;

typedef struct Que

{

    int a, b;

    int flag;

} Que;

int N, QN;

BSTree T = NULL;

int InsertBST(BSTree &Ti, int data)

{

    if(!Ti)

    {

        Ti = (BSTNode *)malloc(sizeof(BSTNode));

        Ti->data = data;

        Ti->lchild = Ti->rchild = NULL;

        return 1;

    }

    else if(Ti->data > data)

        return InsertBST(Ti->lchild, data);

    else

        return InsertBST(Ti->rchild, data);

}

void CreatBSTree()

{

    scanf("%d", &N);

    for(int i = 0; i < N; ++i)

    {

        int data;

        scanf("%d", &data);

        InsertBST(T, data);

    }

 

}

int GetDepth(BSTree Ti, int data, int dep)

{

    if(Ti != NULL)

    {

        if(Ti->data == data)

            return dep + 1;

        else if(Ti->data > data)

            return GetDepth(Ti->lchild, data, dep + 1);

        else if(Ti->data < data)

            return GetDepth(Ti->rchild, data, dep + 1);

    }

    else

        return 0;

 

}

BSTNode *GetFather(BSTree father, BSTree Ti, int data)

{

    if(Ti != NULL)

    {

        if(Ti->data == data)

            return father;

        else if(Ti->data > data)

            return GetFather(Ti, Ti->lchild, data);

        else if(Ti->data < data)

            return GetFather(Ti, Ti->rchild, data);

    }

    else

        return NULL;

}

BSTNode *GetMe(BSTree Ti, int data)

{

    if(Ti != NULL)

    {

        if(Ti->data == data)

            return Ti;

        else if(Ti->data > data)

            return GetMe(Ti->lchild, data);

        else if(Ti->data < data)

            return GetMe( Ti->rchild, data);

    }

    else

        return NULL;

}

void InputQue(Que *Q)

{

    scanf("%d", &QN);

    for(int i = 0; i < QN; ++i)

    {

        int A, B;

        char temp[50];

        scanf("%d %s ", &Q[i].a, temp);

        if(temp[0] == 'i')

        {

            scanf("%s %s", temp, temp);

            if(temp[0] == 'r' && temp[1] == 'o')

            {

                Q[i].b = Q[i].a;

                Q[i].flag = 1;

            }

            else

            {

                if(temp[0] == 'p')

                {

                    Q[i].flag = 3;

                    scanf(" %s %d", temp, &Q[i].b);

                }

                else if(temp[0] == 'l')

                {

                    Q[i].flag = 4;

                    scanf(" %s %s %d", temp, temp, &Q[i].b);

                }

                else if(temp[0] == 'r')

                {

                    Q[i].flag = 5;

                    scanf(" %s %s %d", temp, temp, &Q[i].b);

                }

            }

        }

        else if(temp[0] == 'a')

        {

            scanf("%d %s %s", &Q[i].b, temp, temp);

            if(temp[0] == 'o')

            {

                Q[i].flag = 6;

                scanf(" %s %s %s", temp, temp, temp);

            }

            else if(temp[0] == 's')

            {

                Q[i].flag = 2;

            }

        }

    }

}

//void Printf(int data)           /* 输出改点的所有信息  */

//{

//       printf("\n\n");

//    if(!GetMe(T, data))

//    {

//        printf("NO Exit\n");

//        return;

//    }

//    int dep = GetDepth(T, data, 0);

//    BSTNode *father = GetFather(NULL, T, data);

//    printf("Test %d\n%d 深度是  %d; ",data, data, dep);

//    if(father == NULL)

//        printf("%d is root", data);

//        else

//        {

//            printf("%d 的父亲是 %d;", data, father->data);

//            if(father->lchild != NULL)

//            {

//                  printf("   父亲的左子树是 %d;", father->lchild->data);

//                if(father->lchild->data == data)

//                  printf("   是左子树 ");

//            }

//            if(father->lchild == NULL)

//             {

//                 printf("   父亲无左子树 ");

//

//             }

//            if(father->rchild != NULL)

//             {

//                 printf("   父亲的右子树是 %d;", father->rchild->data);

//                 if(father->rchild->data == data)

//                  printf("   是右子树;");

//             }

//            if(father->rchild == NULL)

//                printf("   父亲无右子树 ");

//        }

//    printf("\n");

//}

//void Traverse(BSTree Ti)  /* 遍历二叉树 */

//{

//    if(Ti != NULL)

//    {

//        Printf(Ti->data);

//        Traverse(Ti->lchild);

//        Traverse(Ti->rchild);

//    }

//    return;

//}

void IsRoot(Que Q)

{

    BSTNode * temp = GetMe(T, Q.a);

    if(!temp)

    {

        printf("No\n");

    }

    else

    {

        if(T->data == Q.a)

            printf("Yes\n");

        else

            printf("No\n");

    }

}

void IsSiblings(Que Q)

{

    if(!GetMe(T, Q.a) || !GetMe(T, Q.b))

        printf("No\n");

    else

    {

        if(Q.a != Q.b && GetFather(NULL, T, Q.a) == GetFather(NULL, T, Q.b))

            printf("Yes\n");

        else

            printf("No\n");

    }

}

void IsParent(Que Q)

{

    if(!GetMe(T, Q.a) || !GetMe(T, Q.b))

        printf("No\n");

    else

    {

        BSTNode * temp = GetFather(NULL, T, Q.b);

        if(temp != NULL && temp->data == Q.a)

        {

            printf("Yes\n");

        }

        else

            printf("No\n");

    }

}

void IsLeft(Que Q)

{

    BSTNode * temp = GetMe(T, Q.b);

    if(!temp)

        printf("No\n");

    else

    {

        if(temp->lchild != NULL && temp->lchild->data == Q.a)

            printf("Yes\n");

        else

            printf("No\n");

    }

}

void IsRight(Que Q)

{

    BSTNode * temp = GetMe(T, Q.b);

    if(!temp)

        printf("No\n");

    else

    {

        if(temp->rchild != NULL && temp->rchild->data == Q.a)

            printf("Yes\n");

        else

            printf("No\n");

    }

}

void IsSameLevel(Que Q)

{

    if(!GetMe(T, Q.a) || !GetMe(T, Q.b))

        printf("No\n");

    else

    {

        if(GetDepth(T, Q.a, 0) == GetDepth(T, Q.b, 0))

            printf("Yes\n");

        else

        {

            printf("No\n");

        }

    }

}

int main()

{

    Que Q[110];

    CreatBSTree();

//    if(T == NULL)

//        printf("----------------NULL\n");

    InputQue(Q);

//   /*           遍历二叉树               */

//    Traverse(T);

//    /*     测试可能不在二叉树中的数值     */

//    printf("\n****随机测试\n");

//    for(int i = 0; i < 10; ++i)

//        Printf(i);

//    /*  检测要测试的点的对应关系是否正确  */

//    printf("\n要测试的点的对应关系\n");

//    for(int i = 0; i < QN; ++i)

//        printf("%d %d %d\n", Q[i].a, Q[i].b, Q[i].flag);

    for(int i = 0; i < QN; ++i)

    {

        if(Q[i].flag == 1)

        {

            IsRoot(Q[i]);

        }

        else if(Q[i].flag == 2)

        {

            IsSiblings(Q[i]);

        }

        else if(Q[i].flag == 3)

        {

            IsParent(Q[i]);

        }

        else if(Q[i].flag == 4)

        {

            IsLeft(Q[i]);

        }

        else if(Q[i].flag == 5)

        {

            IsRight(Q[i]);

        }

        else if(Q[i].flag == 6)

        {

            IsSameLevel(Q[i]);

        }

    }

}
发布了47 篇原创文章 · 获赞 12 · 访问量 7247

猜你喜欢

转载自blog.csdn.net/weixin_43717681/article/details/89437045