Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ ONLY I am having a hard time with my user interface. Its supposed to be you choose a number and that operation will

IN C++ ONLY I am having a hard time with my user interface. Its supposed to be you choose a number and that operation will be made. Right now when the user chooses a number it is not doing the any of the operations. Here is my code:

#include #include using namespace std;

class Pet{ public: string name; int age; Pet(string name, int age){ name=name; age=age;}};

class TreeNode{ public: Pet *pet; TreeNode *left; TreeNode *right; TreeNode(Pet *pet){ pet=pet; left=nullptr; right=nullptr;}}; TreeNode *root;

class ShelterBST{ private: int size; public: ShelterBST(){ root=(nullptr); size=0; } struct Node{ int data; struct Node* left; struct Node* right;}; void insert(Pet *pet){ TreeNode *node = new TreeNode(pet); if (root == nullptr){ root = node; } else { TreeNode *current = root; TreeNode *parent = nullptr; while (current != nullptr){ current = current; if (pet -> name < current->pet->name){ current=current->left;} else if (pet->name > current ->pet->name){ current = current->right;} else { cout << "Pet with name: "<name<<" already exists" << endl; return;}} if (pet->name < parent ->pet->name){ parent->left=node; } else{ parent->right=node; } } size++; } //traversals void inOrder(TreeNode *root){ if (!root) return; inOrder(root->left); cout << root->pet->name << " " << root->pet->age << endl; inOrder(root->right);

} void preOrder(TreeNode *root) { if (!root) return; cout << root->pet->name << " " << root->pet->age << endl; preOrder(root->left); preOrder(root->right); }

void postOrder(TreeNode *root){ if (!root) return; postOrder(root->left); postOrder(root->right); cout << root->pet->name << " " << root->pet->age << endl; }

TreeNode *findMin(TreeNode *root) { while (root->left) root = root->left; return root; } //search TreeNode *search(TreeNode *root, string name){ if (!root) return nullptr; if (root->pet->name == name) return root; if (name < root->pet->name) return search(root->left, name); else return search(root->right, name); };

//finding the parents TreeNode *findParent(TreeNode *root, string petName) { if (!root) return nullptr; if ((root->left && root->left->pet->name == petName) || (root->right && root->right->pet->name == petName)) return root; if (petName < root->pet->name) return findParent(root->left, petName); else return findParent(root->right, petName); }

TreeNode *findSuccessor(TreeNode*root, string petName) { TreeNode *current = search(root, petName); if (!current) return nullptr; if (current->right) return findMin(current->right); TreeNode *successor = nullptr; TreeNode *ancestor = root; while (ancestor != current) { if (current->pet->name < ancestor->pet->name) { successor = ancestor; ancestor = ancestor->left; } else ancestor = ancestor->right; } return successor; };

//deleting nodes TreeNode *deleteNode(TreeNode *root, string petName) { if (!root) return nullptr; if (petName < root->pet->name) root->left = deleteNode(root->left, petName); else if (petName > root->pet->name) root->right = deleteNode(root->right, petName); else { if (!root->left) { TreeNode *temp = root->right; delete root; return temp; } else if (!root->right) { TreeNode *temp = root->left; delete root; return temp; } else { TreeNode *temp = findMin(root->right); root->pet = temp->pet; root->right = deleteNode(root->right, temp->pet->name); } } return root; }

//finding the successor TreeNode *findSuccessor(TreeNode *node) { node = node->right; while (node->left) node = node->left; return node; };

//finding the parent void findParent(string petName) { TreeNode *result = findParent(root, petName); if (result) cout << "Parent Pet Name: " << result->pet->name << ", Age: " << result->pet->age << endl; else cout << "Pet not found" << endl; }

void displayParent(string name) { TreeNode *parent = findParent(root, name); if (parent) cout << "Parent Name: "<< parent->pet->name << ", Age: "<< parent->pet->age << endl; else cout << "Parent not found" << endl; }

//successor void displaySuccessor(string name) { TreeNode *node = search(root, name); if (!node) { cout << "Node not found" << endl; return; } TreeNode *successor = findSuccessor(node); cout << "Successor Name: "<< successor->pet->name << ", Age: "<< successor->pet->age << endl; } //finding the successor void findSuccessor(string petName) { TreeNode *result = findSuccessor(root, petName); if (result) cout << "Successor Pet Name: " << result->pet->name << ", Age: " << result->pet->age << endl; else cout << "Pet not found or node has no successor" << endl; } void searchPet(string name) { TreeNode *result = search(root, name); if (result) cout << "Name: "<pet->name << ", Age: "<pet->age << endl; else cout << "not found" << endl; };//counting the nodes int totalNodes(Node *root){ if (root==NULL) return 0; int i = totalNodes(root->left); int r = totalNodes(root->right); return i+i+r; }//leaf nodes int getleaf(struct Node* Node){ if (Node ==NULL) return 0; if (Node->left == NULL && Node-> right ==NULL) return 1; else return getleaf(Node->left)+getleaf(Node->right); };//find height int maxheight(Node*Node) { if (Node == NULL) return 0; else { int height1 = maxheight(Node->left); int height2= maxheight(Node->right); if (height1>height2) return (height1+1); else return (height2+1); } };//calculating if balance bool balanced(Node* root) { int h1; //height of left subtree int h2; //height right subtree //when tree is empty if (root==NULL) return 1; //get the heights of sub trees h1=maxheight(root->left); h2=maxheight(root->right); if (abs(h1-h2)<= 1 && balanced(root->left) && balanced(root->right)) return 1; return 0; };

void inOrder(){ cout<<"InOrder: "<

void postOrder() { cout << "Postorder:" << endl; postOrder(root); }};

int main(){ ShelterBST tree; tree.insert(new Pet("alex", 4)); tree.insert(new Pet("jeff", 7)); tree.insert(new Pet("liz", 2)); tree.insert(new Pet("Ben", 5)); tree.insert(new Pet("Harry", 10)); tree.insert(new Pet("jack", 3)); tree.insert(new Pet("lexi", 15)); tree.insert(new Pet("allen", 12)); tree.insert(new Pet("olive", 20)); tree.insert(new Pet("duke", 25)); return 0;

int choice; do{ cout<<"Welcome to my implementation of BST"<< endl; cout<<"Please choose the operation you want: "<> choice; switch(choice){ case 1: { string name; int age; cout << "Enter the name of the pet: "; cin >> name; cout << "Enter the age of the pet: "; cin >> age; Pet *pet = new Pet(name, age); tree.insert(pet); break; } case 2: { string name; cout << "Enter the name of the pet to search: "; cin >> name; TreeNode *node = tree.search(root, name); if(node == nullptr){ cout << "Pet not found" << endl; }else{ cout << "Pet found: " << node->pet->name << " " << node->pet->age << endl; } break; } case 3: { string name; cout << "Enter the name of the pet to delete: "; cin >> name; TreeNode*root = tree.deleteNode(root, name); break; } case 4: { cout<< "traversals" <

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

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

What is the education level of your target public?

Answered: 1 week ago

Question

What advertising media and promotional tactics will you use?

Answered: 1 week ago