/******************************************************************************* Função que faz a rotação simples à direita a partir do nó pretendido da AVL. Function that makes the right single rotation. *******************************************************************************/ static void SingleRightRotation (PtAVLNode *pnode) { unsigned int LeftH, RightH; PtAVLNode Node = (*pnode)->PtLeft; (*pnode)->PtLeft = Node->PtRight; Node->PtRight = *pnode; /* atualizar a altura dos nós envolvidos na rotação - updating the node height*/ LeftH = AVLHeight ((*pnode)->PtLeft); RightH = AVLHeight ((*pnode)->PtRight); (*pnode)->Height = LeftH > RightH ? LeftH + 1 : RightH + 1; LeftH = AVLHeight (Node->PtLeft); RightH = (*pnode)->Height; Node->Height = LeftH > RightH ? LeftH + 1 : RightH + 1; *pnode = Node; }
int main() { int n,val[N]; int i,flag = 1,choice; int value; AVLTree root = NULL; printf("==========================软件设计实践I 作业4.1 AVL树==========================\n\n"); printf("== 初始化AVL树 ==\n请输入初始结点个数:\n"); scanf("%d",&n); printf("请输入%d个结点:\n",n); for(i=0;i<n;i++) { scanf("%d",&val[i]); root = Insert(root,val[i]); } Print(root,root->value,0); while(flag) { printf("\n请选择操作:\n1.插入\n2.查找\n3.删除\n4.退出\n\n"); scanf("%d",&choice); switch(choice) { case 1: printf("请输入插入的元素值:\n"); scanf("%d",&value); root = Insert(root,value); Print(root,root->value,0); break; case 2: printf("请输入查找的元素值:\n"); scanf("%d",&value); if(Search(root,value))printf("%d在AVL树中。\n",value); else printf("%d不在AVL树中。\n",value); break; case 3: printf("请输入删除的元素值:\n"); scanf("%d",&value); if(Search(root,value)) DeleteNode(root,value); Print(root,root->value,0); break; case 4: printf("\n== 结束 ==\nAVL树的详细信息:\n"); Display(root); printf("高度: %d\n", AVLHeight(root)); printf("最小值: %d\n", AVLMin(root)->value); printf("最大值: %d\n", AVLMax(root)->value); Print(root,root->value,0); flag = 0; AVLFree(root); break; default: printf("无效输入!"); break; } } }
/******************************************************************************* Função que faz o reequilíbrio do nó pretendido da AVL. Function that balances the tree if necessary in each node that does not respect the equilibrium rule, after insertion and deletion operations. *******************************************************************************/ static void Balance (PtAVLNode *proot) { unsigned int LeftH, RightH; if (*proot == NULL) return; /* subárvore vazia - empty tree */ LeftH = AVLHeight ((*proot)->PtLeft); /* altura subárvore esquerda - left subtree height */ RightH = AVLHeight ((*proot)->PtRight); /* altura subárvore direita - right subtree height */ if (LeftH - RightH == 2) /* subárvore esquerda desequilibrada? - left subtree unbalanced? */ { LeftH = AVLHeight ((*proot)->PtLeft->PtLeft); RightH = AVLHeight ((*proot)->PtLeft->PtRight); if (LeftH >= RightH) SingleRightRotation (proot); else DoubleLeftRightRotation (proot); } else if (RightH - LeftH == 2) /* subárvore direita desequilibrada? - right subtree unbalanced? */ { RightH = AVLHeight ((*proot)->PtRight->PtRight); LeftH = AVLHeight ((*proot)->PtRight->PtLeft); if (RightH >= LeftH) SingleLeftRotation (proot); else DoubleRightLeftRotation (proot); } else (*proot)->Height = LeftH > RightH ? LeftH + 1 : RightH + 1; /* atualizar a altura do nó - updationg the node height */ }
void Display(AVLTree root) { printf("\n== 前序遍历: "); Preorder(root); printf("\n== 中序遍历: "); Inorder(root); printf("\n== 后序遍历: "); Postorder(root); printf("\n"); printf("== 高度: %d\n", AVLHeight(root)); printf("== 最小值: %d\n", AVLMin(root)->value); printf("== 最大值: %d\n", AVLMax(root)->value); }