Question: ERROR: in search: 'root' was not declared in this scope node = root; MovieTree(); ~MovieTree(); void printMovieInventory(); void addMovieNode(int ranking, std::string title, int releaseYear, int
ERROR: in search: 'root' was not declared in this scope
node = root;
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();
MovieNode *search(std::string title);
Given:
#ifndef MOVIETREE_HPP #define MOVIETREE_HPP #include
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
What I have:
#include
#include "MovieTree.hpp"
using namespace std;
MovieTree::MovieTree() { //root = NULL; }
MovieTree::~MovieTree() { //DeleteAll(root); //exit(0); }
int iterateTree(MovieNode * node) { if(node != NULL) { iterateTree(node -> leftChild); cout << "Movie: " << node->title << " " << node->quantity << endl; iterateTree(node -> rightChild); } } void MovieTree::printMovieInventory() { if (root != NULL) { printMovieInventory(); } };
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(string title) { MovieNode *findMovie = search(title);
if(findMovie == NULL) { cout << "Movie not found." << endl; return; }
cout << "Movie Info:" << endl; cout << "===========" << endl; cout << "Ranking:" << findMovie -> ranking << endl; cout << "Title:" << findMovie -> title << endl; cout << "Year:" << findMovie -> year << endl; cout << "Quantity:" << findMovie -> quantity << endl; }
void MovieTree::rentMovie(string title) { MovieNode *rentMovie = search(title);
if(rentMovie == NULL) { cout << "Movie not found." << endl; return; }
if(rentMovie -> quantity <= 0) { cout << "Movie out of stock." << endl; return; }
rentMovie -> quantity = rentMovie -> quantity - 1;
cout << "Movie has been rented." << endl; cout << "Movie Info:" << endl; cout << "===========" << endl; cout << "Ranking:" << rentMovie -> ranking << endl; cout << "Title:" << rentMovie -> title << endl; cout << "Year:" << rentMovie -> year << endl; cout << "Quantity:" << rentMovie -> quantity << endl; }
void MovieTree::deleteMovie(string title) { MovieNode * current = root; while(current != nullptr) { if(current -> title == title) { break; } else if(title < current -> title ) { current = current -> leftChild; } else { current = current -> rightChild; } } if(current == nullptr) { cout << "Movie not found. " << endl; return; } if(current -> leftChild == NULL && current -> rightChild == NULL) { if(current == root) { root = NULL; } else { if(current -> parent -> leftChild == current) { current -> parent -> leftChild = NULL; } else { current -> parent -> rightChild = NULL; } }
delete current; } else if(current -> leftChild == NULL && current -> rightChild != NULL) { if(current == root) { root = current -> rightChild; } else { if(current -> parent -> leftChild == current) { current -> parent -> leftChild = current -> rightChild; } else { current -> parent -> rightChild = current -> rightChild; } } current -> rightChild -> parent = current -> parent; delete current; } else if(current -> leftChild != NULL && current -> rightChild == NULL) { if(current == root) { root = current -> leftChild; } else { if(current -> parent -> leftChild == current) { current -> parent -> leftChild = current -> leftChild; } else { current -> parent -> rightChild = current -> leftChild; } } current -> leftChild -> parent = current -> parent; delete current; } else { MovieNode* successor = current -> rightChild; while(successor -> leftChild != nullptr) successor = successor -> leftChild; MovieNode copy = *successor; deleteMovie(successor -> title); current -> ranking = copy.ranking; current -> title = copy.title; current -> year = copy.year; current -> quantity = copy.quantity; } }
int postOrder(MovieNode *node) { if(node != NULL) { return postOrder(node -> leftChild) + postOrder(node -> rightChild) + 1; } }
void MovieTree::countMovies() { cout << "Count = " << postOrder(root) << endl; }
MovieNode* 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; }
error message for search:
'root' was not declared in this scope node = root;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
