C++ Binary Tree help I am trying to build a program which will help an online mo
ID: 3599748 • Letter: C
Question
C++ Binary Tree help
I am trying to build a program which will help an online movie rental site store their movies in a binary search tree. I am having trouble with the rental function and keep getting this error: THE FIRST PICTURE IS THE EXPECTED OUTPUT THE SECOND IS WHAT MY PROGRAM IS OUTPUTTING
Here is my program we are being tested with a main the teacher wrote:
#include "MovieTree.hpp"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
MovieTree::MovieTree(){
root = NULL;
}
MovieTree::~MovieTree(){
delete root;
}
MovieNode* MovieTree::search(MovieNode *node, string title){
node = root;
if (root == NULL){
cout << "Movie not found." << endl;
}
//node = root;
while (node != NULL){
//int tit_comp = node ->title.compare(title);
if (node && node -> title == title){
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << node -> ranking << endl;
cout << "Title:" << node -> title << endl;
cout << "Year:" << node -> year << endl;
cout << "Quantity:" << node -> quantity << endl;
break;
}
else
{
//cout << "Movie not found." << endl;
break;
}
}
return node;
}
void MovieTree::findMovie(string title){
MovieNode *foundMovie = search(root, title);
if (title < foundMovie -> title)
{
if (foundMovie -> leftChild == NULL)
{
//cout << "Movie not found." << endl;
return;
}
else {
foundMovie = foundMovie->leftChild;
}
}
else
{
if (foundMovie -> rightChild == NULL)
{
//cout << "Movie not found."<< endl;
return;
}
else
{
foundMovie = foundMovie->rightChild;
}
}
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;
}
void printMovieHelp(MovieNode *node)
{
if (node == NULL)
{
return;
}
else
{
printMovieHelp(node -> leftChild);
cout << "Movie: " << node -> title << " " << node -> quantity <<endl;
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 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;
addNode -> parent = NULL;
}
else
{
MovieNode *foundMovie = searchMovieTree(root, title);
if (title < foundMovie -> title)
{
if (foundMovie -> leftChild == NULL)
{
foundMovie -> leftChild = addNode;
addNode -> parent = foundMovie;
}
}
else
{
if (foundMovie -> rightChild == NULL)
{
foundMovie -> rightChild = addNode;
addNode -> parent = foundMovie;
}
}
}
}
void MovieTree::rentMovie(string title){
MovieNode *found_Movie = root;
if (root == NULL){
cout << "Movie not found." << endl;
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 < 0){
found_Movie = found_Movie -> rightChild;
}
else {
if (found_Movie -> quantity > 0)
{
found_Movie -> quantity--;
cout << "Movie has been rented." << endl;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << found_Movie -> ranking << endl;
cout << "Title:" << found_Movie -> title << endl;
cout << "Year:" << found_Movie -> year << endl;
cout << "Quantity:" << found_Movie -> quantity << endl;
break;
}
else if (found_Movie -> quantity == 0){
cout << "Movie out of stock." << endl;
break;
}
}
}
if (found_Movie && found_Movie -> title == title){
//cout << "found the movie" << endl;
return;
}
else
{
cout << "Movie not found." << endl;
}
}
Will give thumbs up with corrections and explanation
[GRADER] Now, I will try to rent each movie Movie Info: Ranking:1 Title:Man on Wire Year: 2008 Quantity:10 Movie Info: Ranking:2 Title: Hands on a Hardbody Year: 1997 Quantity: 30 Movie Info: Ranking:3 Title:Dogtown and Z-Boys Year: 2001 Quantity:3 Movie Info: Ranking:4 Title: Dancin'Outlaw Year: 1991 Quantity:7Explanation / Answer
#include "MovieTree.hpp"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
MovieTree::MovieTree(){
root = NULL;
}
MovieTree::~MovieTree(){
delete root;
}
MovieNode* MovieTree::search(MovieNode *node, string title){
node = root;
if (root == NULL){
cout << "Movie not found." << endl;
}
//node = root;
while (node != NULL){
//int tit_comp = node ->title.compare(title);
if (node && node -> title == title){
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << node -> ranking << endl;
cout << "Title:" << node -> title << endl;
cout << "Year:" << node -> year << endl;
cout << "Quantity:" << node -> quantity << endl;
break;
}
else
{
//cout << "Movie not found." << endl;
break;
}
}
return node;
}
void MovieTree::findMovie(string title){
MovieNode *foundMovie = search(root, title);
if (title < foundMovie -> title)
{
if (foundMovie -> leftChild == NULL)
{
//cout << "Movie not found." << endl;
return;
}
else {
foundMovie = foundMovie->leftChild;
}
}
else
{
if (foundMovie -> rightChild == NULL)
{
//cout << "Movie not found."<< endl;
return;
}
else
{
foundMovie = foundMovie->rightChild;
}
}
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;
}
void printMovieHelp(MovieNode *node)
{
if (node == NULL)
{
return;
}
else
{
printMovieHelp(node -> leftChild);
cout << "Movie: " << node -> title << " " << node -> quantity <<endl;
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 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;
addNode -> parent = NULL;
}
else
{
MovieNode *foundMovie = searchMovieTree(root, title);
if (title < foundMovie -> title)
{
if (foundMovie -> leftChild == NULL)
{
foundMovie -> leftChild = addNode;
addNode -> parent = foundMovie;
}
}
else
{
if (foundMovie -> rightChild == NULL)
{
foundMovie -> rightChild = addNode;
addNode -> parent = foundMovie;
}
}
}
}
void MovieTree::rentMovie(string title){
MovieNode *found_Movie = root;
if (root == NULL){
cout << "Movie not found." << endl;
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 < 0){
found_Movie = found_Movie -> rightChild;
}
else {
if (found_Movie -> quantity > 0)
{
found_Movie -> quantity--;
cout << "Movie has been rented." << endl;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << found_Movie -> ranking << endl;
cout << "Title:" << found_Movie -> title << endl;
cout << "Year:" << found_Movie -> year << endl;
cout << "Quantity:" << found_Movie -> quantity << endl;
break;
}
else if (found_Movie -> quantity == 0){
cout << "Movie out of stock." << endl;
break;
}
}
}
if (found_Movie && found_Movie -> title == title){
//cout << "found the movie" << endl;
return;
}
else
{
cout << "Movie not found." << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.