I have a serious problem with recurive search of subtree in onether one.
I try this but...
public static boolean containsTree (Node t1, Node t2)
{
if (t2 == null)
return true;
// Empty tree is always a subtree.
else
return subTree(t1, t2);
}
public static boolean subTree (Node t1, Node t2)
{
if (t1 == null)
return false; // Big tree is over.
if (t1.getName().equalsIgnoreCase(t2.getName()))
return matchTree(t1, t2);
return (subTree(t1.getChild(), t2) || subTree(t1.getBrother(), t2));
}
private static boolean matchTree (Node t1, Node t2)
{
if (t1 == null && t2 == null)
return true; // Both trees are empty.
if (t1 == null || t2 == null)
return false; // Big tree empty and subtree still not found.
if (!t1.getName().equalsIgnoreCase(t2.getName()))
return false;
return (matchTree(t1.getChild(), t2.getChild()) && matchTree(
t1.getBrother(), t2.getBrother()));
}
seems to be not correctly formed. the containsTree function, stop to search when find a node that is different from onether one.
Below, you can find an example of two tree.
1
/ \ 3
/ \ /\
/ \ / \
2 3 7 8
/\ /\
/ \ / \
3 4 5 6
\
3
/\
/ \
7 8
In this case when the function compare the subtree on the right with subtree on the left, it stop to search when find equals parent node but it have different child node. I need that the function don't stop to search but go throw this point and search for all other child node and their subtree.