Question: C++ assignment, please help! In this assignment, you will continue working with the movie data from the BST assignment and store that data in a

C++ assignment, please help!

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. Implementation details The MovieTree.h file on Moodle includes the prototype for the red-black tree class for this assignment. You need to implement the class functionality in a corresponding MovieTree.cpp file and Assignment10.cpp file. To submit your work, zip all files together and submit them to COG. If you do not get your assignment working on COG, you will have the option of a grading interview. In MovieTree.h, youll notice that there is now a node called *nil. You will need to allocate memory for *nil in the MovieTree constructor and then use it in all cases where you previously used NULL. Use the cout statements in Appendix A to set the order of the menu options. Helper Function to check tree balancing There is a file on Moodle called rbValid.cpp that you can use to check that your treebalancing algorithm is correct. This code checks that the red-black properties have been restored to the tree. The MovieTree.h file includes a definition for the helper function as a private method in the MovieTree class. You will need to copy the code out of the rbValid.cpp file and incorporate it into MovieTree.cpp. Call the function after adding a movie to the tree to verify that your tree balancing works. This is for debugging purposes. Leaving the code in your program when you submit to COG should not affect your results. There are additional BST properties that are not being checked with this code it is not checking that the values in the tree are valid for all left and right sub-trees of a node. There is also a website that shows visually the red-black balancing process: https://www.cs.usfca.edu/~galles/visualization/RedBlack.html. Note: this website uses the maximum value in the left sub-tree as the replacement for a deletion with two children. Your code needs to use the minimum value in the right sub-tree. Appendix A cout statements that COG expects 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: "<title<<" "<quantity<Count movies in the tree cout<<"Tree contains: "<Longest path cout << "Longest Path: " << mt->countLongestPath() << endl; Delete movie cout << "Enter title:" << endl; //If movie not found in tree cout << "Movie not found." << endl; Delete all nodes in the tree //For all movies in tree cout<<"Deleting: "<title<Quit cout << "Goodbye!" << endl; File I/O Error cout << "Could not open file ";

Header File(can't be change) MovieTree.h

#ifndef MOVIETREE_H #define MOVIETREE_H #include  struct 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 

Helper function for checking that a red-black tree is correctly balanced. Add this to MovieTree.cpp file

rbValid.cpp

// Returns 0 if the tree is invalid, otherwise returns the black node height. int MovieTree::rbValid(MovieNode * node) { int lh = 0; int rh = 0; // If we are at a nil node just return 1 if (node == nil) return 1; else { // First check for consecutive red links. if (node->isRed) { if(node->leftChild->isRed || node->rightChild->isRed) { cout << "This tree contains a red violation" << endl; return 0; } } // Check for valid binary search tree. if ((node->leftChild != nil && node->leftChild->title.compare(node->title) > 0) || (node->rightChild != nil && node->rightChild->title.compare(node->title) < 0)) { cout << "This tree contains a binary tree violation" << endl; return 0; } // Deteremine the height of let and right children. lh = rbValid(node->leftChild); rh = rbValid(node->rightChild); // black height mismatch if (lh != 0 && rh != 0 && lh != rh) { cout << "This tree contains a black height violation" << endl; return 0; } // If neither height is zero, incrament if it if black. if (lh != 0 && rh != 0) { if (node->isRed) return lh; else return lh+1; } else return 0; } } 

Text file:

1,Shawshank Redemption,1994,45 2,The Godfather,1972,34 3,The Godfather: Part II,1974,12 4,The Dark Knight,2008,90 5,Pulp Fiction,1994,34 6,12 Angry Men,1957,56 7,Schindler's List,1993,10 8,The Good the Bad and the Ugly,1966,2 9,The Lord of the Rings: The Return of the King,2003,4 10,Fight Club,1999,6 11,The Lord of the Rings: The Fellowship of the Ring,2001,20 12,Star Wars: Episode V - The Empire Strikes Back,1980,12 13,Forrest Gump,1994,1 14,Inception,2010,10 15,One Flew Over the Cuckoo's Nest,1975,10 16,The Lord of the Rings: The Two Towers,2002,5 17,Goodfellas,1990,5 18,The Matrix,1999,5 19,Star Wars: Episode IV - A New Hope,1977,6 20,Seven Samurai,1954,7 21,Interstellar,2014,8 22,City of God,2002,9 23,Se7en,1995,11 24,The Usual Suspects,1995,2 25,The Silence of the Lambs,1991,19 26,It's a Wonderful Life,1946,21 27,Once Upon a Time in the West,1968,12 28,Leon: The Professional,1994,31 29,Life Is Beautiful,1997,3 30,Casablanca,1942,5 31,Raiders of the Lost Ark,1981,6 32,American History X,1998,1 33,Saving Private Ryan,1998,3 34,City Lights,1931,10 35,Spirited Away,2001,10 36,Psycho,1960,6 37,Rear Window,1954,7 38,Whiplash,2014,8 39,The Untouchables,2011,3 40,Modern Times,1936,9 41,Terminator 2: Judgment Day,1991,10 42,Memento,2000,9 43,The Green Mile,1999,8 44,The Pianist,2002,7 45,The Departed,2006,7 46,Apocalypse Now,1979,5 47,Sunset Blvd.,1950,5 48,Gladiator,2000,5 49,Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb,1964,5 50,Back to the Future,1985,5 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!