平衡二叉树的旋转
2017-08-28 22:10
232 查看
平衡二叉树(AVL树)的旋转主要包括左左,右右,左右,右左四种。
1 左左
过程是这样的:
首先记录A->Left,将此结点保存到B,即
之后将B的右孩子挂到A的左边,即
最后attach,将A挂到B(此时为根节点root)的右边,即
2右右
3左右
4右左
5插入
6删除
删除可以看成插入:如果AVL树删除一个结点后,平衡被打破,则可以看成出入一个结点打破平衡,所以可以通过插入的方法做类似处理。
1 左左
typedef struct AVLNode *Position; typedef Position AVLTree; /* AVL树类型 */ struct AVLNode{ ElementType Data; /* 结点数据 */ AVLTree Left; /* 指向左子树 */ AVLTree Right; /* 指向右子树 */ int Height; /* 树高 */ }; int Max ( int a, int b ) { return a > b ? a : b; } AVLTree SingleLeftRotation ( AVLTree A ) { /* 注意:A必须有一个左子结点B */ /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Left; //B指向A->Left,即保存A->Left到B A->Left = B->Right; //B的右孩子挂到A的左边 B->Right = A; //A挂到B的右边 A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Left), A->Height ) + 1; return B; }
过程是这样的:
首先记录A->Left,将此结点保存到B,即
AVLTree B = A->Left。
之后将B的右孩子挂到A的左边,即
A->Left = B->Right;
最后attach,将A挂到B(此时为根节点root)的右边,即
B->Right = A;
2右右
typedef struct AVLNode *Position; typedef Position AVLTree; /* AVL树类型 */ struct AVLNode{ ElementType Data; /* 结点数据 */ AVLTree Left; /* 指向左子树 */ AVLTree Right; /* 指向右子树 */ int Height; /* 树高 */ }; int Max ( int a, int b ) { return a > b ? a : b; } AVLTree SingleRightRotation ( AVLTree A ) { /* 注意:A必须有一个右子结点B */ /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Right; //B指向A->Right,即保存A->Right到B A->Right = B->Left; //B的左孩子挂到A的右边 B->Left = A; //A挂到B的左边 A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Right), A->Height ) + 1; return B; }
3左右
typedef struct AVLNode *Position; typedef Position AVLTree; /* AVL树类型 */ struct AVLNode{ ElementType Data; /* 结点数据 */ AVLTree Left; /* 指向左子树 */ AVLTree Right; /* 指向右子树 */ int Height; /* 树高 */ }; int Max ( int a, int b ) { return a > b ? a : b; } AVLTree SingleLeftRotation ( AVLTree A ) { /* 注意:A必须有一个左子结点B */ /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Left; A->Left = B->Right; B->Right = A; A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Left), A->Height ) + 1; return B; } AVLTree SingleRightRotation ( AVLTree A ) { /* 注意:A必须有一个右子结点B */ /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Right; //B指向A->Right,即保存A->Right到B A->Right = B->Left; //B的左孩子挂到A的右边 B->Left = A; //A挂到B的左边 A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Right), A->Height ) + 1; return B; } AVLTree DoubleLeftRightRotation ( AVLTree A ) { /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */ /* 将A、B与C做两次单旋,返回新的根结点C */ /* 将B与C做右单旋,C被返回 */ A->Left = SingleRightRotation(A->Left); /* 将A与C做左单旋,C被返回 */ return SingleLeftRotation(A); }
4右左
typedef struct AVLNode *Position; typedef Position AVLTree; /* AVL树类型 */ struct AVLNode{ ElementType Data; /* 结点数据 */ AVLTree Left; /* 指向左子树 */ AVLTree Right; /* 指向右子树 */ int Height; /* 树高 */ }; int Max ( int a, int b ) { return a > b ? a : b; } AVLTree SingleLeftRotation ( AVLTree A ) { /* 注意:A必须有一个左子结点B */ /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Left; A->Left = B->Right; B->Right = A; A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Left), A->Height ) + 1; return B; } AVLTree SingleRightRotation ( AVLTree A ) { /* 注意:A必须有一个右子结点B */ /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */ AVLTree B = A->Right; //B指向A->Right,即保存A->Right到B A->Right = B->Left; //B的左孩子挂到A的右边 B->Left = A; //A挂到B的左边 A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1; B->Height = Max( GetHeight(B->Right), A->Height ) + 1; return B; } AVLTree DoubleLeftRightRotation ( AVLTree A ) { /* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */ /* 将A、B与C做两次单旋,返回新的根结点C */ /* 将B与C做左单旋,C被返回 */ A->Right = SingleLeftRotation(A->Right); /* 将A与C做右单旋,C被返回 */ return SingleRightRotation(A); }
5插入
AVLTree Insert( AVLTree T, ElementType X ) { /* 将X插入AVL树T中,并且返回调整后的AVL树 */ if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */ T = (AVLTree)malloc(sizeof(struct AVLNode)); T->Data = X; T->Height = 0; T->Left = T->Right = NULL; } /* (插入空树) 结束 */ else if ( X < T->Data ) { /* 插入T的左子树 */ T->Left = Insert( T->Left, X); /* 如果需要左旋 */ if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 ) if ( X < T->Left->Data ) T = SingleLeftRotation(T); /* 左单旋 */ else T = DoubleLeftRightRotation(T); /* 左-右双旋 */ } /* else if (插入左子树) 结束 */ else if ( X > T->Data ) { /* 插入T的右子树 */ T->Right = Insert( T->Right, X ); /* 如果需要右旋 */ if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 ) if ( X > T->Right->Data ) T = SingleRightRotation(T); /* 右单旋 */ else T = DoubleRightLeftRotation(T); /* 右-左双旋 */ } /* else if (插入右子树) 结束 */ /* else X == T->Data,无须插入 */ /* 更新树高 */ T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1; return T; }
6删除
删除可以看成插入:如果AVL树删除一个结点后,平衡被打破,则可以看成出入一个结点打破平衡,所以可以通过插入的方法做类似处理。