Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 my width function but getting the code to run in visual studios

Please take the time to look at the code and NOT THROW RANDOM CODE in please

I just have a few errors

I am using visual studio and all the 10 codes that were send did not work for me

image text in transcribed

Please provide functions for possible errors

Below is a copy of my code

//binary.h file

#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 *);

void destroySubTree(treeNode *);

int leafCount(treeNode *);

int heightTree(treeNode *);

int widthTree(treeNode *);

public:

// Constructor

binaryTree();

// Destructor

~binaryTree();

//Linked list operations

void insertNode(int);

void displayInOrder();

int leafCount();

int heightTree();

int widthTree();

int getWidth(treeNode *, int);

};

#endif

//binary.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)

{

if (nodePtr)

{

displayInOrder(nodePtr->left);

cout data;

cout

displayInOrder(nodePtr->right);

}

}

void binaryTree::displayInOrder()

{

displayInOrder(root);

}

//*************************************************************************************

//Name:leafCount

//Task:Determines how many nodes are in a linked list

//Called in main

//*************************************************************************************

int binaryTree::leafCount() {

return leafCount(root);

}

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() {

return heightTree(root);

}

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() {

return widthTree(root);

}

int binaryTree::widthTree(treeNode *root) const

{

if (root == NULL)

{

return 0; // check condition root is NULL then return 0

}

else

{

int maxWidth = 0;

int width;

int h = heightTree(root);

int i;

for (i = 1; i

width = getWidth(root, i);

if (width > maxWidth) {

maxWidth = width;

}

}

return maxWidth;

}

}

int binaryTree::getWidth(treeNode *root, int level)

{

if (root == NULL)

return 0;

if (level == 1)

return 1;

else if (level > 1)

return getWidth(root->left, level - 1) + getWidth(root->right, level - 1);

}

//main.cpp

#include "stdafx.h"

#include

#include

#include "binaryTree.cpp"

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();

cout

break;

case 3:

cout

cout

cout

break;

case 4:

cout

cout

break;

case 5:

cout

cout

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

}

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 width

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

=+f) If 18 people are working, what Sales do you predict?

Answered: 1 week ago

Question

5. Prepare for the role of interviewee

Answered: 1 week ago

Question

6. Secure job interviews and manage them with confidence

Answered: 1 week ago