Tuesday, November 29, 2011

Is given tree foldable

/* Returns true if the given tree is foldable */
bool isFoldable(struct node *root)
{
  bool res;
 
  /* base case */
  if(root == NULL)
    return true;
 
  /* convert left subtree to its mirror */
  mirror(root->left);
 
  /* Compare the structures of the right subtree and mirrored
    left subtree */
  res = isStructSame(root->left, root->right);
 
  /* Get the originial tree back */
  mirror(root->left);
 
  return res;
}
 
bool isStructSame(struct node *a, struct node *b)
{
  if (a == NULL && b == NULL)
  return true; }
  if ( a != NULL && b != NULL &&
       isStructSame(a->left, b->left) &&
       isStructSame(a->right, b->right)
     )
  return true; }
 
  return false;
}   
 

No comments:

Post a Comment