【剑指紫金港】1123 Is It a Complete AVL Tree 完全AVL树模板

这篇博客介绍了AVL树的基本概念,它是一种自平衡二叉搜索树。文章通过实例展示了如何进行节点的插入操作,并给出了插入一系列元素后的AVL树的层序遍历序列。此外,还提供了C++代码实现,用于插入元素并检查最终得到的AVL树是否为完全二叉树。对于不完全的二叉树,程序会输出'NO',反之则输出'YES'。
摘要由CSDN通过智能技术生成

A 1123 Is It a Complete AVL Tree

题目链接

Problem Description

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
1

2
3
4
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input

Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output

For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YESif the tree is complete, or NOif not.

Sample Input 1:

5
88 70 61 63 65

Sample Output 1:

70 63 88 61 65
YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO

AC代码
代码实现参照《算法笔记》

#include<bits/stdc++.h>
using namespace std;
const int maxn = 25;
int level[maxn],n,key,ind=0,isComplete=1,flag=0;
struct node{
    int v,h;
    node* l;
    node* r;
};
node* newnode(int x){
    node* root = new node;
    root->v=x;
    root->h=1;
    root->l=root->r=NULL;
    return root;
}
int getHeight(node* root){
    if(root==NULL) return 0;
    return root->h;
}
int getBalanceFactor(node* root){
    return getHeight(root->l)-getHeight(root->r);
}
void updataHeight(node* root){
    root->h=max(getHeight(root->l),getHeight(root->r))+1;
}
void L(node* &root){
    node* temp = root->r;
    root->r=temp->l;
    temp->l=root;
    updataHeight(root);
    updataHeight(temp);
    root=temp;
}
void R(node* &root){
    node* temp = root->l;
    root->l=temp->r;
    temp->r=root;
    updataHeight(root);
    updataHeight(temp);
    root=temp;
}
void insert(node* &root,int x){
    if(root==NULL){
        root=newnode(x);
        return ;
    }
    if(x<root->v){
        insert(root->l,x);
        updataHeight(root);
        if(getBalanceFactor(root)==2){
            if(getBalanceFactor(root->l)==1){
                R(root);
            }else if(getBalanceFactor(root->l)==-1){
                L(root->l);
                R(root);
            }
        }
    }else{
        insert(root->r,x);
        updataHeight(root);
        if(getBalanceFactor(root)==-2){
            if(getBalanceFactor(root->r)==-1){
                L(root);
            }else if(getBalanceFactor(root->r)==1){
                R(root->r);
                L(root);
            }
        }
    }
}
void bfs(node* root){
    queue<node*>q;
    q.push(root);
    while(!q.empty()){
        node* now=q.front();
        q.pop();
        level[ind++]=now->v;
        if(now->l!=NULL){
            if(flag) isComplete=0;
            q.push(now->l);
        }else{
            flag=1;
        }
        if(now->r!=NULL){
            if(flag) isComplete=0;
            q.push(now->r);
        }else{
            flag=1;
        }
    }
}
int main(){
    scanf("%d",&n);
    node* root=NULL;
    for(int i=0;i<n;i++){
        scanf("%d",&key);
        insert(root,key);
    }
    bfs(root);
    for(int i=0;i<ind;i++){
        if(i>0) printf(" ");
        printf("%d",level[i]);
    }
    printf("\n%s",isComplete ? "YES" : "NO");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值