Tuesday, January 17, 2012

Checking whether a tree is subset of another tree

bool identical(node * p,node * q)
{
if(p==NULL && q==NULL)
return true;
else if(p==NULL ||q==NULL)
return false;
else
return(p->data==q->data && identical(p->left,q->left) && identical(p->right,q->right));
}

bool subtree(node *p,node *q)
{
if(q==NULL)
return true;
else if(p==NULL)
return false;
else if(identical(p,q))
return true;
return subtree(p->left,q) || subtree(p->right,q);
}

No comments: