Example #1
0
File: avl.c Project: luaohan/AVL
static void RightBalance(BSTree *T) 
{ 
    BSTree rc,rd; 
    rc = (*T)->rchild; 
    switch(rc->bf)
    { 
        case RH: 
            (*T)->bf = rc->bf = EH; 
            L_Rotate(T); 
            break; 
        case LH: 
            rd = rc->lchild; 
            switch(rd->bf) // note 
            { 
                case RH:  
                    (*T)->bf = LH; rc->bf = EH; break; 
                case EH:  
                    (*T)->bf = rc->bf = EH;  break; 
                case LH: 
                    (*T)->bf = EH; rc->bf = RH; break; 
            } 
            rd->bf = EH; 
            R_Rotate( &((*T)->rchild) ); 
            L_Rotate(T); 
    }  
}
Example #2
0
void RightBalance(TBiNode **T)
{
    TBiNode *R, *Rl;
    R = (*T)->rchild;
    switch(R->bf) {
        case RH: 
	    (*T)->bf = R->bf = EH;
	    L_Rotate(T);
	    break;
	case LH:
	    Rl = R->lchild;
	    switch(Rl->bf) {
		case RH:
		    (*T)->bf = LH;
		    R->bf = EH;
		    break;
		case EH:
		    (*T)->bf = R->bf = EH;
		    break;
		case LH:
		    (*T)->bf = EH;
		    R->bf=RH;
		    break;
	    }
	    Rl->bf = EH;
	    R_Rotate(&(*T)->rchild);
	    L_Rotate(T);
	    break;
    }
}
Example #3
0
void RightBalance(BiTree *T)
{
  BiTree R, Rl;
  R = (*T)->rchild;
  switch (R->bf) {
    case RH:
      (*T)->bf = R->bf = EH;
      L_Rotate(T);
      break;
    case LH:
      Rl = R->lchild;
      switch (Rl->bf) {
        case LH:
          (*T)->bf = RH;
          R->bf = EH;
          break;
        case EH:
          (*T)->bf = R->bf = EH;
          break;
        case RH:
          (*T)->bf = LH;
          R->bf = EH;
          break;
      }
      Rl->bf = EH;
      R_Rotate(&R);
      L_Rotate(T);
  }
}
Example #4
0
/**
*	Name...........:    static void rightBalance(AVLTreePtr *p)
*	Description....:	右平衡函数;当新结点插入到右子树导致树失衡时,调用此函数可恢复平衡
*	Param..........:	p:指向最小失衡子树指针的指针
*	Return.........:
*	Precondition...:
*	Postcondition..:    最小失衡子树恢复平衡;*p指向恢复平衡的子树
**/
static void rightBalance(AVLTreePtr *p)
{
    AVLTreePtr rc, ld;

    if (NULL == *p)
        return;
    rc = (*p)->rchild;
    switch (rc->bf)
    {
    case LH:    //右左型,需要进行双旋处理,先顺时针,再逆时针旋转
        ld = rc->lchild;
        switch (ld->bf)
        {
        case LH:
            (*p)->bf = EH;
            rc->bf = RH;
            break;
        case EH:
            (*p)->bf = rc->bf = EH;
            break;
        case RH:
            (*p)->bf = LH;
            rc->bf = EH;
            break;
        }
        ld->bf = EH;
        R_Rotate(&((*p)->rchild));
        L_Rotate(p);
        break;
    case RH:    //右右型,进行单旋处理,逆时针
        rc->bf = (*p)->bf = EH;
        L_Rotate(p);
        break;
    }
}
Example #5
0
File: avl.c Project: luaohan/AVL
static void LeftBalance(BSTree *T)
{
    BSTree lc, rd;
    lc = (*T)->lchild;
    switch(lc->bf)
    {
        case LH: //R_Rotate
            (*T)->bf = lc->bf = EH;
            R_Rotate(T);
            break;
        case RH:
            rd = lc->rchild;
            switch(rd->bf) // note
            {   
                case LH:
                    (*T)->bf = RH;  lc->bf = EH;  break;
                case EH:
                    (*T)->bf = lc->bf = EH;  break;
                case RH:
                    (*T)->bf = EH;  lc->bf = LH;  break;
            }
            rd->bf = EH;
            L_Rotate( &((*T)->lchild) );
            R_Rotate(T);
    }
}
void LeftBalance(BSTree &T)
{/* 对以指针T所指结点为根的二叉树作左平衡旋转处理,本算法结束时, */
   /* 指针T指向新的根结点。算法9.12 */
	BSTree lc, rd;
	lc = T->lchild; /* lc指向*T的左子树根结点 */
	switch (lc->bf)
	{/* 检查*T的左子树的平衡度,并作相应平衡处理 */
		case LH: /* 新结点插入在*T的左孩子的左子树上,要作单右旋处理 */
			T->bf = lc->bf = EH;
			R_Rotate(T);
			break;
		case RH:  /* 新结点插入在*T的左孩子的右子树上,要作双旋处理 */
			rd = lc->rchild; /* rd指向*T的左孩子的右子树根 */
			switch (rd->bf)
			{/* 修改*T及其左孩子的平衡因子 */
				case LH:
					T->bf = RH;
					lc->bf = EH;
					break;
				case EH:
					T->bf = lc->bf = EH;
				case RH:
					T->bf = EH;
					lc->bf = LH;
			}
			rd->bf = EH;
			L_Rotate(T->lchild); /* 对*T的左子树作左旋平衡处理 */
			R_Rotate(T); /* 对*T作右旋平衡处理 */
	}
}
Example #7
0
void LeftBalance(TBiNode **T)
{
    TBiNode *L, *Lr;
    L = (*T)->lchild;
    switch(L->bf) {
        case LH: 
	    (*T)->bf = L->bf = EH;
	    R_Rotate(T);
	    break;
	case RH:
	    Lr = L->rchild;
	    switch(Lr->bf) {
		case LH:
		    (*T)->bf = RH;
		    L->bf = EH;
		    break;
		case EH:
		    (*T)->bf = L->bf = EH;
		    break;
		case RH:
		    (*T)->bf = EH;
		    L->bf=LH;
		    break;
	    }
	    Lr->bf = EH;
	    L_Rotate(&(*T)->lchild);
	    R_Rotate(T);
	    break;
    }
}
Example #8
0
void LeftBalance(BiTree *T)
{
  BiTree L, Lr;
  L = (*T)->lchild;
  switch(L->bf) {
    case LH:
      (*T)->bf = L->bf = EH;
      R_Rotate(T);
      break;
    case RH:
      Lr = L->rchild;
      switch(Lr->bf) {
        case LH:
          (*T)->bf = RH;
          L->bf = EH;
          break;
        case EH: 
          (*T)->bf = L->bf = EH;
          break;
        case RH:
          (*T)->bf = EH;
          L->bf = LH;
          break;
      }
      Lr->bf = EH;
      L_Rotate(&L);
      R_Rotate(T);
  }
}
Example #9
0
/******************************************************************** 
* 
* RightBalance(TREE_NODE **ppNode)
* 
* 二叉树*ppNode右边偏高,失去平衡,进行右平衡操作
* 
* Returns         :  无
********************************************************************/ 
static void RightBalance(TREE_NODE **ppNode)
{
	TREE_NODE *left_child = AVL_NULL;
	TREE_NODE *right_child = AVL_NULL;
	TREE_NODE *tree_root = AVL_NULL;
	TREE_NODE *pNode = (TREE_NODE *)(*ppNode);

	tree_root = pNode->tree_root;
	right_child = pNode->right_child;
	switch(right_child->bf)
	{
	case RH_FACTOR:
		pNode->bf = right_child->bf = EH_FACTOR;
		L_Rotate(ppNode);
		break;
	case LH_FACTOR:
		left_child = right_child->left_child;
		switch(left_child->bf)
		{
		case RH_FACTOR:
			pNode->bf = LH_FACTOR;
			right_child->bf = EH_FACTOR;
			break;
		case EH_FACTOR:
			pNode->bf = right_child->bf = EH_FACTOR;
			break;
		case LH_FACTOR:
			pNode->bf = EH_FACTOR;
			right_child->bf = RH_FACTOR;
			break;
		}
		left_child->bf = EH_FACTOR;
		R_Rotate(&pNode->right_child);
		L_Rotate(ppNode);
		break;
	case EH_FACTOR:
		pNode->bf = RH_FACTOR;
		right_child->bf = LH_FACTOR;
		L_Rotate(ppNode);
		break;
	}
	(*ppNode)->tree_root = tree_root;
	if(tree_root && tree_root->left_child == pNode)
		tree_root->left_child = *ppNode;
	if(tree_root && tree_root->right_child == pNode)
		tree_root->right_child = *ppNode;
}
Example #10
0
File: avl.c Project: luaohan/AVL
static void LeftBalance_div(BSTree *p, int *shorter)
{
    BSTree  p1,p2;
    if((*p)->bf == 1){ //p缁撶偣鐨勫乏瀛愭爲楂橈紝鍒犻櫎缁撶偣鍚巔鐨刡f鍑?,鏍戝彉鐭?
        (*p)->bf = 0; 
        *shorter = 1; 

    } else if ((*p)->bf == 0){ //p缁撶偣宸︺€佸彸瀛愭爲绛夐珮锛屽垹闄ょ粨鐐瑰悗p鐨刡f鍑?,鏍戦珮涓嶅彉
        (*p)->bf = -1; 
        *shorter = 0; 
    } else {  //p缁撶偣鐨勫彸瀛愭爲楂?
        p1 = (*p)->rchild;//p1鎸囧悜p鐨勫彸瀛愭爲
        if(p1->bf == 0){  //p1缁撶偣宸︺€佸彸瀛愭爲绛夐珮,鍒犻櫎缁撶偣鍚巔鐨刡f涓?2,
            //杩涜宸︽棆澶勭悊锛屾爲楂樹笉鍙?
            L_Rotate(p);
            p1->bf = 1; 
            (*p)->bf = 1; // !!!!!!!
            *shorter = 0;
        } else if (p1->bf == -1){ //p1鐨勫彸瀛愭爲楂橈紝宸︽棆澶勭悊鍚庯紝鏍戝彉鐭?
            L_Rotate(p);
            p1->bf = (*p)->bf = 0; 
            *shorter = 1;
        } else { //p1鐨勫乏瀛愭爲楂?杩涜鍙屾棆澶勭悊(鍏堝彸鏃嬪悗宸︽棆)锛屾爲鍙樼煯
            p2 = p1->lchild;
            p1->lchild = p2->rchild; 
            p2->rchild = p1; 
            (*p)->rchild = p2->lchild; 
            p2->lchild = *p;

            if (p2->bf == 0){ 
                (*p)->bf = 0; 
                p1->bf = 0; 
            } else if (p2->bf == -1){ 
                (*p)->bf = 1;
                p1->bf = 0; 
            } else { 
                (*p)->bf = 0;
                p1->bf = -1; 
            }

            p2->bf = 0; 
            (*p) = p2; 
            *shorter = 1;
        }
    }
}
Example #11
0
void RightBalance(BSTree &T)
{
    BSTree rc,ld;
    rc=T->rchild;
    switch (rc->bf)
    {
    case LH:
        ld=rc->lchild;
        T->bf=rc->bf=ld->bf;
        R_Rotate(T->rchild);
        L_Rotate(T);
        break;
    case RH:
        T->bf=rc->bf=EH;
        L_Rotate(T);
        break;
    }

}
Example #12
0
/******************************************************************** 
* 
* LeftBalance(TREE_NODE **ppNode)
* 
* 二叉树*ppNode左边偏高,失去平衡,进行左平衡操作
* 
* Returns         :  无
********************************************************************/ 
static void LeftBalance(TREE_NODE **ppNode)
{
	TREE_NODE *left_child = AVL_NULL;
	TREE_NODE *right_child = AVL_NULL;
	TREE_NODE *tree_root = AVL_NULL;
	TREE_NODE *pNode = (TREE_NODE *)(*ppNode);

	tree_root = pNode->tree_root;               /*保存当前节点的父节点*/
	left_child = pNode->left_child;             /*保存当前节点的左子树*/
	switch(left_child->bf)
	{
	case LH_FACTOR:                             /*如果左子树的平衡因子为1,证明原始状态为左子树比右子树高*/
		pNode->bf = left_child->bf = EH_FACTOR; /*当前节点的平衡因子和左子树的平衡因子设为0*/
		R_Rotate(ppNode);  /*当前子树右旋*/
		break;
	case RH_FACTOR:                             /*如果左子树的平衡因子为-1,证明原始状态为右子树比左子树高*/
		                                        /*那么平衡因子的计算就需要根据右子树的平衡因子来计算*/
		right_child = left_child->right_child;
		switch(right_child->bf)
		{
		case LH_FACTOR:
			pNode->bf = RH_FACTOR;
			left_child->bf = EH_FACTOR;
			break;
		case EH_FACTOR:
			pNode->bf = left_child->bf = EH_FACTOR;
			break;
		case RH_FACTOR:
			pNode->bf = EH_FACTOR;
			left_child->bf = LH_FACTOR;
			break;
		}
		right_child->bf = EH_FACTOR;
		L_Rotate(&pNode->left_child);          /*将本节点的左子树进行左旋*/
		R_Rotate(ppNode);                      /*将本节点进行右旋*/
		break;
	case EH_FACTOR:                            /*左子树的平衡因子为0,表明原始状态下该子树是平衡的*/
		pNode->bf = LH_FACTOR;
		left_child->bf = RH_FACTOR;
		R_Rotate(ppNode);                     /*将本节点进行右旋*/
		break;
	}
	(*ppNode)->tree_root = tree_root;
	if(tree_root && tree_root->left_child == pNode)
		tree_root->left_child = *ppNode;
	if(tree_root && tree_root->right_child == pNode)
		tree_root->right_child = *ppNode;
}
Example #13
0
void LeftBalance(BSTree &T)
{
    BSTree lc,rd;
    lc=T->lchild;
    switch (lc->bf)//因为要平衡,所以平衡因子不可能为0
    {
    case LH:
        T->bf=lc->bf=EH;
        R_Rotate(T);
        break;
    case RH:
       T->bf=lc->bf=EH;
       L_Rotate(T->lchild);
       R_Rotate(T);
    }

}
Example #14
0
/**
*	Name...........:    static void leftBalance(AVLTreePtr *p)
*	Description....:	左平衡函数;当新结点插入到左子树导致树失衡时,调用此函数可恢复平衡
*	Param..........:	p:指向最小失衡子树指针的指针
*	Return.........:
*	Precondition...:    
*	Postcondition..:    最小失衡子树恢复平衡;*p指向恢复平衡的子树
**/
static void leftBalance(AVLTreePtr *p)
{
    AVLTreePtr  lc, rd;
    
    if (NULL == *p)
        return;

    lc = (*p)->lchild;
    switch (lc->bf)
    {
    case LH:    //LL型,需要进行右旋,顺时针
        (*p)->bf = lc->bf = EH;
        R_Rotate(p);
        break;
    case RH:    //LR型,需要双旋转
        rd = lc->rchild;
        switch (rd->bf)
        {
        case LH:
            (*p)->bf = RH;
            lc->bf = EH;
            break;
        case EH:
            (*p)->bf = lc->bf = EH;
            break;
        case RH:
            (*p)->bf = EH;
            lc->bf = LH;
            break;
        }
        rd->bf = EH;
        L_Rotate(&((*p)->lchild));  //这里不能为L_Rotate(&lc);否则修改的是lc的值,而p的左子树指针不会改变
        R_Rotate(p);
        break;
    }
}