/*----------------------------------------------------*/
PTree avl_insert_tree(PTree _root, int value)
{
    PTree temp = _root;
    PTree pa=NULL,pre=NULL;
    insert_tree_loop(_root,value);        //排序树插入,同二叉排序树的插入
    updata_bf(_root,value);                //更新插入节点上祖先的平衡因子
   

    #ifdef DEBUG
    inorder(_root);
    puts("");
    #endif
   

    pa = search_a(_root,value);             //找到A点,返回。
    pre = search_parent(_root,pa);        //找A点的parent节点,返回。


    if(pa == pre)//equal situation,when search root node
    {
        pa = determine(pa,pre);              //global root
        return (pa);                               //return modified node pointer
    }
    else
    {
        pa = determine(pa,pre);              //part root
        return (_root);                          //return root pointer
    }
}

/*----------------------------------------------------------------*/
void updata_bf(PTree _root,int value)
{
        while(_root)
        {
            _root->bf = height(_root->lchild) - height(_root->rchild);
            if(value < _root->data)
            {
                _root = _root->lchild;
            }   
            else
            {
                _root = _root->rchild;   
            }
        }
}

/*---------------search a----------------------------------------*/
PTree search_a(PTree _root, int value)
{
    PTree temp=_root;
    while(_root)
    {
        if(abs(_root->bf) == 2)
        {
            temp = _root;   
        }
        if(value < _root->data)
        {
            _root = _root->lchild;   
        }
        else if(value > _root->data)
        {
            _root = _root->rchild;   
        }
        else
        {
            break;
        }
    }
    return (temp);   
}

/*-----------------------------------------------------------*/
PTree search_parent(PTree _root, PTree pa)
{
    PTree temp = _root;
    while(_root)
    {
            if(pa->data == _root->data)
            {
                break;
            }
            if(pa->data < _root->data)
            {
                temp= _root;//parent node
                _root = _root->lchild;   
            }
            else
            {
                temp= _root;//parent node
                _root = _root->rchild;
            }
    }

    return (temp);//return parent node
}

/*-------------------------------------------------------------*/
PTree determine(PTree pa, PTree pre)
{
    if(pa->bf ==2 )
    {
        if(pa->lchild->bf == 1)
        {
            pa = exchang_a(pa,pre);//a case
        }
        else if(pa->lchild->bf == -1)
        {
            pa = exchang_b(pa,pre);//b case
        }
    }
    else if(pa->bf == -2)
    {
        if(pa->rchild->bf == 1)
        {
            pa = exchang_d(pa,pre);//d case
        }
        else if(pa->rchild->bf == -1)
        {
            pa = exchang_c(pa,pre);//c case
        }
    }
    return (pa);
}

/*----------------------------------------------------*/
int height(PTree _root)
{
    int ldegree,rdegree;
    if(NULL == _root)
    {
        return (0);
    }
    else
    {  ldegree = height(_root->lchild);
        rdegree = height(_root->rchild);
        if(ldegree > rdegree)
        {
            return (ldegree +1);
        }
        else
        {
            return (rdegree +1);
        }
    }
}

/*---------------case a-------------------------*/
PTree exchang_a(PTree pa, PTree parent)
{
    PTree pb=NULL,temp=NULL;
    temp = pa;
    pb = pa->lchild;
    pa->lchild = pb->rchild;
    pb->rchild = pa;
    pb->bf = 0;
    pa->bf = 0;
    if(temp != parent)//非root节点
    {   
        if(parent->lchild == temp)//link left child tree
        {
            parent->lchild = pb;    
        }
        else//link right child tree
        {
            parent->rchild = pb;
        }   
    }
    return (pb);
}//case a
/*---------------case b-------------------------*/
PTree exchang_b(PTree pa, PTree parent)
{
    PTree pb=NULL,pc=NULL,temp=NULL;
    temp = pa;
    pb = pa->lchild;
    pc = pb->rchild;
    pa->lchild = pc->rchild;
    pc->rchild = pa;
    pb->rchild = pc->lchild;
    pc->lchild = pb;
    pc->bf = 0;
    pa->bf = -1;
    pb->bf = 0;
    if(temp != parent)//非root节点
    {   
        if(parent->lchild == temp)//link left child tree
        {
            parent->lchild = pc;    
        }
        else//link right child tree
        {
            parent->rchild = pc;
        }   
    }
    return (pc);
}//case b
/*---------------case c---------------------------*/
PTree exchang_c(PTree pa, PTree parent)
{
    PTree pb=NULL,temp=NULL;
    temp = pa;
    pb = pa->rchild;
    pa->rchild = pb->lchild;
    pb->lchild = pa;
    pb->bf = 0;
    pa->bf = 0;

    if(temp != parent)//非root节点
    {
        if(parent->lchild == temp)//link left child tree
        {
            parent->lchild = pb;    
        }
        else//link right child tree
        {
            parent->rchild = pb;
        }
    }
    return (pb);   
}//case c
/*---------------case d-------------------------*/
PTree exchang_d(PTree pa, PTree parent)
{
    PTree pb=NULL,pc=NULL,temp=NULL;
    temp = pa;
    pb = pa->rchild;
    pc = pb->lchild;
    pa->rchild = pc->lchild;
    pc->lchild = pa;
    pb->lchild = pc->rchild;
    pc->rchild = pb;
    pc->bf = 0;
    pa->bf = 0;
    pb->bf = -1;
    if(temp != parent)//非root节点
    {   
        if(parent->lchild == temp)
        {
            parent->lchild = pc;    
        }
        else
        {
            parent->rchild = pc;
        }   
    }
    return (pc);
}//case d

posted on 2012-09-10 15:40  gooner  阅读(1419)  评论(0)    收藏  举报