Trent wrote:
> Hi..
> 
> I got trouble here.
> I got this section of code that I am trying to return the total number
> of nodes with only one child.
> 
> 
> 
> template<class elemType>
> int binaryTreeType<elemType>::sCCount(nodeType<elemType> *p)
> {
>       if ( p == NULL )
>               return 0;
>       else
>               if ((p->llink == NULL && p->rlink != NULL) || (p->llink != NULL 
> &&
> p->rlink == NULL))
>                       return 1;

This will fail...

     *
    /
   *
  / \
*   *
  \
   *
  /
*

You probably want to do something like:

if (p->llink == NULL && p->rlink != NULL)
        return 1 + sCCount(p->rlink);
else if (p->llink != NULL && p->rlink == NULL)
        return 1 + sCCount(p->llink);



>               else
>                       return sCCount(p->llink) + sCCount(p->rlink);
> }
> 
> There are a total of 4 nodes with only 1 child, but the function is
> only returning 2.
> 
> Any ideas?
> 
> Thanks


-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to