Tree-AVL Tree

Source: Internet
Author: User

Objective

Through the previous discussion of the two-fork search tree, we know that the lower the height of the binary tree, the shorter the search time is, given the number of nodes.

When we talked about the red and black trees, we said that the red and black trees were not completely "balanced" by the two-fork tree, but were approximately "balanced". So what exactly does this balance mean? Is there a two-prong tree that is completely "balanced"?

Balanced two-pronged tree

What kind of two-fork tree can be described as a balanced binary tree?

1) Empty Tree

2) The absolute value of the difference between the left and right sub-trees of the root is not more than 1, and the subtree is a balanced binary tree.

Such a tree is regarded as a balanced binary tree.

Obviously, when a binary search tree can guarantee the above properties, the overall height of the tree can be well controlled.

   

Binary Tree Imbalance

Now that we know the definition of a balanced binary tree, we understand its many benefits. So how to build a binary search tree into a balanced binary search tree? (we first agreed that the height of the leaf node is 1.) The height of the null node is 0.)

First, let's analyze the status of the binary search tree "unbalanced" in the insert and delete operations. As shown:

  

We inserted a new node 3 on the left side of 2, so that for 1, the left subtree height is 2 and the right subtree height is 1. into an unbalanced state.

In summary, if the higher sub-tree of a node in a balanced binary tree (1 larger than the height of the other subtree) is increased by 1, or the lower subtree of one node (1 smaller than the height of the other subtree) is reduced by 1, the imbalance of the two fork tree is generated.

Below is a list of imbalances.

--generics > because it is 1 left side of the node caused by the imbalance, so called ll imbalance.

--generic > Unbalanced RR with ll imbalance.

--generics > is unbalanced due to the imbalance caused by the node on the right side of the 1 left side, so it becomes LR imbalance.

--Generic > Symmetric RL imbalance with LR imbalance.

  

Recovery operation of unbalanced binary tree

In the red-black tree, our adjustment to the imbalance is based on the color of the nodes, rotating and assigning operations to ensure an "approximate balance."

So how do you adjust a two-fork tree that is supposed to be "balanced"? Let's talk about it in a matter of circumstances.

1) ll imbalance and LR imbalance

For a ll imbalance somewhere, a right-handed operation is needed to resolve the imbalance here. Then look up to see if there are other types of imbalances.

  

The correction for RR imbalances is similar to the process of ll imbalance, where the direction of rotation changes to left-handed.

  

1template<classT>2tnode<t>* avltree<t>::__leftleftrotate (tnode<t> *dest) {                                        3ASSERT (Dest!=nil | | dest->left!=nil);4 5tnode<t> *l = dest->Left ;6Dest->left = l->Right ;7L->right =dest;8 9Dest->height = __maxheight (dest->left,dest->right) +1;TenL->height = __maxheight (l->left,l->right) +1; One  A     if(Root = =dest) -Root =l; -     returnl; the } -template<classT> -tnode<t>* avltree<t>::__rightrightrotate (tnode<t> *dest) { -ASSERT (dest!=0|| dest->right!=0); +  -tnode<t> *r = dest->Right ; +Dest->right = r->left;//between Dest and new left AR->left =dest; at  -Dest->height = __maxheight (dest->left,dest->right) +1; -R->height = __maxheight (r->left,r->right) +1; -  -     if(Root = =dest) -Root =R; in     returnR; -}
View Code

2) for LR imbalance and RL imbalance

For LR imbalance, we need to do a left-hand operation on the 2 node and then right-click on the 1 node. This will eliminate the imbalance under the 1 node. You then need to look up to see if there are other unbalanced nodes.

The handling of the RL imbalance is similar to that of LR.

1template<classT>2tnode<t>* avltree<t>::__leftrightrotate (tnode<t> *dest) {3__rightrightrotate (dest->Left );4     return__leftleftrotate (dest);5 }6template<classT>7tnode<t>* avltree<t>::__rightleftrotate (tnode<t> *dest) {8__leftleftrotate (dest->Right );9     return__rightrightrotate (dest); Ten}
View Code

Insert

Can break a lesson. The operation of the balanced binary tree is to insert a new node, and to delete an existing node. We combine the previous balance recovery method to achieve a balanced binary tree insert operation.

After you insert a node, you may break the balance of the nodes on the path through which the insert passes. Therefore, it is necessary to retrace the current balance of the nodes on the path. This is a very good place for recursion.

  

1template<classT>2tnode<t>* Avltree<t>::__insert (T val,tnode<t> *Curr) {3     if(curr==Nil) {4Tnode<t> *inch=NewTnode<t> (Val,nil,nil,1); 5Assertinch);6         if(Nil = =root)7Root =inch;8Curr =inch;9}Else if(Curr->val >val) {TenCurr->left = __insert (val,curr->Left ); One         if(Std::abs (height (curr->left)-height (curr->right)) >1){ A             if(Val < curr->left->val) { -Curr =__leftleftrotate (curr); -}Else{ theCurr =__leftrightrotate (curr); -             } -         } -}Else if(Curr->val <val) { +Curr->right = __insert (val,curr->Right ); -         if(Std::abs (height (curr->left)-height (curr->right)) >1){ +             if(Val > curr->right->val) { ACurr =__rightrightrotate (curr); at}Else{ -Curr =__rightleftrotate (curr); -             } -         } -}Else{ -std::cout<<"Duplicate Val!\n"<<Std::endl; inAssert0); -     } toCurr->height = __maxheight (curr->left,curr->right) +1; +     returnCurr;  -}
View Code

Delete

The imbalance that comes with the output of a node is similar to the imbalance that is caused by inserting a node on the other.

You still need to backtrack the nodes on the path.

  

1template<classT>2tnode<t>* avltree<t>::__erase (tnode<t> *dest,tnode<t> *currroot) {3 assert (dest);4 5     if(Currroot->val > dest->val) {6Currroot->left = __erase (dest,currroot->Left );7 8         if(Std::abs (height (currroot->left)-height (currroot->right)) >1){9             if(Height (currroot->right->right) > Height (currroot->right->Left ))TenCurrroot =__rightrightrotate (currroot); One             Else ACurrroot =__rightleftrotate (currroot); -         } -}Else if(Currroot->val < dest->val) { theCurrroot->right = __erase (dest,currroot->Right ); -         if(Std::abs (Currroot->left)-height (currroot->right) >1)){ -             if(Height (currroot->left->left) > Height (currroot->left->Right )) -Currroot =__leftleftrotate (currroot); +             Else -Currroot =__leftrightrotate (currroot); +         } A}Else{//Currroot->val = = Dest->val atTnode<t> *Replace; -         if(Currroot->left!=nil && currroot->right!=Nil) { -             if(Height (currroot->left) > Height (currroot->Right )) { -Replace =predecessor (currroot); -Currroot->val = replace->Val; -Currroot->left = __erase (replace,currroot->Left ); in}Else{ -Replace =successor (currroot); toCurrroot->val = replace->Val; +Currroot->right = __erase (replace,currroot->Right ); -             } the}Else{ *Replace = (Currroot->left!=nil)?currroot->left:currroot->Right ; $ Delete dest;Panax NotoginsengCurrroot =Replace; -         } the     } +Currroot->height = ((Currroot==nil)?0: __maxheight (Currroot->left,currroot->right) +1); A     returnCurrroot; the}
View Code

Reference

Http://www.cnblogs.com/skywang12345/p/3603935.html

Tree-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.