Trees - Access Violation reading location 0xFEEEFEF2 for different set of numbers

I have written up code for Tree with the implementation of adding and removing nodes from the tree. When I first tested the tree with a set of numbers then trying to remove numbers that are and aren't on that tree it worked perfectly fine. These were the number i used. 9, 5, 3, 6, 1, 8, 3, 1, 9, 3. I tried to remove 3 and it worked perfectly fine.

However when i used a different set of numbers 18, 82, 97, 16, 11, 9, 44, 86, 38, 74. Trying to remove 82 it throws the access violation error. But if i try to removed a number which isn't on the tree it works fine stating the predefined result that, that number doesn't exist in the tree.

Why is this so ?
Here is my code.

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
#include <iostream>
#include "Tree.h"
using namespace std;

int main() {

	Tree<int> tree;

	int int_array[10] = { 18, 82, 97, 16, 11, 9, 44, 86, 38, 74 };
	int size = sizeof(int_array) / sizeof(int_array[0]);


	cout << "Elements that need to be added into a tree" << endl;
	for (int i = 0; i < size; i++) {
		cout << int_array[i] << " ";
	}

	cout << endl << endl;
	for (int i = 0; i < size; i++) {
		tree.Add(int_array[i]);
	}
	cout << endl;
	tree.Remove(82);

	int i_temp = 0;
	cin >> i_temp;

	return (0);
}

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef __NODE_H__
#define __NODE_H__

#include <iostream>

template <typename T>
class Node {
public:
	Node() {}
	Node(T data) {
		next_right_ = NULL;
		next_left_ = NULL;

		data_ = data;
	}
	~Node() {}

	void SetNextRight(Node<T>* next_right) {
		next_right_ = next_right;
	}
	Node* NextRight() {
		return next_right_;
	}
	void SetNextLeft(Node<T>* next_left) {
		next_left_ = next_left;
	}
	Node* NextLeft() {
		return next_left_;
	}

	void SetData(T data) {
		data_ = data;
	}
	T Data() const {
		return data_;
	}

private:
	Node<T>* next_right_;
	Node<T>* next_left_;

	T data_;
};
#endif // !__NODE_H__

Tree.h
#ifndef __TREE_H__
#define __TREE_H__

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

using namespace std;

template <typename T>
class Tree {
public:
	Tree() {}
	~Tree() {
		PostOrderDeletion(root_);
	}

	void Add(T data) {
		Add(root_, data);
	}

	void Remove(T data) {
		Remove(root_, data);
	}



private:
	void Add(Node<T>* current, T data) {

		if (root_ == NULL) {
			root_ = new Node<T>(data);
			cout << "Added " << data << " as the root" << endl;
			return;
		}
		else if (data <= current->Data()) {
			if (current->NextLeft() != NULL) {
				Add(current->NextLeft(), data);
			}
			else {
				current->SetNextLeft(new Node<T>(data));
				cout << "Added " << data << " as a child of " << current->Data() << endl;
			}
		}
		else if (data > current->Data()){
			if (current->NextRight() != NULL) {
				Add(current->NextRight(), data);
			}
			else {
				current->SetNextRight(new Node<T>(data));
				cout << "Added " << data << " as a child of " << current->Data() << endl;
			}
		}
	}

	void RemoveRoot() {
		if (root_ != NULL) {
			Node<T>* temp_root = root_;
			T temp_root_data = root_->Data();
			T temp_lowest_data;

			// Case of no children
			if (root_->NextLeft() == NULL && root_->NextRight() == NULL) {
				root_ = NULL;
				delete temp_root;
			}

			// Case of one child
			else if (root_->NextLeft() == NULL && root_->NextRight() != NULL) {
				root_ = root_->NextRight();
				delete temp_root;
				temp_root->SetNextRight(NULL);
				cout << "The root node with data " << temp_root_data <<
					" was deleted. The new root contains the data " << root_->Data() << endl;

			}
			else if (root_->NextLeft() != NULL && root_->NextRight() == NULL) {
				root_ = root_->NextLeft();
				delete temp_root;
				temp_root->SetNextLeft(NULL);
				cout << "The root node with data " << temp_root_data <<
					" was deleted. The new root contains the data " << root_->Data() << endl;
			}

			// Case of two children
			else {
				temp_lowest_data = FindLowest(root_->NextRight());
				Remove(root_, temp_lowest_data);
				root_->SetData(temp_lowest_data);
				cout << "The root node containing the data value " << temp_root_data <<
					" was overwritten with the data " << root_->Data() << endl;
			}

		}
		else {
			cout << "[Root Remove] - The tree is empty." << endl;
		}
	}

	void Remove(Node<T>* current, T data) {
		if (root_ != NULL) {
			if (data == root_->Data()) {
				RemoveRoot();
			}
			else {
				if (data <= current->Data() && current->NextLeft() != NULL) {
					(current->NextLeft())->Data() == data ?
						Remove(current, current->NextLeft(), true) :
						Remove(current->NextLeft(), data);
				}
				else if (data > current->Data() && current->NextRight() != NULL) {
					(current->NextRight())->Data() == data ?
						Remove(current, current->NextRight(), false) :
						Remove(current->NextRight(), data);
				}
				else {
					cout << "The data value " << data << " was not found" << endl;
				}
			}
		}
		else {
			cout << "[Remove Recursive] - The tree is empty." << endl;
		}
	}

	void Remove(Node<T>* current, Node<T>* match_test, bool is_left) {
		if (root_ != NULL) {
			Node<T>* delete_node;
			T match_data = match_test->Data();
			T lowest_data;

			// Case of no children
			if (match_test->NextLeft() == NULL && match_test->NextRight() == NULL) {
				delete_node = match_test;
				is_left == true ? current->SetNextLeft(NULL) : current->SetNextRight(NULL);
				delete delete_node;
				cout << "The node containing the data value " << match_data << " was removed" << endl;
			}

			// Case of one child
			if (match_test->NextLeft() == NULL && match_test->NextRight() != NULL) {
				is_left == true ? current->SetNextLeft(match_test->NextRight()) : current->SetNextRight(match_test->NextRight());
				match_test->SetNextRight(NULL);
				delete_node = match_test;
				delete delete_node;
				cout << "The node containing key " << match_data << " was removed" << endl;
			}
			else if (match_test->NextLeft() != NULL && match_test->NextRight() == NULL) {
				is_left == true ? current->SetNextLeft(match_test->NextLeft()) : current->SetNextRight(match_test->NextLeft());
				match_test->SetNextLeft(NULL);
				delete_node = match_test;
				delete delete_node;
				cout << "The node containing the data value " << match_data << " was removed" << endl;
			}
			else {
				lowest_data = FindLowest(match_test->NextRight());
				Remove(match_test, lowest_data);
				match_test->SetData(lowest_data);
			}
		}
		else {
			cout << "[Remove Match-Test] - The tree is empty." << endl;
		}
	}

	T FindLowest(Node<T>* current) {
		if (root_ != NULL) {
			if (current->NextLeft() != NULL) {
				return FindLowest(current->NextLeft());
			}
			else {
				cout << "The lowest value is " << current->Data() << endl;
				return current->Data();
			}
		}
		else {
			cout << "[Find Lowest] - The tree is empty" << endl;
			return NULL;
		}
	}

	void PostOrderDeletion(Node<T>* current) {
		if (current != NULL) {
			if (current->NextLeft() != NULL) {
				PostOrderDeletion(current->NextLeft());
			}
			if (current->NextRight() != NULL) {
				PostOrderDeletion(current->NextRight());
			}
			cout << "Deleting the node containing the data value " << current->Data() << endl;
			delete current;
		}
	}

	Node<T>* root_;

};
#endif // !__TREE_H__ 
Last edited on
Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/

This is definitely a problem:
1
2
delete temp_root;
temp_root->SetNextLeft(NULL);
You cannot do annything with temp_root after it is deleted. That causes undefined behavior.
I've already tried commenting out all the delete temp_root
And the problem is still there
As coder777 said, please could you edit your original post to use code tags, to make your code easier to read. People are more likely to read it and help you, if you put in the effort to make your code readable.
Hope that helps ~
The next problem is that root_ remains uninitialized. Set root_ to nullptr either on line 241 or 59.

Since you do not initialze next_right_ / next_left_ in your default constructor it is very likely that they remain uninitialized too.

Another problem is on line 182: You delete match_test (indirectly, why?) but you continue using it from line 187 on. Try return; after line 183.
Last edited on
this is not smart ...
int int_array[10] = { 18, 82, 97, 16, 11, 9, 44, 86, 38, 74 };
int size = sizeof(int_array) / sizeof(int_array[0]);


before doing any deletion can you show us an initial array and then the output of this array when it's stored in the tree ? There is basicaly three recursive method to implement for that.

If the tree is not correctly set , then I dont see how you could do deletion well..

Thanks
Topic archived. No new replies allowed.