Binary Search Tree - Bool Remove Method

We have been assigned a Binary Search Tree project for homework and for the most part my tree program works perfectly fine.

Print(shows tree-node values in-order), search, insert, and so on work perfectly fine; yet when I use the remove command and immediately use the print command the program crashes during the moment it should display the removed node on the value list, and then gives me an error pop-up.

I think I tried everything from helper methods to dissecting each function individually, though the main problem comes from this remove method. I've went over more than enough websites as well, asked friends, and went through my notes, though haven't found a solution.

Could anyone help point me into the right direction so I could solve the problem?

Thank you for your time.

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
//part of main.cpp file, can't edit this section

 // remove value from tree (if in tree)
    case 'r':
      cin >> value;
      if ( !shrub.remove(value) )
	cout << "Error: Value not found, was not removed!\n";
      else
	cout << "Value successfully removed from tree!\n";
      break;

//part of tree.cpp file, can edit this section

  Node* Tree::findMin(Node* start, Node*&parent)
{
	while (start->left!=NULL)
	{
		parent = start;
		start = start->left;
	}
	return start;
}
bool Tree::remove(int value)
{
	Node* parent;
	Node* victim=search(value, parent);

	//no results
	if (!victim)
		return false;

	//2 children case, swaps values, does not remove
	if (victim->left && victim->right)
	{
		parent = victim;
		Node* min = findMin(victim->right, parent);
		victim->data = min->data;
		victim = min;
	}

	//1 child and 0 child cases
	if (parent!=NULL)
	{
		Node* child = NULL;
		if (victim->left)
			child = victim->left;
		else if (victim->right)
			child = victim->right;

		if (parent->left == victim)
			parent->left = child;
		else
			parent->right = child;
	}

	// make sure we do not destroy root
	if (victim == root)
		root = root->left != NULL ? root->left : root->right;

	victim->left = NULL;
	victim->right = NULL;
	delete victim;
        return true;
}
Topic archived. No new replies allowed.