1123 Is It a Complete AVL Tree (30分)

1.注意左旋右旋时,最后形成的树形式

2.层次遍历可用前序遍历存储实现,也可以用队列实现。

3.左旋即将根节点的右孩子为新的根结点,然后原root为他孩子节点的左节点,原来孩子节点的左孩子变为原root节点的右边孩子

右旋即将根节点的左孩子为新的根结点,然后原root为他孩子节点的右节点,原来孩子节点的右孩子变为原root节点的左孩子

图示理解

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
vector<int> ans;
const int N = 1<<20;
int level[N];
int n;
bool flag = true;

struct Node {
	int data;
	Node *l, *r;
	Node() {
		l = r = NULL;
	}
};
int getH(Node *root) { //计算树的高度
	if(root == NULL) return 0;
	return max(getH(root->l), getH(root->r))+1;
}
void lRotate(Node* &root) {
	Node *tmp = root->r;
	root->r = tmp->l;
	tmp->l = root;
	root = tmp;

}
void rRotate(Node* &root) {
	Node *tmp = root->l;
	root->l = tmp->r;
	tmp->r = root;
	root = tmp;
}
void lrRotate(Node* &root) {
	lRotate(root->l);
	rRotate(root);
}
void rlRotate(Node* &root) {
	rRotate(root->r);
	lRotate(root);
}

void Insert(Node* &root, int val) {
	if(root == NULL) {
		root = new Node;
		root->data = val;
		//cout << "叶子结点无需调整即"<<val << endl;
	} else if(root->data > val) {
		//cout << val << "插入左子树" << root->data<< endl;
		Insert(root->l, val);

		if(getH(root->l)-getH(root->r) >= 2) {//左子树高度相比右子树高2,因为每次都只插入一个数,高度大于一就会调整,所以高度不会>2
			if(val <= root->l->data) {
			//	cout << "右旋" <<val<< endl;
				rRotate(root);
			//	cout << "结果" << root->data << endl;
			} else {
			//	cout << "左右旋" <<val<< endl;
				lrRotate(root);
			//	cout << "结果" << root->data << endl;

			}
		} //else cout << "左子树无需调整即"<<val << endl;

	} else {
		//cout << val << "插入右子树" << root->data<< endl;

		Insert(root->r, val);

		if(getH(root->r)-getH(root->l) >= 2) {//左子树高度相比右子树高2,因为每次都只插入一个数,高度大于一就会调整,所以高度不会>2
			if(val > root->r->data) {
		//		cout << "左旋" <<val<< endl;
				lRotate(root);
		//		cout << "结果" << root->data << endl;

			} else {
			//	cout << "右zuo旋" <<val<< endl;
				rlRotate(root);
			//	cout << "结果" << root->data << endl;

			}
		} //else cout << "右子树无需调整即"<<val << endl;
	}

}


void Order(Node *root, int idx) {
	if(root == NULL) return;
	if(idx > n) flag = false;

	level[idx] = root->data;
//	cout << idx << "对应"<< root->data << endl; 
	Order(root->l, idx*2);
	Order(root->r, idx*2+1);
}
int main() {
	cin >> n;

	Node *root = NULL;

	for(int i=0; i<n; i++) {
		int tmp;

		cin >> tmp;

		Insert(root, tmp);
	}

	Order(root, 1);
	int i=1;
	while(n) {
		if(level[i]) { 
			cout << level[i];
			n--;
			if(n) cout << " ";
			else cout << endl; 
		}
		i++;
	}
	printf("%s\n", flag?"YES" : "NO");
	return 0;
}
/*
5
88 70 61 63 65
*/

 

### 几种典型二叉树的定义与特点 #### 1. 满二叉树 (Full Binary Tree) 满二叉树是指除最后一层外,每一层上的所有结点都有两个子结点的二叉树。换句话说,满二叉树中的每一个内部节点都恰好有两个孩子节点,并且所有的叶子节点都在同一层次上[^1]。 - 特征: - 所有的叶节点均位于最底层。 - 如果高度为 \( h \),则总共有 \( 2^h - 1 \) 个节点[^1]。 ```python class TreeNode: def __init__(self, value=0, left=None, right=None): self.value = value self.left = left self.right = right def is_full_binary_tree(root): if root is None: return True if root.left is None and root.right is None: return True if root.left is not None and root.right is not None: return is_full_binary_tree(root.left) and is_full_binary_tree(root.right) return False ``` --- #### 2. 完全二叉树 (Complete Binary Tree) 完全二叉树是一种特殊的二叉树,除了最后一层之外,其余各层都被填满,并且最后一层的所有节点都集中在左侧[^1]。 - 特征: - 节点数为 \( n \) 的完全二叉树的高度大约为 \( \lfloor \log_2(n+1) \rfloor \)[^2]。 - 可以通过数组轻松表示完全二叉树,其中父节点索引为 \( i \),左孩子的索引为 \( 2i + 1 \),右孩子的索引为 \( 2i + 2 \)[^3]。 ```python from collections import deque def is_complete_binary_tree(root): if not root: return True queue = deque([root]) end_found = False while queue: node = queue.popleft() if node: if end_found: return False queue.append(node.left) queue.append(node.right) else: end_found = True return True ``` --- #### 3. 二叉搜索树 (Binary Search Tree, BST) 二叉搜索树是一种满足特定条件的二叉树:对于任意一个节点,其左子树中所有节点的值小于当前节点的值,而右子树中所有节点的值大于当前节点的值[^1]。 - 特征: - 支持高效的查找、插入和删除操作,时间复杂度通常为 \( O(\log n) \),但在退化情况下可能达到 \( O(n) \)[^2]。 - 中序遍历的结果是一个升序序列。 ```python class TreeNode: def __init__(self, key): self.key = key self.left = None self.right = None def insert_into_bst(root, key): if not root: return TreeNode(key) if key < root.key: root.left = insert_into_bst(root.left, key) elif key > root.key: root.right = insert_into_bst(root.right, key) return root ``` --- #### 4. 平衡二叉树 (Balanced Binary Tree) 平衡二叉树是指任何节点的两棵子树的高度差不超过1的二叉树。AVL树是最典型的平衡二叉树之一。 - 特征: - 插入或删除节点后需重新调整以保持平衡状态。 - 左旋和右旋是常用的调整方法。 ```python def height(node): if not node: return 0 return max(height(node.left), height(node.right)) + 1 def is_balanced(root): if not root: return True lh = height(root.left) rh = height(root.right) if abs(lh - rh) <= 1 and is_balanced(root.left) == True and is_balanced(root.right) == True: return True return False ``` --- #### 总结对比表格 | 类型 | 主要特征 | |------------------|----------------------------------------------------------------------------------------------| | 满二叉树 | 每一层要么全是叶子节点,要么全部是非叶子节点,且叶子节点在同一层 | | 完全二叉树 | 除最后一层外,其他层均为满二叉树;最后一层的节点靠左排列 | | 二叉搜索树 | 对于任一节点,左子树中小于它,右子树中大于它 | | 平衡二叉树 | 各节点左右子树高度差不大于1 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值