Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Binary Search Tree Here is the .hpp file: #ifndef MOVIETREE_HPP #define MOVIETREE_HPP #include struct MovieNode { int ranking; std::string title; int year; int quantity;

C++ Binary Search Tree

image text in transcribed Here is the .hpp 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

Here is the code I have so far:

#include "MovieTree.hpp"

#include

#include

#include

using namespace std;

MovieTree::MovieTree(){

root = NULL;

}

MovieTree::~MovieTree(){

delete root;

}

MovieNode* MovieTree::search(MovieNode *node, string title){

node = root;

if (root == NULL){

cout

}

/ode = root;

while (node != NULL){

//int tit_comp = node ->title.compare(title);

if (node && node -> title == title){

cout

cout

cout ranking

cout title

cout year

cout quantity

break;

}

else

{

//cout

break;

}

}

return node;

}

void MovieTree::findMovie(string title){

MovieNode *foundMovie = search(root, title);

if (title title)

{

if (foundMovie -> leftChild == NULL)

{

//cout

return;

}

else {

foundMovie = foundMovie->leftChild;

}

}

else

{

if (foundMovie -> rightChild == NULL)

{

//cout

return;

}

else

{

foundMovie = foundMovie->rightChild;

}

}

cout

cout

cout ranking

cout title

cout year

cout quantity

}

void printMovieHelp(MovieNode *node)

{

if (node == NULL)

{

return;

}

else

{

printMovieHelp(node -> leftChild);

cout title quantity

printMovieHelp(node -> rightChild);

}

}

void MovieTree::printMovieInventory()

{

if (root == NULL)

{

return;

}

else

{

printMovieHelp(root);

}

}

MovieNode *searchMovieTree(MovieNode *node, string title){

if (node -> title == title)

{

return node -> parent;

}

else if (node ->title > title)

{

if (node -> leftChild == NULL)

{

return node;

}

return searchMovieTree(node -> leftChild, title);

}

else {

if (node -> rightChild == NULL)

{

return node;

}

return searchMovieTree(node -> rightChild, title);

}

}

void addHelper( MovieNode* root, MovieNode* addNode);

void MovieTree::addMovieNode(int ranking, std::string title, int year, int quantity){

MovieNode *addNode = new MovieNode;

addNode -> ranking = ranking;

addNode -> title = title;

addNode -> year = year;

addNode -> quantity = quantity;

addNode -> leftChild = NULL;

addNode -> rightChild = NULL;

if ( root == NULL )

root = addNode;

else

addHelper( root, addNode );

//return root;

}

void addHelper( MovieNode* root, MovieNode* addNode){

//MovieNode *foundMovie = searchMovieTree(root, title);

if( addNode -> title title ){

if( root -> leftChild != NULL )

addHelper( root -> leftChild, addNode );

else

root -> leftChild = addNode;

}

else {

if( root -> rightChild != NULL )

addHelper( root -> rightChild, addNode );

else

root -> rightChild = addNode;

}

}

void MovieTree::rentMovie(string title){

MovieNode *found_Movie = root;

if (root == NULL){

cout

return;

}

found_Movie = root;

while (found_Movie != NULL){

int tit_comp = found_Movie -> title.compare(title);

if (tit_comp > 0){

found_Movie = found_Movie -> leftChild;

}

else if (tit_comp

found_Movie = found_Movie -> rightChild;

}

else {

//found_Movie = root;

if (found_Movie -> quantity > 0)

{

found_Movie -> quantity--;

cout

cout

cout

cout ranking

cout title

cout year

cout quantity

//break;

return;

}

else if (found_Movie -> quantity == 0){

cout

return;

//break;

}

}

}

if (found_Movie && found_Movie -> title == title){

return;

}

else

{

cout

return;

}

}

PLEASE ADD THE FOLLOWING FUNCTIONS:

void deleteMovie(std::string title); void countMovies();

YOU CANNOT CHANGE THE PARAMETERS. You may add helper functions if needed however you may not edit the .hpp file. Will give thumbs up for correct answer with explanation

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, or2 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

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

Database And Expert Systems Applications 15th International Conference Dexa 2004 Zaragoza Spain August 30 September 3 2004 Proceedings Lncs 3180

Authors: Fernando Galindo ,Makoto Takizawa ,Roland Traunmuller

2004th Edition

3540229361, 978-3540229360

More Books

Students also viewed these Databases questions