03_树 _ 树的同构
题目描述
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K8WTy39N-1634046337703)(C:\Users\admin\Desktop\1.JPG)]
现给定两棵树,请你判断它们是否是同构的。
输入格式
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的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 -
输出格式
如果两棵树是同构的,输出“Yes”,否则输出“No”。
yes
输入格式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
算法分析
本题不要求根结点作为第一个元素
第一个数据代表结点本身的信息,后面两个代表左右子结点的位置,若无左右子节点,则以"-"表示
亟待解决:
- 二叉树如何表示
- 建立二叉树
- 同构如何判别
常见的二叉树表示,采用链表表示
但是,也可用数组进行二叉树的表示,一般是将二叉树表现为完全二叉树的形式,进行表示
本题可采用结构数组的形式进行二叉树表示:
- 基本的存储利用数组形式
- 左右儿子采用类似链表的方法进行表示
- 物理上的存储是数组,但是思想上采用的是链表的思想
- 该类存储方式被称为:静态链表
结点 | A | B | C | D |
---|---|---|---|---|
left | -1 | 2 | -1 | -1 |
right | 1 | 3 | -1 | -1 |
position | 0 | 1 | 2 | 4 |
在算法中若想找到根结点,只需要寻找谁的位置未被其他位置指向
需要设计的函数:
- 设计函数读取数据建立二叉树
- 设计函数进行二叉树的同构判别
二叉树表示
constexpr auto maxtree = 10;;
typedef char elementtype;
typedef int tree;
#define null -1;//因为当输入的值没有的时候其结点需为-1
struct treenode
{
elementtype element;//树中需要保存的元素的值
tree left;//树的左子树,相对于是指针的作用
tree right;//树的右子树
}t1[maxtree],t2[maxtree];//定义两颗树
建立树
tree buildtree(struct treenode t[])
{
int n,i,root;
char cl, cr;
char value;
int arr[100];
cin >> n;
if (n)
{
for (i = 0; i < n; i++)
{
arr[i] = 0;
}
for (i = 0; i < n; i++)
{
cin >> t[i].element >> cl >> cr;
//scanf("%c %d %d\n", &t[i].element, &cl, &cr);
if (cl != '-')
{
t[i].left = cl - '0';
arr[t[i].left] = 1;
}
else
{
arr[t[i].left] = null;
}
if (cr != '_')
{
t[i].right = cr - '0';
arr[t[i].right] = 1;
}
else
{
arr[t[i].right] = null;
}
}
for (i = 0; i < n; i++)
{
if (!arr[i])
{
break;
}
}
root = i;
}
return root;
}
二叉树同构判别
int isomorphic(tree r1, tree r2)//输入的是查找到的根节点
{
if ((r1 == null) && (r2 == null))
{
return 1;
}
if (((r1 == null) && (r2 != null)) || ((r1 != null) && (r2 == null)))
{
return 0;
}
if ((t1[r1].element != t2[r2].element))
{
return 0;
}
if ((t1[r1].left == null) && (t2[r2].left == null))
{
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)))
{
return isomorphic(t1[r1].left, t2[r2].left) && isomorphic(t1[r1].right, t2[r2].right);
}
else
{
return isomorphic(t1[r1].left, t2[r2].right) && isomorphic(t1[r1].right, t2[r2].left);
}
}
int main()
{
int r1,r2;
r1=buildtree(t1);
r2 = buildtree(t2);
if (isomorphic(r1, r2))
{
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
}
全部代码
#include<stdio.h>
#include<iostream>
using namespace std;
constexpr auto maxtree = 100;;
typedef char elementtype;
typedef int tree;
constexpr auto null = -1;//因为当输入的值没有的时候其结点需为-1;
struct treenode
{
elementtype element;//树中需要保存的元素的值
tree left;//树的左子树,相对于是指针的作用
tree right;//树的右子树
}t1[maxtree],t2[maxtree];//定义两颗树
//用结构数组表示二叉树:静态链表
tree buildtree(struct treenode t[])
{
int n,i,root;
char cl, cr;
int arr[100];
cin >> n;
if (n)
{
for (i = 0; i < n; i++)
{
arr[i] = 0;
}
for (i = 0; i < n; i++)
{
cin >> t[i].element >> cl >> cr;
//scanf("%c %d %d\n", &t[i].element, &cl, &cr);
if (cl != '-')
{
t[i].left = cl - '0';
arr[t[i].left] = 1;
}
else
{
t[i].left = null;
}
if (cr != '-')
{
t[i].right = cr - '0';
arr[t[i].right] = 1;
}
else
{
t[i].right = -1;
}
}
for (i = 0; i < n; i++)
{
if (!arr[i])
break;
}
root = i;
}
return root;
}
int isomorphic(tree r1, tree r2)//输入的是查找到的根节点
{
if ((r1 == null) && (r2 == null))
{
return 1;
}
if (((r1 == null) && (r2 != null)) || ((r1 != null) && (r2 == null)))
{
return 0;
}
if ((t1[r1].element != t2[r2].element))
{
return 0;
}
if ((t1[r1].left == null) && (t2[r2].left == null))
{
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)))
{
return isomorphic(t1[r1].left, t2[r2].left) && isomorphic(t1[r1].right, t2[r2].right);
}
else
{
return isomorphic(t1[r1].left, t2[r2].right) && isomorphic(t1[r1].right, t2[r2].left);
}
}
int main()
{
int r1,r2;
r1=buildtree(t1);
r2 = buildtree(t2);
if (isomorphic(r1, r2))
{
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
实验结果
最后的结果,空树不对,也不知道啥情况,跟能编译通过的代码对比也查不出来,不知道啥情况。