L3-016. 二叉搜索树的结构
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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. 二叉搜索树的结构的更多相关文章
- PTA 7-2 二叉搜索树的结构(30 分)
7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...
- 【开200数组解决二叉搜索树的建立、遍历】PAT-L3-016. 二叉搜索树的结构——不用链表来搞定二叉搜索树
L3-016. 二叉搜索树的结构 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它 ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- L3-1 二叉搜索树的结构 (30 分)
讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...
- L3-016 二叉搜索树的结构 (30 分) 二叉树
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...
- L3-016 二叉搜索树的结构 (30 分)
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...
- 团体程序设计天梯赛 L3-016. 二叉搜索树的结构
#include <cstdio> #include <cstdlib> #include <string.h> #include <math.h> # ...
随机推荐
- jQuery中的闭包和js中的闭包总结
关于闭包的知识总结下: 一.闭包 1.定义 闭包的关键是作用域,概念是:能有读取其他函数内部的函数 使用的场景有很多,最常见的是函数封装的时候,再就是在使用定时器的时候,会经常用到; //闭包:有参数 ...
- 异步任务分发模块Celery
Celery简介 Celery是一个功能完备即插即用的任务队列.它使得我们不需要考虑复杂的问题,使用非常简单. celery适用异步处理问题,当遇到发送邮件.或者文件上传, 图像处理等等一些比较耗时的 ...
- 初步理解JS的事件机制
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- 安装mysql出现Couldn't find MySQL server (/usr/bin/mysqld_safe)
安装mysql出现Couldn't find MySQL server (/usr/bin/mysqld_safe)Starting MySQL ERROR! Couldn't find MySQL ...
- allure2生成html报告
前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...
- 在GAE中用Python编写webapp进行Post数据采集
#!/usr/bin/env python # -*- coding: cp936 -*- # # Copyright 2007 Google Inc. # # Licensed under the ...
- linux ftp使用相关
ftp 7.7.6.201 21121 name:aaa password:123456
- 大二 Java上学期总结
一学期的Java学习结束了,这学期对程序语言的理解更深了,首先感谢李津老师的教导,这学期收获挺多的,不像上学期,这学期没有任何缺课表现,希望之后的语言程序学习会更加努力. 突然感觉Java的学习如此之 ...
- (ROT-13解密)Flare-On4: Challenge1 login.html
说是FlareOn的逆向 倒不如说是crypto....... 题目不难 F12看源码: document.getElementById("prompt").onclick = f ...
- [Web 前端] 022 js 的基本数据类型及使用
1. Javascript 基本数据类型 1.1 分类 类型 释义 boolean 布尔类型,分 true 与 false number 整型,浮点型 string 字符类型 object 对象类型 ...