二叉搜索树的结构
Description
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子
树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不
空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分
别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,
然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将
{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1
和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双
亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟
结点”都是不正确的。
Input
输入在第一行给出一个正整数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在同一层上"。
题目保证所有给定的整数都在整型范围内。
Output
对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。
Sample Input
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
Sample Output
Yes
Yes
Yes
Yes
Yes
No
No
No
思路
#include<iostream>
#include<string>
using namespace std;
typedef struct TreeNode* Tree;
struct TreeNode {
int v;
Tree Left, Right;
int level;
};
Tree NewNode(int V)
{
Tree T = (Tree)malloc(sizeof(struct TreeNode));
T->v = V;
T->Left = T->Right = NULL;
T->level = 0;
return T;
}
Tree insert(Tree T, int V)
{
if (!T)T = NewNode(V);
else
{
if (V > T->v)
{
T->Right = insert(T->Right, V);
T->Right->level = T->level + 1;
}
else
{
T->Left = insert(T->Left, V);
T->Left->level = T->level + 1;
}
}
return T;
}
Tree MakeTree(int n)
{
Tree T;
int i, V;
cin >> V;
T = NewNode(V);
for (int i = 1; i < n; i++)
{
cin >> V;
T = insert(T, V);
}
return T;
}
Tree findFather(Tree T, int V, Tree parent = nullptr) {
if (!T) return nullptr; // 如果树为空,返回 nullptr
if (T->v == V) return parent; // 找到节点 V,返回其父节点
// 根据值继续在树中查找
if (V < T->v)
return findFather(T->Left, V, T);
else
return findFather(T->Right, V, T);
}
Tree findNode(Tree T, int V) {
if (!T)return nullptr;
if (T->v == V)return T;
if (V < T->v)return findNode(T->Left, V);
else return findNode(T->Right, V);
}
int findLevel(Tree T, int V)
{
if (!T)return -1;
if (T->v == V) return T->level;
if (V < T->v) return findLevel(T->Left, V);
else return findLevel(T->Right, V);
}
void judge(Tree T,int m)
{
for (int i = 1; i <= m; i++)
{
string s;
getline(cin,s);
if (s.find("root")!=-1) {
int a;
sscanf(s.c_str(), "%d", &a);
if (a == T->v) puts("Yes");
else puts("No");
}
else if(s.find("siblings")!=-1) {
int a, b;
sscanf(s.c_str(), "%d and %d are siblings", &a, &b);
if (findNode(T, a) == nullptr || findNode(T, b) == nullptr)
{
puts("No");
continue;
}
if (findFather(T, a) == findFather(T, b)) puts("Yes");
else puts("No");
}
else if (s.find("level")!=-1) {
int a, b;
sscanf(s.c_str(), "%d and %d are on the same level",&a,&b);
if (findNode(T, a) == nullptr || findNode(T, b) == nullptr)
{
puts("No");
continue;
}
if (findLevel(T, a) == findLevel(T, b)) puts("Yes");
else puts("No");
}
else if (s.find("parent")!= -1) {
int a, b;
sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
if (findNode(T, a) == nullptr || findNode(T, b) == nullptr)
{
puts("No");
continue;
}
if(findNode(T,a) == findFather(T, b)) puts("Yes");
else puts("No");
}
else if (s.find("left") != -1) {
int a, b;
sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
if (findNode(T, a) == nullptr || findNode(T, b) == nullptr)
{
puts("No");
continue;
}
if(findNode(T,a)==findNode(T,b)->Left) puts("Yes");
else puts("No");
}
else if (s.find("right") != -1) {
int a, b;
sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
if (findNode(T, a) == nullptr || findNode(T, b) == nullptr)
{
puts("No");
continue;
}
if (findNode(T, a) == findNode(T, b)->Right) puts("Yes");
else puts("No");
}
}
}
int main() {
int n;
Tree T;
cin >> n;
T=MakeTree(n);
int m;
cin >> m;
getchar();
judge(T,m);
}