Question: In this assignment, you will continue working with the movie data from the BST assignment and store that data in a red-black tree. A red-black
In this assignment, you will continue working with the movie data from the BST assignment and store that data in a red-black tree. A red-black tree is a selfbalancing binary search tree. The main difference between this assignment and previous assignments is that you will need to balance the tree each time a new movie node is added or deleted. For each of the movies in the movie nodes in the movie tree, the following information is kept:
- IMDB ranking
- Title
- Year released
- Quantity in stock
- Node color Your program will have a menu similar to previous assignments from which the user can select options. In this assignment, your menu needs to include options for finding a movie, renting a movie, printing the inventory, deleting a movie, counting the number of movies, counting levels in the tree, and quitting the program. Use the same Assignment8Movies.txt file that you used for the BST movie assignment. The name of the input file needs to be handled as a command-line argument.
1. Insert all the movies in the tree.
When the user starts the program they will pass it the name of the text file that contains all movie information. Your program needs to handle that command-line argument, open the file, and read all movie data in the file. From this data, build the red-black tree ordered by movie title. As movies are added to the tree, the red-black tree-balancing algorithm should be applied. All information about the movie should also be included in the movie node in the tree. Note: the data should be added to the tree in the order it is read in.
2. Find 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, display the movie information. If the movie is not found, your program should display, Movie not found. This option is similar to rent movie, however, you are not updating the quantity.
3. 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. When the Quantity is 0, the movie should be deleted from the tree. When a movie is deleted, the tree should be rebalanced. If the movie is not found, your program should display, Movie not found.
4. 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 for more information.
5. 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, delete it if its found, and then perform any necessary tree balancing to restore the red-black tree properties. If the movie is not found in the search process, print Movie not found. and do not attempt to delete.
6. Count movies in the tree.
When the user selects this option, your program should do an in-order tree traversal and count the total movie nodes in the tree and print the value.
7. Count longest path.
When the user selects this option, your program needs to determine the longest path from the root of the tree to the bottom of tree, not including empty nodes.
8. Quit the program.
When the user selects this option, your program should delete the tree using a post-order traversal.
MovieTree.h:
#ifndef MOVIETREE_H #define MOVIETREE_H #includestruct MovieNode{ int ranking; std::string title; int year; int quantity; bool isRed; 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; // Now that we are using nil these NULL's should be overwritten in addMovieNode. leftChild = NULL; rightChild = NULL; parent = NULL; isRed = true; } }; class MovieTree { public: MovieTree(); virtual ~MovieTree(); void printMovieInventory(); int countMovieNodes(); void deleteMovieNode(std::string title); void addMovieNode(int ranking, std::string title, int releaseYear, int quantity); void findMovie(std::string title); void rentMovie(std::string title); bool isValid(); int countLongestPath(); protected: private: void DeleteAll(MovieNode * node); //use this for the post-order traversal deletion of the tree void printMovieInventory(MovieNode * node); void rbAddFixup(MovieNode * node); // called after insert to fix tree void leftRotate(MovieNode * x); //rotate the tree left with x as the root of the rotation void rbDelete(MovieNode * z); //delete a node. Call this from deleteMovieNode, the actual delete functionality happens here. void rightRotate(MovieNode * x); //rotate the tree right with x as the root of the rotation void rbDeleteFixup(MovieNode * node); //called after delete to fix the tree void rbTransplant(MovieNode * u, MovieNode * v); //replace node u in tree with node v. Your solution doesn't necessarily need to use this method int rbValid(MovieNode * node); //check if the tree is valid, with node as the root of the tree int countMovieNodes(MovieNode *node); //number of unique titles in the tree int countLongestPath(MovieNode *node); //longest path from node to a leaf node in the tree MovieNode* searchMovieTree(MovieNode * node, std::string title); MovieNode *root; MovieNode *nil; }; #endif // MOVIETREE_H
expected ouput
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 the movies" << endl;
cout << "6. Count the longest path" << endl;
cout << "7. 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: "<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
