Question
In C++ code only I need help creating user interface. It should look like: Here is my code: #include #include using namespace std; class Pet{
In C++ code only
I need help creating user interface. It should look like:
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; } };
class ShelterBST{ private: TreeNode *root; 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 pet->name){ current=current->left; } else if (pet->name > current ->pet->name){ current = current->right; } else { cout namename pet->name){ parent->left=node; } else{ parent->right=node; } } size++; } void inOrder(TreeNode *node){ if (node==nullptr){ return; } inOrder(node-> left); coutpet->namepet->ageright); } void preOrder(TreeNode *node){ if (node==nullptr){ return; } coutpet->namepet->ageleft); preOrder(node->right); } void postOrder(TreeNode *node){ if (node==nullptr){ return; } postOrder(node->left); postOrder(node->right); coutpet->namepet->ageleft) 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 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 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 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 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 pet->name pet->age
void displayParent(string name) { TreeNode *parent = findParent(root, name); if (parent) cout pet->name pet->age
//successor void displaySuccessor(string name) { TreeNode *node = search(root, name); if (!node) { cout pet->name pet->age pet->name pet->age
//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)left) && balanced(root->right)) return 1; return 0; };
};
int main() { int choice; do{ cout>choice; //using switch to reprint the options switch (choice){ case 1: string pet; cout>pet;
Please choose the operation you want: 1. Insert a node 2. Search a value 3. Delete a node 0 . Quit the systemStep 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