write the definition for the following functions in c++ format #ifndef MOVIETREE
ID: 3598329 • Letter: W
Question
write the definition for the following functions in c++ format
#ifndef MOVIETREE_H
#define MOVIETREE_H
#include <string>
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);
private:
MovieNode *search(MovieNode *node, std::string title);
void Printhelp(MovieNode *printTree);
MovieNode *root;
};
#endif // MOVIETREE_H
Explanation / Answer
Given below is the completed code for MovieTree.cpp file. For the destructor, we need a helper function which will delete the nodes recursively in post order . So added the cleanupHelp() to MovieTree.h file.
In case of any issues, please let me know through comments, I will help. If the answer helped, please do rate it . Thank you.
#ifndef MOVIETREE_H
#define MOVIETREE_H
#include <string>
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);
private:
MovieNode *search(MovieNode *node, std::string title);
void Printhelp(MovieNode *printTree);
void cleanupHelp(MovieNode *node);
MovieNode *root;
};
#endif // MOVIETREE_H
MovieTree.cpp
#include "MovieTree.h"
#include <iostream>
using namespace std;
MovieTree::MovieTree()
{
root = nullptr;
}
MovieTree::~MovieTree()
{
cleanupHelp(root);
}
void MovieTree::cleanupHelp(MovieNode* node)
{
if(node == nullptr) return;
cleanupHelp(node->leftChild);
cleanupHelp(node->rightChild);
delete node;
root = nullptr;
}
void MovieTree::printMovieInventory()
{
Printhelp(root);
}
void MovieTree::addMovieNode(int ranking, std::string title, int releaseYear, int quantity)
{
MovieNode* m = new MovieNode(ranking, title, releaseYear, quantity);
if(root == nullptr)
root = m;
else
{
MovieNode* curr = root;
while(curr != nullptr)
{
if(title < curr->title)
{
if(curr->leftChild == nullptr)
{
m->parent = curr;
curr->leftChild = m;
return;
}
else
curr = curr->leftChild;
}
else
{
if(curr->rightChild == nullptr)
{
m->parent = curr;
curr->rightChild = m;
return;
}
else
curr = curr->rightChild;
}
}
}
}
void MovieTree::findMovie(std::string title)
{
MovieNode* m = search(root, title);
if(m == nullptr)
cout << "Movie " << title << " NOT found" << endl;
else
{
cout << "Movie found. Rank: " << m->ranking << ", Title: " << m->title ;
cout << ", Release Year: " << m->year << ", Quantity: " << m->quantity << endl;
}
}
void MovieTree::rentMovie(std::string title)
{
MovieNode* m = search(root, title);
if(m == nullptr)
cout << "Movie " << title << " NOT found" << endl;
else
{
if(m->quantity == 0)
cout << "Movie can not be rented as quantitye is 0 " << endl;
else
{
m->quantity--;
cout << "1 copy of movie " << m->title << " rented " << endl;
}
}
}
MovieNode* MovieTree::search(MovieNode *node, std::string title)
{
MovieNode* curr = root;
while(curr != nullptr)
{
if(title < curr->title)
curr = curr->leftChild;
else if(title > curr->title)
curr = curr->rightChild;
else
break;
}
return curr;
}
void MovieTree::Printhelp(MovieNode *printTree)
{
if(printTree == nullptr)
return;
Printhelp(printTree->leftChild);
cout << printTree->title;
Printhelp(printTree->rightChild);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.