I'm surprised your teacher isn't allowing for lazy deletion. That said, here's one method (I don't remember the best way though) for deleting from a BST:
Find the node to be deleted. From there you have two ways to get it's replacement:
- go left one node, then right all the way to the rightmost leaf of that subtree. That node will have the property of being greater than everything above or to the left of it, up until the deleted node. It is also less than everything to the right of the deleted node. Hence, a suitable replacement. Remove it from its leaf position and put it in the deleted node's place.
- similarly, you can go right one node, then left recursively to a leaf. Same properties will apply, though mirrored.
This might end up a little messy moving all the pointers around, but it's a conceptually easy way to find a replacement for the deleted node... HTH ;)
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Naa..this is not similar to rotation. Rotation is used for balancing the binary tree which becomes unbalanced after repeated deletions from the BST, and for the purpose of balancing we use AVL trees, Red Black tree and many others -- but I don't think you should complicate matters by taking these things into consideration for the time being.
And now for the deletion part -- there are actually three cases:
1. The node has no children : In this case you can directly remove the node.
2. The node has one child: In this case you just replace the node with the child.
3. The node has two children: This is a bit tricky to handle and toughest among all. In this case you need to delete the node keeping in mind the nature or property of BST is maintained.
Suppose if the node is N then we replace the value of N with either its in-order successor (the left-most child of the right subtree) or the in-order predecessor (the right-most child of the left subtree).
For an excellent simulation you can look here.
HOpe it solved your problem.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Yes -- like I mentioned in my previous post, you can leave out the complicated stuff of balancing the tree since the it doesn't seem to be the focus of your project.
As far as the third case is concerned, assuming you have the pointer to the node to be deleted with you:
// here we accept a reference to the node pointer type
// this relieves you from passing the function a pointer to pointer
// thereby making the deletion easy.
void delete_node( Node*& node )
{
Node*& tmp = node ;
if( node->left == NULL )
{
node = node->right ;
delete tmp ;
}
else if ( node->right == NULL )
{
node = node->left ;
delete tmp ;
}
else
{
// here comes the third case
tmp = node->left ;
while( tmp->right != NULL )
{
tmp = tmp->right ;
}
node->value = tmp->value ;
delete_node( tmp ) ;
}
Hope it helped , bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
No, its a reference thingy which belongs to C++.
int a = 10 ;
int& b = 20 ; // b is a reference or an alias to a
If you haven't been taught that, you can just use double pointers to node type and you task will be done.
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
& can be used to denote a reference or to get the address of a variable, (and also as a bitwise operator). When used at the declaration (e.g. as Node*&) it is the first, and when used in a non-declaration and preceeding a single variable, it is to get the address. When it is between two vars, e.g. a & b it is performing a bitwise-and operation between the two.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
>Any ideas you guys could fish out would be most helpful.
Lazy deletion is definitely the easiest to implement, but it gets tricky when you want to clean the data structure by physically removing "deleted" nodes. So it really just defers the problem until later, where it becomes a bigger problem. :) The only reall reason for using lazy deletion is when the expense of removing a node is too great at the time.
Since you're storing references and not actual data, a deletion by copying scheme is probably much better for you. It's easier to wrap your head around because you only adjust links for a leaf node. You can find a couple of implementations and a description here .
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401