当前位置: 首页 > news >正文

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,即"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

做法(写了特别多的函数去判断):

测试点1有负数。

测试点2中在找同层次的时候,a和b可能都不在树中。

代码:

1.建树

2.判断所给陈述句

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;const int N = 110;
struct Node
{int val;Node* lchild;Node* rchild;
}space[N];
map<int,int> ha;
int idx = 0;void insert(Node* root,int v)
{if(v < root->val){if(root->lchild == NULL){root->lchild = &space[idx++];root->lchild->val = v;}else insert(root->lchild,v);}else if(v > root->val){if(root->rchild == NULL){root->rchild = &space[idx++];root->rchild->val = v;}else insert(root->rchild,v);}
}
void test(Node* root)
{cout << root->val << " ";if(root->lchild) test(root->lchild);if(root->rchild) test(root->rchild);
}
int convert(string& str,int id)
{int flag = 1;if(str[id] == '-')id++,flag = 0;int res = 0;for(int i = id;i < str.size() && '0' <= str[i] && str[i] <= '9';i++)res = res * 10 + str[i] - '0';if(!flag) res = 0 - res;return res;
}
bool check_sibling(Node* root,int a,int b)
{if(root == NULL) return false;if(root->lchild && root->rchild){if(root->lchild->val == a && root->rchild->val == b)return true;}if(check_sibling(root->lchild,a,b)) return true;if(check_sibling(root->rchild,a,b)) return true;return false;
}
bool check_parent(Node* root,int a,int b)
{if(root == NULL) return false;if(root->lchild){if(root->val == a && root->lchild->val == b) return true;}if(root->rchild){if(root->val == a && root->rchild->val == b) return true;}if(check_parent(root->lchild,a,b)) return true;if(check_parent(root->rchild,a,b)) return true;return false;
}
int dep(Node* root,int t,int cnt)
{if(root == NULL) return 0;if(root->val == t) return cnt;int tmp = dep(root->lchild,t,cnt + 1);if(tmp) return tmp;tmp = dep(root->rchild,t,cnt + 1);if(tmp) return tmp;return 0;
}
bool check_child(Node* root,int a,int b,char st)
{if(root == NULL) return false;if(st == 'l' && root->lchild && root->val == b && root->lchild->val == a)return true;if(st == 'r' && root->rchild && root->val == b && root->rchild->val == a)return true;if(check_child(root->lchild,a,b,st)) return true;if(check_child(root->rchild,a,b,st)) return true;return false;
}
signed main()
{int n = 0;cin >> n;Node* root = &space[idx++];for(int i = 0;i < n;i++){int t = 0;cin >> t;ha[t] = 1;if(!i) root->val = t;else insert(root,t);}cin >> n;string str;getline(cin,str);//test(root);cout << endl;while(n--){getline(cin,str);int a = 0,b= 0;bool flag = false;if(str.find("root") != -1){a = convert(str,0);if(root->val == a) flag = true;}else if(str.find("siblings") != -1){a = convert(str,0);b = convert(str,str.find("and") + 4);if(a > b) swap(a,b);if(check_sibling(root,a,b)) flag = true;}else if(str.find("parent") != -1){a = convert(str,0);b = convert(str,str.find("of") + 3);if(check_parent(root,a,b)) flag = true;}else if(str.find("level") != -1)//测试点1有负数,测试点2存在不在树中的数{a = convert(str,0);b = convert(str,str.find("and") + 4);// cout << dep(root,a,1) << " " << dep(root,b,1) << endl;// cout << a << " " << ha[a] << " " << b << " " << ha[b] << endl;if(ha[a] && ha[b] && dep(root,a,1) == dep(root,b,1)) flag = true;}else if(str.find("child")){a = convert(str,0);b = convert(str,str.find("of") + 3);char st = ' ';if(str.find("left") != -1) st = 'l';else st = 'r';if(check_child(root,a,b,st)) flag = true;}if(flag) puts("Yes");else puts("No");}return 0;
}

结果:

相关文章:

  • LeetCode //C - 436. Find Right Interval
  • MySQL进阶-----索引的语法与SQL性能分析
  • 【Python百日进阶-Web开发-Peewee】Day290 - Peewee 的扩展(十)架构迁移(下)/ 映射
  • Unity 学习日记 12.小球撞击冰块游戏
  • RabbitMQ介绍
  • 【WPF应用16】WPF如何让Canvas上的元素响应鼠标点击事件?
  • 企业产品网络安全建设日志0328
  • 单源最短路径
  • Qlib-Server:量化库数据服务器
  • Apache HBase(二)
  • 康耐视visionpro-CogBlobTool工具详细说明
  • 指标监控和归因分析——数据异常波动
  • ssm网上订餐管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目采用线性算法
  • 安装paddle detection心得
  • FFmpeg开发笔记(十五)详解MediaMTX的推拉流
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【node学习】协程
  • 78. Subsets
  • Android 控件背景颜色处理
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • ES6--对象的扩展
  • ES6简单总结(搭配简单的讲解和小案例)
  • flask接收请求并推入栈
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • java正则表式的使用
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 工作中总结前端开发流程--vue项目
  • 解析 Webpack中import、require、按需加载的执行过程
  • 目录与文件属性:编写ls
  • 思否第一天
  • 通过npm或yarn自动生成vue组件
  • 原生Ajax
  • 主流的CSS水平和垂直居中技术大全
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • #100天计划# 2013年9月29日
  • #Linux(Source Insight安装及工程建立)
  • (20050108)又读《平凡的世界》
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (WSI分类)WSI分类文献小综述 2024
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (三)Honghu Cloud云架构一定时调度平台
  • (转)Google的Objective-C编码规范
  • .halo勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .naturalWidth 和naturalHeight属性,
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .NET Core 中插件式开发实现