Question: Help me to fix my program in c++: Programming Requirements: Using the IntBinaryTree class ,add the following member functions: Leaf Counter (which counts and returns

Help me to fix my program in c++:

Programming Requirements:

Using the IntBinaryTree class ,add the following member functions:

Leaf Counter (which counts and returns the number of leaf nodes in the tree)

Tree Height (which counts and returns the height of the tree - the height is the number of levels it contains.

Tree Width (which counts and returns the width of the tree - the width is the largest number of nodes in the same level.)

Write a menu-driven program that will allow the user to:

1. Insert numbers

2. Display the tree (in order)

3. Display Leaf Count

4. Display Tree Height

5. Display Tree Width

6. Exit

Your program should contain high-level validation for numeric input (use a while loop and allow use to re-enter the data).

//*********************************************************************BinaryTree.h

#pragma once #ifndef BINARYTREE_H #define BINARYTREE_H #include using namespace std;

class Binary_Tree { private: struct TreeNode { int value; // The value in 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 NodeCount(TreeNode*); int LeafCount(TreeNode *); int TreeHeight(TreeNode * ); int getWidth(TreeNode * root); void displayInOrder(TreeNode *) const;

public: // Constructor Binary_Tree() { root = NULL ; } // Binary tree operation void insert(int);

void displayInOrder() const { displayInOrder(root); } int Leafs() {

int i = LeafCount(root); return i; } int count() { int c = NodeCount(root); } int TreeHeight() { int c = TreeHeight(root); return c; } int getWidth() { int w = getWidth(root); return w; } }; #endif

//************************************************* BinaryTree.cpp

// Implementation file for the IntBinaryTree class #include #include "BinaryTree.h" using namespace std;

// insert creats a new node to hold num as its value void Binary_Tree::insert(int num) { TreeNode *newNode; newNode = new TreeNode; newNode->value = num; newNode->left = newNode->right = NULL; insert(root, newNode); } //************************************************************* // insert accepts a TreeNode pointer and a pointer to a node. * // The function inserts the node into the tree pointed to by * // the TreeNode pointer. This function is called recursively. * //*************************************************************

void Binary_Tree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == nullptr) nodePtr = newNode; // Insert the node. else if (newNode->value < nodePtr->value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode); // Search the right branch }

//**************************************************************** // The displayInOrder member function displays the values * // in the subtree pointed to by nodePtr, via inorder traversal. * //**************************************************************** void Binary_Tree::displayInOrder(TreeNode *nodePtr) const { if (nodePtr) { displayInOrder(nodePtr->left); cout << nodePtr->value << endl; displayInOrder(nodePtr->right); } } int Binary_Tree::NodeCount(TreeNode *node) // count the number of node { if (node == NULL) { return 0; } else { return(NodeCount(node->left) + 1 + NodeCount(node->right)); } }

int Binary_Tree::LeafCount(TreeNode*node) { //Accepts tree Pointer and return number of leaf nodes if (node == NULL) return 0; else if (node->left == NULL && node->right == NULL) { return 1; } else { return (LeafCount(node->left) + LeafCount(node->right)); } }

int Binary_Tree::TreeHeight( TreeNode* tree) //calculates the hight of tree { int left, right; if (tree == NULL) return(-1); left = TreeHeight(tree->left); right = TreeHeight(tree->right); if (left >= right) return(left = 1); else return(right + 1); }

int Binary_Tree::getWidth( TreeNode* root) { int num; if (root == NULL) return 0; if (num == 1) return 1; else if (num > 1) return getWidth(root->left) + getWidth(root->right); }

//*******************************main.cpp

// This program demonstrates the Binary_Tree class template. // It builds a binary tree with 5 nodes. #include #include #include "BinaryTree.h" int main() { Binary_Tree tree ; int opt, n; while (1) { cout << " Implementation of Binary Tree" << endl; cout << " -------------------------------" << endl; cout << " 1. Insert numbers (validate for numeric):" << endl; cout << " 2. Display the tree (in order)" << endl; cout << " 3. Display Leaf Count" << endl; cout << " 4. Display Tree Height" << endl; cout << " 5. Display Tree Width" << endl; cout << " 6. -Exit" << endl; cout << "Enter your Option " << endl; cin >> opt; switch (opt) { case 1: cout << "Enter The Node" << endl; cin >> n; tree.insert(n); break; case 2: cout << " ------------------------------------------" << endl; cout << " Display Elemnts using Inorder Treversal" << endl; cout << " ------------------------------------------" << endl; tree.displayInOrder(); break; case 3: cout << " Leaf count of the tree is:" << tree.count() << endl; break; case 4: cout << " The Height of the Tree is: " << tree.TreeHeight() << endl; break; case 5: cout << " The Width of the Tree is: " << tree.getWidth(); break; case 6: exit(0); default: cout << "Invalid Choice!Please Select Correct One" << endl; } } return 0; }

//**************************************************Just make sure prog is runing******************

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!