Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9043975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:52:16+00:00 2026-06-16T10:52:16+00:00

I’m currently implementing avl trees. After correcting the insertion procedure , I went to

  • 0

I’m currently implementing avl trees. After correcting the insertion procedure, I went to implement another procedure to delete a given node from the avl tree. But I’m really stuck. It’s not that I don’t understand how it works, or how to implement it, but I really care about the code complexity, and the delete function as I have thought it is REALLY difficult to implement. Could someone point me to short and understandable implementation of the delete function in avl trees?

Here is my code so far:

struct avl_tree {
private:
  struct node {
    node *l, *r;
    int h, size;
    key_t key;
    node( key_t k ) : l( 0 ), r( 0 ), h( 1 ), size( 1 ), key( k ) {}
    void u() {
      h=1+std::max( ( l?l->h:0 ), ( r?r->h:0 ) );
      size=( l?l->size:0 ) + ( r?r->size:0 ) + 1;
    }
  } *root;
  compare_t cmp;

  int h( node *x ) { return ( x?x->h:0 ); }

  node* rotl( node *x ) {
    node *y=x->r;
    x->r=y->l;
    y->l=x;
    x->u(); y->u();
    return y;
  }
  node* rotr( node *x ) {
    node *y=x->l;
    x->l=y->r;
    y->r=x;
    x->u(); y->u();
    return y;
  }
  node* balance( node *x ) {
    x->u();
    if( h( x->l ) > 1 + h( x->r ) ) {
      if( h( x->l->l ) < ( x->l?h( x->l->r ):0 ) )  x->l = rotl( x->l );
      x = rotr( x );
    } else if( h( x->r ) > 1 + h( x->l ) ) {
      if( h( x->r->r ) < ( x->r?h( x->r->l ): 0 ) ) x->r = rotr( x->r );
      x = rotl( x );
    }
    return x;
  }
  node* _insert( node *t, key_t k ) {
    if( t==NULL ) return new node( k );
    if( cmp( k, t->key ) ) { t->l = _insert( t->l, k ); }
    else { t->r = _insert( t->r, k ); }
    return balance( t );
  }
  void _inorder( node *t ) {
    if( t ) {
      _inorder( t->l );
      std::cout << t->key << " ";
      _inorder( t->r );
    }
  }
  node* _find( node *t, key_t k ) {
    if( !t ) return t;
    if( cmp( t->key, k ) ) return _find( t->l, k );
    else if( cmp( k, t->key ) ) return _find( t->r, k );
    else return t;
  }
  node* _min( node *t ) {
    if( !t || !t->l ) return t;
    else return _min( t->l );
  }
  node* _max( node *t ) {
    if( !t || !t->r ) return t;
    else return _max( t->r );
  }
public:
  avl_tree() : root( 0 ) {}

  void insert( key_t k ) { root = _insert( root, k ); }
  void inorder() { _inorder( root ); }
  node* find( key_t k ) { return _find( root, k ); }
  node* min() { return _min( root ); }
  node* max() { return _max( root ); }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:52:17+00:00Added an answer on June 16, 2026 at 10:52 am

    So, if you understand how deletion from AVL-tree works, I just want to say a few words about complexity of this code. Of course it’s asymptotically optimal O(log n), but constant is not the best. You can replace calls of _extractmin and _min into one function. That will work in one pass by returning pair of two pointers (min and result of balance).

    node* _extractmin( node *t ) {
        if ( !t->l ) return t->r;
        t->l = _extractmin(t->l);
        return balance(t);
    }
    
    node* _remove( node *t, key_t k )
    {
        if ( !t ) return t;
    
        if (cmp(k, t->key)) 
            t->l = _remove(t->l, k);
        else if (cmp(t->key, k)) 
            t->r = _remove(t->r, k);
        else
        {
            node *l = t->l;
            node *r = t->r;
            delete t;
            if ( !r ) return l;
            node *m = _min(r);
            m->r = _extractmin(r);
            m->l = l;
            return balance(m);
        }
        return balance(t);
    }
    
    void remove( key_t k ) { root = _remove( root, k ); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am confused How to use looping for Json response Array in another Array.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.