啊啊啊好开心呐,用了跟老师不一样,而且更好理解的一种方法!!!
——在判断同构的时候将节点按度分成三种,即度分别为 0、1、2 的三种进行考虑,度为 0 的叶子结点显然为最小规模的比较情况,而使用递归的方式,将树内节点都化到树的叶子结点处往上比较,这样更易于理解
int Isomprphic(int R1, int R2)
{
if ((Tree1[R1].Data == -1) && (Tree2[R2].Data == -1))
return 1;//空树之间互相同构
if (Tree1[R1].Data != Tree2[R2].Data)
return 0;//根节点数据不相等必然不同构
/*************************度为0*****************************/
if ((Tree1[R1].Left == -1) && (Tree1[R1].Right == -1) && (Tree2[R2].Left == -1) && (Tree2[R2].Right == -1)) {
return 1;
}
/*************************度为1*****************************/
if ((Tree1[R1].Left != -1) && Tree1[R1].Right == -1) {//树1左有右空
if (Tree2[R2].Left != -1 && Tree2[R2].Right == -1) {//树2左有右空
return (Isomprphic(Tree1[R1].Left, Tree2[R2].Left));
}
if (Tree2[R2].Left == -1 && Tree2[R2].Right != -1) {//树2左空右有
return (Isomprphic(Tree1[R1].Left, Tree2[R2].Right));
}
}
if (Tree1[R1].Left == -1 && Tree1[R1].Right != -1) {//树1左空右有
if (Tree2[R2].Left != -1 && Tree2[R2].Right == -1) {//树2左有右空
return (Isomprphic(Tree1[R1].Right, Tree2[R2].Left));
}
if (Tree2[R2].Left == -1 && Tree2[R2].Right != -1) {//树2左空右有
return (Isomprphic(Tree1[R1].Right, Tree2[R2].Right));
}
}
/*************************度为2*****************************/
if (Tree1[R1].Left != -1 && Tree1[R1].Right != -1 && Tree2[R2].Left != -1 && Tree2[R2].Right != -1) {
return ((Isomprphic(Tree1[R1].Left, Tree2[R2].Left) && Isomprphic(Tree1[R1].Right, Tree2[R2].Right)) ||
(Isomprphic(Tree1[R1].Left, Tree2[R2].Right) && Isomprphic(Tree1[R1].Right, Tree2[R2].Left)));
}
}
在这个问题里,我个人
解决了这些问题,也有些新的问题
:
- 又一次更加深入地理解了递归;
- 学会使用 F10 F11 进行断点调试,并学会添加监视;
- 结构体数组的用法似懂非懂;
- 我在此次编程中使用了如下的结构体声明以及函数调用(其他无关代码省略,下面有详细代码)
typedef struct Node Tree;
struct Node {
ElementType Data;
int Left;
int Right;
};
Tree Tree1[10], Tree2[10];//二叉树的数据结构
int BuildTree(Tree T[]);//新建二叉树
R1 = BuildTree(Tree1);
R2 = BuildTree(Tree2);//输入两个二叉树
int BuildTree(Tree T[]){......}
- 问题 —— 如何给结构体 / 数据结构 起名使得完成链表啦、树啦以及将来的图,在编程时更好使用并且易读呢?为什么一会叫 Tree 一会又可以叫 Node 呢?不是代表的都是一个节点吗?
先看浙大的数据结构留下的这个
树的同构问题
给定两棵树 T1 和 T2。
如果 T1 可以通过若干次左右孩子互换就变成 T2,则我们称两棵树是 “同构” 的。例如图 1 给出的两棵树就是同构的,因为我们把其中一棵树的结点 A、B、G 的左右孩子互换后,就得到另外一棵树。而图 2 就不是同构的。现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出 2 棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数 NN (\le 10≤10),即该树的结点数(此时假设结点从 0 到 N-1N−1 编号);随后 NN 行,第 ii 行对应编号第 ii 个结点,给出该结点中存储的 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 <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef int Root;
typedef struct Node Tree;
struct Node {
ElementType Data;
int Left;
int Right;
};
Tree Tree1[10], Tree2[10];//二叉树的数据结构
Root BuildTree(Tree T[]);//新建二叉树
int Isomprphic(int R1, int R2);
void Tell(bool judge);
int main()
{
int R1, R2;
R1 = BuildTree(Tree1);
R2 = BuildTree(Tree2);//输入两个二叉树
if (Isomprphic(R1, R2)) {//判断两者是否同构
printf("yes\n");//输出结果
}
else {
printf("No\n");//输出结果
}
/* code */
return 0;
}
Root BuildTree(Tree T[])
{
int N, i, check[10];
int RootNum=0;
scanf("%d", &N);//输入节点个数
for (i = 0; i<N; i++) {
check[i] = 0;
}//初始化check[]数组,保证所有节点处于未标记状态
for (i = 0; i<N; i++) {
scanf("%s %s %s", &T[i].Data, &T[i].Left, &T[i].Right);
if (T[i].Left != '-') {
T[i].Left = T[i].Left - '0';
check[T[i].Left] = 1;
}
else T[i].Left = -1;
if (T[i].Right != '-') {
T[i].Right = T[i].Right - '0';
check[T[i].Right] = 1;
}
else T[i].Right = -1;
}
while (N--) {
if (check[N] == 0)
return RootNum = N;
}//返回树的根节点在结构数组中的位置
}
int Isomprphic(int R1, int R2)
{
if ((Tree1[R1].Data == -1) && (Tree2[R2].Data == -1))
return 1;//空树之间互相同构
if (Tree1[R1].Data != Tree2[R2].Data)
return 0;//根节点数据不相等必然不同构
/*************************度为0*****************************/
if ((Tree1[R1].Left == -1) && (Tree1[R1].Right == -1) && (Tree2[R2].Left == -1) && (Tree2[R2].Right == -1)) {
return 1;
}
/*************************度为1*****************************/
if ((Tree1[R1].Left != -1) && Tree1[R1].Right == -1) {//树1左有右空
if (Tree2[R2].Left != -1 && Tree2[R2].Right == -1) {//树2左有右空
return (Isomprphic(Tree1[R1].Left, Tree2[R2].Left));
}
if (Tree2[R2].Left == -1 && Tree2[R2].Right != -1) {//树2左空右有
return (Isomprphic(Tree1[R1].Left, Tree2[R2].Right));
}
}
if (Tree1[R1].Left == -1 && Tree1[R1].Right != -1) {//树1左空右有
if (Tree2[R2].Left != -1 && Tree2[R2].Right == -1) {//树2左有右空
return (Isomprphic(Tree1[R1].Right, Tree2[R2].Left));
}
if (Tree2[R2].Left == -1 && Tree2[R2].Right != -1) {//树2左空右有
return (Isomprphic(Tree1[R1].Right, Tree2[R2].Right));
}
}
/*************************度为2*****************************/
if (Tree1[R1].Left != -1 && Tree1[R1].Right != -1 && Tree2[R2].Left != -1 && Tree2[R2].Right != -1) {
return ((Isomprphic(Tree1[R1].Left, Tree2[R2].Left) && Isomprphic(Tree1[R1].Right, Tree2[R2].Right)) ||
(Isomprphic(Tree1[R1].Left, Tree2[R2].Right) && Isomprphic(Tree1[R1].Right, Tree2[R2].Left)));
}
}