Determine whether a binary tree is a sub-structure of another binary tree

Source: Internet
Author: User
Determine whether a binary tree is a sub-structure of another binary tree

1. Question description:

How can we determine whether a binary tree is another sub-structure?
For example:

2

/\

9 8

/\/

2 3 5

/

6

The sub-structure is
9
/\
2 3

2,Analyze the problem:

Algorithms related to binary trees can be solved through recursion. To write a correct recursive program, you must first analyze the conditions for correct recursive termination.

Let's talk about the time when recursion ends.

<1> If root2 of the second binary tree is null, it indicates that root2 is the sub-structure of root1 of the first Binary Tree, and true is returned.

<2> when root1 is empty, root2 is not empty yet. This indicates that root2 is not a sub-structure of root1 and false is returned.

<3> there are two ideas for recursion:

Method 1: Find the node with the same node value as the value of root2 in root1. If the node value is found, determine whether root2 is the sub-structure starting with this node. Therefore, the issubtree () is used to determine.

Method 2: directly determine whether or not the sub-tree of root2 is also a corresponding sub-structure. If the values are different, the left and right subtree of root1. Pay special attention to the recursive logic judgment of the last two sentences.

3. Code:

Method 1:

// Determine whether root2 is a sub-structure starting with root1.

Boolissubtree (bitreenode * root1, bitreenode * root2)

{

// Determine root2 first

If (root2 = NULL)

Return true;

If (root1 = NULL)

Return false;

If (root1-> data! = Root2-> data)

Return false;

Returnissubtree (root1-> LC, root2-> Lc) & issubtree (root1-> RC, root2-> RC );

}

// Recursively search for the same value as root2 in the tree with root1 as the node. If so, call issubtree (root1, root2 );

Boolcheckifsubtree (bitreenode * root1, bitreenode * root2)

{

If (root1 = NULL)

Return false;

Bool result = false;

If (root1-> DATA = root2-> data)

Result = issubtree (root1, root2 );

If (result = false)

Result = checkifsubtree (root1-> LC, root2 );

If (result = false)

Result = checkifsubtree (root1-> RC, root2 );

Return result;

}

Method 2:

// Recursion

Boolcheckifsubtree2 (bitreenode * root1, bitreenode * root2)

{

If (root2 = NULL)

Return true;

If (root1 = NULL)

Return false;

If (root1-> DATA = root2-> data)

Returncheckifsubtree2 (root1-> LC, root2-> Lc) & checkifsubtree2 (root1-> RC, root2-> RC );

Else

Returncheckifsubtree2 (root1-> LC, root2) | checkifsubtree2 (root1-> RC, root2 );

}

4. Code for generating Binary Trees in FIFO order

Struct bitreenode
{
Struct bitreenode * LC;
Struct bitreenode * RC;
Int data;
};

Void createbitree (bitreenode * & root)
{
Int ch;
Cin> CH;
If (CH = 0)
{
Root = NULL;
}
Else
{
Root = (bitreenode *) malloc (sizeof (bitreenode ));
If (root = NULL)
Exit (1 );
Root-> DATA = CH;
// Cout <"--" <root-> data <Endl;
Createbitree (root-> Lc );
Createbitree (root-> RC );
}
}

5. Summary

Be sure to understand recursion well, especially when recursion ends. This is the key to solving most recursive problems.

This code has been debugged in VC ++ 6.0. You are welcome to raise your question.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.