AVL Learning Notes

Source: Internet
Author: User

AVL, balanced binary search tree. Delete, insert, find the complexity of all O (Logn). It's a two-pronged tree. For each node, its left child's key value is less than it, and the right child's key value is greater than it. For any one node, its left and right children's height difference is not greater than 1. The height of the tree is defined as: the height of the empty node is 0, the height of the non-empty node is the maximum value of the child height plus 1.

There is an imbalance in the insertion and deletion process. At this point, the balance of the tree is rotated in the following way. The last row in each column is the result of the rotation, and the above two lines are the corresponding initialization states.

1 insert. After inserting a node in a subtree rooted in a node, it is possible that the height difference between the left and right subtrees of the node is greater than 1 (in fact the height difference at this time is 2), then, depending on the situation, a ll,rr,lr,rl of four rotations can maintain the balance of the tree.

2 Delete. Deleted when the key value is less than the current node key value, is deleted in the left subtree, is greater than the current node key value, is deleted in the right subtree, or the current node is deleted. When the current node is deleted, the successor node is found, then the successor node is replaced with the current one, and then the successor is deleted recursively.

template<class_valyetype,class_functype>classcavltree{protected:    structAvltreenode {_valyetype m_ivalue; Avltreenode*M_pleftson; Avltreenode*M_prightson; intM_nheight; intM_nvaluenumber;    }; Avltreenode*M_proot; _functype*M_pcomparefunc; Avltreenode*_newnode () {Avltreenode* pnode=NewAvltreenode; Pnode->m_pleftson=nullptr; Pnode->m_prightson=nullptr; Pnode->m_nheight=1; Pnode->m_nvaluenumber=0; returnPnode; } Avltreenode* _newnode (Const_valyetype&ivalue) {Avltreenode* pnode=NewAvltreenode; Pnode->m_pleftson=nullptr; Pnode->m_prightson=nullptr; Pnode->m_nheight=1; Pnode->m_ivalue=Ivalue; Pnode->m_nvaluenumber=1; returnPnode; }    int_height (avltreenode*Pnode) {        if(Pnode)returnPnode->M_nheight; return 0; }    void_pushup (avltreenode*Pnode) {        if(!pnode)return; Const intNleftsonheight=_height (pnode->M_pleftson); Const intNrightsonheight=_height (pnode->M_prightson); if(nleftsonheight<nrightsonheight) pnode->m_nheight=1+Nrightsonheight; Elsepnode->m_nheight=1+Nleftsonheight; }    /** Pnode's left child will be the root, return the new root **/Avltreenode* _llrotate (avltreenode*Pnode) {        if(!pnode)returnPnode; Avltreenode* pleftson=pnode->M_pleftson; Pnode->m_pleftson=pleftson->M_prightson; Pleftson->m_prightson=Pnode;        _pushup (Pnode);        _pushup (Pleftson); returnPleftson; }    /** Pnode's right child will be the root, return the new root **/Avltreenode* _rrrotate (avltreenode*Pnode) {        if(!pnode)returnPnode; Avltreenode* prightson=pnode->M_prightson; Pnode->m_prightson=prightson->M_pleftson; Prightson->m_pleftson=Pnode;        _pushup (Pnode);        _pushup (Prightson); returnPrightson; }    /** Pnode's left child's right child will become the root and return the new root **/Avltreenode* _lrrotate (avltreenode*Pnode) {        if(!pnode)returnPnode; Pnode->m_pleftson=_rrrotate (pnode->M_pleftson); return_llrotate (Pnode); }    /** Pnode's right child's left child will become the root and return the new root **/Avltreenode* _rlrotate (avltreenode*Pnode) {        if(!pnode)returnPnode; Pnode->m_prightson=_llrotate (pnode->M_prightson); return_rrrotate (Pnode); } Avltreenode* _rotate (avltreenode*Pnode) {        if(!pnode)returnPnode; if(2==_height (Pnode->m_pleftson)-_height (pnode->M_prightson)) {            if(_height (Pnode->m_pleftson->m_pleftson) >=_height (pnode->m_pleftson->M_prightson)) {Pnode=_llrotate (Pnode); }            ElsePnode=_lrrotate (Pnode); }        Else if(2==_height (Pnode->m_prightson)-_height (pnode->M_pleftson)) {            if(_height (Pnode->m_prightson->m_pleftson) >=_height (pnode->m_prightson->M_prightson)) {Pnode=_rlrotate (Pnode); }            ElsePnode=_rrrotate (Pnode); }        returnPnode; } Avltreenode* _insert (avltreenode* proot,Const_valyetype&iinsertvalue) {        if(nullptr==proot) {Proot=_newnode (Iinsertvalue);returnProot; }        Else if(M_pcomparefunc (iinsertvalue,proot->m_ivalue)) {Proot->m_pleftson=_insert (proot->m_pleftson,iinsertvalue); if(2==_height (Proot->m_pleftson)-_height (proot->M_prightson)) {                if(M_pcomparefunc (iinsertvalue,proot->m_pleftson->m_ivalue)) {Proot=_llrotate (Proot); }                Else{proot=_lrrotate (Proot); }            }        }        Else if(M_pcomparefunc (proot->m_ivalue,iinsertvalue)) {Proot->m_prightson=_insert (proot->m_prightson,iinsertvalue); if(2==_height (Proot->m_prightson)-_height (proot->M_pleftson)) {                if(M_pcomparefunc (iinsertvalue,proot->m_prightson->m_ivalue)) {Proot=_rlrotate (Proot); }                Else{proot=_rrrotate (Proot); }            }        }        Else        {            ++proot->M_nvaluenumber;        } _pushup (Proot); returnProot; } Avltreenode* _delete (avltreenode* proot,Const_valyetype&ideletevalue) {        if(Nullptr==proot)returnnullptr; if(M_pcomparefunc (ideletevalue,proot->m_ivalue)) {Proot->m_pleftson=_delete (proot->m_pleftson,ideletevalue); }        Else if(M_pcomparefunc (proot->m_ivalue,ideletevalue)) {Proot->m_prightson=_delete (proot->m_prightson,ideletevalue); }        Else        {            if(0==--proot->M_nvaluenumber) {                if(nullptr==proot->M_pleftson) {Avltreenode* ptmp=Proot; Proot=proot->M_prightson; Deleteptmp; }                Else if(nullptr==proot->M_prightson) {Avltreenode* ptmp=Proot; Proot=proot->M_pleftson; Deleteptmp; }                Else{Avltreenode* ptmp=proot->M_prightson;  while(Ptmp->m_pleftson) ptmp=ptmp->M_pleftson; Proot->m_ivalue=ptmp->M_ivalue; Proot->m_prightson=_delete (proot->m_prightson,proot->m_ivalue); }            }            Else            {                returnProot;        }} _pushup (Proot); if(Proot&&proot->m_pleftson) Proot->m_pleftson=_rotate (proot->M_pleftson); if(Proot&&proot->m_prightson) Proot->m_prightson=_rotate (proot->M_prightson); if(Proot) proot=_rotate (Proot); returnProot; } Public: Cavltree (_functype*pcomparefunc): M_proot (nullptr), M_pcomparefunc (Pcomparefunc) {}voidInsert (Const_valyetype&iinsertvalue) {M_proot=_insert (M_proot,iinsertvalue); }    voidDelete (Const_valyetype&ideletevalue) {M_proot=_delete (M_proot,ideletevalue); }    intFind (Const_valyetype&isearchvalue) {Avltreenode* pcurrent=M_proot;  while(1)        {            if(!pcurrent) Break; if(M_pcomparefunc (isearchvalue,pcurrent->m_ivalue)) {pcurrent=pcurrent->M_pleftson; }            Else if(M_pcomparefunc (pcurrent->m_ivalue,isearchvalue)) {pcurrent=pcurrent->M_prightson; }            Else returnPcurrent->M_nvaluenumber; }        return 0; }};

AVL Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.