AVL树的构建----高度平衡树实现 (C语言实现)

来源:互联网 发布:js调用摄像头拍照插件 编辑:程序博客网 时间:2024/05/15 13:06

 /*************************************************************************/
                                            构建AVL树--高度平衡
     通过旋转的方法,构建AVL树,InsertAVL()函数插入元素;当结点左边子树集
高时调用LeftBalance()修复AVL树,当结点右边子树集高时调用RightBalance()修复
AVL树。RotateLeft(),RotateRight()函数完成旋转过程。下面只写RightBalance()
和RotateLeft(),其他原理相同。
                                                                                  Leo,2006/10/9
                                                                          stefzeus@163.com

/*************************************************************************/
typedef enum Boolean
        {FALSE,TRUE}Boolean;
typedef enum BalanceFactor
        {LH,EH,RH}BalanceFactor;

typedef struct TreeNode
        {
         TreeElem        entry;
         TreeNode        *right;
         TreeNode        *left;
         BalanceFactor   bf;
        }TreeNode;


/*InsertAVL:insert newnode in AVL terr starting at the root
    pre:    The root of the AVL tree is pointed by root, and newnode is a
            new node to be inserted into the tree.
    post:   newnode has been inserted into the AVL tree with taller equal to
            TRUE if the height of the tree has increased, FALSE otherwise.
    Uses:   InsertAVL() recursively, RightBalance(), LeftBalance()      */
TreeNode *InsertAVL(TreeNode *root,TreeNode *NewNode,Boolean *taller)
{
    if(!root)
        {
         root=NewNode;
         root->right=root->left=NULL;
         root->bf=EH;
         *taller=TRUE;
        }
    else if( EQ(NewNode->entry.key,root->entry.key))
        printf("Duplicate key is not allowed in AVL tree.");
    else if( LT(NewNode->entry.key,root->entry.key))
        {
         root->left=(root->left,NewNode,taller);
         if(taller)             /* left subtree is taller */
             switch(root->bf)
                {
                    case    LH:    /* Node was left high */
                        root=LeftBalance(root,taller);
                        break;
                    case    RH:       /* Node now has balanced height */ 
                        root->bf=EH;
                        *taller=FALSE;
                        break;
                    case    EH:       /*Node is now left hight  */
                        root->bf=LH;
                        break;
                }
        }
    else{
         root->right=(root->right,NewNode,taller);
         if(taller)
            switch(root->bf)
                {
                 case   LH:    /* Node now has balanced height */ 
                    root->bf=EH;
                    *taller=FALSE;
                    break;
                 case   RH:      /* Node was right high */
                    root=RightBalance(root,taller);
                    break;
                 case   EH;       /*Node is now right hight  */
                    root->bf=RH;
                    break;
                }
        }
    return root;
}

/*RotateLeft:rotate a binary tree to the left.
    pre:     p is the root of the nonempty AVL subtree being rotated,
             and its right child is nonemtry.
    post:    The right child of p becomes the new p.The old p become the
             left child of the new p.              */
TreeNode *RotateLeft(TreeNode *p)
{
    TreeNode    *rightchild=p;
    if(!p)
        printf("It is impossible to rotate an empty tree in RotateLeft.");
    else if (!p->right)
        printf("It is impossible to make an empty subtree the root in "
               "RotateLeft.");
    else{
         rightchild=p->right;
         p->right=rightchild->left;
         rightchild->left=p;
        }
    return rightchild;
}

/*RightBalance:right balance a binary tree.
    pre:       A node of an AVL tree has become doubly unbalanced to the right.
    post:      The AVL properties have been restored.
    uses:      RotateBalance(),RotateLeft().  */
TreeNode *RightBalance(TreeNode *root,Boolean *taller)
{
    TreeNode *rs=root->right;   /* right subtree of root */
    TreeNode *ls;               /* left subtree of right subtree */

    switch(rs->bf)
        {
         case   RH:
            root->bf=rs->bf=EH;
            root=RotateLeft(root);  /* single rotation left */
            *taller=FALSE;
            break;
         case   EH:
            printf("Tree is already balanced.");
            break;
         case   LH:        /* double totation left   */
            ls=rs->left;
            switch(rs->bf)
                {
                 case   RH:
                    root->bf=LH;
                    rs->bf=EH;
                    break;
                 case   EH:
                    root->bf=rs->bf=EH;
                    break;
                 case   LH:
                    root->bf=EH;
                    rs->bf=RH;
                    break;
                }
            ls->bf=EH;
            root->right=RotateRight(rs);
            root=RotateLeft(root);
            *taller=FALSE;
        }
    return root;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑浏览器卡顿怎么办 文档里输入不了怎么办 淘宝退款卖家不处理怎么办 抵押合同丢了怎么办 抵押合同丢失了怎么办 发票货物名称多怎么办 发票上少打一个字怎么办 发票名称带星号怎么办 小贷太多还不上怎么办 生日当天买保险怎么办 我挪用公司货款怎么办 车辆改名字保险怎么办 工伤报案周六日怎么办 五菱宏光s1门下沉怎么办 新手机版本更新怎么办 戴尔电脑开机黑屏怎么办 淘宝退货不发货怎么办 铝被酸腐蚀怎么办 新娘头饰氧化了怎么办 合金饰品变黑了怎么办 麻醉机fico2升高怎么办 快递被恶意投诉怎么办 顺风快递收件人拒收怎么办 手机联系人没了怎么办 收件人号码错了怎么办 收件人写错了怎么办 快递联系不到收件人怎么办 小孩烫伤有水泡怎么办 被烫伤的疤痕怎么办 微店别人下单后怎么办 月经超了七天怎么办 收件人不在指定地址怎么办 2017款宝来熄火后异响怎么办 我身高一八米怎么办呀 规格型号错了发票怎么办 合同签错了怎么办 柜子背板起泡怎么办啊 显卡红灯不亮怎么办 小米设置全英文怎么办 小米手机变英文怎么办 阿迪贝壳头变黄怎么办