#include<stdio.h>
#include<stdlib.h>
struct AVLNode{
int data;
int height;
struct AVLNode *left;
struct AVLNode *right;
};
struct queue{
struct AVLNode **data;
int front,rear;
int size;
int maxsize;
};
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(temp->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(temp->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 SingleRightRotation(T);
}
struct AVLNode *AVLinsert(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=AVLinsert(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=AVLinsert(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);
}
}
struct queue *create(int maxSize){
struct queue *q;
q=(struct queue *)malloc(sizeof(struct queue));
q->data=(struct AVLNode **)malloc(sizeof(struct AVLNode *)*maxSize);
q->front=q->rear=0;
q->size=0;
q->maxsize=maxSize;
return q;
}
void inqueue(struct queue *q,struct AVLNode *T){/*入队*/
if(q->size==q->maxsize){
printf("queue is full\n");
return;
}
q->data[q->rear++]=T;
q->rear=q->rear%q->maxsize;
q->size++;
}
struct AVLNode *dequeue(struct queue *q){/*出队*/
if(q->size==0){
printf("queue is empty\n");
exit(-1);
}
struct AVLNode *temp= q->data[q->front++];
q->front=q->front%q->maxsize;
q->size--;
return temp;
}
int isEmpty(struct queue *q){
return q->size==0;
}
int getTotalASL(struct AVLNode *T){/*求ASL的核心代码,利用队列和层序遍历*/
int total=0;/*记录总层次*/
int num=0;/*累加当前层入队的个数*/
int stage=1;/*记录当前层次的层数*/
struct queue *q;
q=create(60);
inqueue(q,T);/*首先把头指针入队*/
int count=1;/*记录当前层的个数*/
while(!isEmpty(q)){
for(int i=0;i<count;i++){/*每次for循环将同一层的出队,并将层次累加*/
T=dequeue(q);
total+=stage;
if(T->left!=NULL){
inqueue(q,T->left);
num++;
}/*左右不空,将左右子节点入队*/
if(T->right!=NULL){
inqueue(q,T->right);
num++;
}
}
count=num;/*更新当前层的个数*/
num=0;/*更新num的值,重新计数*/
stage++;
}
return total;
}
int Count(struct AVLNode *T){
if(T==NULL){
return 0;
}
return 1+Count(T->left)+Count(T->right);
}
int main(){
struct AVLNode *T;
T=NULL;
for(int i=1;i<=10;i++){
T=AVLinsert(T,i);
}
printf("前序遍历:");
preOrder(T);
printf("\n中序遍历:");
inOrder(T);
printf("\n后序遍历:");
postOrder(T);
printf("\nAVL树节点总数=%d\n",Count(T));
printf("AVL树所有节点所在层次总和=%d\n",getTotalASL(T));
printf("AVL树平均ASL=%.2lf\n",(double)getTotalASL(T)/Count(T));
return 0;
}