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
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
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<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
