#include
#include
typedef struct node{
int value;
struct node *left_child;
struct node *right_child;
struct node *father;
}RBT;
//向右旋转
void AVL_right(RBT ** tree){
if(*tree==NULL) return ;
RBT *node=NULL;
node=*tree;
RBT *mark=NULL;
mark=node->left_child;
node->left_child=mark->right_child;
mark->right_child=node;
if(node->father!=NULL){
if(node=node->father->left_child){
node->father->left_child=mark;
}
else{
node->father->right_child=mark;
}
}
else{
*tree=mark;
}
if(node->left_child!=NULL){
node->left_child->father=node;
}
mark->father=node->father;
node->father=mark;
}
//向左旋转
void AVL_left(RBT **tree){
if(*tree==NULL) return ;
RBT *node=NULL;
node=*tree;
RBT *mark=NULL;
mark=node->right_child;
node->right_child=mark->left_child;
mark->left_child=node;
if(node->father!=NULL){
if(node==node->father->left_child){
node->father->left_child=mark;
}
else{
node->father->right_child=mark;
}
}
else{
*tree=mark;
}
if(node->right_child!=NULL){
node->right_child->father=node;
}
mark->father=node->father;
node->father=mark;
}
//节点的加入
void insert_node(RBT **tree,int num){
RBT *temp=NULL;
temp=(RBT*)malloc(sizeof(RBT));
temp->value=num;
temp->left_child=NULL;
temp->right_child=NULL;
RBT *bj=NULL;
if(*tree==NULL){
*tree=temp;
temp->father=NULL;
return ;
}
bj=*tree;
while(bj){
if(bj->value
bj->right_child=temp;
temp->father=bj;
return ;
}
bj=bj->right_child;
}
else if(bj->value>num){
if(bj->left_child==NULL){
bj->left_child=temp;
temp->father=bj;
return ;
}
bj=bj->left_child;
}
else{
printf("error\n");
free(temp);
temp=NULL;
return ;
}
}
}
//创建BST 树
void create(RBT **tree,int a[],int len){
if(a==NULL || len<=0) return ;
for(int i=0 ;i
}
}
//先序遍历BST树
void Traver(RBT *tree){
if(tree==NULL) return ;
printf("%4d",tree->value);
Traver(tree->left_child);
Traver(tree->right_child);
}
int main(){
RBT *tree=NULL;
int a[]={11,2,14,1,7,15,5,8};
create(&tree,a,sizeof(a)/sizeof(a[0]));
Traver(tree);
printf("\n------------------------------------\n");
AVL_right(&tree);
Traver(tree);
printf("\n------------------------------------\n");
AVL_left(&tree);
Traver(tree);
printf("\n------------------------------------\n");
return 0;
}