BST memory leak

I have been writing and debugging the code for a binary search tree. I have finished it to the specifications, and according to my test driver, it works fine. By running it with Valgrind on a Linux machine, I have found and resolved several memory leaks. But, I have not been able to find the source of the last few. I am new to C++, so I still don't know the ins and outs of pointers. I was wondering if somebody could help me find the last few sources of the memory leaks in my code?

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
/*
BST::BST(void) 
{
	size = 0;
	root = 0;
}
	
BST::~BST(void)
{
	while (size != 0)
		remove(root->data);
}

NodeInterface * BST::getRootNode()
{
	return root;
}
	
bool BST::add(int data_in)
{
	bool added = 0;
 
	if (root == NULL)
	{
		root = new Node(data_in);
		size = 1;
		added = 1;
	}	
	else	
		added = add(root, data_in);

	return added;
}
	
bool BST::add(Node * current_node, int data_in)
{
	if (data_in < current_node->data)
	{
		if (current_node->leftChild != NULL)
		{
			return add(current_node->leftChild, data_in);
		}
		else
		{
			current_node->leftChild = new Node(data_in);
			current_node->leftChild->parent = current_node;
			++size;
			return 1;
		}
	}
	else if (data_in > current_node->data)
	{
		if (current_node->rightChild != NULL)
		{
			return add(current_node->rightChild, data_in);
		}
		else
		{
			current_node->rightChild = new Node(data_in);
			current_node->rightChild->parent = current_node;
			++size;
			return 1;
		}
	}
	else
		return 0;
}

bool BST::remove(int data_in)
{
	bool removed = 0;

	if (size == 1 && root->data == data_in)
	{
		delete root;
		root = NULL;
		size = 0;
		removed = 1;
	}
	else if (size > 1)
		removed = remove(root, data_in);

	return removed;
}

bool BST::remove(Node * current, int data_in)
{
	if (data_in < current->data)
	{
		if (current->leftChild != NULL)
			return remove(current->leftChild, data_in);
		else
			return 0;
	}
	else if (data_in > current->data)
	{
		if (current->rightChild != NULL)
			return remove(current->rightChild, data_in);
		else
			return 0;
	}
	else
	{
		if(current->leftChild != NULL && current->rightChild != NULL)
		{
			Node * replace = findMax(current->leftChild);
			
			//if (current == root && root->leftChild == replace)
			//{
				replace->rightChild = root->rightChild;
				root->rightChild->parent = replace;
				replace->parent = NULL;
				root->leftChild = NULL;
				root->rightChild = NULL;
				delete root;
				root = replace;
			//}
		}
		else if (current->leftChild != NULL && current->rightChild == NULL)
		{
			if (current == root)
			{
				root = current->leftChild;
				root->parent = NULL;
				current->leftChild = NULL;
				delete current;
			}
			else
			{
				current->leftChild->parent = current->parent;
				if (current->parent->leftChild == current)
					current->parent->leftChild = current->leftChild;
				else if (current->parent->rightChild == current)
					current->parent->rightChild = current->leftChild;
				current->leftChild = 0;
				current->parent = 0;
				delete current;
			}
		}
		else if (current->leftChild == NULL && current->rightChild != NULL)
		{
			if (current == root)
			{
				current->rightChild->parent = 0;
				root = current->rightChild;
				current->rightChild = 0;
				delete current;
			}
			else
			{
				current->rightChild->parent = current->parent;
				if (current->parent->rightChild == current)
					current->parent->rightChild = current->rightChild;
				else if (current->parent->leftChild == current)
					current->parent->leftChild = current->rightChild;
				current->rightChild = 0;
				current->parent = 0;
				delete current;
			}
		}
		else
		{
			current = current->parent;
			
			if (current->leftChild != NULL && current->leftChild->data == data_in)
			{
				Node * temp = current->leftChild;
				current->leftChild = 0;
				temp->parent = 0;
				delete temp;
			}
			else if (current->rightChild != NULL && current->rightChild->data == data_in)
			{
				Node * temp = current->rightChild;
				current->rightChild = 0;
				temp->parent = 0;
				delete temp;
			}
		}
		--size;
		return 1;
	}
}

Node * BST::findMax(Node * current_node)
{
	while (current_node->rightChild != NULL)
		current_node = current_node->rightChild;
	return current_node;
}
*/

Last edited on
p.s. Sorry, the formatting was removed upon posting.

That's what the code-tag is for. [code] /* code here */ [/code]
Last edited on
Thanks - formatting fixed.
Topic archived. No new replies allowed.