White Session - isomorphic -c language tree implementation .md

Newer and more comprehensive "data structures and algorithms," the update site, more python, go, waiting for you teaching artificial intelligence: https://www.cnblogs.com/nickchen121/p/11407287.html

First, understand the meaning of problems

Given two trees T1 and T2. If T1 can be interchanged by several times about the child becomes T2, the two trees we call a "homogeneous." Now given the two trees, you determine whether they are isomorphic.

Input format: Input information given binary 2:

  • First given node in the tree of the tree in a row, followed by N rows

  • I-th row corresponding to the i-th node number, the node gives letters stored in its left child node number, the right child node number
  • If the child node is empty, then given in the respective position "-"

As shown below, there are various representation methods, we list the following two:

Second, the idea of ​​solving

  1. Representation of Binary Tree
  2. Build binary tree
  3. Isomorphic discrimination

2.1 Representation of Binary Tree

Binary tree represents the array of structures: static list

/* c语言实现 */

#define MaxTree 10
#define ElementType char
#define Tree int
#define Null -1

struct TreeNode
{
  ElementType Element;
  Tree Left;
  Tree Right;
} T1[MaxTree], T2[MaxTree];

2.2 application framework to build

We need to design function:

  • Read data to build a binary tree
  • Binary isomorphic discrimination
/* c语言实现 */

int main():
{
  建二叉树1;
  建二叉树2;
  判别是否同构并输出;
  
  return 0;
}

int main()
{
  Tree R1, R2;
  
  R1 = BuildTree(T1);
  R2 = BuildTree(T2);
  if (Isomorphic(R1, R2)) printf("Yes\n");
  else printf("No\n");
  
  return 0;
}

2.3 How to build a binary tree

/* c语言实现 */

Tree BuildTree(struct TreeNode T[])
{
  ...;
  scanf("%d\n", &N); // 输入需要建立树的长度
  if (N) {
    ...;
    for (i=0; i<N; i++) {
      scanf("%c %c %c\n", &T[i].Element, &cl, &cr);
      ...;
    }
    ...;
    Root = ??? // 可以通过T[i]中没有任何结点的left(cl)和right(cr)指向他这个条件获取。
  }
  return Root;
}
/* c语言实现 */

Tree BuildTree(struct TreeNode T[])
{
  ...;
  scanf("%d\n", &N); // 输入需要建立树的长度
  if (N) {
    for (i=0; i<N; i++) check[i] = 0;
    for (i=0; i<N; i++) {
      scanf("%c %c %c\n", &T[i].Element, &cl, &cr);
      if (cl != '-'){
        T[i].Left = cl-'0';
        check[T[i].Left] = 1;
      }
      else T[i].Left = Null;
      ...;  // 对cr的对应处理
    }
    for (i=0; i<N; i++)
      if (!check[i]) break;
    Root = i; // 可以通过T[i]中没有任何结点的left(cl)和right(cr)指向他这个条件获取。
  }
  return Root;
}

2.4 How to determine two binary isomorphic

/* c语言实现 */

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));
  }
}

Guess you like

Origin www.cnblogs.com/nickchen121/p/11518873.html