Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prompt: You are going to write a function that will use recursion to countthe nu

ID: 3611382 • Letter: P

Question

Prompt:
You are going to write a function that will use recursion to countthe number of nodes in a tree. Your main program will call a publicmember function called numNodes. This function will then call aprivate member function that will use recursion to count the numberof nodes in the entire tree. I need help making the numNodesfunction and the comments that is in bold. Theanswer to this project is 19 nodes.

#include <iostream>
using namespace std;

class IntBinaryTree
{
private:
   struct TreeNode
   {
      intvalue;         // The valuein the node
      TreeNode *left;    //Pointer to left child node
      TreeNode *right;   //Pointer to right child node
   };

   TreeNode *root;       //Pointer to the root node

   // Private member functions
   void insert(TreeNode *&, TreeNode *&);

public:
   // Constructor
   IntBinaryTree()
      { root = NULL; }
   {return numNodes(root);}

   void insertNode(int)
    
   // Destructor
//~IntBinaryTree()
//{ destroySubTree(root); }
    
   // Binary tree operations
   void insertNode(int);
   // bool searchNode(int);
   void remove(int);

};

int IntBinaryTree::countNodes(TreeNode *nodePtr)
{
   if (nodePtr == NULL)
       //write only one lineof code here
       return 0;
   else
       //write only one lineof code here
       return(1+count(nodePtr->right), count(nodePtr->left))
}

//void IntBinaryTree::numNode(TreeNode *nodePtr)

void IntBinaryTree:: insertNode(int num)
{
   TreeNode *newNode;      //Point to a new node.

   //Create a new node and store num in it.
   newNode = new TreeNode;
   newNode->value = num;
   newNode->left = newNode->right = Num;
   //Insert the node.
   insert(root, newNode);
}

int main()
{
   IntBinaryTree tree;
   tree.insertNode(14);
   tree.insertNode(10);
   tree.insertNode(8);
   tree.insertNode(6);
   tree.insertNode(5);
   tree.insertNode(7);
   tree.insertNode(9);
   tree.insertNode(12);
   tree.insertNode(11);
   tree.insertNode(13);
   tree.insertNode(22);
   tree.insertNode(30);
   tree.insertNode(32);
   tree.insertNode(24);
   tree.insertNode(20);
   tree.insertNode(17);
   tree.insertNode(15);
   tree.insertNode(18);
   tree.insertNode(21);

   cout << "The number of nodes in the treeis: " << tree.numNodes() << endl;

system("pause");
return 0;
}

Explanation / Answer

#include using namespace std; class IntBinaryTree {       private:               struct TreeNode               {                     TreeNode(int num)                     {                           value=num;                           left=NULL;                           right=NULL;                     }                     int value;         // Thevalue in the node                     TreeNode *left;    // Pointer to left child node                     TreeNode *right;   // Pointer to right child node               };               TreeNode *root;       // Pointer tothe root node               // Private member functions               bool insertNode(int ,TreeNode *&);               void print(TreeNode* );               int numNodes(TreeNode *nodePtr);          public:               // Constructor               IntBinaryTree(){root = NULL;}               ~IntBinaryTree(){}               bool insertNode(int);               void print();               void remove(int);               int numNodes(); }; int IntBinaryTree::numNodes() {       return numNodes(root); } int IntBinaryTree::numNodes(TreeNode *nodePtr) {     if (nodePtr == NULL)     return 0;     intleft_count=left_count=numNodes(nodePtr->left);     intright_count=right_count=numNodes(nodePtr->right);     return left_count+right_count+1; } bool IntBinaryTree::insertNode(int num) {       return insertNode(num , root); } bool IntBinaryTree::insertNode(int num,TreeNode*& nodePtr) {     if(nodePtr==NULL)     {           nodePtr=newTreeNode(num);           returntrue;     }     else if(numvalue)     return insertNode (num,nodePtr->left);     else if(num>nodePtr->value)     return insertNode (num,nodePtr->right);     else return false; } void IntBinaryTree::print() {     print(root); } void IntBinaryTree::print(TreeNode* nodePtr) {     if(nodePtr->left)     print(nodePtr->left);    cout