数据结构实验之查找一:二叉排序树
Time Limit: 400 ms Memory Limit: 65536 KiB
Problem Description
对应给定的一个序列可以唯一确定一棵二叉排序树。然而,一棵给定的二叉排序树却可以由多种不同的序列得到。例如分别按照序列{3,1,4}和{3,4,1}插入初始为空的二叉排序树,都得到一样的结果。你的任务书对于输入的各种序列,判断它们是否能生成一样的二叉排序树。
Input
输入包含若干组测试数据。每组数据的第1行给出两个正整数N (n < = 10)和L,分别是输入序列的元素个数和需要比较的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列生成一颗二叉排序树。随后L行,每行给出N个元素,属于L个需要检查的序列。
简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。
Output
对每一组需要检查的序列,如果其生成的二叉排序树跟初始序列生成的二叉排序树一样,则输出"Yes",否则输出"No"。
Sample Input
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0
Sample Output
Yes
No
No
Hint
Source
xam
本题解题思路:
根据题目中给出的序列分别建立二叉排序树
然后再进行对比。其中,对比有两种方式
一:直接对比两棵树的根节点以及左右子树的数据对应情况
二:将建好的树的结点数据按照相同的遍历方式遍历出来,再进行比对
(第二种方式直接可以省略建树的过程,直接排序序列对比,但是这样投机取巧与本题设计的原意相背)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
///本题解题思路:根据题目中给出的序列分别建立二叉排序树
///然后再进行对比。其中,对比有两种方式
///一:直接对比两棵树的根节点以及左右子树的数据对应情况
///二:将建好的树的结点数据按照相同的遍历方式遍历出来,再进行比对
///(第二种方式好像直接可以省略建树的过程,直接排序序列对比就好了)
struct node
{
int data;
struct node *L_child, *R_child;
};
struct node * Binary_sort_tree(struct node *, int);
int Judge(struct node *, struct node *);
int main ()
{
int N, L, x, i;
struct node *Root, *Root2;
while(~scanf("%d", &N))///此处由于题目条件所限,有两种写法
{///此为写法一
if(N == 0) break;
scanf("%d", &L);
///while(~scanf("%d %d", &N, &L) && N != 0) 此为写法二
///不能写成 while(~scanf("%d %d", &N, &L)
/// { if(N == 0) break; }
///结束输入的时候会无法按题意结束
Root = NULL;///注意一定要置空!否则Root直接有一个随机值
for(i = 0; i < N; i++)///会导致二叉树的建立失败
{
scanf("%d", &x);
Root = Binary_sort_tree(Root, x);
}
while(L--)
{
Root2 = NULL;///注意一定要置空!!!
for(i = 0; i < N; i++)
{
scanf("%d", &x);
Root2 = Binary_sort_tree(Root2, x);
}
if(Judge(Root, Root2)) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
struct node *Binary_sort_tree(struct node *Root, int x)
{
if(Root == NULL)
{///当根节点为空时,创建该节点并存入数据
Root = (struct node *)malloc(sizeof(struct node));
Root->data = x;
Root->L_child = Root->R_child = NULL;
}
else
{
if(x < Root->data)
{///如果存入的数据小于该根节点数据,则将其放入该根节点的左子树中
Root->L_child = Binary_sort_tree(Root->L_child, x);
}
else
{///如果存入的数据大于该根节点数据,则将其放入该根节点的右子树中
Root->R_child = Binary_sort_tree(Root->R_child, x);
}
}
return Root;
};
int Judge(struct node *Root1, struct node *Root2)
{
if(Root1 == NULL && Root2 == NULL)
{///当两个根节点都为空时,两个都是空树,则符合条件,返回1
return 1;
}
else if(Root1 != NULL && Root2 != NULL)
{///当两个根节点都不为空时,分情况讨论
if(Root1->data != Root2->data)
{///当两根节点的数据不同时,不符合条件,返回0
return 0;
}
else if(Judge(Root1->L_child, Root2->L_child)
&& Judge(Root1->R_child, Root2->R_child))
{///当两根节点数据相同时,判断两个树的左右子树数据是否相同,若相同,则符合条件,返回1
return 1;
}
}
return 0;///当两个根节点一个为空一个不为空时,不符合条件,返回0
}///该判定函数中共存在四种不同情况