AVL树的插入+层次遍历+判断是否为完全二叉树板子

#include<bits/stdc++.h>
using namespace std;

const int N=100;
int l[N],r[N],v[N],h[N],idx;
int pos[N];
int p[N];
queue<int> q;
int root=0;
int n;
int update(int u){//更新深度
    h[u]=max(h[l[u]],h[r[u]])+1;
}

int R(int &u){//右旋
    int p=l[u];
    l[u]=r[p];
    r[p]=u;
    update(u),update(p);
    u=p;
}

int L(int &u){//左旋
    int p=r[u];
    r[u]=l[p];
    l[p]=u;
    update(u),update(p);
    u=p;
}

int get_balance(int u){//获取左子树-右子树高度差
    return h[l[u]]-h[r[u]];
}

void insert(int &u,int w){//在u节点处插入值为w的结点
    if(!u){
        u=++idx;
        v[u]=w;
    }else if(w<v[u]){
        //左
        insert(l[u],w);
        if(get_balance(u)==2){
            if(get_balance(l[u])==1){
                //右旋
                R(u);
            }else{
                L(l[u]);
                R(u);
            }
        }
    }else{
        //右
        insert(r[u],w);
        if(get_balance(u)==-2){
            if(get_balance(r[u])==-1){
                //左旋
                L(u);
            }else{
                R(r[u]);
                L(u);
            }
        }
    }
    update(u);
}

bool bfs(int root){//层次遍历 同时通过pos判断是否为完全二叉树
    pos[root]=1;
    q.push(root);
    int num=0;
    bool res=true;
    while(q.size()){
        int t=q.front();
        p[num++]=t;
        if(pos[t]>n)res=false;
        if(l[t]){
            pos[l[t]]=pos[t]*2;
            q.push(l[t]);
        }
        if(r[t]){
            pos[r[t]]=pos[t]*2+1;
            q.push(r[t]);
        }
        q.pop();
    }
    
    return res;
}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        int w;
        cin>>w;
        insert(root,w);
    }
    
    int res=bfs(root);
    cout<<v[p[0]];
    for(int i=1;i<n;i++)cout<<" "<<v[p[i]];
    cout<<endl;
    
    if(res)cout<<"YES";
    else cout<<"NO";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断一棵二叉树是否为 AVL 树,需要满足以下两个条件: 1. 该二叉树的所有子树都是 AVL 树; 2. 该二叉树的任意两个子树高度差的绝对值不超过 1。 其中,子树的高度指的是该子树的根节点到叶子节点的最长路径长度。 因此,判断一棵二叉树是否为 AVL 树,需要对该二叉树进行递归遍历,依次判断每个子树是否为 AVL 树,并计算每个子树的高度,最后再判断任意两个子树高度差的绝对值是否不超过 1。如果满足以上两个条件,则该二叉树为 AVL 树,否则不是 AVL 树。 具体实现过程可以参考以下代码(假设二叉树的节点结构为 Node,其包含 left、right、height 三个属性): ```python def is_avl_tree(root): # 辅助函数,计算节点高度 def get_height(node): if not node: return 0 return node.height # 判断一棵二叉树是否为 AVL 树 def is_avl_tree_helper(node): if not node: return True # 判断左右子树是否为 AVL 树 if not is_avl_tree_helper(node.left) or not is_avl_tree_helper(node.right): return False # 计算左右子树的高度差 height_diff = abs(get_height(node.left) - get_height(node.right)) if height_diff > 1: return False # 更新当前节点的高度 node.height = max(get_height(node.left), get_height(node.right)) + 1 return True # 判断根节点是否为空,为空则返回 False if not root: return False return is_avl_tree_helper(root) ``` 注意,以上代码仅为示例,具体实现可能因语言和实际情况的不同而有所差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值