Balanced binary tree in C language implementation (create, insert, find, delete, rotate) "Data structure"

Source: Internet
Author: User

The Balanced Binary tree (AVL) is either an empty tree or a non-empty binary search tree with the following properties:

(1). The left and right subtrees of any node are AVL trees;

(2). The absolute value of the left and right subtree height difference of any node is not more than 1.


For any node T in a binary tree, its "balance factor" (Balance Factor, BF) is defined as a bf (t) = Hl-hr, where Hl and Hr are respectively the height of the left and right subtrees of T.


With the definition of the equilibrium factor, the absolute value of no more than 1 of the height difference of the left and right subtree of the AVL tree can be expressed as "the balance factor of any node in an AVL tree can only be evaluated in the set { -1,0,1}". This is the quantitative standard of balance.


When an AVL tree inserts or deletes a node, the balance factor of the node is probably not in the above set range, destroying the tree balance, then need to do "balanced" processing, that is, the corresponding local "rotation" adjustment, so that the adjusted tree to achieve balance.


(1). Single rotation adjustment (left, right, right)


(2). Double rotation adjustment (left-right, right-left)



Basic operations:

//=========================================================================================
// Balanced binary Tree---AVL//=========================================================================================//Define
An avltree typedef struct AVLTREENODE *avltree;
    typedef struct avltreenode{int Data;
    Avltree left;
    Avltree right;
int Height; //=========================================================================================//Balanced binary Tree insert operation (in sequence ) Avltree avl_insertion (ElementType x, Avltree T) {//inserts X into the tree T and returns the adjusted tree if (!).
        T) {t = malloc (sizeof (Avltreenode));
        T->data = X;
        t->height = 0;
    T->left = T->right = NULL;    else if (x < t->data) {T->left = Avl_insertion (x, T->left); Recursively compare and insert, update the inserted left subtree to T-left if (getheight (t->left)-getheight (T->right) = 2) if (X < T->le  Ft->data) T = singleleftrotation (t);
Zodan Else                t = doubleleftrightrotation (t);  Left-right double rotation} else if (x > T->data) {t->right = Avl_insertion (x, t->right); Recursively compare and insert, update the inserted right subtree to T->right if (getheight (t->left)-getheight (t->right) = = 2) if (X > t  >right->data) T = singleleftrotation (t); Right Tan else T = doublerightleftrotation (T); Right-left double rotation}//else X = = T->data without inserting t->height = Max (GetHeight (t->left), getheight (T->right))   + The height of 1;//tree is equal to that of the subtree plus a return T; Returns the inserted and adjusted tree}//=========================================================================================//
    Zodan algorithm Avltree singleleftrotation (Avltree a) {//NOTE: A must have a Zoozi Node B//Zodan with A and B, update the height of A and B, and return the new root node B avltree 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), GetHeight (a->height)) + 1;
return B; //=========================================================================================//left-right double rotation algorithm AVLTree Doubleleftrightrotation (Avltree a) {//NOTE: A must have a Zoozi node B, and B must have a right child node C//will be a.b and C to do a single spin two times, return the new root node c A->left = Singlerightrot   ation (A->left)//will b and C do right Tan, C is returned to return singleleftrotation (A); Zodan A with C, C is returned}//=========================================================================================// Balancing binary Tree Lookup operations Avltree Find (ElementType X, Avltree T) {if (!)
    T) return NULL;
    if (x < T->data) return find (x, t->left);
    else if (x > T->data) return Find (x, t->right);
else return T; }//=========================================================================================//Balance binary Tree Delete operation/
The deletion method is consistent with the two-fork lookup tree, and the difference is that after the deletion completes, the tree's//balance needs to be maintained up to the root node (the adjustment tree) from the deletion node's parent. Avltree Delete (ElementType X, Avltree T) {if (!) T) {//Tree is empty, error printf ("error!");
    return NULL;   } if (x < t->data) T->left = Delete (x, T->left);
    Recursively finds and deletes the specified element x else if (x > T->data) t->right = delete (x, t->right);
            else{if (t->left && t->right) {//This node contains about two subtree avltree Tmp = t->right;
            while (Tmp->left!= NULL)//Find the minimum value of the right subtree of T to fill the location of the deleted element Tmp = tmp->left;
            T->data = tmp->data; T->right = Delete (Tmp->data, t->right);//delete the displaced Tmp->data if (GetHeight (t->left)-GetHeight (T-&G T
                right) = = 2) {//T as the root node, deleted elements in the right subtree, at this time to determine the height difference between the Zuozi and the right-hand subtree is not >=2//If, then Judge Zuozi is Zodan or left-right double rotation
                If the right subtree of the t->left is longer than the left subtree, then the left-right double rotation//If the T->left Supi the right subtree is long, then the Zodan//A /A is T, B is t->left//b if C branch length (t->left-&gt ; left), theFor Zodan///If D branch Length (t->left->right), then left/right double/C D  :: if (t->left->right!= NULL) && getheight (t->left->right) > getheight (t->left->left)) doubleleftrightrotation (T);  Call left-right double rotation algorithm else singleleftrotation (T);
            Call Zodan Algorithm}}else{//This node contains one or 0 subtree avltree Tmp = T; if (!
            T->left) T = t->right; else if (!
            T->right) T = t->left;  Free (TMP);
    Release TMP}} t->height = Max (GetHeight (T->left), GetHeight (T->right));
return T; }


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.