The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
#include <iostream>
#include <string>
I am having trouble with the insert function. I need help and explanation in how to take the integers in and store them into an array. The program is pretty much done except I need to make my input function take the integers one at the time.
Here are the specifications and guidelines.
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.)
Menu-driven program :
1. Insert numbers (validate for numeric)
2. Display the tree (in order)
3. Display Leaf Count
4. Display Tree Height
5. Display Tree Width
I wanna be able to for example:
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
I am trying to learn this for my own benefit as I am reviewing for my certification exam coming up
#include <sstream>
#include "IntBinaryTree.h"
using namespace std;
/*
int main()
{
// Create a binary tree to hold integers.
BinaryTree<int> oak;
// Array populated with test values.
int testValues[] =
{ 1, 2, 9, 21, 4, 17, 99, 15, 98, 100, 3, 5, 6, 10 };
int height, width;
// Populate the oak binary tree with the values in
// the testValues array.
for (int x = 0; x < 14; x++)
oak.insertNode(testValues[x]);
// Display the values in the tree.
oak.
cout << endl;
// Get the height and width and display these values.
height = oak.treeHeight();
width = oak.getWidth();
cout << "Height is " << height << endl;
cout << "Width is " << width << endl;
return 0;
}
*/
int displayMenu()
{
int option;
cout << "\n" << endl;
cout << "*********************** Binary Tree ***********************" << endl;
cout << "************************* MENU *************************" << endl;
cout << "* 1) Insert numbers *" << endl;
cout << "* 2) Display the tree *" << endl;
cout << "* 3) Display Leaf Count *" << endl;
cout << "* 4) Display Tree Height *" << endl;
cout << "* 5) Display Tree Width *" << endl;
cout << "**************************************************************" << endl;
cout << "* Enter menu option [1-7]: *" << endl;;
cout << "**************************************************************" << endl;
cin >> option;
cout << " " << endl;
return option;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //Also validation for menu code
cout << "\n\n" << endl;
}
int main()
{
IntBinaryTree tree;
int option,height, width;
int value[];
while (true)
{
option = displayMenu();
if (option == 1)
{
cout << "Insert numbers: \n";
cin >> value;
cout << "\n" << endl;
tree.insertNode(value[num]); //call the appendNode function
cout << "\n" << endl;
cout << "Numbers were added succefully! .\n" << endl;
}
else if (option == 2)
{
cout << "This is the tree diplayed: \n";
tree.displayInOrder();
}
else if (option == 3)
{
cout << "The number of leafs in the tree is: \n";
tree.leafCount();
}
else if (option == 4)
{
cout << "The height of the tree is: \n";
tree.treeHeight();
}
else if (option == 5)
{
cout << "The width of the tree is: \n";
tree.getWidth();
}
else if (option == 6)
{
break;
}
else
{ //Validation for integets (Correcting my mistakes)
cout << "Invalid Choice! Try Again! " << endl;
cout << "\n\n" << endl;
cout << "You are either entering an invalided menu choice number or not an integer! \n" << endl;
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
}
}
return 0;
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly