1、LL型
void LL_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->left;
node->left = temp->right;
temp->right = node;
node->height = max(getHeight(node->left),getHeight(node->right))+1;
temp->height = max(getHeight(temp->left),getHeight(temp->right))+1;
*nodePtr = temp;
}
2、RR型
void RR_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->right;
node->right = temp->left;
temp->left = node;
node->height = max(getHeight(node->left),getHeight(node->right))+1;
temp->height = max(getHeight(temp->left),getHeight(temp->right))+1;
*nodePtr = temp;
}
3、LR型
void LR_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->left->right;
RR_Rotation(node->left,&(node->left));
LL_Rotation(node,&node);
*nodePtr = temp;
}
4、RL型
void RL_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->right->left;
LL_Rotation(node->right,&(node->right));
RR_Rotation(node,&node);
*nodePtr = temp;
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode{
int data,height;
struct TreeNode *left,*right;
}TreeNode;
int getHeight(TreeNode* node){
if(node == NULL){
return 0;
}else{
return node->height;
}
}
int max(int a,int b){
return a > b ? a : b;
}
void RR_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->right;
node->right = temp->left;
temp->left = node;
node->height = max(getHeight(node->left),getHeight(node->right))+1;
temp->height = max(getHeight(temp->left),getHeight(temp->right))+1;
*nodePtr = temp;
}
void LL_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->left;
node->left = temp->right;
temp->right = node;
node->height = max(getHeight(node->left),getHeight(node->right))+1;
temp->height = max(getHeight(temp->left),getHeight(temp->right))+1;
*nodePtr = temp;
}
void LR_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->left->right;
RR_Rotation(node->left,&(node->left));
LL_Rotation(node,&node);
*nodePtr = temp;
}
void RL_Rotation(TreeNode* node,TreeNode** nodePtr){
TreeNode* temp = node->right->left;
LL_Rotation(node->right,&(node->right));
RR_Rotation(node,&node);
*nodePtr = temp;
}
void avlInsert(TreeNode** T,int data){
if(*T == NULL){
*T = (TreeNode*)malloc(sizeof(TreeNode));
(*T)->data = data;
(*T)->height = 0;
(*T)->right = (*T)->left = NULL;
}else if(data < (*T)->data){
avlInsert(&(*T)->left,data);
//拿到当前节点左右子树的高度
int LH = getHeight((*T)->left);
int RH = getHeight((*T)->right);
//判断高度差
if(LH-RH == 2){
if(data < (*T)->left->data){//LL型调整
LL_Rotation(*T,T);
}else{//LR调整
//RR_Rotation((*T)->left,&(*T)->left);
//LL_Rotation(*T,T);
LR_Rotation(*T,T);
}
}
}else if(data > (*T)->data){
avlInsert(&(*T)->right,data);
//拿到当前节点左右子树的高度
int LH = getHeight((*T)->left);
int RH = getHeight((*T)->right);
if(RH - LH == 2){
if(data > (*T)->right->data){//RR型调整
RR_Rotation(*T,T);
}else{//RL调整
//LL_Rotation((*T)->right,&(*T)->right);
//RR_Rotation(*T,T);
RL_Rotation(*T,T);
}
}
}
(*T)->height = max(getHeight((*T)->left),getHeight((*T)->right))+1;
}
void preOrder(TreeNode* T){
if(T){
printf("%d ",T->data);
preOrder(T->left);
preOrder(T->right);
}
}
int main() {
TreeNode* T = NULL;
int num[5] = {5,4,3,2,1};
for(int i = 0; i < sizeof (num)/sizeof (num[0]);i++){
avlInsert(&T,num[i]);
}
preOrder(T);
return 0;
}
程序运行结果:
4 2 1 3 5