Question
Help me to fix my error in this programmn in c++ I have error in my int Binary_Tree::getWidth( TreeNode* root) { int num; if (root
Help me to fix my error in this programmn in c++
I have error in my 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); }
and also just fix my hight adn width that give me the right number as following:
Test your program as follows:
Insert the following numbers (one at a time through menu option 1): 10, 87, 9, 55, 13, 40, 22,1,0,77, 0, 4, 55, 33, 22
Display the tree
Display the leaf count
Display the tree height
Display the tree width
#ifndef BINARYTREE_H #define BINARYTREE_H #include
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
// 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started