Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the decimal value of the binary 0 1 0 1 0 1 0 1 ?

Answered: 1 week ago