MOOC Zhejiang University Data Structure-03-Tree 1 Tree Isomorphism (25 points)

Refer to the code written in this article 03-Tree 1 Tree Isomorphism (25 points)
1. The character constant in C language is a character enclosed in single quotes (note that single quotes are not double quotes: if(cl!= '-'){ //Wrongly write single quotes as double quotes will report an error.
String reading: scanf("%s", string);
character reading: scanf("%c", &c); (note this Need to add and take the address operation, but the string is not needed, because it is a string array, string is directly the name of the array, it is the first address, so there is no need to take the address.
2. Variables cannot be used to define the size of the array when defining the array .
initialize the array, int a [3] = { 0}; and int a [3] = {0,0,0 }; it appears first embodiment can automatically assign the latter elements are all 0.
3. the function ismorphic If the if changed to else if can also run through, in fact, I don’t understand, when should I use else if
4. Yes and No are wrongly written as YES and NO. Why do I make such a mistake? I found out after looking for an hour (Reflection: Should I give up programming and cry)

#include<stdio.h>
#define Null -1
#define MAXSIZE 10 //不需要加分号
typedef int Tree;
struct TreeNode{
    
    
    char Data;
    Tree Left;
    Tree Right;
}T1[MAXSIZE],T2[MAXSIZE];
int ismorphic(Tree T1,Tree T2);
Tree BuildTree(struct TreeNode T[]);
Tree BuildTree(struct TreeNode T[]){
    
    
    int Root=Null,N; //刚开始将节点置为空,若为空树的时候可返回Null
    char cl,cr;
    scanf("%d",&N);
    if(N){
    
       //加入对树为空的判断
    int check[MAXSIZE]={
    
    0};//置为零
        for(int i=0;i<N;++i){
    
       //把N个节点读入树
            scanf("\n%c %c %c",&T[i].Data,&cl,&cr);//&T[i].Data,&cl,&cr是不是不需要&  蒙圈。回来再做把
            if(cl!='-'){
    
      //将单引号错写为双引号会报错comparison between pointer and integer
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else{
    
    
                T[i].Left = Null;
            }
            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 ismorphic(Tree R1,Tree R2){
    
    
    if(R1==Null && R2==Null) return 1; //不加这么多括号写也可以((R1==Null) && (R2==Null))
    if(((R1==Null) && (R2!=Null))||((R2==Null) && (R1!=Null))) return 0;//把||错写成|
    if(((R1!=Null) && (R2!=Null))&&(T1[R1].Data!=T2[R2].Data)) return 0;
    
    if((R1!=Null) && (R2!=Null)&&(T1[R1].Data==T2[R2].Data)&&(T1[R1].Left==Null)&&(T2[R2].Left==Null))//
        return ismorphic(T1[R1].Right,T2[R2].Right);
    if((R1!=Null)&&(R2!=Null)&&(T1[R1].Data==T2[R2].Data)&&(T1[R1].Left!=Null)&&(T2[R2].Left!=Null)&&(T1[T1[R1].Left].Data==T2[T2[R2].Left].Data)) //(R1!=Null)&& (R2!=Null)&&(T1[R1].Data==T2[R2].Data&& 
        return (ismorphic(T1[R1].Left,T2[R2].Left)&&ismorphic(T1[R1].Right,T2[R2].Right));
    else return(ismorphic(T1[R1].Left,T2[R2].Right) && ismorphic(T1[R1].Right,T2[R2].Left));
    
}
int main(){
    
    
    Tree R1,R2;
    R1 = BuildTree(T1);
    R2 = BuildTree(T2);
    if (ismorphic(R1,R2)) printf("Yes");
    else printf("No"); //错写成了YES和NO,为啥会犯这样的错误呢?
}



Guess you like

Origin blog.csdn.net/weixin_43919570/article/details/105102520