Binary Tree Learning three: AVL tree

Source: Internet
Author: User

1. AVL Tree:

1) its Zuozi (TL) and right sub-tree (TR) are AVL trees;

2) | Hl-hr|<=1, where HL and HR are the height of TL and TR;

3) The AVL tree with a height of h, node 2*h-1.

AVL tree lookups, insertions, deletions in average and worst case are O (logn), insertions and deletions may take one or more rotations to rebalance. The idea of rotation balance of AVL tree: The height of the subtree with the unbalance point should remain unchanged, and after the new node is inserted, the root is traced back to the first node with a non-0 balance factor. The rotation method is as follows:

1) ll type: Left rotation

      

2) RR type: Right rotation

    

3) LR Type: Turn right at the unbalanced child node, then turn left.

    

4) RL Type: Turn left at unbalanced child nodes, then reed right

    

Note that the minimum tree involved in the rotation process should keep the nature of the search binary tree.

The core part of the C + + implementation of the AVL tree is balanced rotation:

1#include"stdafx.h"2#include <time.h>3#include <stdlib.h>4#include <iostream>5 using namespacestd;6 7typedefstructavltree{8     intNdata;9avltree*Plchild;Tenavltree*Prchild; One     intnheight; A }avltree; -  -avltree* Llrotate (avltree*proot); theavltree* Rrrotate (avltree*proot); -avltree* Lrrotate (avltree*proot); -avltree* Rlrotate (avltree*proot); -  + intCompare (intAintb) { -     return(A > B?)a:b); + } A intHeight (avltree*proot) { at     if(null==proot) -     { -         return-1; -     } -     Else -     { in         return(proot->nheight); -     } to } +  -avltree* Insert (avltree* proot,intndata) { the     if(null==proot) *     { $Proot =NewAvltree;Panax NotoginsengProot->ndata =Ndata; -Proot->nheight =0; theProot->plchild =NULL; +Proot->prchild =NULL; A     } the     Else if(proot->ndata>ndata) +     { -Proot->plchild = Insert (proot->Plchild, ndata); $         if(Height (proot->plchild)-height (proot->prchild) = =2) $         { -             if(proot->plchild->ndata>ndata) -             { theProot =llrotate (proot); -             }Wuyi             Else the             { -Proot =lrrotate (proot); Wu             } -         } About     } $     Else if(proot->ndata<ndata) -     { -Proot->prchild = Insert (proot->Prchild, ndata); -         if(Height (proot->prchild)-height (proot->plchild) = =2) A         { +             if(proot->prchild->ndata<ndata) the             { -Proot =rrrotate (proot); $             } the             Else the             { theProot =rlrotate (proot); the             } -         } in     } theProot->nheight = Compare (height (proot->plchild), height (proot->prchild)) +1; the     returnProot; About } the  the //ll Rotate theavltree* Llrotate (avltree*proot) { +avltree*ptemp; -  thePtemp = proot->Plchild;BayiProot->plchild = ptemp->Prchild; thePtemp->prchild =Proot; the  -Proot->nheight = Compare (height (proot->plchild), height (proot->prchild)) +1; -Ptemp->nheight = Compare (Height (ptemp->plchild), proot->nheight) +1; the     returnptemp; the } the  the //RR Rotation -avltree* Rrrotate (avltree*proot) { theavltree*ptemp; the  thePtemp = proot->Prchild;94Proot->prchild = ptemp->Plchild; thePtemp->plchild =Proot; the  theProot->nheight = Compare (height (proot->plchild), height (proot->prchild)) +1;98Ptemp->nheight = Compare (Height (ptemp->prchild), proot->nheight) +1; About     returnptemp; - }101 102 //LR Rotation103avltree* Lrrotate (avltree*proot) {104Proot->plchild = Rrrotate (proot->plchild); the     returnllrotate (proot);106 }107 108 //RL Rotation109avltree* Rlrotate (avltree*proot) { theProot->prchild = Llrotate (proot->prchild);111     returnrrrotate (proot); the }113  the //Output Tree the voidPrinttree (avltree*proot) { the     if(null==proot)117     {118         return;119     } -     Static intn =0;121Printtree (proot->plchild);122cout << ++n <<"\ t"<< Proot->ndata <<"\ t"<< Proot->nheight <<"\ n";123Printtree (proot->prchild);124 } the 126 intMain ()127 { -avltree* Proot =NULL;129Srand ((unsignedint) Time (NULL)); the      for(inti =0; I <Ten; i++)131     { theProot = Insert (Proot, rand ()% -);133     }134 Printtree (proot);135     return 0;136}

Binary Tree Learning three: AVL tree

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.