C++ recursion problem Please complete the program below exactly as described. Do
ID: 3830638 • Letter: C
Question
C++ recursion problem
Please complete the program below exactly as described. Don’t add anything extra to it (methods, extra variables, features, etc.) and don’t leave any features described below out. You are going to write a function that will use recursion to count the number of nodes in a tree. Your main program will call a public member function called numNodes. This function will then call a private member function, countNodes, which will use recursion to count the number of nodes in the entire tree
https://pastebin.com/VKLEDApC
Thank you.
Explanation / Answer
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 *&);
int countNodes(TreeNode*);
public:
// Constructor
IntBinaryTree()
{ root = NULL; }
// Destructor
~IntBinaryTree()
{ destroySubTree(root); }
// Binary tree operations
void insertNode(int);
bool searchNode(int);
void remove(int);
int numNodes(TreeNode*);
};
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 + countNodes(nodePtr->left) + countNodes(nodePtr->right);
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.