Question: I am getting a segment fault! This is what I am given: #ifndef MOVIETREE_HPP #define MOVIETREE_HPP #include struct MovieNode { int ranking; std::string title; int

I am getting a segment fault!

This is what I am given:

#ifndef MOVIETREE_HPP #define MOVIETREE_HPP #include struct MovieNode { int ranking; std::string title; int year; int quantity;

MovieNode *parent; MovieNode *leftChild; MovieNode *rightChild;

MovieNode(){ parent = leftChild = rightChild = nullptr; }

MovieNode(int r, std::string t, int y, int q) { ranking = r; title = t; year = y; quantity = q; parent = leftChild = rightChild = nullptr; } };

class MovieTree { public: MovieTree(); ~MovieTree(); void printMovieInventory(); void addMovieNode(int ranking, std::string title, int year, int quantity); void findMovie(std::string title); void rentMovie(std::string title); void deleteMovie(std::string title); void countMovies(); private: MovieNode *search(std::string title); MovieNode *root; }; #endif

This is the code I have and the only code I am allowed to change:

#include #include #include #include "MovieTree.hpp"

MovieTree::MovieTree() { root = NULL; }

MovieTree::~MovieTree() { //DeleteAll(root); //exit(0); }

void MovieTree::printMovieInventory() { if(root != NULL) { printMovieInventory(); } }

void printMovieInventory(MovieNode *node) { if(node->leftChild != NULL) { printMovieInventory(node->leftChild); } cout<<"Movie: "<title<<" "<quantity<rightChild != NULL) { printMovieInventory(node->rightChild); } }

void MovieTree::addMovieNode(int ranking, std::string title, int year, int quantity) { MovieNode *addNode = new MovieNode(ranking, title, year, quantity); addNode -> parent = NULL; addNode -> leftChild = NULL; addNode -> rightChild = NULL; if(root == NULL) { root = addNode; } else { MovieNode *temp = root; while(temp != NULL) { if(title.compare(temp -> title) < 0) { if(temp -> leftChild != NULL) { temp = temp -> leftChild; } else { temp -> leftChild = addNode; addNode -> parent = temp; temp = NULL; } } else { if(temp -> rightChild != NULL) { temp = temp -> rightChild; } else { temp -> rightChild = addNode; addNode -> parent = temp; temp = NULL; } } } } }

void MovieTree::findMovie(std::string title) { MovieNode *findMovie = search(title);

if(findMovie == NULL) { std::cout << "Movie not found." << std::endl; return; }

std::cout << "Movie Info:" << std::endl; std::cout << "===========" << std::endl; std::cout << "Ranking:" << findMovie -> ranking << std::endl; std::cout << "Title:" << findMovie -> title << std::endl; std::cout << "Year:" << findMovie -> year << std::endl; std::cout << "Quantity:" << findMovie -> quantity << std::endl; }

void MovieTree::rentMovie(std::string title) { MovieNode *rentMovie = search(title);

if(rentMovie == NULL) { std::cout << "Movie not found." << std::endl; return; }

if(rentMovie -> quantity <= 0) { std::cout << "Movie out of stock." << std::endl; return; }

rentMovie -> quantity = rentMovie -> quantity - 1;

std::cout << "Movie has been rented." << std::endl; std::cout << "Movie Info:" << std::endl; std::cout << "===========" << std::endl; std::cout << "Ranking:" << rentMovie -> ranking << std::endl; std::cout << "Title:" << rentMovie -> title << std::endl; std::cout << "Year:" << rentMovie -> year << std::endl; std::cout << "Quantity:" << rentMovie -> quantity << std::endl; }

void MovieTree::deleteMovie(std::string title) { MovieNode * node = root; while(node != NULL) { if(node -> title == title) { break; } else if(title < node -> title ) { node = node -> leftChild; } else { node = node -> rightChild; } } if(node == NULL) { std::cout << "Movie not found. " << std::endl; return; } if(node -> leftChild == NULL && node -> rightChild == NULL) { if(node == root) { root = NULL; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = NULL; } else { node -> parent -> rightChild = NULL; } }

delete node; } else if(node -> leftChild == NULL && node -> rightChild != NULL) { if(node == root) { root = node -> rightChild; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = node -> rightChild; } else { node -> parent -> rightChild = node -> rightChild; } } node -> rightChild -> parent = node -> parent; delete node; } else if(node -> leftChild != NULL && node -> rightChild == NULL) { if(node == root) { root = node -> leftChild; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = node -> leftChild; } else { node -> parent -> rightChild = node -> leftChild; } } node -> leftChild -> parent = node -> parent; delete node; } else { MovieNode* successor = node -> rightChild; while(successor -> leftChild != NULL) successor = successor -> leftChild; MovieNode copy = *successor; deleteMovie(successor -> title); node -> ranking = copy.ranking; node -> title = copy.title; node -> year = copy.year; node -> quantity = copy.quantity; } }

int postOrder(MovieNode *node) { if(node != NULL) { return postOrder(node -> leftChild) + postOrder(node -> rightChild) + 1; } }

void MovieTree::countMovies() { std::cout << "Count = " << postOrder(root) << std::endl; }

MovieNode* MovieTree::search(std::string title) { MovieNode * node = new MovieNode; node = root; while(node != NULL) { if(title.compare(node -> title) < 0) { node = node -> leftChild; } else if(title.compare(node -> title) > 0) { node = node -> rightChild; } else if(title.compare(node -> title) == 0) { break; } }

return node; }

Test Expected Got
Adding two items to the tree and renting a movie, printing the count of the movies between operations.
[GRADER] Initially, the library is empty. [GRADER] The result of `movies->countMovies()` is: Count = 0 [GRADER] I will add 5 copies of "Elephant" to the collection. [GRADER] Now, the result of `movies->countMovies()` is: Count = 1 [GRADER] Now, the result of `movies->printMovieInventory()` is: Movie: Elephant 5 [GRADER] Now, I will try to search for "Elephant": Movie Info: =========== Ranking:98 Title:Elephant Year:2006 Quantity:5 [GRADER] After renting the movie, there should be four copies remaining. Movie has been rented. Movie Info: =========== Ranking:98 Title:Elephant Year:2006 Quantity:4 [GRADER] If I add 2 copies of "Pacific Rim" to the library, it should be second alphabetically. [GRADER] Now, the result of `movies->countMovies()` is: Count = 2 [GRADER] Now the contents of the library are: Movie: Elephant 4 Movie: Pacific Rim 2 [GRADER] All done! [GRADER] No leaked memory (Good).
[GRADER] Initially, the library is empty. [GRADER] The result of `movies->countMovies()` is: Count = 0 [GRADER] I will add 5 copies of "Elephant" to the collection. [GRADER] Now, the result of `movies->countMovies()` is: Count = 1 [GRADER] Now, the result of `movies->printMovieInventory()` is: ***Error*** Segmentation fault

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!