Binary tree exercises (1. Inorder traversal in reverse order output from leaf nodes 2. Judging whether it is isomorphic)

What you need to know before doing the question is: the bracket notation of the binary tree and the order of inorder traversal.

Bracket notation: outside the brackets is the parent node, inside the brackets "," the left side of the sign is the left subtree", and the right side of "is the right subtree. For example, a(b(c(d,e)), f(g,h(i,j))) becomes a tree as shown in the figure:

 In-order traversal: It is to traverse in order: left subtree ---> root node ---> right subtree.

first question:

  1. Topic

 2. Ideas:

       First set the nodes of the tree, because I want to reverse the order, so I additionally set the parent pointer to point to the parent node.

typedef struct Node
{
 char data;
 Node *left;
 Node *right;
 Node *parents;
}BTNode;

        Because it is input in the form of parentheses, it is necessary to write a function to create a binary tree through this input method:

//由括号表示法创建二叉树
void CreateBTree(BTNode *&root, char str[])
{
 int i=0, flag;
 BTNode *p=NULL, *temp=NULL;
 stack<Node *> S;
 while( str[i]!='\0')
 {
  switch( str[i] )
  {
   case '(':flag=1; S.push(p); break;//表示其后创建的为左儿子
   case ')':S.pop( );break;//栈顶元素的左右儿子处理完了, 出栈
   case ',':flag=2;break;//其后创建的为右儿子
   default:
           p=new Node;
     p->data=str[i]; p->left=p->right=NULL;

     if( root==NULL ) //如果当前每根,说明是第一个结点,设为根
                    {
                        root=p;
                        p->parents = NULL;
                    }

     else //有根节点了,就把栈顶元素放到对应的子树上
     {
      temp=S.top();
      p->parents = temp; //设置p的父结点
      switch( flag )
      {
       case 1:temp->left=p; break;
       case 2:temp->right=p; break;
      }
     }
  }
  i++;
 }
}

         The title requires output in the order of inorder traversal:

//中序遍历二叉树
void InOrder(BTNode *root)
{
    //cout << "进来了1" << endl ;
    if(root)
    {
        //cout << "进来了2" << endl ;
        InOrder(root->left);

        if(root->left == NULL && root->right == NULL)
        {
            //cout << "进来了3" << endl ;
            BTNode *p = root;
            while(p != NULL)
            {
                //cout << "进来了4" << endl ;
                cout << p->data << ' ';
                p = p->parents;
            }
            cout << endl;
        }


        InOrder(root->right);
    }
}

  3. All codes: 

#include<iostream>
#include<stack>

using namespace std;

typedef struct Node
{
 char data;
 Node *left;
 Node *right;
 Node *parents;
}BTNode;

//由括号表示法创建二叉树
void CreateBTree(BTNode *&root, char str[])
{
 int i=0, flag;
 BTNode *p=NULL, *temp=NULL;
 stack<Node *> S;
 while( str[i]!='\0')
 {
  switch( str[i] )
  {
   case '(':flag=1; S.push(p); break;//表示其后创建的为左儿子
   case ')':S.pop( );break;//栈顶元素的左右儿子处理完了, 出栈
   case ',':flag=2;break;//其后创建的为右儿子
   default:
           p=new Node;
     p->data=str[i]; p->left=p->right=NULL;

     if( root==NULL ) //如果当前每根,说明是第一个结点,设为根
                    {
                        root=p;
                        p->parents = NULL;
                    }

     else //有根节点了,就把栈顶元素放到对应的子树上
     {
      temp=S.top();
      p->parents = temp; //设置p的父结点
      switch( flag )
      {
       case 1:temp->left=p; break;
       case 2:temp->right=p; break;
      }
     }
  }
  i++;
 }
}


//中序遍历二叉树
void InOrder(BTNode *root)
{
    //cout << "进来了1" << endl ;
    if(root)
    {
        //cout << "进来了2" << endl ;
        InOrder(root->left);

        if(root->left == NULL && root->right == NULL)
        {
            //cout << "进来了3" << endl ;
            BTNode *p = root;
            while(p != NULL)
            {
                //cout << "进来了4" << endl ;
                cout << p->data << ' ';
                p = p->parents;
            }
            cout << endl;
        }


        InOrder(root->right);
    }
}

int main()
{
 char str1[2000];
 BTNode *root1=NULL;
 cin>>str1;

    //判断一棵二叉树是否为相似同构
 root1=NULL;
 CreateBTree(root1, str1);
 InOrder(root1);

    return 0;
}
//a(b(c(d,e)),f(g,h(i,j)))

Second question: 

1. Topic description

 Two, ideas

        This question does not need a parent pointer, just remove it, and the method of creating a binary tree is the same as the previous question. The main thing is how to judge isomorphism.

First design a function to judge whether two trees are isomorphic.

//判断两二叉树是否相似
int Like(BTNode *p1, BTNode *p2)
{
 if(p1==NULL && p2==NULL)
      return 1;
    else if( p1==NULL || p2==NULL) //有一个先结束了,说明不同构
         return 0;
    else return Like(p1->left, p2->left)&Like(p1->right, p2->right); //左子树的左侧和右子树的左侧比,右侧和右侧比
}

         Design another function and use the previous function to judge the two subtrees.

//判断一棵树是否对称同构
int SymmTree(BTNode *root)
{
 if( root==NULL)//当树为空时
     return 1;
 else
     return Like(root->left, root->right); //判断一棵树的两个子树
}

3. Answer: 

#include<iostream>
#include<stack>

using namespace std;

typedef struct Node
{
 char data;
 Node *left;
 Node *right;
}BTNode;

//由括号表示法创建二叉树
void CreateBTree(BTNode *&root, char str[])
{
 int i=0, flag;
 BTNode *p=NULL, *temp=NULL;
 stack<Node *> S;
 while( str[i]!='\0')
 {
  switch( str[i] )
  {
   case '(':flag=1; S.push(p); break;//表示其后创建的为左儿子
   case ')':S.pop( );break;//栈顶元素的左右儿子处理完了, 出栈
   case ',':flag=2;break;//其后创建的为右儿子
   default:
           p=new Node;
     p->data=str[i]; p->left=p->right=NULL;

     if( root==NULL ) //如果当前每根,说明是第一个结点,设为根
      root=p;
     else //有根节点了,就把栈顶元素放到对应的子树上
     {
      temp=S.top();
      switch( flag )
      {
       case 1:temp->left=p; break;
       case 2:temp->right=p; break;
      }
     }
  }
  i++;
 }
}


//判断两二叉树是否相似
int Like(BTNode *p1, BTNode *p2)
{
 if(p1==NULL && p2==NULL)
      return 1;
    else if( p1==NULL || p2==NULL) //有一个先结束了,说明不同构
         return 0;
    else return Like(p1->left, p2->left)&Like(p1->right, p2->right); //左子树的左侧和右子树的左侧比,右侧和右侧比
}

//判断一棵树是否对称同构
int SymmTree(BTNode *root)
{
 if( root==NULL)//当树为空时
     return 1;
 else
     return Like(root->left, root->right); //判断一棵树的两个子树
}

int main()
{
 char str1[2000];
 BTNode *root1=NULL;
 cin>>str1;

    //判断一棵二叉树是否为相似同构
 root1=NULL;
 CreateBTree(root1, str1);
 if( SymmTree(root1) )
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
        
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_63484669/article/details/128363826