Help with BST Destructor and Size Function

I'm currently getting these two errors:

Access Violation Error in my Destructor
Access Violation in my Size function

I know what they mean but I'm not sure where exactly my leak is.
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
// Helper Function for the Destructor
  void binST::binST_Destroy(Node* n)
{
	if(n->left != nullptr)
		binST_Destroy(n->left);
	if(n->right != nullptr)
		binST_Destroy(n->right);
	delete n;
        n=nullptr;
}

// Helper Size Function
int binST::binST_Size(Node* n)
{
	if(n=nullptr)
		return 0;
	else
		return(binST_Size(n->left)+binST_Size(n->right)+1);
}

// Insert Function
void binST::binST_Insert(Node*& n, int item)
{
	// If node is null, create a new node with input data
	if(n==nullptr)
	{
		n=new Node(item);
	}
	else
	{
		// Check to see if data being inserted is < current node->data
		if(item < (n->data))
			// recursive call with left child node input node
			binST_Insert((n->left), item);
		// Check to see if data being inserted is > current node->data
		else if(item > (n->data))
		{
			// recursive call with left child node input node
			binST_Insert((n->right), item);
		}
		else
			// If item is a duplicate, output duplicate message
			cout<<item<<" is a duplicate"<<endl;
	}
}

// Erase Function (I'm almost positive that it has to be somewhere inside here)
void binST::binST_Erase(Node* n, int item)
{
	// Find the item 
	bool found = false;
	Node* predecessor=nullptr;
	Node* current=n;
	if(current==nullptr)
		return;
	while(current!=nullptr)
	{
		if(current->data==item)
		{
			found = true;
			predecessor = current;
			break;
		}
		else
		{
			predecessor = current;
			if(item > (current->data))
				current=current->right;
			else
				current=current->left;
		}
	}
	if(!found)
	{
		cout<<item<<" not in Tree."<<endl;
		return;
	}
	// CASE 1: Removing a node with a single child
	if((current->left==nullptr && current->right != nullptr) || (current->left != nullptr && current->right==nullptr))
	{
		// Right Leaf Present, No Left Leaf
		if(current->left==nullptr && current->right != nullptr)
		{
			// If predecessor's left tree equals Node n
			if(predecessor->left==current)
			{
				// then predecessor's left tree becomes n's right tree
				// and delete n
				predecessor->left=current->right;
				delete current;
				current=nullptr;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
			// If predecessor's right tree equals Node n
			else
			{
				// then predecessor's right tree becomes n's right tree
				// and delete n
				predecessor->right=current->right;
				delete current;
				current=nullptr;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
		}
		else // Left Leaf Present, No Right Leaf Present
		{
			if(predecessor->left==current)
			{
				predecessor->left=current->left;
				delete current;
				current=nullptr;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
			else
			{
				predecessor->right=current->left;
				delete current;
				current=nullptr;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
		}
		return;
	}
	// CASE 2: Removing a Leaf Node
	if(current->left==nullptr && current->right==nullptr)
	{
		if(predecessor->left==current)
			predecessor->left=nullptr;
		else
			predecessor->right=nullptr;
		delete current;
		cout<<item<<" has been removed from the Tree."<<endl;
		return;
	}
	// CASE 3: Node has two children
	// Replace Node with smallest value in right subtree
	if(current->left != nullptr && current->right != nullptr)
	{
		Node* check=current->right;
		if((current->left==nullptr)&&(current->right==nullptr))
		{
			current=check;
			delete check;
			current->right==nullptr;
			cout<<item<<" has been removed from the Tree."<<endl;
		}
		else // Right child has children
		{
			// If the node's right child has a left child
			// Move all the way down left to locate smallest element
			if((current->right)->left!=nullptr)
			{
				Node* leftCurrent;
				Node* leftCurrentPred;
				leftCurrentPred=current->right;
				leftCurrent=(current->right)->left;
				while(leftCurrent->left != nullptr)
				{
					leftCurrentPred=leftCurrent;
					leftCurrent=leftCurrent->left;
				}
				current->data=leftCurrent->data;
				delete leftCurrent;
				leftCurrentPred->left==nullptr;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
			else
			{
				Node* temp=current->right;
				current->data=temp->data;
				current->right=temp->right;
				delete temp;
				cout<<item<<" has been removed from the Tree."<<endl;
			}
		}
		return;
	}
}


If someone with a fresher pair of eyes could help me out with a little advice, then that would be greatly appreciated. Thank you!
Last edited on
Okay, I've got Size worked out. Moving on to the Destructor.
Destructor's fixed! HAPPY DANCE!!
Topic archived. No new replies allowed.