1. 程式人生 > 其它 >二叉平衡樹的左旋右旋

二叉平衡樹的左旋右旋

技術標籤:# 演算法題

typedef struct TNode
{
	int data;
	struct TNode *left;//指向左孩子結點
	struct TNode *right;//指向右孩子結點
	struct TNode *father;//指向父結點
}TNode_t , *pTNode_t;

void RR(pTNode_t father)//對父親進行右旋
{
    pTNode_t ancestor=father->father;
    pTNode_t childRight=father->left->right;
    father->left->right=father;//重連左孩子的右孩子
    pTNode_t lChild=father->left;
    father->left=childRight;//重連父結點的左孩子
    lChild->father=ancestor;//重連左孩子的父親
    father->father=lChild;//重連父親的父親
}


void LL(pTNode_t father)//對父親進行左旋
{
    pTNode_t ancestor=father->father;
    pTNode_t childLeft=father->right->left;
    father->right->left=father;//重連右孩子的左孩子
    pTNode_t rChild=father->right;
    father->right=childLeft;//重連父結點的右孩子
    rChild->father=ancestor;//重連右孩子的父親
    father->father=rChild;//重連父親的父親
}