Question
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
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: "<
} 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: "<
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: "<
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started