020 given two binary trees T1, T2 determines whether T1 is a child tree of T2 (keep it up), t1t2
Given two binary trees T1 and T2, determine whether T1 is a child tree of T2.
First, find the root node of T1 in T2. If not,
T1 is not a child tree of T2. If we can find it, we can
Traverse T1 and check whether each node of T1 exists T2
.
Code:
Struct TreeNode {int data; TreeNode * leftChild; TreeNode * rightChild;}; bool isExited (const TreeNode * vRoot1, const TreeNode * vRoot2, TreeNode * vRes) {if (vRoot1 = NULL) return false; if (vRoot2 = vRoot1) {vRes = vRoot1; return true;} bool Flag = false; Flag = isExited (vRoot1, vRoot2-> leftChild, vRes); if (! Flag) {Flag = isExited (vRoot1, vRoot2-> rightChild, vRes);} return Flag;} bool isSame (const TreeNode * vRoot1, const TreeNode * vRoot2) {if (vRoot1 = NULL & vRoot2 = NULL) return true; if (vRoot1! = VRoot2) return false; bool Flag = isSame (vRoot1-> leftChild, vRoot2-> leftChild); if (! Flag) return false; Flag = isSame (vRoot1-> rightChild, vRoot2-> rightChild); return Flag;} bool isSubTree (const TreeNode * vRoot1, const TreeNode * vRoot2) {TreeNode * Temp = NULL; // if (! IsExited (vRoot1, vRoot2, Temp) return false; return isSame (vRoot1, Temp );}
Homogeneous Binary Tree of data structures
The fifth function is to swap some child nodes in the left and right subtree.
# Include <iostream. h>
# Include <malloc. h>
# Define FALSE 0
# Define TRUE 1
# Define OK 1
# Define Max size 100
Typedef int status;
Typedef int elemtype;
Typedef struct binode
{
Elemtype data;
Struct binode * lchild, * rchild;
} Binode, * bitree;
Status treecreated = FALSE;
Int leafcount = 0;
Status createbitree (bitree * t );
Status preordertraverse (bitree t );
Int height (bitree t );
Void swap (bitree * t );
Void leafcounts (bitree t );
Void main ()
{
Int choice = 0;
Status leave = FALSE, flag;
Binode * bt;
Cout <"=========== Binary Tree demo program ======================" <endl;
Do
{
Cout <"1: Create a binary tree and traverse the result input in the first order. If it is null, it is expressed as 0." <endl;
Cout <"2: traverse Binary Tree in sequence and recursively traverse Binary Tree" <endl;
Cout <"3: Number of leaves" <endl;
Cout <"4: Calculate the height of a binary tree" <endl;
Cout <"5: Turning left and right of the tree" <endl;
Cout <"0: Exit" <endl;
Cout <"------- enter your choice:" <endl;
Cin> choice;
Switch (choice)
{
Case 1:
If (treecreated)
{
Cout <"sorry, the tree has been already created! "<Endl;
Break;
};
Cout <"Enter the number representing the tree:" <endl;
Flag = createbitree (& bt );
If (flag = OK)
{
Cout <"you have created a tree! "<Endl;
Treecreated = TRUE;
}
Break;
Case 2:
If (! Treecreated)
{
Cout <"sorry, you must create a tree for further steps! "<Endl;
Break ...... remaining full text>
Data Structure Problem "write a program to determine whether two binary trees are equal"
2. program source code:
Public class SimiliarTreeTest {
Public static boolean test (BinNode t1, BinNode t2 ){
If (t1 = null & t2 = null)
Return true;
Else if (t1! = Null & t2! = Null)
{
Return test (t1.left, t2.left) & test (t1.right, t2.right );
}
Return false;
}
}
3. Test class:
Public class SimiliarilityTest {
Public static void main (String [] args ){
BST <Integer> intTree = new BST <Integer> ();
BST <Character> charTree = new BST <Character> ();
BST <String> strTree = new BST <String> ();
Integer [] numbers = };
Character [] chars = };
String [] strs = {"this", "is", "so", "dispointed", "and", "I", "am", "trying "};
For (int I = 0; I <strs. length; I ++ ){
StrTree. insert (new BinNode <String> (strs [I]);
}
For (int I = 0; I <numbers. length; I ++)
IntTree. insert (new BinNode <Integer> (numbers [I]);
For (int I = 0; I <chars. length; I ++)
CharTree. insert (new BinNode <Character> (chars [I]);
Boolean B = SimiliarTreeTest. test (intTree. root, strTree. root );
System. out. println ("intTree and strTree are similiar with each other? "+ B );
System. out. println ("-----------------");
B = SimiliarTreeTest. test (intTree. root, charTree. root );
System. out. println ("intTree and charTree are similiar with each other? "+ B );
}
}
(Note: BST is a binary search tree)... the remaining full text>