Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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: "<title<<" "<quantity<

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago

Question

=+what kinds of policies and practices should be developed?

Answered: 1 week ago

Question

=+ Of the HR issues mentioned in the case,

Answered: 1 week ago