zoukankan      html  css  js  c++  java
  • L3-016

    L3-016 二叉搜索树的结构 (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
    
    作者: 陈越
    单位: 浙江大学
    时间限制: 400 ms
    内存限制: 64 MB
    代码长度限制: 16 KB

    用指针实现的二叉树,map判断相等之前要记得判断是否存在。

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    struct node {
        int val;
        node *left, *right;
        node(int _val): val(_val) {
            left = nullptr;
            right = nullptr;
        }
    };
    node* insert(int val, node* root) {
        if(root==NULL) {
            root = new node(val);
            return root;
        }
        if(val<(root->val)) root->left = insert(val, root->left);
        else root->right = insert(val, root->right);
        return root;
    }
    map<int,int> H;
    map<int,int> parent;
    map<int,int> leftson;
    map<int,int> rightson;
    
    void bfs(node* root) {
        queue<node*> q;
        H[root->val] = 1;
        q.push(root);
        while(!q.empty()) {
            node *now = q.front(); q.pop();
            if(now->left!=NULL) {
                node *lson = now->left;
                H[lson->val] = H[now->val]+1;
                parent[lson->val] = now->val;
                leftson[now->val] = lson->val;
                q.push(now->left);
            }
            if(now->right!=NULL) {
                node *rson = now->right;
                H[rson->val] = H[now->val]+1;
                parent[rson->val] = now->val;
                rightson[now->val] = rson->val;
                q.push(now->right);
            }
        }
    }
    int n, m;
    string s;
    
    int main() {
        scanf("%d", &n);
        node *root = new node(inf);
        for (int x, i=1; i<=n; ++i) {
            scanf("%d", &x);
            root = insert(x, root);
        }
        root = root->left;
        bfs(root);
        scanf("%d", &m);
        getchar();
        while(m--) {
            bool fg=false;
            getline(cin, s);
            int cnt = 0, x, y;
            for (int i=0; s[i]; ++i) if(s[i]==' ') ++cnt;
            if(cnt==3) {
                sscanf(s.c_str(), "%d", &x);
                fg = (root->val==x);
            } else if(cnt==4) {
                sscanf(s.c_str(), "%d and %d", &x, &y);
                fg = (parent.count(x)&&parent.count(y)&&parent[x]==parent[y]);
            } else if(cnt==5) {
                sscanf(s.c_str(), "%d is the parent of %d", &x, &y);
                fg = (parent.count(y)&&parent[y]==x);
            } else if(cnt==7) {
                sscanf(s.c_str(), "%d and %d", &x, &y);
                fg = (H.count(x) && H.count(y) && H[x]==H[y]);
            } else if(cnt==6) {
                char op[10];
                sscanf(s.c_str(), "%d is the %s child of %d", &x, op, &y);
                if(!strcmp(op, "left")) fg = (leftson.count(y) && leftson[y]==x);
                else fg = (rightson.count(y) && rightson[y]==x);
            }
            puts(fg?"Yes": "No");
        }
        return 0;
    }
  • 相关阅读:
    c# linq查询的等于大于符号是什么意思?
    c# Socket tcpClient处理连接超时方式timeout
    不同网段服务器与客户端连接问题解决方法TCP/IP_C/S
    Http请求响应及SpringMVC相关注解解析
    Nginx部署项目,转发规则,负载均衡配置
    Linux redis设置后台启动
    Linux centos7安装Jdk nginx redis
    SpringCloud整合SpringSecurity JWT进行认证 ,鉴权
    C#中class与struct的区别概述
    列举ASP.NET 页面之间传递值的几种方式
  • 原文地址:https://www.cnblogs.com/acerkoo/p/10599423.html
Copyright © 2011-2022 走看看