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
如果是26分或者28分的注意下 萬一都沒有啊!!!怎麼整 因爲用map 如果都沒有不就說明說都!不存在嘛!這邊沒有考慮 多交了好幾發纔得到這個!ggggg
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* Insert(struct node* root, int x)
{
if(root == NULL)
{
root = new node();
root->data = x;
root->left = root->right = NULL;
}else
{
if(x < root->data)
root->left = Insert(root->left,x);
else if(x > root->data)
root->right = Insert(root->right,x);
}
return root;
}
//void Inorder(struct node* root)
//{
// if(root)
// {
// Inorder(root->left);
// printf("%d ",root->data);
// Inorder(root->right);
// }
// return ;
//}
map<int,int>mp;
void GetId(struct node*root,int x)
{
mp[root->data] = x;
if(root->left)
GetId(root->left,x * 2);
if(root->right)
GetId(root->right,x * 2 + 1);
return ;
}
int main()
{
int n,x;
scanf("%d",&n);
struct node* root = NULL;
for(int i = 1; i <= n; i ++)
{
scanf("%d",&x);
root = Insert(root,x);
}
int t;
scanf("%d",&t);
GetId(root,1);
// map<int,int>::iterator it = mp.begin();
// while(it != mp.end())
// {
// cout<<it->first<<" "<<it->second<<endl;
// it++;
// }
while(t--)
{
int A,B;
char str[10];
scanf("%d",&A);
scanf("%s",str);
int flag;
if(strcmp(str,"and") == 0)
{
scanf("%d%s%s",&B,str,str);
if(strcmp(str,"siblings") == 0)
{
if(mp[A] / 2 == mp[B] / 2)
flag = 1;
else
flag = 0;
}else
{
for(int i = 0 ; i < 3; i ++)
scanf("%s",str);
int cnt1 = 0,cnt2 = 0;
int g1 = mp[A];
while(g1 != 0){
g1 = g1 / 2;
cnt1++;
}
int g2 = mp[B];
while(g2 != 0){
g2 = g2 / 2;
cnt2++;
}
if(cnt1 == cnt2 && mp[A] != 0 && mp[B] != 0)
flag = 1;
else
flag = 0;
}
}else
{
scanf("%s%s",str,str);//the
if(strcmp(str,"root") == 0)
{
if(A == root->data)
flag = 1;
else
flag = 0;
}else if(strcmp(str,"parent") == 0)
{
scanf("%s%d",str,&B);
if(mp[B] / 2 == mp[A])
flag = 1;
else
flag = 0;
}else if(strcmp(str,"left") == 0)
{
scanf("%s%s%d",str,str,&B);
if(mp[B] * 2 == mp[A])
flag = 1;
else
flag = 0;
}else if(strcmp(str,"right") == 0)
{
scanf("%s%s%d",str,str,&B);
if(mp[B] * 2 + 1 == mp[A])
flag = 1;
else
flag = 0;
}
}
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}