Question
In C++ code Your ShelterBST class should implement the following functions. However, this time, you will sort your ShelterBST by Pet Name. Insert a node
In C++ code
Your ShelterBST class should implement the following functions. However, this time, you will sort your ShelterBST by Pet Name. Insert a node to ShelterBST This time, you will need to ensure there are no duplicates in your insert function (i.e., the tree should refuse to insert a Pet with a duplicate name). Find the parent for a given pet name. Your private method should take as input an existing Pet name stored in your tree, and return the TreeNode* that is its ancestor/parent node. Your public method should display the Pet name and age of the parent node. You do not need to consider the situation when the input value is not stored in your BST. Only input the values already stored in your BST. Find the successor for a given node with 2 children. You do not need to consider the situation when the input node has one or no children. Your private method should take as input an existing Pet name stored in your tree, and return the TreeNode* that is its successor node. Your public method should display the Pet name and age of the successor node. Delete a node based on a Pet name Delete a node based on the pet name given by the user. After deleting the node, your BST should be reconstructed. If the node to be deleted has two children, reconstruct the tree using the inorder successor (you should use the method you will write for D). You do not need to consider the situation when the input value is not stored in your BST. Only input the values already stored in your BST.
This is the code I have:
#include
using namespace std;
class ShelterBST { private: struct Pet { string name; int age; Pet(string x, int y) : name(x), age(y) {} };
struct TreeNode { Pet pet; TreeNode *left; TreeNode *right; TreeNode(Pet p) : pet(p), left(nullptr), right(nullptr) {} };
TreeNode *root;
TreeNode *insert(TreeNode *root, Pet *pet) { if (!root) return new TreeNode(*pet);
if (pet->name < root->pet.name) root->left = insert(root->left, pet); else root->right = insert(root->right, pet);
return root; } //returnin the pointer to treenode that will match the given age TreeNode *search(TreeNode *root, int age) { if (!root) return nullptr; if (root->pet.age == age) return root; if (age < root->pet.age) return search(root->left, age); else return search(root->right, age); } //THe three 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; }
public: ShelterBST(){ root=(nullptr);}
void insertPet(string name, int age) { root = insert(root, new Pet(name, age)); } //displaying pet name found if not error message void searchPet(int age) { TreeNode *result = search(root, age); if (result) cout << "Name: "<
void inorderDisplay() { cout << "Inorder:" << endl; inorder(root); }
void preorderDisplay() { cout << "Preorder:" << endl; preorder(root); }
void postorderDisplay() { cout << "Postorder:" << endl; postorder(root); } };
int main() { ShelterBST tree; // inserting 10 pets tree.insertPet ("Allie", 5); tree.insertPet ("James", 10); tree.insertPet ("Jack", 13); tree.insertPet ("Lucy", 7); tree.insertPet ("Duke", 9); tree.insertPet ("Nala", 12); tree.insertPet ("Linda", 14); tree.insertPet ("Justin", 34); tree.insertPet ("vic", 17); tree.insertPet ("AJ", 4); // the inorder display tree.inorderDisplay(); // the preorder display tree.preorderDisplay(); // the postorder display tree.postorderDisplay(); // a successful search tree.searchPet (7); tree.searchPet(34); // an unsuccessful search tree.searchPet (100); // we're assuming no Pet in the tree is aged 100 return 0; }
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