4-11 Isomorphic (10分)(树的同构)(函数题)

Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are isomorphic because they are the same if the children of A, B, and G, but not the other nodes, are swapped. Give a polynomial time algorithm to decide if two trees are isomorphic.

在这里插入图片描述

Format of functions:

int Isomorphic( Tree T1, Tree T2 );

where Tree is defined as the following:

typedef struct TreeNode *Tree;
struct TreeNode {
    ElementType Element;
    Tree  Left;
    Tree  Right;
};

The function is supposed to return 1 if T1 and T2 are indeed isomorphic, or 0 if not.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;

typedef struct TreeNode *Tree;
struct TreeNode {
    ElementType Element;
    Tree  Left;
    Tree  Right;
};

Tree BuildTree(); /* details omitted */

int Isomorphic( Tree T1, Tree T2 );

int main()
{
    Tree T1, T2;
    T1 = BuildTree();
    T2 = BuildTree();
    printf(%d\n”, Isomorphic(T1, T2));
    return 0;
}

/* Your function will be put here */

Sample Output 1 (for the trees shown in Figure 1):
1
Sample Output 2 (for the trees shown in Figure 2):
0

在这里插入图片描述

int Isomorphic( Tree T1, Tree T2 )
{
    if(T1==NULL&&T2==NULL)
        return 1;
    if((T1==NULL&&T2!=NULL)||(T1!=NULL&&T2==NULL))
        return 0;
    if(T1->Element!=T2->Element)
        return 0;
    if(T1->Left==NULL&&T2->Left==NULL)
        return Isomorphic(T1->Right,T2->Right);
    if((T1->Left!=NULL&&T2->Left!=NULL)&&(T1->Left->Element==T2->Left->Element))
        return Isomorphic(T1->Left,T2->Left)&&Isomorphic(T1->Right,T2->Right);
    else
        return Isomorphic(T1->Left,T2->Right)&&Isomorphic(T1->Right,T2->Left);
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是求两百以内同构数的Python程序,并且用了函数实现同构数的判断: ```python def is_isomorphic(num1, num2): num1_str = str(num1) num2_str = str(num2) if len(num1_str) != len(num2_str): return False mapping = {} for i in range(len(num1_str)): if num1_str[i] not in mapping: mapping[num1_str[i]] = num2_str[i] elif mapping[num1_str[i]] != num2_str[i]: return False return True for i in range(1, 200): for j in range(i, 200): if is_isomorphic(i**2, j**2): print(i, j) ``` 程序中is_isomorphic函数接受两个参数num1和num2,分别表示需要判断的两个数字。函数首先将这两个数字转换成字符串,然后比较它们的长度是否相等。如果长度不相等,则说明它们不可能是同构数,直接返回False。接着,函数维护一个字典mapping,用于记录num1中每个数字所对应的num2中的数字。遍历num1_str中的每个数字,如果这个数字还没有被映射到任何数字,就将它和num2_str中对应位置的数字建立映射;否则,如果这个数字已经被映射到另一个数字,就检查这个数字是否和num2_str中对应位置的数字相等。如果所有数字都能够成功映射并且相等,说明这两个数字是同构数,返回True。否则,返回False。 在主程序中,我们使用两个嵌套的for循环遍历所有小于200的数字对,对于每一对数字,我们都调用is_isomorphic函数进行判断。如果判断结果为True,就说明这两个数字是同构数,将它们输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值