Summary of balanced binary tree (AVL Tree)

Source: Internet
Author: User

 

I. definition overview

AVL TreeIs the first self-balancing Binary Search Tree. The height of the two Subtrees on any node in the AVL tree is the largest difference, so it is also called the height balancing tree. Search, insert, and delete are both O (logN). Adding or deleting a tree may require one or more tree rotations to rebalance the tree.

NodeBalance factorIs the height of its left subtree minus the height of its right subtree (sometimes opposite ). A node with a balance factor 1, 0, or-1 is considered to be balanced. Nodes with the equilibrium factor-2 or 2 are considered unbalanced and need to rebalance the tree. The balance factor can be directly stored in each node, or calculated from the height of the subtree that may be stored in the node.

What we see is a sort-balanced binary tree.

 

II. General Nature

AVL Tree has the following properties: it is an empty tree or its left and right subtree. The absolute value of the height difference cannot exceed 1, and both left and right subtree are a balanced binary tree. In the balanced binary search tree, we can see that its height is generally well maintained at O (log2n), and the time complexity of each operation (O (log2n )) at the same time, this greatly reduces the time complexity of operations. In addition, the formula for the node of the minimum binary balancing tree is F (n) = F (n-1) + F (n-2) + 1, which is similar to a recursive series.

 

Iii. General Operations

The basic operation of the AVL Tree generally involves the same algorithm that operates on the unbalanced Binary Search Tree. However, the so-called "AVL rotation" should be performed in advance or subsequently ".

1. Insert

You can insert a given value to the AVL Tree as if it were an unbalanced Binary Search Tree, and then fold back from the bottom to the root node, it is completed by rotating on all nodes that become unbalanced during insertion. Because there can be a maximum of 1.5 times of log on the way back to the root node.NEach AVL rotation takes a constant time, And the insert process consumes O (logN) Time

2. Delete

To delete a node from the AVL Tree, you can rotate the node to be deleted into a leaf node, and then directly cut the leaf node. Because a maximum of logs are generated during rotation to a leaf node.NNodes are rotated, and each AVL rotation consumes a constant amount of time. The deletion process consumes O (logN) Time.

3. Search

It can be performed like a common binary search tree, so O (logN) Time, because the AVL Tree is always balanced. No special preparation is required, and the tree structure will not change due to search. (This is opposite to the stretch tree search. It will change the tree structure because of the search .)

 

Iv. operations on the left and right sides

1. one-way right-hand balance processing LL: because a node is inserted in the left Tree of the left sub-tree root node of * a, the balance factor of * a is increased from 1 to 2, as a result, the subtree with * a as the root is out of balance, and a right rotation operation is required;

2. one-way left-hand balancing RR: Because the node is inserted in the right tree of the right sub-tree root node of * a, * the balance factor of a is changed from-1 to-2, as a result, the subtree with * a as the root is out of balance, and a left rotation operation is required.

 

3. bidirectional rotation (first left and then right) Balance processing LR: Because the node is inserted in the right subtree of the Left subtree of * a, * the balance factor of a is increased from 1 to 2, as a result, the subtree with * a as the root is out of balance, and the operation needs to be rotated twice (left-handed first and right-handed later ).

4. bidirectional rotation (first right and then left) Balance processing RL: Because the node is inserted in the left subtree of the right subtree of * a, * the equilibrium factor of a is changed from-1 to-2, as a result, the subtree with * a as the root is out of balance, you need to perform two rotation operations (first right and then left.

 

V. Related code implementation (the code has been modified according to network documents and has not been tested)

1. Basic struct and variables

# Define LH + 1 // left height # define EH 0 // height # define RH-1 // right height // type of the balanced binary tree struct AVLNode {int data; int bf; // The balance factor of the bf node. It can only be 0,-, which is the depth of the Left subtree minus the depth of the right subtree to obtain the struct AVLNode * lchild, * rchild; // left and right child pointer };

 

2. Right-hand operation:

Void R_Rotate (AVLNode * & p) {AVLNode * lc = p-> lchild; // lc points to the left subtree node p-> lchild = lc-> rchild of p; // The right subtree of lc is attached to the left subtree of p lc-> rchild = p; p = lc; // p points to the new root node}

 

3. Left-hand operation:

Void L_Rotate (AVLNode * & p) {AVLNode * rc = p-> rchild; // rc points to the right child root node p-> rchild = rc-> lchild of p; // rc-> lchild = p; p = rc; // p points to the new root node}

 

4. Perform the left balancing operation on the tree:

Void LeftBalance (AVLNode * & T) {AVLNode * lc, * rd; lc = T-> lchild; // switch (lc-> bf) {case LH: // 1 the new node is inserted in the left Tree of the left child of * T, and must be processed by a single right hand T-> bf = lc-> bf = EH; r_Rotate (T); break; case RH: //-1 The new node is inserted in the right tree of the left child of * T, which must be processed in double rotation. rd = lc-> rchild; // rd points to the right sub-tree root switch (rd-> bf) of the left child of * T) {// according to the effect after rotation, the right rotation of T and its left children is similar to case LH: T-> bf = RH; lc-> bf = EH; break; case EH: T-> bf = lc-> bf = EH; break; case RH: T-> bf = EH; lc-> bf = LH ;} rd-> bf = EH; L_Rotate (T-> lchild); R_Rotate (T );}}

 

5. Perform the right balancing operation on the tree:

void RightBalance(AVLNode *&T){    AVLNode *rc,*rd;    rc=T->rchild;     switch(rc->bf)    {     case RH:         T->bf=rc->bf=EH;        L_Rotate(T);        break;    case LH:        rd=rc->lchild;         switch(rd->bf)        {         case RH:             T->bf=LH;            rc->bf=EH;            break;        case EH:              T->bf=rc->bf=EH;             break;        case LH:              T->bf=EH;             rc->bf=RH;        }        rd->bf=EH;        R_Rotate(T->rchild);         L_Rotate(T);     }}

6. insert operation:

Int InsertAVL (AVLNode * & T, int data, int * taller) {if (! T) // This is the initial condition or new data has been inserted at an appropriate location {T = (AVLNode *) malloc (sizeof (AVLNode); T-> data = data; t-> lchild = T-> rchild = NULL; T-> bf = EH; * taller = 1;} else {if (data = T-> data) {* taller = 0; return 0;} if (data <T-> data) {if (! InsertAVL (T-> lchild, data, taller) return 0; if (* taller) switch (T-> bf) {case LH: // The left subtree is taller than the right subtree before insertion. After insertion, the left subtree is longer than the right subtree, and the ranking tree is out of balance (T ); // perform the right balancing operation on the sorting tree * taller = 0; break; case EH: T-> bf = LH; * taller = 1; // It indicates a long height. In fact, the absolute value of the balance factor of the parent node or grandfather node is greater than 1 break. case RH: // The height of the Left subtree is higher than that of the right subtree before insertion, after insertion, the left and right subtree depth is equal to T-> bf = EH; * taller = 0; // The flag is not long and high} else {if (! InsertAVL (T-> rchild, data, taller) return 0; if (* taller) switch (T-> bf) {// Insert the front left subtree to the height of the right subtree, after insertion, the left and right subtree depth is equal. case LH: T-> bf = EH; // The flag is not tall * taller = 0; break; case EH: // It indicates a long height. In fact, the absolute value of the balance factor of the parent or grandfather node is greater than 1 T-> bf = RH; * taller = 1; break; case RH: // The right subtree is higher than the left subtree before insertion. After insertion, the right subtree is longer and longer than the left subtree, And the sorttree is out of balance (T ); // perform the right balancing operation on the sorting tree * taller = 0 ;}} return 1 ;}

 

7. Delete ..To be continued

 

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.