A1123 Is It a Complete AVL Tree (30) AVL完全二叉树

本文详细介绍了AVL树的数据结构,包括节点定义、高度获取、平衡因子计算等核心概念。通过具体实例,展示了如何在AVL树中进行节点插入,并在保持树的平衡性的同时更新树的高度。此外,还提供了平衡旋转操作(如左旋、右旋)的实现方法,以及使用广度优先搜索遍历AVL树的完整代码。
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int k=0,n,num;;
struct node
{
	int id,height;
	node *lchild,*rchild;
};
int getheight(node *root)
{
	if(root==NULL)
	return 0;
	else
	return  root->height;
}
int getbalancefactor(node *root)
{
	return getheight(root->lchild)-getheight(root->rchild);
}
void updateheight(node* root)
{
	root->height= max(getheight(root->lchild),getheight(root->rchild))+1;
}
void L(node* &root)
{
	node *temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateheight(root);
	updateheight(temp);
    root=temp;
}
void R(node* &root)
{
	node *temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	updateheight(root);
	updateheight(temp);
	root=temp;
}
void insert(node* &root,int index)
{
	if(root==NULL)
	{
		root=new node;
		root->lchild=NULL;
		root->rchild=NULL;
		root->id=index;
		root->height=1;
		return;
	}
	if(index>=root->id)
	{
		insert(root->rchild,index);
		updateheight(root);
		if(getbalancefactor(root)==-2)
		{
			if(getbalancefactor(root->rchild)==-1)
			{
				L(root);
			}
			else if(getbalancefactor(root->rchild)==1)
			{
				R(root->rchild);
				L(root);
			}
		}
	}
	else
	{
		insert(root->lchild,index);
		updateheight(root);
		if(getbalancefactor(root)==2)
		{
			if(getbalancefactor(root->lchild)==1)
			{
				R(root);
			}
			else if(getbalancefactor(root->lchild)==-1)
			{
				L(root->lchild);
				R(root);
			}
		}
	}
}
void BFS(node* root)
{
	queue<node*> q;
	q.push(root);
	bool isc=true,flag=false;
	while(!q.empty())
	{
		node* temp=q.front();
		q.pop();
		printf("%d",temp->id);
		k++;
		if(k!=n)
		printf(" ");
		else
		printf("\n");
		if(temp->lchild!=NULL)
		{
			
			if(flag)
			{
				isc=false;
			}
			q.push(temp->lchild);
		}
		else
		{
			flag=true;
		}
		if(temp->rchild!=NULL)
		{
			if(flag)
			{
				isc=false;
			}
			q.push(temp->rchild);
		}
        else
        {
        	flag=true;
		}
	}
	if(isc)
	printf("YES\n");
	else
	printf("NO\n");
}
int main()
{
	cin>>n;
	node *root=NULL;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num);
		insert(root,num);
	}
	BFS(root);
}

### 完全二叉树、满二叉树和平衡二叉树的定义及区别 #### 一、完全二叉树 (Complete Binary Tree) 完全二叉树是指除最后一层外,其余各层节点数都达到最大值,并且最后一层的节点全部集中在该层最左侧的二叉树。如果一棵具有 `n` 个节点的二叉树的高度为 `h`,那么当且仅当其所有节点与相同高度的满二叉树前 `n` 个节点位置一一对应时,这棵树才是完全二叉树[^1]。 #### 二、满二叉树 (Full Binary Tree) 满二叉树是一种特殊的二叉树,在这种结构中,除了叶子节点以外的所有其他节点都有两个子节点。换句话说,每一层上的所有可能的位置都被填满了节点。对于高度为 `h` 的满二叉树来说,它恰好有 `(2^(h+1)-1)` 个节点[^2]。 #### 三、平衡二叉树 (Balanced Binary Tree, AVL Tree) 平衡二叉树也称为AVL树,是以Adelson-Velsky 和 Landis的名字命名的一种自调整二叉查找树。它的特点是任何节点的两个子树之间的高度差最多等于1;因此整个数据结构保持相对平坦的状态以便于快速访问任意元素。为了维持这样的特性,在每次插入或者删除之后都需要执行旋转操作来重新恢复平衡状态[^1]。 #### 四、三种类型的对比分析 | **属性/类型** | **完全二叉树** | **满二叉树** | **平衡二叉树** | |---------------|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------| | 节点分布 | 所有的内部节点要么有两个孩子,要么是一个叶节点位于最后面一层左边连续排列 | 每个非终端节点均拥有正好两个儿子 | 左右子树高度差异不超过1 | | 高度 | 对应同样数量节点的标准形式下最低限度的高度 | 给定层数情况下最多的节点数目 | 自动调节以确保接近理想形态 | | 应用场景 | 堆排序算法实现常用 | 理论研究意义较大 | 动态数据库索引优化 | ```java // Java 实现判断是否为完全二叉树的方法之一 public boolean isCompleteTree(TreeNode root){ if(root==null)return true; Queue<TreeNode> queue=new LinkedList<>(); queue.add(root); boolean end=false; //标记遇到第一个 null 后不能再出现有效节点 while(!queue.isEmpty()){ TreeNode node=queue.poll(); if(node!=null){ if(end) return false;//已经遇到了空节点后再发现新的节点则不是完全二叉树 queue.offer(node.left); queue.offer(node.right); } else{ end=true;//一旦找到一个null就设置标志位表示后面不应该再有任何实际数值节点存在 } } return true; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值