(Solved) : Please Help Program Needs Implement Binary Search Tree Able Write Code Complete Would Appr Q37276693 . . .

Can you please help me with this program. It needs toimplement Binary Search Tree. I have been able to write some code,but it is not complete and I would appreciate any help possible.The code needs to implement commands that are mentioned in thedocument below. I have also provided my codebelow.

# include
# include
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*root;

WE WRITE PAPERS FOR STUDENTS

Tell us about your assignment and we will find the best writer for your project

Write My Essay For Me

class BST
{
public:
void find(int, node **, node **);
void insert(node *, node *);
void del(int);
void inorder(node *);
//void postorder(node *);
void display(node *, int);
       void case_a(node *par, node*loc);
       void case_b(node *par, node*loc);
       void case_c(node *par, node*loc);
      

BST()
{
root = NULL;
}
};

int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<“—————–“<<endl;
cout<<“Operations on BST”<<endl;
cout<<“—————–“<<endl;
cout<<“1.Insert Element “<<endl;
cout<<“2.Delete Element “<<endl;
cout<<“3.Inorder Traversal”<<endl;
cout<<“4.Display”<<endl;
cout<<“5.Quit”<<endl;
cout<<“Enter your choice : “;
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<“Enter the movie to be inserted : “;
cin>>temp->info;
bst.insert(root, temp);
case 2:
if (root == NULL)
{
cout<<“Tree is empty, nothing to delete”<<endl;
continue;
}
cout<<“Enter the number to be deleted : “;
cin>>num;
bst.del(num);
break;
case 3:
cout<<“Inorder Traversal of BST:”<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:   
          cout<<“Display BST:”<<endl;
bst.display(root,1);
cout<<endl;
break;
case 5:
exit(1);
default:
cout<<“Wrong choice”<<endl;
}
}
}

void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}

void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<“Root Node is Added”<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<“Element already in the tree”<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);   
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<“Node Added To Left”<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<“Node Added To Right”<<endl;
return;
}   
}
}

void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<“Tree empty”<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<“Item not present in tree”<<endl;
return;
}
if (location->left == NULL && location->right ==NULL)
case_a(parent, location);
if (location->left != NULL && location->right ==NULL)
case_b(parent, location);
if (location->left == NULL && location->right !=NULL)
case_b(parent, location);
if (location->left != NULL && location->right !=NULL)
case_c(parent, location);
free(location);
}

void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}

void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}

void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}

void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<“Tree is empty”<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<info<<” “;
inorder(ptr->right);
}
}

void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<: “;
else
{
for (i = 0;i < level;i++)
cout<<” “;
}
cout<info;
display(ptr->left, level+1);
}
}

Description: Rather than just using a simple list to hold the movie and person information from Project 1 we will extend it to use a binary search tree to hold the information and allow some additional commands. Details: You will once again read the current input in at startup. The movies and the people will be built into two that this is unique. For people, it will be the name- -we will assume that this is unique for our data. The user will then be able to issue commands that will allow them to search and modify the tree. On exit, the modified tree should be saved to the same file for use in subsequent runs. Commands The commands to implement are: Find movie : find a movie. Your program should prompt the user and get the title and search the tree Find person: find a person. Your program should prompt the user and get the person’s name and then search the tree for any people that match the name given. Delete movie: delete a move from the movie tree the movie should be input using the title. Delete Person: Delete a person from the person tree. The person should be entered by name. Add Movie Add a new movie to the tree. Add PersonAdd a new person to the tree List Movies : List all the movies in the tree. List People: List all the people in the tree. Exit: save the tree containing the movies and the tree containing the people to files and exit the program Hints: Do it in easy sections. Build a tree for one item first. Submission: Submit your source files and data files on BlackBoard in the space provided. Show transcribed image text Description: Rather than just using a simple list to hold the movie and person information from Project 1 we will extend it to use a binary search tree to hold the information and allow some additional commands. Details: You will once again read the current input in at startup. The movies and the people will be built into two that this is unique. For people, it will be the name- -we will assume that this is unique for our data. The user will then be able to issue commands that will allow them to search and modify the tree. On exit, the modified tree should be saved to the same file for use in subsequent runs. Commands The commands to implement are: Find movie : find a movie. Your program should prompt the user and get the title and search the tree Find person: find a person. Your program should prompt the user and get the person’s name and then search the tree for any people that match the name given. Delete movie: delete a move from the movie tree the movie should be input using the title. Delete Person: Delete a person from the person tree. The person should be entered by name. Add Movie Add a new movie to the tree. Add PersonAdd a new person to the tree List Movies : List all the movies in the tree. List People: List all the people in the tree. Exit: save the tree containing the movies and the tree containing the people to files and exit the program Hints: Do it in easy sections. Build a tree for one item first. Submission: Submit your source files and data files on BlackBoard in the space provided.

Expert Answer


I lOVE this Professional essay writing website. This is perhaps the fifth time I am placing an order with them, and they have not failed me not once! My previous essays and research papers were of excellent quality, as always. With this essay writing website, you can order essays, coursework, projects, discussion, article critique, case study, term papers, research papers, research proposal, capstone project, reaction paper, movie review, speech/presentation, book report/review, annotated bibliography, and more.

Post your homework questions and get original answers from qualified tutors!

PLACE YOUR ORDER

Share your love