Implementation and analysis of the insertion and deletion lookup algorithm for AVL Trees-2 (tree height method)

Source: Internet
Author: User
Tags cdata

In the previous article (AVL tree Insert Delete lookup algorithm implementation and analysis-1 (equilibrium factor method)), this paper introduces how to use the balance factor to record the height difference of the left and right sub-tree to realize the algorithm of inserting and deleting the AVL tree, and analyzes some defects of this method, here, I'm going to use another method to implement these algorithms, and personally feel that the implementation of the previous article is simpler and more clear-minded.


Before introducing this approach, let's talk about how to find the height (or depth) of a binary tree. Its code and explanation are as follows:

int bitreedepth (Bitree BT)
{
    ///Seek the depth of the tree//
    from the definition of two-fork tree depth, the depth of the binary tree should be the maximum of its left and right subtree depth plus 1,
    //and the root node is calculated as the No. 0 layer, and the height is 0. If
    (BT = = null)  //If it is an empty tree, return-1
        return-1;
    else
    {
        int nldepth = bitreedepth (bt->lchild);//The depth of the left tree
        int nrdepth = bitreedepth (bt->rchild);// Find the depth of the right tree
        if (nldepth >= nrdepth)
        {
            return nldepth+1;
        }
        else
        {
            return nrdepth+1;}}
}
Understand how to ask for the height of the tree, the following is the beginning of the AVL tree Insert Delete and find the solution of the algorithm:

Type is defined as:

typedef int BOOL;
typedef char DataType;

typedef struct AVLNODE
{
    DataType Data;      Data value
    struct Avlnode *lchild;//Pointer to Zuozi
    struct avlnode *rchild;//Pointer to right subtree
    int height;//Record the height of the tree with this node root
} Avlnode, *avltree;


Some of the auxiliary functions are used, as follows:

static int Max (int a, int b) {return (a > B? a:b);}//------------------------------------------------------
    ----------------------static int Height (Avltree at) {if (at = = NULL) return-1;
Return at->height; }//--------------------------------------------------------------------------Avltree GetParent (Avltree at, DataType x) {//Return the parent node of the node with a value of X if (! at | |
    At->data = = x) return NULL;
            if ((at->lchild && at->lchild->data = = x) | |
        (at->rchild && At->rchild->data = = x))
    return at;
    else if ((At->lchild && x < at->lchild->data)) return GetParent (At->lchild, x);
else return GetParent (At->rchild, x); }//--------------------------------------------------------------------------avlnode* findmin (Avltree at) {//
        Find Minimum node if (at) {while (at->lchild) {at = at->lchild; }} return at;
} 

The code and explanation of its rotation are as follows:

Static Avltree Singlerotatewithright (Avltree at) {//Right single rotation if (!
    at) return NULL;
    Avltree x = at->rchild;
    At->rchild = x->lchild;


    X->lchild = at;
    At->height = Max (height (at->lchild), height (at->rchild)) +1;
    X->height = Max (Height (x->rchild), at->height) +1;
Returns the new root return x; }//--------------------------------------------------------------------------static Avltree Singlerotatewithleft
(Avltree at) {//Left single rotation if (!
    at) return NULL;
    Avltree x = at->lchild;
    At->lchild = x->rchild;

    X->rchild = at;
    At->height = Max (height (at->lchild), height (at->rchild)) +1;
    X->height = Max (Height (x->lchild), at->height) + 1;
Returns the new root return x; }//--------------------------------------------------------------------------------static Avltree Doublerotatewithright (Avltree at) {//Right double rotation, returns the new root if (at->lchild! = NULL) {at->lchild = Singlero TaTewithleft (At->lchild);
    Return Singlerotatewithright (at); }}//--------------------------------------------------------------------------static Avltree Doublerotatewithleft (Avltree at) {///double rotation, returns the new root if (at->rchild! = NULL) {at->rchild = Singlero
        Tatewithright (At->rchild);
    Return Singlerotatewithleft (at); }
}

All the preparation is good, here is our topic: INSERT, Delete and find, its code and explanation are as follows:

avlnode* Insertnode (Avltree at, DataType x) {//If x does not exist, insert tree if (at = = NULL) {at = (Avltree) malloc (siz
        EOF (Avlnode));
        if (at = = null) {//insert failed return null;
            } else {//Initialize new node At->data = x;
            At->lchild = NULL;
            At->rchild = NULL;
        at->height = 0; }} else {if (x < At->data) {//insert in left subtree At->lchild = Inser

            Tnode (At->lchild, x); if (height (at->lchild)-height (at->rchild) = = 2) {//Loss of imbalance if (X < A T->lchild->data) {//if inserted in left subtree, then single left rotation at = Singlerotatewithle
                ft (at); } else {//if inserted in right subtree, double left rotation at = DOUBLEROTATEWITHL
                EFT (at);
    }
            }    } else if (x > At->data) {//insert in right subtree At->rchild = Insertnode (at-&gt ;

            Rchild, x); if (height (at->rchild)-height (at->lchild) = = 2) {//Loss of imbalance if (x > A T->rchild->data) {//if inserted in right subtree, then single right rotation at = Singlerotatewithri
                Ght (at); } else {//if inserted in left subtree, double right rotation at = Doublerotatewithri
                Ght (at); }}}}//After inserting, recalculate the tree height and return the new tree's root pointer at->height = Max (height (at->lchild), Height (at->
    rchild)) +1;
return at; }//----------------------------------------------------------------------------------------avlnode* Deletenode (
    Avltree at, DataType x) {///if empty, returns NULL, otherwise returns the root of the deleted node (at = = null) {return null; } if (x = = At->data) {//Find the node AV you want to deleteLtree Min = findmin (at->rchild);
            if (Min! = NULL) {//right subtree exists at->data = min->data; if (Min! = at->rchild) {//at->rchild exists left subtree avltree Parent = GetParent (at-
                >rchild, Min->data);
            Parent->lchild = min->rchild;
            } else {//at->rchild does not exist left subtree At->rchild = min->rchild;
            }} else {//right subtree does not exist Min = at;
        at = at->lchild;
    } free (Min);
        } else if (x < At->data) {//delete in its left subtree At->lchild = Deletenode (At->lchild, x);
            if (height (at->rchild)-height (at->lchild) = = 2) {//delete after loss of balance if (At->rchild) {//If the Supi right subtree of at->rchild is high after deletion, the left double rotation if (Height (At->rchild->lchild) > Height(At->rchild->rchild))
                at = Doublerotatewithleft (at);
            else//Otherwise, the left single rotation at = Singlerotatewithleft (at); }}} else if (x > At->data) {//delete in its right subtree At->rchild = Deletenode (AT-&GT;RC
        Hild, x);
            if (height (at->lchild)-height (at->rchild) = = 2) {//delete after loss of balance if (At->lchild) {//If the right subtree of at->lchild is higher than the left subtree after deletion, the right double rotation if (Height (at->lchild->rchild) > He
                Ight (at->lchild->lchild)) at = Doublerotatewithright (at);
            else//Otherwise, right single rotation at = Singlerotatewithright (at); }}}//recalculates the depth of the at and returns the root pointer of the deleted tree if (at! = NULL) {at->height = Max (Height (At->lchild)
    , Height (At->rchild)) +1;
} return at; }

//--------------------------------------------------------------------------------Avltree Findatnode (Avltree at, DataType c) {if (!
    at) return NULL;
    else if (at->cdata = = c)//Find returns its pointer return at;
    else if (c < at->cdata) {//Find in its left subtree         return Findatnode (At->lchild, C);
    } else {//Find in its right subtree         return Findatnode (At->rchild, C); }
}

Algorithm Analysis:

1, this realization method is I feel better than the realization method, the idea is clearer, the code is easier to understand than the previous article.

2, this implementation method in the case of no reference type, this method as mentioned in the previous article, do not need to use double pointers, reduce the chance of error, but also make the code easier to read. It changes the value of the pointer variable by returning a pointer to the root of the tree and assigning the value to the original pointer variable. and reduces the number of switch...case statements that appear in the previous algorithm.

3, as to the time complexity and space complex with the previous one in the implementation of the algorithm is the same, all the time complexity of the algorithm are log2n.


If the algorithm has errors, also hope that readers point out.

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.