9-7 二叉搜索樹的結構(30 分)

二叉搜索樹或者是一棵空樹,或者是具備下列性質的二叉樹: 若它的左子樹不空,則左子樹上全部結點的值均小於它的根結點的值;若它的右子樹不空,則右子樹上全部結點的值均大於它的根結點的值;它的左、右子樹也分別爲二叉搜索樹。(摘自百度百科)ios

給定一系列互不相等的整數,將它們順次插入一棵初始爲空的二叉搜索樹,而後對結果樹的結構進行描述。你須要能判斷給定的描述是否正確。例如將{ 2 4 1 3 0 }插入後,獲得一棵二叉搜索樹,則陳述句如「2是樹的根」、「1和4是兄弟結點」、「3和0在同一層上」(指自頂向下的深度相同)、「2是4的雙親結點」、「3是4的左孩子」都是正確的;而「4是2的左孩子」、「1和3是兄弟結點」都是不正確的。ui

輸入格式:

輸入在第一行給出一個正整數N(≤),隨後一行給出N個互不相同的整數,數字間以空格分隔,要求將之順次插入一棵初始爲空的二叉搜索樹。以後給出一個正整數M(≤),隨後M行,每行給出一句待判斷的陳述句。陳述句有如下6種:spa

  • 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在同一層上"。

題目保證全部給定的整數都在整型範圍內。code

輸出格式:

對每句陳述,若是正確則輸出Yes,不然輸出No,每句佔一行。ci

輸入樣例:

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

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 220;
int BST[maxn];
int n,m;
typedef struct TNode
{
int data;
struct TNode *left,*right;
}TNode,*BiTree;
void buildtree(BiTree &T,int x)//建樹
{
if(T==NULL)
{
T=(BiTree)malloc(sizeof(BiTree));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else if(x<T->data)
{
buildtree(T->left,x) ;
}
else if(x>T->data)
{
buildtree(T->right,x);
}
}
bool Search_BST(BiTree &T,int x)
{
if(T==NULL||T->data==x) return true;
if(T->data>x) return Search_BST(T->left,x);
else return Search_BST(T->right,x);
}
int Get_value(BiTree &T)
{
if(T==NULL) return -1;
return T->data;
}
bool Judge_Parent(BiTree T,int x,int y)
{
if(T==NULL)
{
return false;
}
if(T->data==x)
{
if(Get_value(T->left)==y||Get_value(T->right)==y)
return true;
}
return (Judge_Parent(T->left,x,y)||Judge_Parent(T->right,x,y));
}
bool Judge_LChild(BiTree T,int x,int y)
{
if(T==NULL)
{
return false;
}
if(T->data==y)
{
if(Get_value(T->left)==x)
return true;
}
return (Judge_LChild(T->left,x,y)||Judge_LChild(T->right,x,y));
}
bool Judge_RChild(BiTree T,int x,int y)
{
if(T==NULL)
{
return false;
}
if(T->data==y)
{
if(Get_value(T->right)==x)
return true;
}
return (Judge_RChild(T->left,x,y)||Judge_RChild(T->right,x,y));
}
void Height(BiTree& T,int e,int h,int &c)
{
if(T==NULL) return;
if(T->data==e) c=h;
Height(T->left,e,h+1,c);
Height(T->right,e,h+1,c);
}
bool Judge_Same(BiTree T,int x,int y)
{
int f1 , f2;
Height(T,x,0,f1);
Height(T,y,0,f2);
if(f1==f2) return true;
return false;
}
bool Judge_Bother(BiTree T,int x,int y)
{
if(T==NULL) return false;
if(T->left!=NULL&&T->right!=NULL)
{
if(T->left->data==x&&T->right->data==y) return true;
if(T->left->data==x&&T->right->data==y) return true;
}
Judge_Bother(T->left,x,y);
Judge_Bother(T->right,x,y);
}
int main()
{
scanf("%d",&n);
BiTree T=NULL;
for(int i=0;i<n;i++) scanf("%d",&BST[i]);
for(int i=0;i<n;i++) buildtree(T,BST[i]);
scanf("%d",&m);
int _a,_b,_c;
string a,b,c;
while(m--)
{
scanf("%d",&_a);
cin>>a;
if(a=="is")
{
cin>>a>>b;
if(b=="parent")
{
cin>>c>>_b;
if(Judge_Parent(T,_a,_b))
printf("Yes\n");
else
printf("No\n");
continue;
}
else if(b=="left")
{
cin>>b>>c;
cin>>_b;
if(Judge_LChild(T,_a,_b))
printf("Yes\n");
else
printf("No\n");
continue;
}
else if(b=="right")
{
cin>>b>>c;
cin>>_b;
if(Judge_RChild(T,_a,_b))
printf("Yes\n");
else
printf("No\n");
continue;
}
else if(b=="root")
{
if(T!=NULL&&T->data==_a)
printf("Yes\n");
else
printf("No\n");
continue;
}
}
else if(a=="and")
{
cin>>_b;
cin>>b>>c;
if(c=="siblings")
{
if(Judge_Bother(T,_a,_b))
printf("Yes\n");
else
printf("No\n");
continue;
}
else
{
getline(cin,b);
if(Judge_Same(T,_a,_b)) // 層數相同
printf("Yes\n");
else
printf("No\n");
continue;
}
}
}
}get

相關文章
相關標籤/搜索