Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with my c++ code. Program Specifications Add the following functionality to your Assignment 5 Binary Search Tree implementation: 1. Delete a movie.

I need help with my c++ code.

Program Specifications Add the following functionality to your Assignment 5 Binary Search Tree implementation:

1. 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 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. A movie node should also be deleted when the quantity goes to 0 for any movie. Be sure to test your program for movie nodes with 0 children, 1 child, or 2 children!

2. Count movies in the tree. When the user selects this option, your program should traverse the tree in any order, count the total number of movie nodes in the tree, and print the count.

3. Delete the tree in the destructor using a postorder traversal. When the user selects quit, the destructor for the MovieTree class should be called. In the destructor, all of the nodes in the tree should be deleted. You need to use a postorder tree traversal for the delete or you will get segmentation fault errors. You may implement a helper function. Menu: After the tree has been built, display a menu with the following options: (includes functionality from Assignment 5).

1. Find a movie. When the user selects this option from the menu, they should be prompted for the name of the movie. Your program should then search the tree and display all information for that movie. If the movie is not found in the tree, your program should display, Movie not found. The findMovie method included in the MovieTree.hpp header file is a void type. All movie information should be displayed in the findMovie method. This should already be implemented from Assignment 5.

2. 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. If the movie is not found, your program should display, Movie not found. Just like findMovie, rentMovie is also a void type. Information about the movie rented should be displayed in the rentMovie method. If the quantity of a movie reaches 0, delete the movie after displaying the updated movie information. This should be mostly implemented from Assignment 5.

3. 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 and your textbook for more information. This should already be implemented from Assignment 5.

4. Delete 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 delete the movie node from the tree. If the movie is not found, your program should display, Movie not found.

5. Count movies in the tree. When the user selects this option, your program should traverse the tree, counting the total number of movie nodes in the tree, and print the count.

6. Quit the program. When the user selects this option, your program should exit after freeing all dynamically allocated memory.

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 movies" << endl;

cout << "6. 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<

Delete a movie

cout << "Enter title:" << endl;

//If movie not found in tree

cout << "Movie not found." << endl;

Count movies

cout << "Count = " << count << endl;

Quit

cout << "Goodbye!" << endl;

MovieTree.hpp Header File:

#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(){};

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;

parent = leftChild = rightChild = nullptr;

}

};

class MovieTree

{

public:

MovieTree();

~MovieTree();

void printMovieInventory();

void addMovieNode(int ranking, std::string title,

int releaseYear, int quantity);

void findMovie(std::string title);

void rentMovie(std::string title);

void deleteMovie(std::string title);

void countMovies();

private:

MovieNode *search(MovieNode *node, std::string title);

MovieNode *root;

};

#endif // MOVIETREE_HPP

I need help with my own code, can you help me check where is wrong?

here is my code

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

using namespace std;

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

MovieTree::~MovieTree() { } void print(MovieNode *node) { if(!node) { return; } print(node->leftChild); cout << "Movie: " << node->title << " " << node->quantity << endl; print(node->rightChild); }

void MovieTree::printMovieInventory() { print(root); }

void MovieTree::addMovieNode(int ranking, string title, int releaseYear, int quantity) { MovieNode *movie = new MovieNode(ranking, title, releaseYear, quantity); if (root == NULL) { root = movie; }

else { MovieNode *current = root; while (true) { if (movie->title <= current->title) { if (current->leftChild == NULL) { current->leftChild = movie; movie->parent = current; return; } else { current = current->leftChild; } } else { if (current->rightChild == NULL) { current->rightChild = movie; movie->parent = current; return; } else current = current->rightChild; } } } }

void MovieTree::findMovie(string title) { MovieNode* tmp = search(root, title);

if (!tmp) { cout << "Movie not found." << endl; } else { cout << "Movie Info:" << endl; cout << "===========" << endl; cout << "Ranking:" << tmp->ranking << endl; cout << "Title:" << tmp->title << endl; cout << "Year:" << tmp->year << endl; cout << "Quantity:" << tmp->quantity << endl; }

}

void MovieTree::rentMovie(string title) { MovieNode *movie = search(root, title); if (movie != NULL) { movie->quantity--;

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

if (movie->quantity == 0) { deleteMovie(movie->title); } } else { cout << "Movie not found." << endl; } }

MovieNode *search(MovieNode *node,string title)

{

if(node==NULL)

{

return NULL;

}

if(title.compare(node->title)==0)

{

return node;

}

MovieNode *found=search(node->leftChild,title);

if(found!=NULL)

return found;

found=search(node->rightChild,title);

return found;

} void countm(MovieNode *node, int *c) { if(node==NULL) return;

countm(node->leftChild,c); *c=*c+1; countm(node->rightChild,c); } void MovieTree::countMovies() { int count=0; countm(root,&count); cout << "Count = " << count << endl; }

MovieNode* MovieTree::search(MovieNode *node, std::string title) { MovieNode *tmp = node; while (tmp != NULL) { if (!tmp->title.compare(title)) { return tmp; } if (tmp->rightChild == NULL&&tmp->leftChild == NULL) { return NULL; } else if (tmp->title.compare(title)>0) { tmp = tmp->leftChild; } else { tmp = tmp->rightChild; } } return tmp; }

MovieNode *minimum(MovieNode *ptr)

{

if (ptr != NULL)

{

while (ptr->leftChild != NULL)

ptr = ptr->leftChild;

return ptr;

}

else

return NULL;

}

MovieNode *successor(MovieNode *temp,string title)

{

MovieNode *parent;

MovieNode *ptr = search(temp, title);

if (ptr->rightChild != NULL)

return minimum(ptr->rightChild);

else

{

parent = ptr->parent;

while (parent != NULL)

{

if (parent->leftChild == ptr)

return parent;

else

{

ptr = parent;

parent = ptr->parent;

continue;

}

}

return NULL;

}

} void MovieTree::deleteMovie(string title) { MovieNode *x,*y,*z = search(root, title); if (z == NULL) { cout <<"Movie not found. "; return;

}

if (z->leftChild == NULL || z->rightChild == NULL)

y = z;

else

y = successor(root, z->title);

if (y->leftChild != NULL)

x = y->leftChild; //x is used as children of y

else

x = y->rightChild;

if (x != NULL)

x->parent = y->parent;

if (y->parent == NULL)

root = x;

else

if (y == y->parent->leftChild)

y->parent->leftChild = x;

else

y->parent->rightChild = x;

if (y != z)

{

z->title = y->title;

z->ranking = y->ranking;

z->year = y->year;

z->quantity = y->quantity;

}

free(y);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Database And Knowledge Base Systems

Authors: S Krishna

1st Edition

9810206208, 978-9810206208

More Books

Students also viewed these Databases questions

Question

2. Why does unpredictability matter?

Answered: 1 week ago