In this assignment, you will continue work with the movie data and store that da
ID: 3812920 • Letter: I
Question
In this assignment, you will continue work with the movie data and store that data in a red-black tree. A red-black tree is a self- balancing binary search tree. 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
Use the Assignment10Movies.txt file. The name of the input file needs to be handled as a command-line argument.
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. 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. 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.” 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. 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 it’s 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. 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. 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. Quit the program. When the user selects this option, your program should delete the tree using a post-order traversal.
The MovieTree.h file 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. In MovieTree.h, you’ll 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.
There is a file called rbValid.cpp that you can use to check that your tree- balancing 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
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;
Print the inventory
Count movies in the tree cout<<"Tree contains: "<<mt.countMovieNodes()<<" movies." << endl;
Longest path cout << "Longest Path: " << mt->countLongestPath() << endl;
Delete movie cout << "Enter title:" << endl;
Delete all nodes in the tree
Quit cout << "Goodbye!" << endl;
File I/O Error cout << "Could not open file ";
movieTree.h
helper function goes in movieTree.cpp, Assignment10.cpp calls the functions defined and created in movieTree
Explanation / Answer
you will continue work with the film knowledge and store that knowledge in a very red-black tree. A red-black tree may be a self- leveling binary search tree. you may have to be compelled to balance the tree every time a brand new film node is additional or deleted. for every of {the films|the films|the flicks} within the film nodes within the movie tree, the subsequent data is kept: - IMDB ranking - Title - Year discharged - amount available - Node color
Use the Assignment10Movies.txt file. The name of the input data has to be handled as a command-line argument.
Insert all the flicks within the tree. once the user starts the program they'll pass it the name of the document that contains all film data. Your program has to handle that command-line argument, open the file, and browse all film knowledge within the file. From this knowledge, build the red-black tree ordered by film title. As movies ar additional to the tree, the red-black tree-balancing rule ought to be applied. All data regarding the film ought to even be enclosed within the film node within the tree. Note: the info ought to be additional to the tree within the order it's browse in. notice a film. once the user selects this selection from the menu, they ought to be prompted for the name of the film. If the film is found within the tree, show the film data. If the film isn't found, your program ought to show, “Movie not found.” this selection is comparable to rent film, however, you're not change the number. Rent a film. once the user selects this selection from the menu, they ought to be prompted for the name of the film. If the film is found within the tree, your program ought to update the number available property of the film and show the new data regarding the film. once the number is zero, the film ought to be deleted from the tree. once a film is deleted, the tree ought to be rebalanced. If the film isn't found, your program ought to show, “Movie not found.” Print the complete inventory. once the user selects this selection from the menu, your program ought to show all film titles and also the amount on the market in sorted order by title. See the lecture notes on in-order tree traversal for a lot of data. Delete a film. once the user selects this selection, they ought to be prompted for the title of the film to delete. Your code ought to then search the tree for that film, delete it if it’s found, then perform any necessary tree leveling to revive the red-black tree properties. If the film isn't found within the search method, print “Movie not found.” and don't decide to delete. Count movies within the tree. once the user selects this selection, your program ought to do associate degree in-order tree traversal and count the entire film nodes within the tree and print the worth. Count longest path. once the user selects this selection, your program has to verify the longest path from the foundation of the tree to rock bottom of tree, not together with empty nodes. Quit the program. once the user selects this selection, your program ought to delete the tree employing a post-order traversal.
The MovieTree.h file includes the example for the red-black tree category for this assignment. you would like to implement the category practicality in a very corresponding MovieTree.cpp file and Assignment10.cpp file. In MovieTree.h, you’ll notice that there's currently a node referred to as *nil. you may have to be compelled to assign memory for *nil within the MovieTree creator then use it altogether cases wherever you antecedently used NULL. Use the cout statements in Appendix A to line the order of the menu choices.
There is a file referred to as rbValid.cpp that you simply will use to examine that your tree- leveling rule is correct. This code checks that the red-black properties are restored to the tree.The MovieTree.h file includes a definition for the helper perform as a non-public methodology within the MovieTree category. you may have to be compelled to copy the code out of the rbValid.cpp file and incorporate it into MovieTree.cpp. decision the perform when adding a film to the tree to verify that your tree leveling works. this is often for debugging functions. going away the code in your program after you withstand COG shouldn't have an effect on your results. There ar further BST properties that don't seem to be being checked with this code – it's not checking that the values within the tree ar valid for all left and right sub-trees of a node.
There is additionally an internet site that shows visually the red-black leveling process: https://www.cs.usfca.edu/~galles/visualization/RedBlack.html. Note: this web site uses the utmost price within the left sub-tree because the replacement for a deletion with 2 kids. Your code has to use the minimum price within the right sub-tree.
Appendix A
Display menu cout << "======Main Menu======" << endl; cout << "1. notice 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 film cout << "Enter title:" << endl;
Display found film data 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 film not found cout << "Movie not found." << endl;
Rent a film //If film is available
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 film not found in tree
cout << "Movie not found." << endl;
Print the inventory
//For all movies in tree
cout<<"Movie: "<<node->title<<" "<<node->quantity<<endl;
Count movies within the tree cout<<"Tree contains: "<<mt.countMovieNodes()<<" movies." << endl;
Longest path cout << "Longest Path: " << mt->countLongestPath() << endl;
Delete film cout << "Enter title:" << endl;
//If film not found in tree
cout << "Movie not found." << endl;
Delete all nodes within the tree
//For all movies in tree
cout<<"Deleting: "<<node->title<<endl;
Quit cout << "Goodbye!" << endl;
File I/O Error cout << "Could not open file ";
movieTree.h
#ifndef MOVIETREE_H
#define MOVIETREE_H
#include <string>
struct MovieNode;
MovieNode(int in_ranking, std::string in_title, int in_year, int in_quantity)
{
ranking = in_ranking;
title = in_title;
year = in_year;
amount = in_quantity;
// currently that we have a tendency to ar victimization cypher these NULL's ought to 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); // referred to as when insert to mend tree
void leftRotate(MovieNode * x); //rotate the tree left with x because the root of the rotation
void rbDelete(MovieNode * z); //delete a node. decision this from deleteMovieNode, the particular delete practicality happens here.
void rightRotate(MovieNode * x); //rotate the tree right with x because the root of the rotation
void rbDeleteFixup(MovieNode * node); //called when delete to mend the tree
void rbTransplant(MovieNode * u, MovieNode * v); //replace node u in tree with node v. Your resolution does not essentially have to be compelled to use this methodology
int rbValid(MovieNode * node); //check if the tree is valid, with node because the root of the tree
int countMovieNodes(MovieNode *node); //number of distinctive titles within the tree
int countLongestPath(MovieNode *node); //longest path from node to a leaf node within the tree
MovieNode* searchMovieTree(MovieNode * node, std::string title);
MovieNode *root;
MovieNode *nil;
};
#endif // MOVIETREE_H
helper perform goes in movieTree.cpp, Assignment10.cpp calls the functions outlined and created in movieTree
// Returns zero if the tree is invalid, otherwise returns the black node height.
int MovieTree::rbValid(MovieNode * node)
gonadotrophin = 0;
int rh = 0;
// If we have a tendency to ar at a zero node simply come one
if (node == nil)
return 1;
else
initial check for consecutive red links.
if (node->isRed)