栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

求平衡二叉树的ASL(C语言,利用了回溯法和栈)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

求平衡二叉树的ASL(C语言,利用了回溯法和栈)

#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->dataright=AVLinsert(T->right,num);
        if(getHeight(T->right)-getHeight(T->left)==2){
            if(T->right->dataheight=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;
}

 ​​​​​​​

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/511088.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号