write the definition for the following functions in c++ format #ifndef MOVIETREE
ID: 3605299 • Letter: W
Question
write the definition for the following functions in c++ format
#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP
#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);
void deleteMovie(std::string title);
void countMovies();
private:
MovieNode *search(MovieNode *node, std::string title);
MovieNode *root;
};
#endif // MOVIETREE_HPP
Explanation / Answer
#include "MovieTree.hpp" #include #include using namespace std; //called by the destructor void deleteHelper(MovieNode *delNode){ //post-order traversal if (delNode != NULL){ deleteHelper(delNode->leftChild); deleteHelper(delNode->rightChild); //cout rightChild; } return root; } else { return NULL; } } //helper function to search tree --> returns node pointer MovieNode* MovieTree::search(MovieNode *node, std::string title){ MovieNode* mySearchNode = node; //check if tree is empty if(mySearchNode != NULL){ //If root is the node you are looking for if (mySearchNode->title == title){ return mySearchNode; } //easier to search the tree this way instead of recursion else { while (mySearchNode != NULL){ //not going to lie, the comparison stuff is still confusing if (mySearchNode->title > title){ mySearchNode = mySearchNode->leftChild; } else if (mySearchNode->title rightChild; } //if the movie node is found, this will return it else if (mySearchNode->title == title){ return mySearchNode; } } } } else { cout rightChild = tempRightChild; tempRightChild->parent = node; //node->rightChild = addMovieHelper(node->rightChild, ranking, title, year, quantity); //newMovieNode->parent = node; //return node; } return node; } //create a new struct node for each movie and add it to the tree void MovieTree::addMovieNode(int ranking, std::string title, int releaseYear, int quantity){ root = addMovieHelper(root, ranking, title, releaseYear, quantity); } //print tree InOrder = left->root->right void MovieTree::printMovieInventory(){ //helper recursion function printHelper(root); } //function that uses search() to get the data for a node void MovieTree::findMovie(std::string title){ MovieNode* tempMovieNode = root; //helper function to search tree for the movie tempMovieNode = search(tempMovieNode, title); cout title; int minRightQuantity = minRightNode->quantity; //minRightNode has a child --> assign minRightNode's parent to the target's parent and the target's parent rightChild to target's rightChild //this closes the links around target and allows you delete target //the minimum value node underneath the target node has a right child ~~~ this means it is bigger than minRightNode if (minRightNode->rightChild != nullptr){ /*cout title = minRight; target->quantity = minRightQuantity; } } else { cout rightChild != NULL){ counterInt += countMovieHelper(counterNode->rightChild); } return counterInt; } else { return counterInt; } } //will call helper function void MovieTree::countMovies(){ //traverse the tree in any order and count the total of all the movies in tree int totalMovies = 0; totalMovies = countMovieHelper(root); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.