Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with C++ programming, I only need help with deleteMovie function Pro

ID: 3605922 • Letter: I

Question

I need help with C++ programming, I only need help with deleteMovie function

Program Specifications Add the following functionality to your Assignment 5 Binary Search Tree implementation:

1. Delete a movie. When the user selects this option, they should be prompted for the title of the movie to delete. Your code should then search the tree for that movie node, remove it from the tree, re-assign pointers to bypass the removed node, and free the memory used by the node. If the node to be deleted has both left and right children, replace the deleted node with the node with the minimum value in its right subtree. If the movie is not found in the search process, print “Movie not found.” and do not attempt to delete. A movie node should also be deleted when the quantity goes to 0 for any movie. Be sure to test your program for movie nodes with 0 children, 1 child, or 2 children!

2. Count movies in the tree. When the user selects this option, your program should traverse the tree in any order, count the total number of movie nodes in the tree, and print the count.

3. Delete the tree in the destructor using a postorder traversal. When the user selects quit, the destructor for the MovieTree class should be called. In the destructor, all of the nodes in the tree should be deleted. You need to use a postorder tree traversal for the delete or you will get segmentation fault errors. You may implement a helper function. Menu: After the tree has been built, display a menu with the following options: (includes functionality from Assignment 5).

1. Find a movie. When the user selects this option from the menu, they should be prompted for the name of the movie. Your program should then search the tree and display all information for that movie. If the movie is not found in the tree, your program should display, “Movie not found.” The findMovie method included in the MovieTree.hpp header file is a void type. All movie information should be displayed in the findMovie method. This should already be implemented from Assignment 5.

2. Rent a movie. When the user selects this option from the menu, they should be prompted for the name of the movie. If the movie is found in the tree, your program should update the Quantity in stock property of the movie and display the new information about the movie. If the movie is not found, your program should display, “Movie not found.” Just like findMovie, rentMovie is also a void type. Information about the movie rented should be displayed in the rentMovie method. If the quantity of a movie reaches 0, delete the movie after displaying the updated movie information. This should be mostly implemented from Assignment 5.

3. Print the entire inventory. When the user selects this option from the menu, your program should display all movie titles and the quantity available in sorted order by title. See the lecture notes on in-order tree traversal and your textbook for more information. This should already be implemented from Assignment 5.

4. Delete a movie. When the user selects this option from the menu, they should be prompted for the name of the movie. If the movie is found in the tree, your program should delete the movie node from the tree. If the movie is not found, your program should display, “Movie not found.”

5. Count movies in the tree. When the user selects this option, your program should traverse the tree, counting the total number of movie nodes in the tree, and print the count.

6. Quit the program. When the user selects this option, your program should exit after freeing all dynamically allocated memory.

Display menu

cout << "======Main Menu======" << endl;

cout << "1. Find a movie" << endl;

cout << "2. Rent a movie" << endl;

cout << "3. Print the inventory" << endl;

cout << "4. Delete a movie" << endl;

cout << "5. Count movies" << endl;

cout << "6. Quit" << endl;

Find a     movie

cout << "Enter title:" << endl;

Display   found     movie     information

cout << "Movie Info:" << endl;

cout << "===========" << endl;

cout << "Ranking:" << foundMovie->ranking << endl;

cout << "Title:" << foundMovie->title << endl;

cout << "Year:" << foundMovie->year << endl;

cout << "Quantity:" << foundMovie->quantity << endl;

If     movie     not found

cout << "Movie not found." << endl;

Rent a     movie

//If movie is in stock

cout << "Movie has been rented." << endl;

cout << "Movie Info:" << endl;

cout << "===========" << endl;

cout << "Ranking:" << foundMovie->ranking << endl;

cout << "Title:" << foundMovie->title << endl;

cout << "Year:" << foundMovie->year << endl;

cout << "Quantity:" << foundMovie->quantity << endl;

//If movie not found in tree

cout << "Movie not found." << endl;

Print the inventory

//For all movies in tree

cout<<"Movie: "<<node->title<<" "<<node->quantity<<endl;

Delete     a     movie

cout << "Enter title:" << endl;

//If movie not found in tree

cout << "Movie not found." << endl;

Count     movies

cout << "Count = " << count << endl;

Quit

cout << "Goodbye!" << endl;

MovieTree.hpp      Header   File:

#ifndef MOVIETREE_HPP

#define MOVIETREE_HPP

#include <string>

struct MovieNode

{

int ranking;

std::string title;

int year;

int quantity;

MovieNode *parent;

MovieNode *leftChild;

MovieNode *rightChild;

MovieNode(){};

MovieNode(int in_ranking, std::string in_title,

int in_year, int in_quantity)

{

ranking = in_ranking;

title = in_title;

year = in_year;

quantity = in_quantity;

parent = leftChild = rightChild = nullptr;

}

};

class MovieTree

{

public:

MovieTree();

~MovieTree();

void printMovieInventory();

void addMovieNode(int ranking, std::string title,

int releaseYear, int quantity);

void findMovie(std::string title);

void rentMovie(std::string title);

void deleteMovie(std::string title);

void countMovies();

private:

MovieNode *search(MovieNode *node, std::string title);

MovieNode *root;

};

#endif // MOVIETREE_HPP

I only need help with deleteMovie function

Explanation / Answer

//I have implemented, the search, minimum, successor and deleteMovie function,

//though I can't just check them on your code, but I have checked them on my BST code

//so, you may or may not get some minor errors on your compiler

//if you couldn't understand anything, just ask (comment), i will try my best to explain

//the search function i used, is inOrder serach

MovieNode *search(MovieNode *node,string title)

{

               if(node==NULL)

               {

                              return NULL;

               }

               if(title.compare(node->title)==0)

               {

                              return node;

               }

               MovieNode *found=search(node->leftChild,title);

               if(found!=NULL)

                              return found;

               found=search(node->rightChild,title);

               return found;

}

MovieNode *minimum(MovieNode *ptr)

{

               if (ptr != NULL)

               {

                              while (ptr->leftChild != NULL)

                                             ptr = ptr->leftChild;

                              return ptr;

               }

               else

                              return NULL;

}

MovieNode *successor(MovieNode *temp,string title)

{

               MovieNode *parent;

               MovieNode *ptr = search(temp, title);

               if (ptr->rightChild != NULL)

                              return minimum(ptr->rightChild);

               else

               {

                              parent = ptr->parent;

                              while (parent != NULL)

                              {

                                             if (parent->leftChild == ptr)

                                                            return parent;

                                             else

                                             {

                                                            ptr = parent;

                                                            parent = ptr->parent;

                                                            continue;

                                             }

                              }

                              return NULL;

               }

}

void deleteMovie(string title)

{

               MovieNode *x,*y,*z = search(root, title);

               //y is used to select node to delete, as it can be a successor as well, when z node has 2 children

               //z is the node which has the title given in the arguments

               if (z == NULL)

               {

                              cout <<"Movie not found ";

                              return;

               }

               if (z->leftChild == NULL || z->rightChild == NULL)

                              y = z;//when z has 0 or 1 child

               else

                              y = successor(root, z->title);

//when z has 2 children, so find another node (successor) to delete, but successor's data will be copied to z

              

               //here we link the children and parents

               if (y->leftChild != NULL)

                              x = y->leftChild;                                                              //x is used as children of y

               else

                              x = y->rightChild;

               if (x != NULL)

                              x->parent = y->parent;

               if (y->parent == NULL)

                              root = x;

               else

                              if (y == y->parent->leftChild)

                                             y->parent->leftChild = x;

                              else

                                             y->parent->rightChild = x;

               //if y !=z, then y, which is going to be deleted is a successor

               if (y != z) //putting value of successor

               {

                              z->title = y->title;

                              z->ranking = y->ranking;

                              z->year = y->year;

                              z->quantity = y->quantity;

               }

               free(y);

//                           cout << "Its deleted ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote