determine if a binary tree is a sub-structure of another binary tree
1. Title Description:
How to tell if a binary tree is another sub-structure.
Like what:
2
/ \
9 8
/ \ /
2 3 5
/
6
There are sub structures that are
9
/ \
2 3 2, analyze the problem:
The problem of binary tree algorithm can be solved by recursion. Then write a correct recursive program, first of all must analyze the correct recursive end condition.
With this question, when is the end of the recursion?
<1> Second binary tree Root2 is empty, indicating that Root2 is the substructure of the first binary tree root1, which returns True.
<2> when Root1 is empty, Root2 is not empty at this point, stating that Root2 is not a root1 substructure and returns false.
<3> recursion Here are two ideas:
Method One: Now ROOT1 find the node value with the value of Root2 equal to the node, if found to determine whether the ROOT2 is not the beginning of the sub-structure of the node. So, first issubtree () judgment.
Method Two: Is the direct judgment, the same recursive judgment root2 the left and right sub-tree is also the corresponding sub-structure. If the values are not the same, they are recursively searched for the left and right subtree of the ROOT1. In particular, we should pay attention to the logical judgment of the last two sentence recursion.
3. Code:
method One:
Determine if Root2 is the substructure of the ROOT1 beginning
Boolissubtree (Bitreenode *root1,bitreenode *root2)
{
Judge 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 finds the tree with ROOT1 as the node, whether there is the same value as Root2, and if so, calls 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 Two:
One recursive completion
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, with the first order to generate two fork tree code
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 feel good about recursion, especially when the recursion is over. This is the key to solving most recursive problems. This code has been adopted in vc++6.0 debugging, welcome to ask questions.