You MUST work alone on these problems. You may consult your book and your notes. These problems are due ON PAPER at the beginning of class on Tuesday, December 5. If you aren't in class you MUST turn the problems in before class, no papers will be accepted after class. For these problems trees contain strings (but this doesn't really matter).
Part A
Consider the functions below. What is the big-Oh running time of IsValueEqual in the average case. Assume that both s and t contain N nodes. You MUST justify your answer (with a recurrence relation/solution or by reasoning and explanation) for credit.
bool InTree(const string & s, Tree * t)
// postcondition: returns true if s in t, otherwise returns false
{
if (t != 0)
{
if (t->info == s) return true;
return InTree(s, t->left) || InTree(s, t->right);
}
return false;
}
bool FirstInSecond(Tree * s, Tree * t)
// postcondition: returns true if all values of s are in t
// returns false otherwise
{
if (s != 0)
{
if (InTree(s->info,t))
{
return FirstInSecond(s->left,t) &&
FirstInSecond(s->right,t);
}
return false;
}
return true;
}
bool IsValueEqual(Tree * s, Tree * t)
// postcondition: returns true if s is value equal to t,
// returns false otherwise
{
return FirstInSecond(s,t) && FirstInSecond(t,s);
}
Part B
Describe an O(N) algorithm for determining if two trees are value equal. If your answer to Part A is O(N), just write "see Part A".
Write a function IsEquivalent that returns true if its two tree parameters are equivalent and false otherwise. You must also give the big-Oh running time of your function with a justification.
Write a function IsIsomorphic that returns true if two trees are isomorphic. You must give the big-Oh complexity (in the average case) of your function with a justification.