Binary Search Tree Delete Node()

My grader said that my delete function doesn't even work. I've ran it myself and debugged prior to turning it in and from what I could see it did work. Wondering if there was something that I missed or fat-fingered? Advice would be appreciated.

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
void BST::BST_Erase(int item)
{
	// Call Erase Recursive Function
	BST_Erase(root, item);
}

// Pre-Con: Root and item passed as parameters
// Post-Con: Item is deleted from the tree
void BST::BST_Erase(Node* n, int item)
{
	// Find the item 
	bool found = false;
	Node* predecessor=nullptr;
	Node* current=n;
	if(current==nullptr)
		 cout<<"Tree is empty"<<endl;return;
	while(current!=nullptr)
	{
		if(current->data==item)
		{
			found = true;
			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;
	}
}
Please mention the error that you get.
Apparently my delete function isn't removing the specified node from the tree. I just double checked it and it doesn't work, which I find odd cause I could have sworn that it worked prior to submitting.
I got it fixed. My problem was line 16cout<<"Tree is empty"<<endl;return. I needed the return on the next line.
Topic archived. No new replies allowed.