Ejemplo n.º 1
0
Status InsertAVL(BiTree *T, int e, Status *taller)
{
  if (*T) {
    if (e == (*T)->data) {
      *taller = FALSE;
      return FALSE;
    }
    if (e < (*T)->data) {
      if (!InsertAVL(&(*T)->lchild, e, taller))
        return FALSE;
      if (*taller) {
        switch((*T)->bf) {
          case LH:
            LeftBalance(T);
            *taller = FALSE;
            break;
          case EH:
            (*T)->bf = LH;
            *taller = TRUE;
            break;
          case RH:
            (*T)->bf = EH;
            *taller = FALSE;
            break;
        }
      }
    } else {
      if (!InsertAVL(&(*T)->rchild, e, taller))
        return FALSE;
      if (*taller) {
        switch ((*T)->bf) {
          case LH:
            (*T)->bf = EH;
            *taller = FALSE;
            break;
          case EH:
            (*T)->bf = RH;
            *taller = TRUE;
            break;
          case RH:
            RightBalance(T);
            *taller = FALSE;
            break;
        }
      }
    }
  } else {
    *T = (BiTree) malloc(sizeof(BiTNode));
    (*T)->data = e;
    (*T)->lchild = (*T)->rchild = NULL;
    (*T)->bf = EH;
    *taller = TRUE;
  }
  return TRUE;
}
Ejemplo n.º 2
0
int DeleteAVL(BSTree &T,float key,bool &lower)
{
	//0表示不存在该元素,1表示正常删除
	if(!T) return 0;
	else if(key==T->data)
	{
		BSTree p,q = T;
		float temp; //存放T的左子树中的最大值,作为T的新数据
		if(!T->rchild)                                 //要删除没有右子树的结点,重接左子树,并删除结点(含叶子结点)
		{
			T = T->lchild;
			free(q);
			lower = true;
		}
		else if(!T->lchild)                            //要删除没有左子树的结点,重接右子树,并删除结点(含叶子结点)
		{
			T = T->rchild;
			free(q);
			lower = true;
		}
		else                                //左右子树都有,找*p左子树的最大值作为新的根结点,将p变为叶子结点删除                    
		{
			p = T->lchild;
			while(p->rchild)
			{
				p = p->rchild;
			}
			temp = p->data;
			DeleteAVL(T,temp,lower);      //temp的值一定存在,且为叶子结点
			q->data = temp;
		}
	}//else_if
	else if(key<T->data) //在左子树中继续删除
	{
		if(!DeleteAVL(T->lchild,key,lower)) return 0;
		if(lower&&--T->bf<=-2)
		{
			RightBalance(T);
			lower = false;
		}//if_lower 相当于T的右子树新插入一个结点引发的不平衡状态

	}//else_if
	else //在右子树中继续删除
	{
		if(!DeleteAVL(T->rchild,key,lower)) return 0;
		if(lower&&++T->bf>=2)
		{
			LeftBalance(T);
			lower = false;
		}//if_lower 相当于T的左子树新插入一个结点引发的不平衡状态

	}//else

	return 1;//正常删除
}
Ejemplo n.º 3
0
bool InsertAVL(TBiNode **T, int e, bool *taller)
{
    if(!*T) {
	*T = (TBiNode *)malloc(sizeof(TBiNode));
	(*T)->data = e;
	(*T)->lchild = (*T)->rchild = NULL;
	(*T)->bf = EH;
	*taller = true;
    } else {
	if(e == (*T)->data) {
	    *taller = false;
	    return false;
	} else if (e < (*T)->data) {
	    if(!InsertAVL(&(*T)->lchild, e, taller))
		return false;
 	    if(taller) {
		switch((*T)->bf) {
		    case LH:
			LeftBalance(T);
			*taller = false;
			break;
		    case EH:
			(*T)->bf = LH;
			*taller = true;
			break;
		    case RH:
			(*T)->bf = EH;
			*taller = false;
			break;
		}
	    }
	} else {
	    if(!InsertAVL(&(*T)->rchild, e, taller))
		return false;
 	    if(taller) {
		switch((*T)->bf) {
		    case RH:
			RightBalance(T);
			*taller = false;
			break;
		    case EH:
			(*T)->bf = RH;
			*taller = true;
			break;
		    case LH:
			(*T)->bf = EH;
			*taller = false;
			break;
	        }
            }
	}
    }
    return true;	
}
Ejemplo n.º 4
0
int DeleteAVL(BST &T,int x,int &shorter)
{
    int k;
    BST q;
    if(T==NULL)
    {
        printf("there is no tree");
        return 0;
    }
    else if(x<T->data.key)
    {
        k=DeleteAVL(T->lchild,x,shorter);
        if(shorter==1)
            LeftBalance(T,shorter);
        return k;
    }
    else if(x>T->data.key)
    {
        k=DeleteAVL(T->rchild,x,shorter);
        if(shorter==1)
        {
            RightBalance(T,shorter);
        }
        return k;
    }
    else
    {
        q=T;
        if(T->rchild==NULL)
        {
            T=T->lchild;
            delete q;
            shorter=1;
        }
        else if(T->lchild==NULL)
        {
            T=T->rchild;
            delete q;
            shorter=1;
        }
        else
        {
            Delete(q,q->lchild,shorter);
            if(shorter==1)
            {
                LeftBalance(T,shorter);
            }
            T=q;
        }
        return 1;


    }
}
Ejemplo n.º 5
0
 void Delete(BST q,BST &r,int &shorter)
 {
     if(r->rchild==NULL)
     {
         q->data=r->data;
         q=r;
         r=r->lchild;
         delete q;
         shorter=1;
     }
     Delete(q,r->rchild,shorter);
     if(shorter==1)
       RightBalance(r,shorter);
 }
Ejemplo n.º 6
0
Status InsertAVL(BSTree &T, ElemType e, Boolean &taller) { // 算法9.11
  // 若在平衡的二叉排序树T中不存在和e有相同关键字的结点,
  // 则插入一个数据元素为e的新结点,并返回1,否则返回0。
  // 若因插入而使二叉排序树失去平衡,则作平衡旋转处理,
  // 布尔变量taller反映T长高与否
  if (!T) {  // 插入新结点,树"长高",置taller为TRUE
    T = (BSTree) malloc (sizeof(BSTNode));  T->data = e;
    T->lchild = T->rchild = NULL;  T->bf = EH;  taller = TRUE;
  }
  else {
    if (EQ(e.key, T->data.key))    // 树中已存在和e有相同关键字的结点
      { taller = FALSE;  return 0; } // 则不再插入
    if (LT(e.key, T->data.key)) {    // 应继续在*T的左子树中进行搜索
      if (InsertAVL(T->lchild, e, taller)==0) return 0;    // 未插入
      if (taller)  // 已插入到*T的左子树中且左子树"长高"
        switch (T->bf) {   // 检查*T的平衡度
          case LH:   // 原本左子树比右子树高,需要作左平衡处理
             LeftBalance(T);   taller = FALSE;  break;
          case EH:   // 原本左、右子树等高,现因左子树增高而使树增高
             T->bf = LH;  taller = TRUE;  break;
          case RH:   // 原本右子树比左子树高,现左、右子树等高
             T->bf = EH;  taller = FALSE;  break;   
        } // switch (T->bf)
    } // if
    else {    // 应继续在T↑的右子树中进行搜索
      if (InsertAVL(T->rchild, e, taller)==0) return 0;
      if (taller)         // 已插入到*T的右子树且右子树长高
        switch (T->bf) {  // 检查*T的平衡度
          case LH:   // 原本左子树比右子树高,现左、右子树等高
             T->bf = EH;  taller = FALSE;  break;   
          case EH:   // 原本左、右子树等高,现因右子树增高而使树增高
             T->bf = RH;  taller = TRUE;  break;
          case RH:   // 原本右子树比左子树高,需要作右平衡处理
             RightBalance(T);  taller = FALSE;  break;
        } // switch (T->bf)
    } // else
  } // else 
  return 1;
} //InsertAVL
Ejemplo n.º 7
0
Status InsertAVL(BSTree &T,ElemType e,Boolean &taller)
{
    if(!T)
    {
        T=(BSTNode*)malloc(sizeof(BSTNode));
        if(!T)exit(0);
        T->data=e;
        T->lchild=T->rchild=NULL;
        taller=TRUE;
        T->bf=EH;
    }
    else
    {
        if(e==T->data)//子树没有增长
        {
            taller=FALSE;
            return FALSE;
        }
        else if(e<T->data)
        {
            if(InsertAVL(T->lchild,e,taller))
                if(taller)
                {
                    switch (T->bf)
                    {
                    case LH:
                        LeftBalance(T);
                        taller=FALSE;
                        break;
                    case EH:
                        T->bf=LH;
                        taller=TRUE;
                        break;
                    case RH:
                        T->bf=EH;
                        taller=FALSE;
                        break;
                    }

                }
        }
        else
        {
            if(InsertAVL(T->rchild, e, taller))
                if(taller)
                {
                    switch(T->bf)
                    {
                    case LH:
                        T->bf = EH;
                        taller = FALSE;
                        break;

                    case EH:
                        T->bf = RH;
                        taller = TRUE;
                        break;

                    case RH:
                        RightBalance(T);
                        break;
                    }
                }
        }
    }
    return OK;
}
Ejemplo n.º 8
0
Archivo: avl.c Proyecto: luaohan/AVL
int InsertAVL(BSTree *T, ElemType *e, int *taller)
{
    if (*T == NULL)
    {
        *T = (BSTree)malloc( sizeof(BSTNode) );
        if (*T == NULL)
        {
            return ERROR;
        }

        (*T)->data = *e;
        (*T)->lchild = (*T)->rchild = NULL;
        (*T)->bf = EH;

        *taller = TRUE;
    } 
    else 
    {
        if (*e == (*T)->data)
        {
            *taller = FALSE;
            return FALSE;
        }

        if (*e < (*T)->data) //insert to T->lchild
        {
            if (InsertAVL( &((*T)->lchild), e, taller) == -1) // no insert
            {
                return FALSE;
            }

            //inserted
            if (*taller == TRUE) //taller
            {
                switch((*T)->bf)
                {
                    case LH:
                        LeftBalance(T);  *taller = FALSE;  break;
                    case EH:
                        (*T)->bf = LH;  *taller = TRUE;  break;
                    case RH:
                        (*T)->bf = EH;  *taller = FALSE;  break;
                }
            }
        } 
        else  //insert to T->rchild
        {
            if (InsertAVL( &((*T)->rchild), e, taller) == -1) //no insert
            {
                return FALSE;
            }

            //inserted
            if (*taller == TRUE)
            {
                switch ((*T)->bf)
                {
                    case LH:
                        (*T)->bf = EH;  *taller = FALSE;  break;
                    case EH:
                        (*T)->bf = RH;  *taller = TRUE;  break;
                    case RH:
                        RightBalance(T);  *taller = FALSE;  break;
                }
            }
        }
    }

    return OK;
}
Ejemplo n.º 9
0
Archivo: avl.c Proyecto: luaohan/AVL
int DeleteAVL(BSTree *T, int key, int *shorter){  
    if (*T == NULL)  
    {//No such key  
        *shorter = FALSE;  
        return 0;  
    }  
    else  
    {  
        if (key == (*T)->data)    //鎵惧埌浜嗛渶瑕佸垹闄ょ殑缁撶偣  
        {  
            //濡傛灉璇ョ粨鐐圭殑lchild 鍜? 
            //rchild 鑷冲皯鏈変竴涓负NULL  
            //鍒欏ぇ鍔熷憡鎴愶紝鍚﹀垯璇峰弬鐓? 
            //涓嬫柟瑙i噴  
            BSTree q = *T;  
            if ((*T)->lchild == NULL)//濡傛灉璇ョ粨鐐圭殑lchild 涓篘ULL  
            {  
                (*T) = (*T)->rchild;  
                free(q);  
                *shorter = TRUE;  
                return 1;  
            }  
            else if ((*T)->rchild == NULL){//濡傛灉璇ョ粨鐐圭殑rchild 涓?NULL  
                (*T) = (*T)->lchild;//濡傛灉涓嶆槸&锛堝紩鐢級鐨勫己澶у姛鑳斤紝杩欏彞璇濇槸娌℃湁鎰忎箟鐨? 
                free(q);  
                *shorter = TRUE;  
                return 1;  
            }  
            else {  
                //鍒犻櫎涓€涓乏鍙冲瀛愰兘涓嶄负绌虹殑缁撶偣  
                //浣胯缁撶偣鐨勭洿鎺ュ墠椹眕鐨刣ata鏇挎崲璇ョ粨鐐圭殑data  
                //鐒跺悗鏀瑰彉key=p.data  
                BSTree s = (*T)->lchild;  
                while (s->rchild)  
                    s = s->rchild;  
                (*T)->data = s->data;  
                key = s->data;//Now, delete the vertice whose data was the new key  
            }  
        }  
        if (key < (*T)->data){//杩欓噷涓嶪nsertAVL涓嶅悓  
            if (!DeleteAVL(&((*T)->lchild), key, shorter)) return 0;  
            if (*shorter == TRUE){  
                switch((*T)->bf){  
                    case LH:(*T)->bf = EH; *shorter = TRUE;break;  
                    case EH:(*T)->bf = RH; *shorter = FALSE;break;  
                    case RH:RightBalance(T);   
                            if ((*T)->rchild->bf == EH)  
                                *shorter = FALSE;  
                            else   
                                *shorter = TRUE;break;  
                }  
            }  
        }  
        else{  
            if (!DeleteAVL(&((*T)->rchild), key, shorter)) return 0;  
            if (*shorter == TRUE){  
                switch((*T)->bf){  
                    case LH:LeftBalance(T);  
                            if ((*T)->lchild->bf == EH)  
                                *shorter = FALSE;  
                            else   
                                *shorter = TRUE;break;  
                    case EH:(*T)->bf = LH; *shorter = FALSE;break;  
                    case RH:(*T)->bf = EH; *shorter = TRUE;break;  
                }  
            }  
        }  
    }  
    return 1;  
}//Delete  
Ejemplo n.º 10
0
/*****************************************************************************************
*      int avlTreeInsert
*	(
*	tAVLTree *pTree ,      //树结构的指针
*	TREE_NODE **ppNode ,  //待插入节点所在的子树的指针的指针
*	TREE_NODE *pInsertNode,  //待插入的节点
*	int *growthFlag  //子树是否长高的标志 *growthFlag=1表示长高1层 *growthFlag=0表示没有
*	)
* 
*   将一个节点插入一颗子树之中,插入过程之中可能导致子树不
*  平衡,此函数还将执行递归平衡操作,直到所有子树均平衡为止
* 
* Returns         :  1:成功
*                         0:失败
******************************************************************************************/ 
static int avlTreeInsert
(
 tAVLTree *pTree , 
 TREE_NODE **ppNode , 
 TREE_NODE *pInsertNode,
 int *growthFlag
 )
{
	int compFlag = 0;
	TREE_NODE *pNode = (TREE_NODE *)(*ppNode);

	if(pTree->count == 0)
	{
		pTree->pTreeHeader = pInsertNode;
		pInsertNode->bf = EH_FACTOR;
		pInsertNode->left_child = pInsertNode->right_child = AVL_NULL;
		pInsertNode->tree_root = AVL_NULL;
#ifdef ORDER_LIST_WANTED
		pTree->pListHeader = pTree->pListTail = pInsertNode;
		pInsertNode->prev = pInsertNode->next = AVL_NULL;
#endif
		return 1;
	}

	compFlag = ((*pTree->keyCompare)(pNode , pInsertNode));
	if(!compFlag)
	{
		*growthFlag = 0;
		return 0;
	}

	if(compFlag < 0)
	{
		if(!pNode->left_child)
		{
			pNode->left_child = pInsertNode;
			pInsertNode->bf = EH_FACTOR;
			pInsertNode->left_child = pInsertNode->right_child = AVL_NULL;
			pInsertNode->tree_root = (TREE_NODE *)pNode;
#ifdef ORDER_LIST_WANTED
			orderListInsert(pTree,pNode, pInsertNode, INSERT_PREV);
#endif
			switch(pNode->bf)
			{
			case EH_FACTOR:
				pNode->bf = LH_FACTOR;
				*growthFlag = 1;
				break;
			case RH_FACTOR:
				pNode->bf = EH_FACTOR;
				*growthFlag = 0;
				break;
			}
		}
		else
		{
			if(!avlTreeInsert(pTree, &pNode->left_child,pInsertNode, growthFlag))
				return 0;

			if(*growthFlag)
			{
				switch(pNode->bf)
				{
				case LH_FACTOR:
					LeftBalance(ppNode);
					*growthFlag = 0;
					break;
				case EH_FACTOR:
					pNode->bf = LH_FACTOR;
					*growthFlag = 1;
					break;
				case RH_FACTOR:
					pNode->bf = EH_FACTOR;
					*growthFlag = 0;
					break;
				}
			}
		}
	}

	if(compFlag > 0)
	{
		if(!pNode->right_child)
		{
			pNode->right_child = pInsertNode;
			pInsertNode->bf = EH_FACTOR;
			pInsertNode->left_child = pInsertNode->right_child = AVL_NULL;
			pInsertNode->tree_root = (TREE_NODE *)pNode;
#ifdef ORDER_LIST_WANTED
			orderListInsert(pTree,pNode, pInsertNode, INSERT_NEXT);
#endif
			switch(pNode->bf)
			{
			case EH_FACTOR:
				pNode->bf = RH_FACTOR;
				*growthFlag = 1;
				break;
			case LH_FACTOR:
				pNode->bf = EH_FACTOR;
				*growthFlag = 0;
				break;
			}
		}
		else
		{
			if(!avlTreeInsert(pTree, &pNode->right_child,pInsertNode, growthFlag))
				return 0;

			if(*growthFlag)
			{
				switch(pNode->bf)
				{
				case LH_FACTOR:
					pNode->bf = EH_FACTOR;
					*growthFlag = 0;
					break;
				case EH_FACTOR:
					pNode->bf = RH_FACTOR;
					*growthFlag = 1;
					break;
				case RH_FACTOR:
					RightBalance(ppNode);
					*growthFlag = 0;
					break;
				}
			}
		}
	}

	return 1;
}
Ejemplo n.º 11
0
/******************************************************************** 
* 
* avlDelBalance(tAVLTree *pTree , TREE_NODE *pNode,int L_R_MINUS)
* 
* 删除节点之后,二叉树可能已经不平衡了,此时需要用
* 此函数来实现删除节点之后的平衡操作。
* 子树自平衡的过程中,可能出现一种情况:那就是子树自身平衡了,但是
* 破坏了父亲的平衡性,所以此函数做了递归平衡操作,能够使最小不平衡
* 子树之上的所有祖先节点都能够平衡。
* 最坏可能的情况就是从最小不平衡字数的树根一直到整个大树的树根节点
* 之间的所有子树都不平衡,不过这种概率很低,一般来说递归最多三次就
* 可以实现整个树的平衡
* pTree 		:  二叉树指针
* pNode		:  最小不平衡子树的根节点
* L_R_MINUS	:  
*			LEFT_MINUS    -- 左边失去平衡,树高减少了1层
*                      RIGHT_MINUS  -- 右边失去平衡,树高减少了1层
*
* Returns         :  无
******************************************************************/ 
static int avlDelBalance
(
 tAVLTree *pTree , 
 TREE_NODE *pNode,
 int L_R_MINUS
 )
{
	TREE_NODE *tree_root = AVL_NULL;

	tree_root = pNode->tree_root;
	if(L_R_MINUS == LEFT_MINUS)
	{
		switch(pNode->bf)
		{
		case EH_FACTOR:
			pNode->bf = RH_FACTOR;
			break;
		case RH_FACTOR:
			RightBalance(&pNode);
			if(!tree_root)
				pTree->pTreeHeader = pNode;
			if(pNode->tree_root && pNode->bf == EH_FACTOR)
			{
				if(pNode->tree_root->left_child == pNode)
					avlDelBalance(pTree , pNode->tree_root , LEFT_MINUS);
				else
					avlDelBalance(pTree , pNode->tree_root , RIGHT_MINUS);
			}
			break;
		case LH_FACTOR:
			pNode->bf = EH_FACTOR;
			if(pNode->tree_root && pNode->bf == EH_FACTOR)
			{
				if(pNode->tree_root->left_child == pNode)
					avlDelBalance(pTree , pNode->tree_root , LEFT_MINUS);
				else
					avlDelBalance(pTree , pNode->tree_root , RIGHT_MINUS);
			}
			break;
		}
	}

	if(L_R_MINUS == RIGHT_MINUS)
	{
		switch(pNode->bf)
		{
		case EH_FACTOR:
			pNode->bf = LH_FACTOR;
			break;
		case LH_FACTOR:
			LeftBalance(&pNode);
			if(!tree_root)
				pTree->pTreeHeader = pNode;
			if(pNode->tree_root && pNode->bf == EH_FACTOR)
			{
				if(pNode->tree_root->left_child == pNode)
					avlDelBalance(pTree , pNode->tree_root , LEFT_MINUS);
				else
					avlDelBalance(pTree , pNode->tree_root , RIGHT_MINUS);
			}
			break;
		case RH_FACTOR:
			pNode->bf = EH_FACTOR;
			if(pNode->tree_root && pNode->bf == EH_FACTOR)
			{
				if(pNode->tree_root->left_child == pNode)
					avlDelBalance(pTree , pNode->tree_root , LEFT_MINUS);
				else
					avlDelBalance(pTree , pNode->tree_root , RIGHT_MINUS);
			}
			break;
		}
	}

	return 1;
}