L3-016 二叉搜索树的结构 (30 分)
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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
【题意】
中文题面...题意很明白...思路也很清晰 但就是做不对 很气!这道题必须写一篇题解纪念一下
【解题思路】
先说一下几个坑点:
①其实我最开始是用数组做的,但100个点显然是会爆栈的,然而数据很水过了。所以数组做法肯定不现实,除非是用数组模拟指针。
②既然用指针做就很容易出现段错误,先要确保访问的两个节点都在树中,然后当要访问左孩子是要保证左孩子非空,同理右孩子也是,不然就很容易段错误。
③节点不是正整数,有可能是负数,所以想要存节点的深度什么的就一定要用map。
方法一:
常规思路,好像没啥可说的..反正就是注意细节。
方法二:
在建树的时候记录每个节点的父节点,这样就不用像上一种方法那样写这么多搜索函数了。
但是随之会产生很多问题,就是前面坑点中的第二条,一定要注意细节!!如果出现段错误了,一般就是访问了空节点。
【代码】
方法一:
#include<bits/stdc++.h>
using namespace std;
int flag=0;
typedef struct Node
{
int val;
struct Node *left,*right;
}Node,*ANode;
ANode p;
ANode insertNode(ANode root,int x)
{
if(root==NULL)
{
root=new Node();
root->val=x;
root->left=root->right=NULL;
}
else if(x<root->val)
root->left=insertNode(root->left,x);
else root->right=insertNode(root->right,x);
return root;
}
ANode findNode(ANode root,int x)
{
if(root!=NULL)
{
if(x<root->val)findNode(root->left,x);
else if(x>root->val)findNode(root->right,x);
else return root;
}
}
int judgeLeft(ANode root,int x,int y)
{
ANode t=findNode(root,x);
//ANode t=p;
//printf("%d\n",t->left->val);
if(t->left!=NULL &&y==t->left->val)return 1;
else return 0;
}
int judgeRight(ANode root,int x,int y)
{
ANode t=findNode(root,x);
//ANode t=p;
//printf("%d\n",t->right->val);
if(t->right!=NULL &&y==t->right->val)return 1;
else return 0;
}
void judgeSibling(ANode root,int x,int y)
{
if(root!=NULL && root->left!=NULL && root->right!=NULL)
{
if((root->left->val==x && root->right->val==y) ||
(root->left->val==y && root->right->val==x))flag=1;
judgeSibling(root->left,x,y);
judgeSibling(root->right,x,y);
}
}
int getlevel(ANode root,int x,int level)
{
if(root!=NULL)
{
if(x<root->val)getlevel(root->left,x,level+1);
else if(x>root->val)getlevel(root->right,x,level+1);
else return level;
}
}
int main()
{
ANode root=NULL;
int n,m,a,b,x;
set<int>st;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
st.insert(x);
root=insertNode(root,x);
}
string str;
scanf("%d",&m);
while(m--)
{
scanf("%d",&a);
cin>>str;
flag=0;
if(str=="and")
{
scanf("%d",&b);
getline(cin,str);
if(str[str.size()-1]=='s')//兄弟节点
{
if( st.count(a) && st.count(b))
{
judgeSibling(root,a,b);
if(flag)printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else//在同一层
{
if(st.count(a) && st.count(b))
{
if(getlevel(root,a,1)==getlevel(root,b,1))printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
}
else
{
cin>>str;
cin>>str;
if(str=="root")
{
if(st.count(a)&&root->val==a)printf("Yes\n");
else printf("No\n");
}
else if(str=="parent")
{
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b))
{
if(judgeLeft(root,a,b) || judgeRight(root,a,b))printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else if(str=="left")
{
cin>>str;
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b))
{
if(judgeLeft(root,b,a))printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else
{
cin>>str;
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b))
{
if(judgeRight(root,b,a))printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
}
}
}
方法二:
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int val;
struct Node *left,*right;
}Node,*ANode;
map<int,int>level;
map<int,ANode>fa;
ANode insertNode(ANode root,int x,int deep,ANode pre)
{
if(root==NULL)
{
root=new Node();
root->val=x;
root->left=root->right=NULL;
if(pre==NULL)pre=root;
fa[root->val]=pre;
level[root->val]=deep;
}
else if(x<root->val)
root->left=insertNode(root->left,x,deep+1,root);
else if(x>root->val)
root->right=insertNode(root->right,x,deep+1,root);
return root;
}
int main()
{
int n,k,m,x,a,b;
ANode root=NULL;
scanf("%d",&n);
set<int>st;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
root=insertNode(root,x,1,root);
st.insert(x);
}
string str;
scanf("%d",&m);
while(m--)
{
scanf("%d",&a);
cin>>str;
if(str=="and")
{
scanf("%d",&b);
getline(cin,str);
if(str[str.size()-1]=='s')//兄弟节点
{
if( st.count(a) && st.count(b))
{
if(a!=root->val && b!=root->val && fa[a]->val==fa[b]->val)printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else//在同一层
{
if(st.count(a) && st.count(b))
{
if(level[a]==level[b])printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
}
else
{
cin>>str;
cin>>str;
if(str=="root")
{
if(st.count(a) && root->val==a)printf("Yes\n");
else printf("No\n");
}
else if(str=="parent")
{
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b))
{
if(root->val!=b &&fa[b]->val==a )printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else if(str=="left")
{
cin>>str;
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b) && root->val!=a)
{
ANode t=fa[a];
//printf("%d\n",t->left->val);
if(t != NULL && t->left != NULL &&t->left->val==a && t->val==b)printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
else
{
cin>>str;
cin>>str;
scanf("%d",&b);
if(st.count(a) && st.count(b) && root->val!=a)
{
ANode t=fa[a];
//printf("%d\n",t->right->val);
if(t != NULL && t->right != NULL && t->right->val==a &&t->val==b)printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
}
}
}