Consider the following declarations when answering parts h: class b_node {public
ID: 3578346 • Letter: C
Question
Consider the following declarations when answering parts h: class b_node {public: string client; b node *right, *left;}; class BST {public: BST(); int Count_Nodes_with_Two_Children(b_node *r); private: b_node *root;}; Implement the recursive function called "Count Nodes with Two Children" that returns an integer representing the number of nodes with two children in a binary search tree given a pointer to the root of the tree. Assume that the function is originally invoked by another member function.Explanation / Answer
int Count_Nodes_with_Two_Children(b_node *r)//function which counts the nodes which have two childs...and //returns..
{
if(b_node == NULL)//if node is null then returning zero
{
return 0;
}
if(b_node->right!=NULL && b_node->left !=NULL)//checking whether two nodes are present are not... if present
{
int c = Count_Nodes_with_Two_Children(b_node->left) + Count_Nodes_with_Two_Children(b_node->right);//calling each child again
return c+1;//adding count value...and returning count..
}
else//if doesn't have two childs...
{
//calling each child again
int c = Count_Nodes_with_Two_Children(b_node->left) + Count_Nodes_with_Two_Children(b_node->right);
return c;//for those nodes who have no two childs,count is not added...
}
}