Question: C++ Programming help- - Delete a movie: deleteMovie(std::string title) Your code needs to search the tree for that movie node, remove it from the tree,
C++ Programming help-- Delete a movie: deleteMovie(std::string title) Your code needs to 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. Be sure to test your program for movie nodes with 0 children, 1 child, or 2 children!
I have all the other functions done if you need them, but I'm only having problems with the delete function.
Header File:
#ifndef MOVIETREE_HPP #define MOVIETREE_HPP #include /* MovieNode: Node object to store in * the binary search tree (BST). Each movie's * info will be stored in one node. */ class MovieNode { public: // instance variables int ranking; std::string title; int year; int quantity; MovieNode *leftChild; MovieNode *rightChild; // constructors MovieNode() { ranking = -1; year = 0; quantity = -1; leftChild = rightChild = nullptr; }; 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; leftChild = rightChild = nullptr; } }; /* MovieTree: Class implementing the BST: * - You will implement the methods marked TODO. * - Root of tree is pointed at by 'root' */ class MovieTree { private: // instance var pointing to root node in tree MovieNode* root; public: // Constructor MovieTree(); // Destructor (TODO) ~MovieTree(); // Descr: see 'print the entire inventory' // in the homework manual. (TODO) void printMovieInventory(); // Descr: add movie to BST, at spot in tree // alphabetically-sorted by title. (TODO) // param rating: IMDB rating of movie // param title: title of movie // param releaseYear: release year of movie // param quantity: # of copies available to rent void addMovieNode(int ranking, std::string title, int releaseYear, int quantity); // Descr: Search the BST for the given title and // print that movie's info in the predefined // format. See 'Find a movie' in the manual. (TODO) // param title: title of node to find void findMovie(std::string title); // Descr: update the inventory to indicate a movie // has been rented and print predefined info. // See 'Rent a movie' in the manual. (TODO) // param title: title of node to rent void rentMovie(std::string title); // Descr: Delete the specified node from the BST. // See 'Delete a movie' in the hw manual. (TODO) // param title: title of node to delete void deleteMovie(std::string title); // Descr: Count & print the # of nodes in the tree // See 'Count movies in the tree'. (TODO) void countMovies(); }; #endif // MOVIETREE_HPP This is what I have so far and i keep getting an error
double free or corruption (fasttop)
this is my code so far:
MovieNode *minimumNode(MovieNode *node) { MovieNode *current = node; while(current->leftChild != NULL) { current=current->leftChild; } return current; } MovieNode* deleteMovieHelper(MovieNode *node, std::string title) { if(root == NULL) return root;
else if(title < node->title) { node->leftChild = deleteMovieHelper(node->leftChild, title); } else if(title > node->title) { node->rightChild = deleteMovieHelper(node->rightChild, title); } else { //Case 1: no children if(node->leftChild == NULL && node->rightChild == NULL) { delete node; node = NULL; } //case 2: one child else if(node->leftChild == NULL) { MovieNode temp = node; node = node->rightChild; delete temp; } else if(node->rightChild == NULL) { MovieNode temp = node; node = node->leftChild; delete node; } else { MovieNode * = findMin(node->rightChild); node->title = temp->title; node->rightChild= deleteMovieHelper(node->rightChild, temp->title); } } return node; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
