编写程序,从键盘输入6个整数,逐个插入到AVL树中,在每次插入时重新平衡。根据你输入的整数序列,在草稿纸上画出该AVL树,遍历输出该树并检查结果是否正确。(C语言)

#include<stdio.h>
#include<stdlib.h>
struct AVLNode{
    int Data;
    int Height;
    struct AVLNode *Left;
    struct AVLNode *Right;
};
int getHeight(struct AVLNode *T){
    if(T==NULL){
        return 0;
    }
    return T->Height;
}
int Max(int n1,int n2){
    if(n1>n2){
        return n1;
    }
    return n2;
}
struct AVLNode *SingleLeftRotation(struct AVLNode *T){
    struct AVLNode *temp;
    temp=T->Left;
    T->Left=temp->Right;
    temp->Right=T;
    T->Height=1+Max(getHeight(T->Left),getHeight(T->Right));
    temp->Height=1+Max(getHeight(T->Left),T->Height);
    return temp;
}
struct AVLNode *SingleRightRotation(struct AVLNode *T){
    struct AVLNode *temp;
    temp=T->Right;
    T->Right=temp->Left;
    temp->Left=T;
    T->Height=1+Max(getHeight(T->Left),getHeight(T->Right));
    temp->Height=1+Max(T->Height,getHeight(T->Right));
    return temp;
}
struct AVLNode *DoubleLeftRotation(struct AVLNode *T){
    T->Left=SingleRightRotation(T->Left);
    return SingleLeftRotation(T);
}
struct AVLNode *DoubleRightRotation(struct AVLNode *T){
    T->Right=SingleLeftRotation(T->Right);
    return SingleLeftRotation(T);
}
struct AVLNode *insertAVL(struct AVLNode *T,int num){
    if(T==NULL){
        T=(struct AVLNode *)malloc(sizeof(struct AVLNode));
        T->Data=num;
        T->Height=1;
        T->Left=NULL;
        T->Right=NULL;
    }else if(T->Data>num){
        T->Left=insertAVL(T->Left,num);
        if(getHeight(T->Left)-getHeight(T->Right)==2){
            if(T->Left->Data>num){
                T=SingleLeftRotation(T);
            }else{
                T=DoubleLeftRotation(T);
            }
        }
    }else if(T->Data<num){
        T->Right=insertAVL(T->Right,num);
        if(getHeight(T->Right)-getHeight(T->Left)==2){
            if(T->Right->Data<num){
                T=SingleRightRotation(T);
            }else{
                T=DoubleRightRotation(T);
            }
        }
    }
    T->Height=1+Max(getHeight(T->Left),getHeight(T->Right));
    return T;
}
void preOrder(struct AVLNode *T){
    if(T!=NULL){
        printf("%4d",T->Data);
        preOrder(T->Left);
        preOrder(T->Right);
    }
}
void inOrder(struct AVLNode *T){
    if(T!=NULL){
        inOrder(T->Left);
        printf("%4d",T->Data);
        inOrder(T->Right);
    }
}
void postOrder(struct AVLNode *T){
    if(T!=NULL){
        postOrder(T->Left);
        postOrder(T->Right);
        printf("%4d",T->Data);
    }
}
int main(){
    struct AVLNode *T;
    T=NULL;
    int num;
    for(int i=0;i<6;i++){
        printf("第%d个data=",i+1);
        scanf("%d",&num);
        T=insertAVL(T,num);
    }
    printf("先序遍历:");
    preOrder(T);
    printf("\n中序遍历:");
    inOrder(T);
    printf("\n后序遍历:");
    postOrder(T);
    return 0;
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值