PAT 1123. Is It a Complete AVL Tree

本文介绍如何根据给定序列构建AVL树,并实现层序遍历。此外,还探讨了如何判断构建的AVL树是否为完全二叉树的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.按照给定序列依次插入建立AVL树.

2.树的层序遍历.

3.判断该AVL树是否为完全二叉树.

对于考点3: 建立队列,进行层序遍历时将每一个非空结点的左右孩子节点均加入队列,

               然后从头到尾遍历该队列,如果再遍历完所有非空节点之前遇到空节点,则

               说明该AVL树并非为完全二叉树。

#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode{          // THE DEFINITION OF THE AVLNODE;
    int KEY;
    int HEIGHT;
    struct AVLNode * left;
    struct AVLNode * right;
}AVLNode;

int HEIGHT ( AVLNode * AVLtree ){
    if ( AVLtree == NULL ){
        return 0;
    }else{
        return (HEIGHT(AVLtree->left) > HEIGHT(AVLtree->right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right)) + 1;
    }
}

AVLNode * LL_ROTATION ( AVLNode * AVLtree ){
    AVLNode * temp ;
    temp = AVLtree -> left ;
    AVLtree -> left = temp -> right ;
    temp -> right = AVLtree ;
    
    AVLtree-> HEIGHT = (HEIGHT(AVLtree->left) > HEIGHT(AVLtree->right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right)) + 1;
    temp -> HEIGHT = ( HEIGHT(temp->left) > AVLtree->HEIGHT ? HEIGHT(temp->left) : AVLtree->HEIGHT ) + 1;
    return temp;
}

AVLNode * RR_ROTATION ( AVLNode * AVLtree ){
    
    AVLNode * temp ;
    temp = AVLtree -> right;
    AVLtree -> right = temp -> left;
    temp -> left = AVLtree;
    
    AVLtree -> HEIGHT = (HEIGHT(AVLtree ->left) > HEIGHT(AVLtree -> right) ? HEIGHT(AVLtree->left) : HEIGHT(AVLtree->right) ) + 1;
    temp -> HEIGHT = ( AVLtree-> HEIGHT > HEIGHT(temp -> right) ? AVLtree -> HEIGHT : HEIGHT( temp-> right) ) + 1;
    return temp;
}

AVLNode * LR_ROTATION ( AVLNode * AVLtree ){
    AVLNode * temp ;
    temp = AVLtree -> left;
    AVLtree -> left = RR_ROTATION(temp);
    
    return LL_ROTATION(AVLtree);
}

AVLNode *RL_ROTATION ( AVLNode * AVLtree ){
    
    AVLNode * temp ;
    
    temp = AVLtree -> right;
    AVLtree -> right = LL_ROTATION(temp);
    return RR_ROTATION(AVLtree);
}

AVLNode * AVL_INSERT ( AVLNode * AVLtree , int key ){
    if ( AVLtree == NULL ){
        AVLtree = (AVLNode *) malloc( sizeof(AVLNode) );
        AVLtree -> KEY = key ;
        AVLtree -> HEIGHT = 0 ;
        AVLtree -> left = NULL;
        AVLtree -> right = NULL;
    }else if ( key < AVLtree -> KEY ){
        AVLtree->left = AVL_INSERT(AVLtree->left, key);
        if( HEIGHT(AVLtree->left) - HEIGHT(AVLtree ->right) >= 2 ){
            if ( key < AVLtree -> left -> KEY ){
                AVLtree = LL_ROTATION(AVLtree);
            }else{
                AVLtree = LR_ROTATION(AVLtree);
            }
        }
    }else{
        AVLtree->right =  AVL_INSERT(AVLtree->right, key);
        if (HEIGHT(AVLtree -> right) - HEIGHT(AVLtree -> left) >= 2){
            if( key > AVLtree -> right ->KEY ){
                AVLtree = RR_ROTATION(AVLtree);
            }else{
                AVLtree = RL_ROTATION(AVLtree);
            }
        }
    }
    return AVLtree;
}


int main(int argc, const char * argv[]) {
    
    int N ;
    scanf ( "%d" , &N );
    
    int *KEY = (int *)malloc( N * sizeof(int) );
    
    AVLNode *AVLtree = NULL;
    
    for ( int i = 0 ; i < N ; i++ ){
        scanf("%d",&KEY[i]);
        AVLtree = AVL_INSERT(AVLtree, KEY[i]);
    }
    
    int isCompleteBinTREE = 1;
    
    AVLNode ** QUEUE  =(AVLNode **)malloc( 100 * sizeof(AVLNode *) );
    
    int front = 0;
    int rear = 0;
    
    QUEUE[rear++] = AVLtree;
    while (front != rear) {
        AVLNode *temp = QUEUE[front];
        front++;
        if( temp != NULL ){
            QUEUE[rear++] = temp->left;
            QUEUE[rear++] = temp->right;
        }
    }
    
    int cnt = 0;
    int *result = (int *)malloc(N * sizeof(int) );
    for ( int i = 0 ; i < rear ; i++ ){
        if(QUEUE[i] == NULL ){
            isCompleteBinTREE = 0;
        }else{
            result[cnt++] = QUEUE[i]->KEY;
        }
        if ( cnt == N ){
            break;
        }
    }
    for (int i = 0 ; i < N ; i++ ){
        printf("%d",result[i]);
        if ( i != N-1 ){
            printf(" ");
        }else{
            printf("\n");
        }
    }
    
    if (isCompleteBinTREE == 1){
        printf("YES\n");
    }else{
        printf("NO\n");
    }
    
    
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值