算法 树的同构

全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案汇总

题目:给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。

 

 

图1

图2

现给定两棵树,请你判断它们是否是同构的。

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:

如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):

8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -

输出样例1:

Yes

输入样例2(对应图2):

8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:

No

解答:

直接使用视频里的思路来做:

首先定义基本变量:

#include <iostream>
using namespace std;

#define MaxTree 100
#define ElementType char
#define Tree int
#define Null -1
struct TreeNode
{
	ElementType Element;
	Tree Left;
	Tree Right;
} T1[MaxTree], T2[MaxTree];
int check[MaxTree];

每个树一共有三个变量,一个代表本元素值,一个左索引一个右索引。建立每个树的数组,并建立一个check数组。

Tree BuildTree(struct TreeNode T[]);
int Isomorphic(Tree R1, Tree R2);

int main()
{
	Tree R1, R2;
	R1 = BuildTree(T1);
	R2 = BuildTree(T2);
	if (R1 == -1 && R2 == -1) {
		printf("Yes\n");
	}
	else if ((R1 == -1 && R2 != -1)||(R1 != -1 && R2 == -1)) {
		printf("No\n");
	}
	else{
		if (Isomorphic(R1, R2)) printf("Yes\n");
		else printf("No\n");
	}

	system("pause");
	return 0;
}

视频里说的很详细,这里就不再画蛇添足了,注意我们的程序里当两个树都是空的,则返回的值是-1

Tree BuildTree(TreeNode T[])
{
	int N;
	int Root = -1;
	cin >> N;
	if (N) {
		for (int i = 0; i<N; i++) check[i] = 0;//先都初始化为0
		for (int i = 0; i<N; i++) {
			char cl, cr;//之前不小心把这个写成了int,结果出现奇怪的错误
			cin>>T[i].Element >> cl >> cr;
			if (cl != '-') {
				T[i].Left = cl - '0';
				check[T[i].Left] = 1;
			}
			else T[i].Left = Null;
			//对cr的对应处理
			if (cr != '-') {
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else T[i].Right = Null;
		}
		int i;
		for ( i= 0; i<N; i++)
			if (!check[i]) break;
		Root = i;
	}
	return Root;
}

建立树的过程非常简单,这里就不再赘述了。


int Isomorphic(Tree R1, Tree R2)
{
	if ((R1 == Null) && (R2 == Null)) /* both empty */
		return 1;
	if (((R1 == Null) && (R2 != Null)) || ((R1 != Null) && (R2 == Null)))
		return 0; /* one of them is empty */
	if (T1[R1].Element != T2[R2].Element)
		return 0; /* roots are different */
	if ((T1[R1].Left == Null) && (T2[R2].Left == Null))
		/* both have no left subtree */
		return Isomorphic(T1[R1].Right, T2[R2].Right);
	if (((T1[R1].Left != Null) && (T2[R2].Left != Null)) &&
		((T1[T1[R1].Left].Element) == (T2[T2[R2].Left].Element)))
		/* no need to swap the left and the right */
		return (Isomorphic(T1[R1].Left, T2[R2].Left) &&
			Isomorphic(T1[R1].Right, T2[R2].Right));
	else /* need to swap the left and the right */
		return (Isomorphic(T1[R1].Left, T2[R2].Right) &&
			Isomorphic(T1[R1].Right, T2[R2].Left));
}

通过不断递归去判断R1与R2是否同构来得到最终答案。

在第4 5 6个if-else语句中,可以这样连起来理解:如果判断已经通过了前面1 2 3的if语句,说明两棵树要么左右都不空,要么都只是空一边。所以第4个树的意义是如果都是左边空了,那么去判断右边的如何。

而第5和6的条件则是基于如果某一棵树左边不空应该怎么办,如果左边不空,说明要么右边都是空的,要么左右都不空:这个时候就使用第5 和6两个if -else语句:第5个语句判断左边如果都不空,而且两棵树的左边的元素都相等,则说明不用交换左右,而是直接分别比较左右两边即可。

第6个语句的情况是:左边不空,但是两棵树左边的元素不一样,那可能第一棵的右边的树和第二棵左边的树同构,得交换左右进行递归判断。

需要注意的是如果第一棵树左边不空右边空,第二棵树左边空右边不空,则对应到第6个判断里。

 

完整代码粘贴如下:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

#define MaxTree 100
#define ElementType char
#define Tree int
#define Null -1
struct TreeNode
{
	ElementType Element;
	Tree Left;
	Tree Right;
} T1[MaxTree], T2[MaxTree];
int check[MaxTree];
Tree BuildTree(struct TreeNode T[]);
int Isomorphic(Tree R1, Tree R2);

int main()
{
	Tree R1, R2;
	R1 = BuildTree(T1);
	R2 = BuildTree(T2);
	if (R1 == -1 && R2 == -1) {
		printf("Yes\n");
	}
	else if ((R1 == -1 && R2 != -1)||(R1 != -1 && R2 == -1)) {
		printf("No\n");
	}
	else{
		if (Isomorphic(R1, R2)) printf("Yes\n");
		else printf("No\n");
	}

	system("pause");
	return 0;
}


Tree BuildTree(TreeNode T[])
{
	int N;
	int Root = -1;
	cin >> N;
	if (N) {
		for (int i = 0; i<N; i++) check[i] = 0;//先都初始化为0
		for (int i = 0; i<N; i++) {
			char cl, cr;//之前不小心把这个写成了int,结果出现奇怪的错误
			cin>>T[i].Element >> cl >> cr;
			if (cl != '-') {
				T[i].Left = cl - '0';
				check[T[i].Left] = 1;
			}
			else T[i].Left = Null;
			//对cr的对应处理
			if (cr != '-') {
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else T[i].Right = Null;
		}
		int i;
		for ( i= 0; i<N; i++)
			if (!check[i]) break;
		Root = i;
	}
	return Root;
}

int Isomorphic(Tree R1, Tree R2)
{
	if ((R1 == Null) && (R2 == Null)) /* both empty */
		return 1;
	if (((R1 == Null) && (R2 != Null)) || ((R1 != Null) && (R2 == Null)))
		return 0; /* one of them is empty */
	if (T1[R1].Element != T2[R2].Element)
		return 0; /* roots are different */
	if ((T1[R1].Left == Null) && (T2[R2].Left == Null))
		/* both have no left subtree */
		return Isomorphic(T1[R1].Right, T2[R2].Right);
	if (((T1[R1].Left != Null) && (T2[R2].Left != Null)) &&
		((T1[T1[R1].Left].Element) == (T2[T2[R2].Left].Element)))
		/* no need to swap the left and the right */
		return (Isomorphic(T1[R1].Left, T2[R2].Left) &&
			Isomorphic(T1[R1].Right, T2[R2].Right));
	else /* need to swap the left and the right */
		return (Isomorphic(T1[R1].Left, T2[R2].Right) &&
			Isomorphic(T1[R1].Right, T2[R2].Left));
}

测试全部通过。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dezeming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值