#include#include 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->height),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 DoubleLeftRotation(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 right=AVLinsert(T->right,num); if(getHeight(T->right)-getHeight(T->left)==2){ if(T->right->data 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){ int total=0; int num=1; struct stack *s; s=createStack(40); while(1){ if(T->state==0){ total+=num; T->state=1; if(T->left!=NULL){ push(s,T); T=T->left; num++; } }else if(T->state==1){ T->state=2; if(T->right!=NULL){ push(s,T); T=T->right; num++; } }else{ T->state=0; T=pop(s); num--; if(T->state==2&&isEmpty(s)){ 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树节点总数=%dn",Count(T)); printf("AVL树所有节点所在层次总和=%dn",getTotalASL(T)); printf("AVL树平均ASL=%.2lfn",(double)getTotalASL(T)/Count(T)); return 0; }



