Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, Is there a possibility where you can change this code so user can input info

ID: 3808151 • Letter: H

Question

Hi, Is there a possibility where you can change this code so user can input information through the console?
Such as: Mike - 100, Tom - 75, Steve - 99, Carry - 58, Stephanie - 82, George - 42
Like instead of having presets, can somone help me make this program where the user is allowed to enter the following through console and it sorts as the following in the program prompt?

C++ Program Specification
"Build a binary search tree, using links (not an array) for 15 records. The data in these records will hold names and their associated weights. Read the data from the screen.
Required functionality (Each # should be separate methods):
Build the tree from the unique set of names (names are the key value) and their associated weights.
Execute a preorder traversal
Execute an inorder traversal
Execute a postorder traversal
Find and print the height of the tree using recursion, do not add a height variable to the tree structure, the algorithm stack should hold this.
Determine the number of leaves and print the result (remember a leaf has no children).
Implement search functionality that will search for a name and indicate the weight for that individual if they exist in the structure, otherwise stating no match exists.
Determine the lowest weight contained in the tree.
Find the first name in alphabetical order (this should not go through every node, unless the tree happens to be a linked list)."

___________________________________________________________

#include<iostream>
#include<cstdlib>
using namespace std;
  
struct node
{
int name;
struct node *left, *right;
};

struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->name = item;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, int name)
{
if (node == NULL)
       return newNode(name);
if (name < node->name)
node->left = insert(node->left, name);
else if (name > node->name)
node->right = insert(node->right, name);   
return node;
}
void preorder(struct node* root)
{
if (root)
{
cout<<(root->name)<<" ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct node* root)
{
if (root)
{
postorder(root->left);
postorder(root->right);
cout<<(root->name)<<" ";
}
}

void inorder(struct node* root)
{
if (root)
{
inorder(root->left);
cout<<(root->name)<<" ";
inorder(root->right);
}
}

int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
int l = height(node->left);
int r = height(node->right);
if (l > r)
return(l+1);
else
          return(r+1);
}
}
int LeafCount(struct node* node)
{
if(node == NULL)   
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return LeafCount(node->left)+LeafCount(node->right);
}

int main()
{
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
insert(root, 35);
insert(root, 92);
insert(root, 56);
insert(root, 74);
insert(root, 87);
insert(root, 86);
insert(root, 76);
insert(root, 83);
insert(root, 75);
cout<<endl<<"PreOrder : ";
   preorder(root);
cout<<endl<<"InOrder : ";
inorder(root);
cout<<endl<<"PostOrder : ";
postorder(root);
cout<<endl<<"Leaf Count : "<<LeafCount(root);
   cout<<endl<<"Height : "<<height(root);
return 0;
}

Explanation / Answer

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
  
struct node
{
   int key;
   string name;
   struct node *left, *right;
};
struct node *newNode(int item, string dataName)
{
   struct node *temp = (struct node *)malloc(sizeof(struct node));
   temp->key = item;
   (temp->name).assign(dataName);
   temp->left = temp->right = NULL;
   return temp;
}
struct node* insert(struct node* node, int data, string name)
{
   if (node == NULL)
   return newNode(data,name);
   if (data < node->key)
       node->left = insert(node->left,data, name);
   else if (data > node->key)
       node->right = insert(node->right,data, name);   
   return node;
}
void preorder(struct node* root){
   if (root){
       cout<<(root->key)<<" - "<<(root->name)<<" ";
       preorder(root->left);
       preorder(root->right);
   }
}
void postorder(struct node* root)
{
   if (root)
   {
       postorder(root->left);
       postorder(root->right);
       cout<<(root->key)<<" - "<<(root->name)<<" ";
   }
}
void inorder(struct node* root)
{
   if (root)
   {
       inorder(root->left);
       cout<<(root->key)<<" - "<<(root->name)<<" ";
       inorder(root->right);
   }
}
int height(struct node* node){
   if (node==NULL)
       return 0;
   else{
       int l = height(node->left);
       int r = height(node->right);
   if (l > r)
       return(l+1);
   else
   return(r+1);
   }
}
int LeafCount(struct node* node){
   if(node == NULL)   
       return 0;
   if(node->left == NULL && node->right==NULL)
       return 1;
   else
   return LeafCount(node->left)+LeafCount(node->right);
}
int main()
{
   struct node *root = NULL;
   int n,key;
   string name;
   cout<<"Enter number of nodes you want to enter in tree : ";
   cin>>n;
   cout<<"Enter Name and Key : ";
   cin>>name>>key;
   root = insert(root,key,name);
   for(int i=0;i<n-1;i++){
       cout<<"Enter Name and Key : ";
       cin>>name>>key;
       insert(root,key,name);
   }
   cout<<endl<<"PreOrder : ";
   preorder(root);
   cout<<endl<<"InOrder : ";
   inorder(root);
   cout<<endl<<"PostOrder : ";
   postorder(root);
   cout<<endl<<"Leaf Count : "<<LeafCount(root);
   cout<<endl<<"Height : "<<height(root);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote