Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ CODE ONLY I need helping inserting 10 animals into the tree but whenever I try to insert i keep getting errors. These 10

IN C++ CODE ONLY

I need helping inserting 10 animals into the tree but whenever I try to insert i keep getting errors. These 10 animals should be inserted into the program and the interface allows the user to add another one on top of the 10.

Heres 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++; } void inOrder(TreeNode *node){ if (node==nullptr){ return; } inOrder(node-> left); cout<pet->name<<""<pet->age<right); } void preOrder(TreeNode *node){ if (node==nullptr){ return; } cout<pet->name<<""<pet->age<left); preOrder(node->right); } void postOrder(TreeNode *node){ if (node==nullptr){ return; } postOrder(node->left); postOrder(node->right); cout<pet->name<<""<pet->age<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; }

//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; };

};

int main(){ ShelterBST tree; int choice; do{ cout << "Please choose the operation you want: " << endl; cout << "1. Insert a node" << endl; cout << "2. Search a value" << endl; cout << "3. Delete a node" << endl; cout << "4. Quit the system" << endl; cin >> 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; } } }while(choice != 0); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions