#include <iostream>
#include "Tree.h"
#include "Movie.h"
int main(){
//create a tree for ints
Tree<int> intee;
//add new nodes to the tree
intee.addElement(12);
intee.addElement(17);
intee.addElement(23); // Stucks Here!!
intee.addElement(20);
intee.addElement(10);
intee.addElement(6);
intee.addElement(14);
intee.addElement(11);
intee.addElement(9);
intee.addElement(22);
intee.addElement(7);
if(intee.addElement(14))
{
cout << "Error: entered 14 twice!!" << endl;
}
//print the nodes in ascending order
intee.printTree();
cout << "6 7 9 10 11 12 14 17 20 22 23 **********" << endl;
//delete a node with only right child
intee.deleteElement(6);
intee.printTree();
cout << "7 9 10 11 12 14 17 20 22 23 **********" << endl;
//delete a node with only left child
intee.deleteElement(23);
//delete leaf that is a right child
intee.deleteElement(22);
intee.printTree();
cout << "7 9 10 11 12 14 17 20 **********" << endl;
//delete leaf that is a left child
intee.deleteElement(14);
//delete node with two children
if (!intee.deleteElement(10))
{
cout << "Error: delete an element failed!!" << endl;
}
intee.printTree();
cout << "7 9 11 12 17 20 **********" << endl;
//delete node that does not exist
if (intee.deleteElement(10))
{
cout << "Error: Delete an element that does not exist returns true!!" << endl;
}
//print the nodes in ascending order
intee.printTree();
cout << "7 9 11 12 17 20 **********" << endl;
//delete the root
intee.deleteElement(12);
intee.addElement(3);
intee.addElement(700);
intee.printTree();
cout << "3 7 9 11 17 20 700 **********" << endl;
//create a tree for movies
Movie mymovie("Great Movie",9);
Tree<Movie> movies;
movies.addElement(mymovie);
//add new movies
movies.addElement(Movie("Good Movie",8));
movies.addElement(Movie("The Greatest Movie",9));
movies.addElement(Movie("Medium Minus Movie",6));
movies.addElement(Movie("Bad Movie",3));
//print the nodes in ascending order
cout << "Best Movie ever: " << *movies.getMaximum() << endl;
cout << "Best Movie ever: The Greatest Movie(9) *******" << endl;
movies.printTree();
cout << "Bad Movie(3) Medium Minus Movie(6) Good Movie(8) Great Movie(9) The Greatest Movie(9) *******" << endl;
cout << "found a good one: " << *movies.findNode(Movie("Good Movie",8)) << endl;
cout << "found a good one: Good Movie(8) *******" << endl;
cout << "Good Luck!" << endl;
return 0;
}
#ifndef _MOVIE_H_
#define _MOVIE_H_
#include <string>
#include <ostream>
usingnamespace std;
class Movie {
public:
Movie(string name = "No Name" , int score = -1): _name(name), _score(score){};
int getScore() const;
string getName() const;
/* movie1 is smaller than movie2,
* if its score is smaller.
* If the scores are the same, their names should be lexicographic compared*/
booloperator<(const Movie& ) const;
/* movie1 equals movie2 if their scores and names are the same */
booloperator==(const Movie&) const;
/* Should output the movie name and movie score */
friend ostream& operator<<(ostream& out, const Movie& node);
private:
int _score;
string _name;
};
#endif
#ifndef _TREE_H_
#define _TREE_H_
#include <iostream>
#include "Node.h"
usingnamespace std;
template <class T>
class Tree{
public:
Tree():_root(NULL){};
/*Should return the root of the tree
* if the tree is empty should return NULL
*/
Node<T>* getRoot() const{
return _root;
}
/* Should return the node that contains the input paramter.
* if such node does not exist, should return NULL
*/
Node<T>* findNode(const T& element) const;
/*Should return the node with the maximal value
* if the tree is empty should return NULL
*/
Node<T>* getMaximum() const;
/* Should delete the node with input parameter from the tree,
* if a node with this value does not exist, returns false
* if the deletion succeeded returns true
* otherwise, returns false
*/
bool deleteElement(const T& element);
/* Should delete the node todelete from the tree,
* if the deletion succeeded returns true
* otherwise, returns false
*/
bool deleteNode(Node<T>* todelete);
/*Should add a node with input value to the tree
* if a node with the same value exists, the function should return false
* otehrwise, it should return true
*/
bool addElement(const T& element);
/*Should print the tree in ascending order.
* (first value is the smallest node in the tree)
* if the tree is empty, it should print 'The tree is empty'
*/
void printTree() const;
private:
Node<T>* _root;
// void inorder(Node<T>*) const;
bool deleteAndFix(Node<T>* tmp);
};
/********************************************************************/
template <class T>
Node<T>* Tree<T>::findNode(const T& element) const{
Node<T>* tmp = _root;
while (tmp != NULL) {
if (tmp->getElement() == element) {
return tmp;
} else {
if (tmp->getElement() < element) {
tmp = tmp->getLeft();
} else {
tmp = tmp->getRight();
}
}
}
return NULL;
}
/********************************************************************/
template <class T>
Node<T>* Tree<T>::getMaximum() const{
Node<T>* tmp = _root;
while (tmp->getRight())
tmp = tmp->getRight();
return tmp;
}
/********************************************************************/
template <class T>
bool Tree<T>::deleteElement(const T& element){
Node<T>* tmp;
tmp = findNode(element);
return deleteAndFix(tmp);
}
/********************************************************************/
template <class T>
bool Tree<T>::deleteNode(Node<T>* todelete){
Node<T>* tmp = findNode(todelete->_element);
return deleteAndFix(tmp);
}
/********************************************************************/
template <class T>
bool Tree<T>::addElement(const T& element){
if (_root == NULL){
_root = new Node<T> ;
_root->setElement(element);
returntrue;
}
if (findNode(element))
returnfalse;
Node<T>* tmp = _root;
Node<T>* next = NULL;
bool isLeft = true;
do {
if (element < tmp->getElement() ){
next = tmp->getLeft();
isLeft = true;
}
else {
next = tmp->getRight();
isLeft = false;
}
} while (next != NULL);
next = new Node<T>;
next->setElement(element);
next->setParent(tmp);
if (isLeft)
tmp->setLeft(next);
else
tmp->setRight(next);
}
/********************************************************************/
template <class T>
void Tree<T>::printTree() const{
// if (_root == NULL){
cout << "The Tree is Empty." << endl;
return;
// }
}
/********************************************************************/
template <class T>
bool Tree<T>::deleteAndFix(Node<T>* tmp){
if (tmp == NULL)
returnfalse;
if (tmp->getRight() == NULL){
if (tmp->getLeft() == NULL){
delete tmp;
returntrue;
} else {
tmp->getLeft()->setParent(tmp->getParent());
tmp->getParent()->setLeft(tmp->getLeft());
delete tmp;
returntrue;
}
} else {
Node<T>* tmp2 = tmp;
tmp = tmp->getRight();
while (tmp->getLeft() != NULL)
tmp = tmp ->getLeft();
if (tmp->getParent() != tmp2)
tmp->getParent()->setLeft(NULL);
tmp->setRight(tmp2->getRight());
tmp->setLeft(tmp2->getLeft());
tmp->setParent(tmp2->getParent());
if (tmp2->getLeft() != NULL)
tmp2->getLeft()->setParent(tmp);
tmp2->getRight()->setParent(tmp);
if (tmp2->getParent()->getLeft() == tmp2)
tmp2->getParent()->setLeft(tmp);
else
tmp2->getParent()->setRight(tmp);
delete tmp2;
returntrue;
}
returnfalse;
}
#endif
Your Tree<int>::AddElement and Tree<Movie>::AddElement not always return a value;
Your Movie::operator == is always recursive.
It is warnings after first compilation (msvc2010), actually it's errors.
About Movie::operator == , maybe you wanted:
yea, i fixed the return issues.
but i still have have a problem with the do while loop on addElement().
i dont know why, but it gets itno infinite loop.