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

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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",即"A和B是兄弟结点";
  • "A is the parent of B",即"A是B的双亲结点";
  • "A is the left child of B",即"A是B的左孩子";
  • "A is the right child of B",即"A是B的右孩子";
  • "A and B are on the same level",即"A和B在同一层上"。

题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出“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;
struct tree
{
tree *left, *right, *f;
int height,data;
}*root;
int n,d,k,a,b;
map<int,tree *> q;
map<int,int>o;
tree *creat(int d)
{
tree *p = new tree();
p->left = NULL;
p->right = NULL;
p->f = NULL;
p->height = ;
p->data = d;
q[d] = p;
o[d] = ;
return p;
}
void insert_(tree *root,tree *t)
{
if(t->data > root->data)
{
if(root -> right == NULL)root -> right = t,t -> f = root,t-> height = root -> height + ;
else insert_(root -> right,t);
}
else
{
if(root -> left == NULL)root -> left = t,t -> f = root,t-> height = root -> height + ;
else insert_(root -> left,t);
}
}
int main()
{
cin>>n;
if(n)
{
cin>>d;
root = creat(d);
}
for(int i = ;i < n;i ++)
{
cin>>d;
tree *p = creat(d);
insert_(root,p);
}
cin>>k;
for(int i = ;i < k;i ++)
{
cin>>a;
string op;
cin>>op;
if(op == "is")
{
cin>>op>>op;
if(op == "root")
{
if(o[a] && root == q[a])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
else if(op == "parent")
{
cin>>op>>b;
if(o[a] && o[b] && q[b] -> f == q[a])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
else if(op == "left")
{
cin>>op>>op>>b;
if(o[a] && o[b] && q[b] -> left == q[a])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
else
{
cin>>op>>op>>b;
if(o[a] && o[b] && q[b] -> right == q[a])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
else
{
cin>>b>>op>>op;
if(op == "on")
{
cin>>op>>op>>op;
if(o[a] && o[b] && q[a] -> height == q[b] -> height)
{
cout<<"Yes"<<endl;
}
else cout<<"No"<<endl;
}
else
{
if(o[a] && o[b] && q[a] -> f == q[b] -> f)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}
}

数组建树。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
int pos;
struct BST {
int l,r,f,d,h;
BST() {
l = r = f = d = h = ;
}
}t[];
map<int,int> mp;
void Insert(int h,int no,int d) {
if(d < t[no].d) {
if(t[no].l) Insert(h + ,t[no].l,d);
else {
t[no].l = ++ pos;
t[pos].d = d;
t[pos].f = no;
t[pos].h = h + ;
}
}
else {
if(t[no].r) Insert(h + ,t[no].r,d);
else {
t[no].r = ++ pos;
t[pos].d = d;
t[pos].f = no;
t[pos].h = h + ;
}
}
}
int main() {
int n,d,m;
cin>>n;
cin>>d;
t[++ pos].d = d;
mp[d] = pos;
for(int i = ;i < n;i ++) {
cin>>d;
Insert(,,d);
mp[d] = pos;
}
cin>>m;
string s;
int a,b;
for(int i = ;i < m;i ++) {
cin>>a;
cin>>s;
bool flag = pos && mp[a] != ;
if(s == "is") {
cin>>s>>s;
if(s == "root") {
flag &= t[].d == a;
}
else if(s == "parent") {
cin>>s>>b;
flag &= mp[b] != && t[t[mp[b]].f].d == a;
}
else if(s == "left") {
cin>>s>>s>>b;
flag &= mp[b] != && t[t[mp[b]].l].d == a;
}
else {
cin>>s>>s>>b;
flag &= mp[b] != && t[t[mp[b]].r].d == a;
}
}
else {
cin>>b>>s>>s;
flag &= mp[b] != ;
if(s == "on") {
cin>>s>>s>>s;
flag &= t[mp[a]].h == t[mp[b]].h;
}
else {
flag &= t[mp[a]].f == t[mp[b]].f;
}
}
puts(flag ? "Yes" : "No");
}
}

L3-016. 二叉搜索树的结构的更多相关文章

  1. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

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

    二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历   二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...

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

    二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...

  4. 【开200数组解决二叉搜索树的建立、遍历】PAT-L3-016. 二叉搜索树的结构——不用链表来搞定二叉搜索树

    L3-016. 二叉搜索树的结构 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它 ...

  5. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  6. L3-1 二叉搜索树的结构 (30 分)

    讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...

  7. L3-016 二叉搜索树的结构 (30 分) 二叉树

    二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...

  8. L3-016 二叉搜索树的结构 (30 分)

    二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...

  9. 团体程序设计天梯赛 L3-016. 二叉搜索树的结构

    #include <cstdio> #include <cstdlib> #include <string.h> #include <math.h> # ...

随机推荐

  1. OperationCenter Docker运行环境及其依赖启动脚本

    1.Portainer docker rm -f portainer docker run -d -p : --name portainer --restart always portainer/po ...

  2. ssm的自动类型转换器

    1.jsp页面将String 转换成employee类型 <form action="testConversionServiceConverer" method=" ...

  3. hibernate缓存机制与N+1问题

    在项目中遇到的趣事 本文基于hibernate缓存机制与N+1问题展开思考, 先介绍何为N+1问题 再hibernate中用list()获得对象: /** * 此时会发出一条sql,将30个学生全部查 ...

  4. setTimeout(function(){}, 0);

    for (var i = 0; i < 3; i++) { setTimeout(function() { console.log(i); }, 0); console.log(i); } 结果 ...

  5. windows下简单安装postgres

    目前版本是PostgreSQL 9.6,它经过以下平台认证: 32位Windows Windows 7,8和10 Windows 2008 Server 64位Windows Windows 7,8和 ...

  6. 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nvarchar

    .TrimEnd() 怀疑是否SqlBulkCopy是否存在某种bug,故而在系统中改写代码,用单个sql的插入数据方式,用循环逐条导入.结果是没问题.难道真的是SqlBulkCopy有某种bug?上 ...

  7. ELK7.4.0分析nginx json日志

    ELK7.4.0单节点部署 环境准备 安装系统,数据盘设置为/srv 内核优化参考 我们需要创建elk专用的账号,并创建所需要的目录并授权 useradd elk; mkdir /srv/{app,d ...

  8. IIS7下配置web.config隐藏index.php

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  9. python函数-内置函数

    官方文档[http://www.cnblogs.com/dingkailinux/p/7954484.html] 1.int()函数,表示整形,如 1,2,-1,-2. 2.float()函数,表示浮 ...

  10. 第9周总结&实验报告7

    完成火车站售票程序的模拟. 要求:(1)总票数1000张:(2)10个窗口同时开始卖票:(3)卖票过程延时1秒钟:(4)不能出现一票多卖或卖出负数号票的情况.一:实验代码 package first; ...