PTA L3-016 二叉搜索树的结构 (30分) 【BST|测试点细节】

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

Reference: 测试数据细节在这里插入图片描述

  • 注意查询数据不在树内的情况,注意负数情况
  • 谨慎初始化数据
#include<iostream>
#include<cstring> //memset
#include<string>
#include<cstdio> //scanf
#include<string>
#include<utility> // pair
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<functional> //less greater 
using namespace std;
// Work for the PAT 2020.09
#define LL long long int
#define ll LL
#define INF 0x3f3f3f3f
const int maxn = 5e2 + 20;
int N, Q;
int sz, cs, kz;
int Fa[maxn]; // 记录id的父亲节点Fa[id];
int Lc[maxn]; // 记录id的左节点Lc[id]
int Rc[maxn]; // 记录id的右节点Rc[id]
int Lev[maxn]; // 记录层数
set<int> S; // 通过 set 来标记一个元素在不在 树 内部
map<int, int> M;// 考虑到可能存在负数,所以通过 map 来代替 vis 索引
int RM[maxn];
string str[12];
struct Node {
	int data;
	int numed; // 标记这个结点是否已经被赋值了
	int level;
	Node *Left;
	Node *Right;
	Node() {
		data = 0; numed = 0; level = 0; Left = NULL; Right = NULL;
	}
};
Node *rt = new Node(); // 初始化根节点
void init_(); // 初始化
void read_();
void work_();
int Str_Int(string A); // 将 Stirng 转换成 Int
void Insert(Node *rt, int x) {
	if (rt->numed == 0) { // 当前节点没有被赋值
		rt->data = x; rt->numed = 1;
	}
	else if (x < rt->data) {
		if (rt->Left == NULL) rt->Left = new Node();
		Insert(rt->Left, x);
	}
	else {
		if (rt->Right == NULL) rt->Right = new Node();
		Insert(rt->Right, x);
	}
}
void Dfs(Node *rt, int lev) {
	if (rt == NULL) return;
	Lev[M[rt->data]] = lev;
	if (rt->Left != NULL) {
		Fa[M[rt->Left->data]] = M[rt->data];
		Lc[M[rt->data]] = M[rt->Left->data];
		Dfs(rt->Left, lev + 1);
	}
	if (rt->Right != NULL) {
		Fa[M[rt->Right->data]] = M[rt->data];
		Rc[M[rt->data]] = M[rt->Right->data];
		Dfs(rt->Right, lev + 1);
	}
}
int main() {
	init_();
	read_();
	return 0;
}

void init_() {
	sz = cs = kz = 0;
	for (int i = 1; i <= N; i++) {
		Fa[i] = i; // 初始情况下所有节点的父亲指向自己,在这里代表没有父亲
	}
}

int Str_Int(string A) {
	int len = A.length(); int f = 0; int tmp = 0;
	if (A[0] == '-') f = 1;
	for (int i = f; i < len; i++) {
		tmp = tmp * 10 + A[i] - '0';
	}
	if (f) tmp = tmp * -1;
	return tmp;
}

void read_() {
	scanf("%d", &N); int x;
	for (int i = 1; i <= N; i++) {
		scanf("%d", &x);
		M[x] = ++cs;
		RM[cs] = x;
		S.insert(x); Insert(rt, x);
	}
	Dfs(rt, 0);
	scanf("%d", &Q); getchar(); string Tmp;
	for (int j = 1; j <= Q; j++) {
		getline(cin, Tmp); kz = 1;
		int len = Tmp.length();
		for (int i = 0; i < 12; i++) {
			str[i].clear();
		}
		for (int i = 0; i < len; i++) { // 将 整行的字符串类型进行分块
			if (Tmp[i] == ' ') kz++;
			else str[kz] += Tmp[i];
		}
		work_();
	}
}
void work_() {
	int num1 = 0; int num2 = 0; int flag = 0;
	if (str[4] == "root") {
		num1 = Str_Int(str[1]);
		if (S.find(num1) != S.end()) // 检查数据是否在树内部
			if (num1 == rt->data) flag = 1;
	}
	else if (str[5] == "siblings") {
		num1 = Str_Int(str[1]); num2 = Str_Int(str[3]);
		int id1 = M[num1]; int id2 = M[num2];
		if (S.find(num1) != S.end() && S.find(num2) != S.end())
			if (id1 != id2 && Fa[id1] == Fa[id2]) flag = 1;
	}
	else if (str[4] == "parent") {
		num1 = Str_Int(str[1]); num2 = Str_Int(str[6]);
		int id1 = M[num1]; int id2 = M[num2];
		if (S.find(num1) != S.end() && S.find(num2) != S.end())
			if (id1 != id2 && Fa[id2] == id1) flag = 1;
	}
	else if (str[4] == "left") {
		num1 = Str_Int(str[1]); num2 = Str_Int(str[7]);
		int id1 = M[num1]; int id2 = M[num2];
		if (S.find(num1) != S.end() && S.find(num2) != S.end())
			if (id1 != id2 && Lc[id2] == id1) flag = 1;
	}
	else if (str[4] == "right") {
		num1 = Str_Int(str[1]); num2 = Str_Int(str[7]);
		int id1 = M[num1]; int id2 = M[num2];
		if (S.find(num1) != S.end() && S.find(num2) != S.end())
			if (id1 != id2 && Rc[id2] == id1) flag = 1;
	}
	else if (str[8] == "level") {
		num1 = Str_Int(str[1]); num2 = Str_Int(str[3]);
		int id1 = M[num1]; int id2 = M[num2];
		if (S.find(num1) != S.end() && S.find(num2) != S.end())
			if (Lev[id1] == Lev[id2]) flag = 1;
	}
	//cout << num1 << "   " << num2 << endl;
	if (flag) puts("Yes");
	else puts("No");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值