removing every other node in tree
Given a bst how can you remove every other node in the tree
Here is the code for removing one node in the tree and also deleting the whole tree. how can i modify one of these to make it remove every other
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void remove( const Comparable & x, BinaryNode * & t )
{
if( t == nullptr )
return; // Item not found; do nothing
if( x < t->element )
remove( x, t->left );
else if( t->element < x )
remove( x, t->right );
else if( t->left != nullptr && t->right != nullptr ) // Two children
{
t->element = findMin( t->right )->element;
remove( t->element, t->right );
}
else
{
BinaryNode *oldNode = t;
t = ( t->left != nullptr ) ? t->left : t->right;
delete oldNode;
}
}
|
1 2 3 4 5 6 7 8 9 10
|
void makeEmpty( BinaryNode * & t )
{
if( t != nullptr )
{
makeEmpty( t->left );
makeEmpty( t->right );
delete t;
}
t = nullptr;
}
|
Topic archived. No new replies allowed.