static int handleAvlNodeBf(avlNode_t* e,int type) { bbTree_t* bbt = (bbTree_t*) e->bbtRoot; if( NULL == e) return -1; return 0; switch(e->bf) { case 0: // equal. printf("Equal===> set Node Bf=(%d)\n",type); e->bf = type;// left 1, right 2 break; case 1: // left high if(type == 1) // left high, insert left, need leftBalance & after balance equal. leftBalance(e); else // Left high ,insert right... so hight equal. e->bf = 0; bbt->isTaller = 0; break; case 2: // right high if(type == 1) e->bf = 0; else rightBallance(e); bbt->isTaller = 0; break; default: return -1; break; } return 0; }
/** * Name...........: static Status deleteAVLTree_Interface(AVLTreePtr *t, KeyType key, Status *shorter); * Description....: 在平衡二叉树删除指定关键字 * Param..........: t:指向平衡二叉树指针的指针 key:要删除的指定关键字 shorter:标志值,调用此函数只需传入一个类型为Status的任意值变量即可 * Return.........: TRUE:删除成功 FALSE:删除失败 * Precondition...: * Postcondition..: 若删除成功,*t将指向一棵删除了结点的平衡二叉树;若删除失败,*t(平衡二叉树)不会改变 **/ static Status deleteAVLTree_Interface(AVLTreePtr *t, KeyType key, Status *shorter) { AVLTreePtr p,q; if (NULL == *t) return FALSE; if (key == (*t)->data.key) { *shorter = TRUE; if (NULL != (*t)->lchild && NULL != (*t)->rchild) { p = (*t); q = p->lchild; //循环不变量:p是q的前驱 while (NULL != q->rchild) { p = q; q = q->rchild; } //q指向待删结点,为左子树最大值 (*t)->data = q->data; if (p != *t) p->rchild = q->lchild; //重接p的右子树 else p->lchild = q->lchild; //重接p的左子树 free(q); } else //此结点小于2个孩子 { p = *t; if (NULL != p->lchild) *t = (*t)->lchild; else *t = (*t)->rchild; free(p); } } else if (key < (*t)->data.key) { if (FALSE == deleteAVLTree_Interface(&((*t)->lchild), key, shorter)) return FALSE; if (TRUE == *shorter) { switch ((*t)->bf) { case LH: (*t)->bf = EH; //原左高后等高 *shorter = TRUE;//总体高度小1 break; case EH: (*t)->bf = RH; //原等高后右高 *shorter = FALSE;//高度总体不变 break; case RH: //LR型 leftBalance(t); *shorter = FALSE; break; } } } else { if (FALSE == deleteAVLTree_Interface(&((*t)->rchild), key, shorter)) return FALSE; if (TRUE == *shorter) { switch ((*t)->bf) { case LH: rightBalance(t); *shorter = FALSE; break; case EH: (*t)->bf = LH; *shorter = FALSE;//高度总体不变 break; case RH: (*t)->bf = EH; *shorter = TRUE;//总体高度小1 break; } } } return TRUE; }
/** * Name...........: static Status insertAVLTree_Interface(AVLTreePtr *t, RcdType elem, Status *taller); * Description....: 在平衡二叉树插入指定元素 * Param..........: t:指向平衡二叉树指针的指针 elem:指定插入的元素 taller:标志值,调用时传入了一个Status类型任意值实参即可 * Return.........: TRUE:插入成功 FALSE:插入失败 * Precondition...: * Postcondition..: 若插入成功,*t将指向一棵插入了新结点的平衡二叉树;若插入失败,*t(平衡二叉树)不会改变,*taller最终为FALSE **/ static Status insertAVLTree_Interface(AVLTreePtr *t, RcdType elem, Status *taller) { if (NULL == *t) //树为空,则直接生成一个结点,并作为根节点 { *t = (AVLTreePtr)malloc(sizeof(AVLTreeNode)); if (NULL == *t) return OVERFLOW; (*t)->bf = EH; (*t)->data = elem; (*t)->lchild = (*t)->rchild = NULL; *taller = TRUE; //插入了新结点,置*taller为TRUE return TRUE; } else if (elem.key == (*t)->data.key) //要插入的结点和根结点相同,则不用插入 { *taller = FALSE; return FALSE; } else if (elem.key < (*t)->data.key) //elem值比根结点小,则插入到左子树 { if (FALSE == insertAVLTree_Interface(&((*t)->lchild), elem, taller)) return FALSE; if (TRUE == *taller) { switch ((*t)->bf) { case LH: //插入后,左子树与右子树高度差为2 leftBalance(t); //左平衡处理 *taller = FALSE; //处理后,不用再处理了,置taller为FALSE break; case EH: //原左右子树等高 (*t)->bf = LH; //在左子树插入后,平衡因子变为1 *taller = TRUE; //置taller为TRUE break; case RH: //原右子树高于左子树 (*t)->bf = EH; //在左子树插入后,平衡因子变为0 *taller = FALSE; break; } } } else if (elem.key > (*t)->data.key) //elem值比根结点大,则插入到右子树 { if (FALSE == insertAVLTree_Interface(&((*t)->rchild), elem, taller)) return FALSE; if (TRUE == *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; } } } return TRUE; }