binary search tree countnodes

Ok so i have this function countnodes that calculates height and number of nodes in tree. I just don't understand how i can implement the function so it derives both height and nodes in same function.
1
2
3
4
5
template    <typename  NodeType>
int     CBSTree<NodeType>::CountNodes(const CTreeNode<NodeType>  *nodePtr, int  currDepth, int  &numNodes )const
{

}  // end of "CBSTree::CountNodes" 

i know for height i can do this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(nodePtr == NULL)
        return 0;

    else
    {
        NodeType ldepth = CountNodes(nodePtr->m_left,currDepth,numNodes);
        NodeType rdepth = CountNodes(nodePtr->m_right,currDepth,numNodes);

        if(ldepth > rdepth)
            return (ldepth+1);
        else
            return (rdepth+1);
    }
     

and for counting nodes this
1
2
3
4
5
6
7
8
9
10
11
 if ( nodePtr == NULL )
       return 0;  // The tree is empty.  It contains no nodes.
    else
   {
       NodeType count = 1;   // Start by counting the root.
       count += CountNodes(nodePtr->m_left,currDepth,numNodes);  // Add the number of nodes
                                        //     in the left subtree.
       count += CountNodes(nodePtr->m_right,currDepth,numNodes); // Add the number of nodes
                                        //    in the right subtree.
       return count;  // Return the total.
    }


Then i have this function that suppose to call countnode function
1
2
3
4
5
template    <typename  NodeType>
void    CBSTree<NodeType>::GetTreeInfo(int  &numNodes, int  &height) const
{
    
}

Topic archived. No new replies allowed.