Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, can someone please help me review my code I just have a few errors I listed the requirements Please provide functions for possible errors

Hi, can someone please help me review my code

I just have a few errors

I listed the requirements

Please provide functions for possible errors

image text in transcribed

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;

int left = heightTree(root->left);

int right = heightTree(root->right);

return max(left, right)+1;

}

int binaryTree::widthTree(treeNode *root, int columnTree)

{

if (!root)

{

return 0;

}

if (columnTree == 1)

{

return 1;

}

else if (columnTree > 1)

{

return widthTree(root->left, columnTree - 1) + widthTree(root->right, columnTree - 1);

}

}

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

Mastering Influxdb Database A Comprehensive Guide To Learn Influxdb Database

Authors: Cybellium Ltd ,Kris Hermans

1st Edition

B0CNGGWL7B, 979-8867766450

More Books

Students also viewed these Databases questions

Question

Identify the cause of a performance problem. page 363

Answered: 1 week ago