void insert( const Comparable & x, AvlNode * & t )
{
if( t == NULL )
t = new AvlNode( x, NULL, NULL );
else if( x < t->element )
{
insert( x, t->left );
if( height( t->left ) - height( t->right ) == 2 )
if( x < t->left->element )
rotateWithRightChild( t );
else
doubleWithLeftChild( t );
}
else if( t->element < x )
{
insert( x, t->right );
if( height( t->right ) - height( t->left ) == 2 )
if( x < t->left->element )
rotateWithRightChild( t );
else
doubleWithLeftChild( t );
}
else
; // Duplicate; do nothing
t->height = max( height( t->left ), height( t->right ) ) + 1;
}
int height( AvlNode *t ) const { return t == NULL ? -1 : t->height; }
How can they cal the height of tree ?
1 0 -1
height(-1) - height(null) = 1 ?? Not balance ?