inorder traversal function of binary tree

As part of an assignment I have been asked to implement an in order traversal function for a binary search tree that permits visit to delete the node visited. Does the code that I have displayed meet his criteria?

1
2
3
4
5
6
7
8
9
10
void inorder (node *tree)
{
	if(tree != NULL)
	{
		inorder(tree->left);
		cout << tree->data;
		delete tree->data;
		inorder(tree->right);
	}
}
Well, you are deleting the data, not the node itself, but your problem statement above says to delete the node. One or the other seems wrong to me.
How about:
1
2
3
4
5
6
7
8
9
10
void inorder (node *tree)
{
	if(tree != NULL)
	{
		inorder(tree->left);
		cout << tree->data;
		inorder(tree->right);
                delete tree;
	}
}

?
How about:
1
2
3
4
5
6
7
8
9
10
11
void inorder (node *tree)
{
	if(tree != NULL)
	{
		inorder(tree->left);
		cout << tree->data;
		inorder(tree->right);
                delete tree;
                tree = NULL;    //takes care of dangling pointers
	}
}


?
I'm curious. The problem as stated is a bit vague. Do you need to perform a search, and delete only the data found? Or do you just need to traverse the tree and delete all nodes?

Your code does the latter. The original statement infers something closer to the former.
I have to display the data of each node before deleting the individual node, would the code that I have originally do this?
Topic archived. No new replies allowed.