Implementation of the AVL tree (C language Implementation)

Source: Internet
Author: User

Recently the data structure of the bad, but holding a little niece really review not go in ... That's the end of the tree.


There was no systematic understanding of the balance tree at the time.


Specific gratitude is not affixed, carefully say how to adjust the balance of the binary tree


If the insertion breaks the original balance, the "Trouble Junction" is called the RR Insert, which is on the right side of the right subtree of the discovery, requiring RR rotation (right paddle)

Make the following adjustments



"Trouble knot" on the left side of the left subtree of the found person, thus called ll Insert, need ll to rotate (left paddle)



"Trouble knot" on the right side of the left subtree, thus called LR insertion, requires LR rotation




"Trouble knot" on the left side of the right subtree, so called RL Insert, need RL rotation



Each insert as long as according to the above situation to make the corresponding adjustment is good ~

The implementation code for AVL is attached below:


typedef struct AVLTREENODE *avltree;typedef struct avltreenode{elementtype Data; Avltree left; Avltree Right;int Height; }; Avltree avl_insertion (ElementType x,avltree t) {{/* * {* * * insert X into the Avlavl tree T and return the adjusted AVLAVL tree */if (! T) {if (! T) {/* If an empty tree is inserted, a new tree */t= (avltree) malloc (sizeof (struct avltreenode) with one node is created); T->date = X; t->height = 0; T->left = T->right =null; }else if (x<t->date) {//Inserts the left subtree of T t->left = Avl_insertion (X,t->left); if (Gerheight (t->left)-getheight (t >right) ==2)//need to turn left if (x<t->left->data) t=singleleftrotation (t);//Zodan elset= doubleleftright (t);//left/right double-spin} else if (x>t->date) {//Insert right subtree of T t->right =avl_insertion (x,t->right); if (GetHeight (t->left)-getheight ( T->right) ==-2)//need to turn right if (x>t->right->data) t=singlerightrotation (T);//Right Paddle elset= Doublerightleftrotation (T);//Right-left double-spin}//else X = = T->data No need to insert T->height = Max (GetHeight (T->left), getheight (t >right)) +1;return T; }avltree singleleftrotation (Avltree a) {//a must have a left dial hand node b//make A and B Zodan, moreNew AB height, returns the new root node bavltree B = a->left; A->left = b-> right; b-> right = A; A-Height = Max (GetHeight (A->left), GetHeight (a->right)) +1; B->height = Max (GetHeight (b->left), a->height) +1;return B;} Avltree doubleleftrightrotation (Avltree A) {//a must have a left dial hand node B, and B must have a right child node c//will AB and C do two radio, return the new node Ca->left = Singlerightrotation (a->left);//Make BC right paddle, return Creturn singleleftrotation (A);//Make AC Zodan, C return}



Implementation of the AVL tree (C language Implementation)

Related Article

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.