PTA - 二叉搜索树的结构 (30 分)——建树,输入处理

3 篇文章 0 订阅

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


题意:

定义:二叉搜索树是一棵空树,或者是具有下列性质的二叉树:

  • 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
  • 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  • 它的左、右子树也分别为二叉搜索树。

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树。
给出 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在同一层上"。

输入样例

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

思路:

两个要解决的大问题:

1. 如何建树?

要理解顺次插入的意思,就是从根节点开始判断,找到第一个满足的位置插入。

所以每插入一个值 x,从根节点开始判断:

  • 如果插入值 x 小于当前节点 u 的值,说明 x 应该插入到左子树。
    如果其左儿子没有值的话,那么就找到了空位置,将 x 插入到该位置;
    否则,将 x 继续和其左儿子判断,即递归到左儿子。
  • 同理,如果插入值 x 大于当前节点 u 的值,说明 x 应该插入到右子树。
    如果其右儿子没有值的话,那么就找到了空位置,将 x 插入到该位置;
    否则,将 x 继续和其右儿子判断,即递归到右儿子。

开一个结构体数组 node 来记录所有点,其中有相关元素:该点中存的值,左儿子,右儿子,深度,父节点。
注意这里的 node[i],i 是点的编号,并不是该点中存的值。
因为所有点中存的值都互不相等,所以可以用 map 映射出每个点在树中的节点编号。

struct node{
	int x, left, right;
	int flor;
	int fa;
}node[N];
int cnt;

void dfs(int x, int u)
{
	if(x < node[u].x) //如果x小于当前节点u的值,那么在u的左子树中 
	{
		if(!node[u].left) //u没有左儿子,就让x作为左儿子 
		{
			cnt++;
			node[cnt].x = x;
			node[u].left = cnt;
			node[cnt].fa = u;
			node[cnt].flor = node[u].flor + 1;
			mp[x] = cnt;
		}
		else{ //有左儿子就递归到左儿子 
			dfs(x, node[u].left);
		}
	}
	else //x在u的右子树中 
	{
		if(!node[u].right) //u没有右儿子,就让x作为右儿子 
		{
			cnt++;
			node[cnt].x = x;
			node[u].right = cnt;
			node[cnt].fa = u;
			node[cnt].flor = node[u].flor + 1;
			mp[x] = cnt;
		}
		else{ //有右儿子就递归到右儿子 
			dfs(x, node[u].right);
		}
	}
}

于是,就解决了建树问题。


2. 如何解决输入问题?

此外,注意到输入格式,现在要知道每次询问的是哪种格式,还要得到其中的数字,这怎么搞?

发现这些句子中有唯一出现的单词,我们只要发现所给的询问出现了特定的单词,就能锁定是哪种句子。
判断字符串 s 中是否出现了子串 t,可用 string 中的 find 函数
if(s.find(t) != s.npos) return true; ——发现串 t 第一次出现的位置不为空,则找到。

但是,锁定了句子的种类,如何得到其中的数字呢?
可以用 sscanf:从已知字符串读取格式化输入
用法为:
sscanf(char *str, char *format, ...) ——第一个位置为字符数组str[],第二个位置为输入格式,第三个位置为需要信息的存储位置
在本例中,sscanf(s, "%d and %d are on the same level", &x, &y);
就相当于,把字符串 s 当作从键盘输入的,然后像用 scanf 一样接收其中想要的信息。

于是便解决输入问题了。


完整Code:

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
map<int,int> mp;

const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N];

struct node{
	int x, left, right;
	int flor;
	int fa;
}node[N];
int cnt;

void dfs(int x, int u)
{
	if(x < node[u].x) //如果x小于当前节点u的值,那么在u的左子树中 
	{
		if(!node[u].left) //u没有左儿子,就让x作为左儿子 
		{
			cnt++;
			node[cnt].x = x;
			node[u].left = cnt;
			node[cnt].fa = u;
			node[cnt].flor = node[u].flor + 1;
			mp[x] = cnt;
		}
		else{ //有左儿子就递归到左儿子 
			dfs(x, node[u].left);
		}
	}
	else //x在u的右子树中 
	{
		if(!node[u].right) //u没有右儿子,就让x作为右儿子 
		{
			cnt++;
			node[cnt].x = x;
			node[u].right = cnt;
			node[cnt].fa = u;
			node[cnt].flor = node[u].flor + 1;
			mp[x] = cnt;
		}
		else{ //有右儿子就递归到右儿子 
			dfs(x, node[u].right);
		}
	}
}

signed main(){
	cin>>n;
	
	int x; cin>>x; //先确定根节点 
	node[1].x = x;
	node[1].flor = 1;
	mp[x] = 1;
	cnt = 1;
	
	for(int i=2;i<=n;i++)
	{
		cin>>x;
		dfs(x, 1);
	}
	
	cin>>m;
	string s;
	getline(cin, s);
	
	while(m--)
	{
		getline(cin, s);
		int x = 0, y = 0;
		
		if(s.find("root") != s.npos)
		{
			sscanf(s.c_str(), "%d is the root", &x);
			if(mp[x] == 1) cout<<"Yes\n";
			else cout<<"No\n";
		}
		if(s.find("siblings") != s.npos)
		{
			sscanf(s.c_str(), "%d and %d are siblings", &x, &y);
			x = mp[x], y = mp[y];
			
			if(!x || !y) cout<<"No\n"; //需要先判断该值是否在树中
			else if(node[x].fa != node[y].fa) cout<<"No\n";
			else cout<<"Yes\n";
		}
		if(s.find("parent") != s.npos)
		{
			sscanf(s.c_str(), "%d is the parent of %d", &x, &y);
			x = mp[x], y = mp[y];
			
			if(!x || !y) cout<<"No\n";
			else if(node[y].fa == x) cout<<"Yes\n";
			else cout<<"No\n";
		}
		if(s.find("left") != s.npos)
		{
			sscanf(s.c_str(), "%d is the left child of %d", &x, &y);
			x = mp[x], y = mp[y];
			
			if(!x || !y) cout<<"No\n";
			else if(node[y].left == x) cout<<"Yes\n";
			else cout<<"No\n";
		}
		if(s.find("right") != s.npos)
		{
			sscanf(s.c_str(), "%d is the right child of %d", &x, &y);
			x = mp[x], y = mp[y];
			
			if(!x || !y) cout<<"No\n";
			else if(node[y].right == x) cout<<"Yes\n";
			else cout<<"No\n";
		}
		if(s.find("level") != s.npos)
		{
			sscanf(s.c_str(), "%d and %d are on the same level", &x, &y);
			x = mp[x], y = mp[y];
			
			if(!x || !y) cout<<"No\n";
			else if(node[y].flor == node[x].flor) cout<<"Yes\n";
			else cout<<"No\n";
		}
	}
	
	return 0;
}

经验:

好长时间没做过类似的题了,导致建树都不会了ww…

还好,碰到了这道题,学会了几个知识点:

  1. 如何构建一棵二叉搜索树?
  2. 掌握了用 find 函数sscanf 来处理此类的输入。

不错。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值