Question: change this c++ remove AVL tree function into a Boolean function, so that all nodes don't need to be checked for balancing all the time.
change this c++ remove AVL tree function into a Boolean function, so that all nodes don't need to be checked for balancing all the time.
void remove( const Comparable & x, AvlNode * & 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 { AvlNode *oldNode = t; t = ( t->left != nullptr ) ? t->left : t->right; delete oldNode; } balance( t ); } static const int ALLOWED_IMBALANCE = 1;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
