03-树1 树的同构

5 篇文章 0 订阅
5 篇文章 0 订阅
#include <stdio.h>

#define Null -1

typedef int Tree;
typedef char ElementType;

typedef struct TreeNode TNode;

struct TreeNode {
    ElementType Data;
    Tree Left;
    Tree Right;
};


Tree getTree(TNode nodes[], int length) {
    int flagArr[length];
    for (int i = 0; i < length; i++)
        flagArr[i] = 0;
    
    char data, left, right;
    for (int i = 0; i < length; i++) {
        scanf(" %c %c %c", &data, &left, &right);//这里%c前要加空格,否则第一个接受的就是\n,真坑
        TNode node;
        node.Data = data;
        if (left == '-') {
            node.Left = Null;
        } else {
            node.Left = left - '0';
            flagArr[node.Left] =1;
        }
        
        if (right == '-') {
            node.Right = Null;
        } else {
            node.Right = right - '0';
            flagArr[node.Right] =1;
        }
        nodes[i] = node;
    }
    
    Tree root = Null;
    for (int i = 0; i < length; i++){
        if(flagArr[i] == 0){
            root = i;
            break;
        }
    }
    
    return root;
}

int Isomorphic(Tree R1,Tree R2,TNode nodes1[],TNode nodes2[]){
    if(R1 == Null && R2 == Null){
        return 1;
    }else if((R1 == Null && R2 != Null) || (R1 != Null && R2 == Null)){
        return 0;
    }else if(nodes1[R1].Data != nodes2[R2].Data){
        return 0;
    }else if(nodes1[R1].Left == Null && nodes2[R1].Left == Null){
        return Isomorphic(nodes1[R1].Right,nodes2[R2].Right,nodes1,nodes2);
    }else if((nodes1[R1].Left != Null && nodes2[R2].Left != Null) && (nodes1[nodes1[R1].Left].Data == nodes2[nodes2[R2].Left].Data)){
        int isLeft = Isomorphic(nodes1[R1].Left,nodes2[R2].Left,nodes1,nodes2);
        int isRight = Isomorphic(nodes1[R1].Right,nodes2[R2].Right,nodes1,nodes2);
        return isLeft && isRight;
    }else{
        int isLeft = Isomorphic(nodes1[R1].Left,nodes2[R2].Right,nodes1,nodes2);
        int isRight = Isomorphic(nodes1[R1].Right,nodes2[R2].Left,nodes1,nodes2);
        return isLeft && isRight;
    }
}
//遍历数组元素,因为之前一直出错,所以就像看看输入的数据是否和预期一样,真实答案不需要这个方法
void printTree(Tree root,TNode nodes[]){
    
    if(root != Null){
        TNode node = nodes[root];
        printf("%c %d %d\n",node.Data,node.Left,node.Right);
        
        printTree(node.Left,nodes);
        printTree(node.Right,nodes);
    }
}

int main() {
    int len1;
    scanf("%d\n", &len1);
    if(len1 < 0){
        printf("No");
        return 0;
    }
    TNode nodes1[len1];
    Tree tree1 = getTree(nodes1,len1);//获取树
    
    
    int len2;
    scanf("%d\n", &len2);
    
    if(len1 != len2){
        printf("No");
        return 0;
    }
    
    TNode nodes2[len2];
    Tree tree2 = getTree(nodes2,len2);//获取树
    int result = Isomorphic(tree1,tree2,nodes1,nodes2);//判断是否同构
    if(result){
        printf("Yes");
    }else{
        printf("No");
    }
    //    printTree(tree1,nodes1);
    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值