Question
I cannot figure out these memory leaks. I tried free'ing them and deleting them. Any help? #include #include #include #include MovieTree.hpp using namespace std; MovieTree::MovieTree()
I cannot figure out these memory leaks. I tried free'ing them and deleting them. Any help?
#include
#include
#include
#include "MovieTree.hpp"
using namespace std;
MovieTree::MovieTree()
{
root = NULL;
}
MovieTree::~MovieTree()
{
//DeleteAll(root);
//exit(0);
}
void inventory(MovieNode *node)
{
if(node->leftChild != NULL)
{
inventory(node->leftChild);
}
cout<<"Movie: "< if(node->rightChild != NULL) { inventory(node->rightChild); } } void MovieTree::printMovieInventory() { if(root != NULL) { inventory(root); } } 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(std::string title) { MovieNode *findMovie = search(title); if(findMovie == NULL) { std::cout << "Movie not found." << std::endl; return; } std::cout << "Movie Info:" << std::endl; std::cout << "===========" << std::endl; std::cout << "Ranking:" << findMovie -> ranking << std::endl; std::cout << "Title:" << findMovie -> title << std::endl; std::cout << "Year:" << findMovie -> year << std::endl; std::cout << "Quantity:" << findMovie -> quantity << std::endl; } void MovieTree::rentMovie(std::string title) { MovieNode *rentMovie = search(title); if(rentMovie == NULL) { std::cout << "Movie not found." << std::endl; return; } if(rentMovie -> quantity <= 0) { std::cout << "Movie out of stock." << std::endl; return; } rentMovie -> quantity = rentMovie -> quantity - 1; std::cout << "Movie has been rented." << std::endl; std::cout << "Movie Info:" << std::endl; std::cout << "===========" << std::endl; std::cout << "Ranking:" << rentMovie -> ranking << std::endl; std::cout << "Title:" << rentMovie -> title << std::endl; std::cout << "Year:" << rentMovie -> year << std::endl; std::cout << "Quantity:" << rentMovie -> quantity << std::endl; } void MovieTree::deleteMovie(std::string title) { MovieNode * node = root; while(node != NULL) { if(node -> title == title) { break; } else if(title < node -> title ) { node = node -> leftChild; } else { node = node -> rightChild; } } if(node == NULL) { std::cout << "Movie not found. " << std::endl; return; } if(node -> leftChild == NULL && node -> rightChild == NULL) { if(node == root) { root = NULL; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = NULL; } else { node -> parent -> rightChild = NULL; } } delete node; } else if(node -> leftChild == NULL && node -> rightChild != NULL) { if(node == root) { root = node -> rightChild; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = node -> rightChild; } else { node -> parent -> rightChild = node -> rightChild; } } node -> rightChild -> parent = node -> parent; delete node; } else if(node -> leftChild != NULL && node -> rightChild == NULL) { if(node == root) { root = node -> leftChild; } else { if(node -> parent -> leftChild == node) { node -> parent -> leftChild = node -> leftChild; } else { node -> parent -> rightChild = node -> leftChild; } } node -> leftChild -> parent = node -> parent; delete node; } else { MovieNode* successor = node -> rightChild; while(successor -> leftChild != NULL) successor = successor -> leftChild; MovieNode copy = *successor; deleteMovie(successor -> title); node -> ranking = copy.ranking; node -> title = copy.title; node -> year = copy.year; node -> quantity = copy.quantity; } } int postOrder(MovieNode *node) { if(node != NULL) { return postOrder(node -> leftChild) + postOrder(node -> rightChild) + 1; } else return 0; } void MovieTree::countMovies() { std::cout << "Count = " << postOrder(root) << std::endl; } MovieNode* MovieTree::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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started