Question
Hi, can someone please help me with my code I tried fixing it numerous times and it's must not working it has to be divided
Hi, can someone please help me with my code
I tried fixing it numerous times and it's must not working
it has to be divided into 3FILES(binaryTree.cpp,binaryTree.h,main.cpp)
My main issue is the switch when i insert the functions for count,height,and width
Also, the width function return value
Besides that, thats all I need to figure out
Please take the time to look at the code and not throw random code in please
I just have a few errors
I listed the requirements
Please provide functions for possible errors
Below is a copy of my code
//main.cpp
#include "stdafx.h"
#include
#include
#include "binaryTree.h"
using namespace std;
//Function Prototypes
void menu();
int main()
{
int choice = 0;
int input;
binaryTree tree;
//create do-while loop to at least run the code once
do
{
menu();
cin >> choice;
cout
switch (choice)
{
case 1:
cout
cin >> input;
tree.insertNode(input);
cout
break;
case 2:
cout
tree.displayInOrder();
break;
case 3:
cout
tree.leafCount();
cout
break;
case 4:
cout
tree.heightTree();
break;
case 5:
cout
tree.widthTree();
break;
case 6:
exit(0);
break;
default:
cout
cout
cout
}
} while (choice != 6);
return 0;
} //end of int main()
//*************************************************************************************
//Name:menu
//Task: displays the menu
//does not return anything
//Called in main
//*************************************************************************************
void menu()
{
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
}
//binarytree.cpp
#include "stdafx.h"
#include "binaryTree.h"
#include
#include
#include
using namespace std;
//Default Constructor initializes head to nullptr
binaryTree::binaryTree()
{
root = nullptr;
}
binaryTree::~binaryTree()
{
destroySubTree(root);
}
void binaryTree::destroySubTree(treeNode *nodePtr)
{
if (nodePtr)
{
if (nodePtr->left)
{
destroySubTree(nodePtr->left);
}
if (nodePtr->right);
{
destroySubTree(nodePtr->right);
}
delete nodePtr;
}
}
void binaryTree::insert(treeNode *&nodePtr, treeNode *&newNode)
{
if (!nodePtr)
{
nodePtr = newNode;
}
else if (newNode->data data)
{
insert(nodePtr->left, newNode); //Search the left branch
}
else
{
insert(nodePtr->right, newNode); //Search the right branch
}
}
void binaryTree::insertNode(int number)
{
treeNode *newNode = nullptr; //pointer to a new node
//Create a new node and store number in it
newNode = new treeNode;
newNode->data = number;
newNode->left = newNode->right = nullptr;
//Insert the node
insert(root, newNode);
}
void binaryTree::displayInOrder(treeNode *nodePtr) const
{
if (nodePtr)
{
displayInOrder(nodePtr->left);
cout data;
cout
displayInOrder(nodePtr->right);
}
}
void binaryTree::displayInOrder() const
{
displayInOrder(root);
}
//*************************************************************************************
//Name:leafCount
//Task:Determines how many nodes are in a linked list
//Called in main
//*************************************************************************************
int binaryTree::leafCount(treeNode *nodePtr)
{
if (!nodePtr)
{
return 0;
}
if (nodePtr->left == NULL && nodePtr->right == NULL)
return 1;
else
return leafCount(nodePtr->left) + leafCount(nodePtr->right);
}
int binaryTree::heightTree(treeNode *root)
{
if (!root)
{
return 0;
}
else
{
int left = heightTree(root->left);
int right = heightTree(root->right);
return max(left, right) + 1;
}
}
int binaryTree::widthTree(treeNode *root)
{
if (root == NULL)
{
return 0; // check condition root is NULL then return 0
}
else
{
int left = heightTree(root->left);
int right = heightTree(root->right);
int left = widthTree(root->left);
int right = widthTree(root->right);
return max((left + right + 1), max(left, right));
}
}
binarytree.h
#ifndef BINARYTREE_H
#define BINARYTREE_H
class binaryTree
{
private:
//Declare structure from node list
//structure for stack nodes
struct treeNode
{
int data; //inserts data into the node
treeNode *left; //pointer to left child node
treeNode *right; //pointer to right child node
};
//pointer to the root node
treeNode *root;
//private member functions
void insert(treeNode *&, treeNode *&);
void displayInOrder(treeNode *) const;
void destroySubTree(treeNode *);
public:
// Constructor
binaryTree();
// Destructor
~binaryTree();
//Linked list operations
void insertNode(int);
void displayInOrder() const;
int leafCount(treeNode *);
int heightTree(treeNode *);
int widthTree(treeNode *, int);
};
#endif
Functional Requirements: A looping, menu-driven program that allows the user to create a binary tree and to display data about the tree Pro gramming Requirements Using the IntBinary Tree class from Chapter 21 as the starting point, 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 shouldcontain high-level validation for numeric input (use a while loop and allow use to re-enter the data) Testyour 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 widthStep 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