#include<stdio.h>
#include<stdlib.h>
struct AVLNode{
int data;
int height;
int state;
struct AVLNode *left;
struct AVLNode *right;
};
struct stack{
struct AVLNode **data;
int top;
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->state=0;
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 stack *createStack(int maxSize){
struct stack *s;
s=(struct stack *)malloc(sizeof(struct stack));
s->data=(struct AVLNode **)malloc(sizeof(struct AVLNode *)*maxSize);
s->top=-1;
s->maxsize=maxSize;
return s;
}
int isFull(struct stack *s){
return s->top==s->maxsize-1;
}
int isEmpty(struct stack *s){
return s->top==-1;
}
void push(struct stack *s,struct AVLNode *T){
if(isFull(s)){
printf("Stack is full.\n");
return;
}
s->data[++s->top]=T;
}
struct AVLNode *pop(struct stack *s){
if(isEmpty(s)){
printf("Stack is empty.\n");
exit(-1);
}
return s->data[s->top--];
}
int getTotalASL(struct AVLNode *T){/*求ASL的核心代码,利用回溯法和栈*/
int total=0;
int num=1;/*用num记录当前节点的层次*/
struct stack *s;
s=createStack(40);
while(1){
if(T->state==0){/*状态为0表示还没被访问过*/
total+=num;/*第一次访问时将层次加到总和里*/
T->state=1;/*向左访问,状态设为1*/
if(T->left!=NULL){
push(s,T);/*如果左不空,在向左访问前要保存现场,将当前指针压入栈中*/
T=T->left;/*然后向左走*/
num++;/*并将层数加1*/
}
}else if(T->state==1){/*状态为1表示左边已经访问过了*/
T->state=2;/*向右访问,状态设为2*/
if(T->right!=NULL){
push(s,T);/*如果右不空,在向右访问前要保存现场,将当前指针压入栈中*/
T=T->right;/*然后向右走*/
num++;/*并将层数加1*/
}
}else{/*最后一种情况T->stata为2,表示左右都走过了*/
T->state=0;/*状态设为0,还原状态*/
T=pop(s);/*弹出之前保存在栈中的指针,回到上一层*/
num--;/*并将层数减1*/
if(T->state==2&&isEmpty(s)){/*如果栈空了且当前节点状态为2表示访问结束,回到了根节点*/
T->state=0;/*这时将根节点的状态还原以便下次调用函数*/
break;/*跳出循环*/
}
}
}
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;
}