Code Stucks while running! need some help!

Hi, I have a code that passes compilation, but stucks while running with no error message!

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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;
}

Last edited on
Movie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <ostream>
#include "Movie.h"	

using namespace std;

	int Movie::getScore() const {
		return _score;
	}

	string Movie::getName() const {
		return _name;
	}

	bool Movie::operator < (const Movie& movie2) const {
		if (this ==  &movie2) {
			return false;
		}
		if (getScore() < movie2.getScore() )
			return true;
		if (getScore() > movie2.getScore() )
			return false;

		if ((getName().compare(movie2.getName())) < 0 )
			return true;
		return false;
	}

	bool Movie::operator==(const Movie& movie2) const {
		if (*this ==  movie2) {
			return true;
		}
		if (!(getScore() == movie2.getScore() ) && !(getName() == movie2.getName()) ) // str1.compare(str2)
			return true;
		return false;
	}

	ostream& operator<<(ostream& out, const Movie& movie) {
		out << movie._name << "(" << movie._score << ")." << endl;
		return out;
	}


Movie.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef _MOVIE_H_
#define _MOVIE_H_

#include <string>
#include <ostream>

using namespace 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*/
	bool operator<(const Movie& ) const;
	
	/* movie1 equals movie2 if their scores and names are the same */
	bool operator==(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 


Tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef _TREE_H_
#define _TREE_H_

#include <iostream>
#include "Node.h"

using namespace 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);
		return true;
	}
	if (findNode(element))
		return false;
	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)
		return false;
	if (tmp->getRight() == NULL){
		if (tmp->getLeft() == NULL){
			delete tmp;
			return true;
		} else {
			tmp->getLeft()->setParent(tmp->getParent());
			tmp->getParent()->setLeft(tmp->getLeft());
			delete tmp;
			return true;
		}
	} 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;
		return true;
	}
	return false;
}

#endif 
Node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef NODE_H_
#define NODE_H_

#include <iostream>
#include <list>

using namespace std;

template <class T> 
class Node{
public:
	Node();
	Node(T element);
	Node(const Node& other);
	~Node();
	Node& operator=(const Node& node);
    bool operator<(const Node&) const;
	bool operator==(const Node&);
	bool operator!=(const Node&);
    
	/*This function should call the operator<< of the node's element*/
	template <class T2> friend ostream& operator<<(ostream&, const Node<T2>&);

	Node* getParent() const;
	void setParent(Node* node);

	Node* getRight() const;
	void setRight(Node* node);

	Node* getLeft() const;
	void setLeft(Node* node);

	T getElement() const;
	void setElement(T ele);

	Node* getThis() const; //returns a non-const pointer of This pointer

	
	Node<T>* getNext() const;

	
	Node<T>* getPrevious() const;

private:
	Node* m_parent;
	Node* m_right;
	Node* m_left;
	T m_element;
};
/********************************************************************/
template <class T>
Node<T>::Node(){
//	m_element = 0;
	m_parent = 0;
	m_left = 0;
	m_right = 0;
}
/********************************************************************/
template <class T>
Node<T>::Node(T element){
	m_element = element;
	m_parent = 0;
	m_left = 0;
	m_right = 0;
}
/********************************************************************/
template <class T>
Node<T>::Node(const Node& other){
	setElement(node.getElement());
	setLeft(node.getLeft());
	setRight(node.getRight());
	setParent(node.getParent());
}
/********************************************************************/
template <class T>
Node<T>::~Node(){}
/********************************************************************/
template <class T>
Node<T>& Node<T>::operator=(const Node& node){
	setElement(node.getElement());
	setLeft(node.getLeft());
	setRight(node.getRight());
	setParent(node.getParent());
	return this;
}
/********************************************************************/
template <class T>
bool Node<T>::operator<(const Node& other) const{
	return (m_element < other.getElement());
}
/********************************************************************/
template <class T>
bool Node<T>::operator==(const Node& other){
	if (((*this) == 0) && (other == 0))
		return true;
	if ((((*this) != 0) && (other == 0)) || (((*this) == 0) && (other != 0)))
		return false;
	else
		return (getElement() == other.getElement());
}
/********************************************************************/
template <class T>
bool Node<T>::operator!=(const Node& other){
	return (!(*this == other));
}
/********************************************************************/
template <class T2>
ostream& operator<<(ostream& out, const Node<T2>& node){
	out << node.getElement();
	return out;
}
/********************************************************************/
template <class T>
Node<T>* Node<T>::getParent() const{
	return m_parent;
}
/********************************************************************/
template <class T>
void Node<T>::setParent(Node* node){
	m_parent = node;
}
/********************************************************************/
template <class T>
Node<T>* Node<T>::getRight() const{
	return m_right;
}
/********************************************************************/
template <class T>
void Node<T>::setRight(Node* node){
	m_right = node;
}
/********************************************************************/
template <class T>
Node<T>* Node<T>::getLeft() const{
	return m_left;
}
/********************************************************************/
template <class T>
void Node<T>::setLeft(Node* node){
	m_left = node;
}
/********************************************************************/
template <class T>
T Node<T>::getElement() const{
	return m_element;
}
/********************************************************************/
template <class T>
void Node<T>::setElement(T element){
	m_element = element;
}
/********************************************************************/
template <class T>
Node<T>* Node<T>::getThis() const //returns a non-const pointer of This pointer
{
	if (getRight() != 0)
		return (getRight()->getParent());
	if (getLeft() != 0)
		return (getLeft()->getParent());
	if (getParent()->getLeft() !=0)
		if (getElement() == (getParent()->getLeft())->getElement())
			return (getParent()->getLeft());
	if (getParent()->getRight() !=0)
		if (getElement() == (getParent()->getRight())->getElement())
			return (getParent()->getRight());
}

/********************************************************************/
template <class T>
Node<T>* Node<T>::getNext() const{
	
	Node<T>* current = getThis();
	
	if (current->getRight() != 0){
		current = current->getRight();
		while (current->getLeft() != 0){
			current = current->getLeft();
		}
		return current;
	}
	else
	{
		while (current != (current->getParent())->getLeft()){
			current = current->getParent();
		}
		current = current->getParent();
		return current;
	}
}
/********************************************************************/
template <class T>
Node<T>* Node<T>::getPrevious() const
{
	Node<T>* current = this;
	if (current->getLeft() != 0){
		current = current->getLeft();
		while (current->getRight() != 0){
			current = current->getRight();
		}
		return current;
	}
	else
	{
		while (!(current == current->getParent()->getRight())){
			current = current->getParent();
		}
		current = current->getParent();
		return current;
	}
}

#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:
1
2
3
4
5
6
bool Movie::operator==(const Movie& movie2) const {
		if (this ==  &movie2) {
			return true;
		}
		return (!(getScore() == movie2.getScore() ) && !(getName() == movie2.getName()) ); // str1.compare(str2)
	}
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.
Topic archived. No new replies allowed.