1066 Root of AVL Tree // AVL平衡二叉搜索树模板

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.

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

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

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

 模板:

#include <iostream>
#include <algorithm>
using namespace std;
int n, x;

struct node {
	int val;
	node *left, *right;
};

node *rotateLeft(node *root) {
	node *t = root->right;
	root->right = t->left;
	t->left = root;
	return t;
}

node *rotateRight(node *root) {
	node *t = root->left;
	root->left = t->right;
	t->right = root;
	return t;
}

node *rotateLeftRight(node *root) {
	root->left = rotateLeft(root->left);
	return rotateRight(root);
}

node *rotateRightLeft(node *root) {
	root->right = rotateRight(root->right);
	return rotateLeft(root);
}

int height(node *root) {
	if (root == NULL) {
		return 0;
	} else {
		return max(height(root->left), height(root->right)) + 1;
	}
}

node *insert(node *root, int val) {
	if (root == NULL) {
		root = new node();
		root->val = val;
		root->right = root->left = NULL;
	} else if (val < root->val) {
		root->left = insert(root->left, val);
		if (height(root->left) - height(root->right) == 2) {
			if (val < root->left->val) {
				root = rotateRight(root);
			} else {
				root = rotateLeftRight(root);
			}
		}
	} else {
		root->right = insert(root->right, val);
		if (height(root->right) - height(root->left) == 2) {
			if (val > root->right->val) {
				root = rotateLeft(root);
			} else {
				root = rotateRightLeft(root);
			}
		}
	}
	return root;
}

int main() {
	cin >> n;
	node *root = NULL;
	for (int i = 0; i < n; i++) {
		cin >> x;
		root = insert(root, x);
	}
	cout << root->val;
	return 0;
}

参考博客:1066. Root of AVL Tree (25)-PAT甲级真题_柳婼的博客-CSDN博客 

数据结构图文解析之:AVL树详解及C++模板实现 - 腾讯云开发者社区-腾讯云 (tencent.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 搜索(Balanced Binary Search Tree)是一种特殊的二搜索,它具有以下特点: 1. 每个节点的左子和右子的高度差不超过 1。 2. 每个节点都有一个因子,它是左子的高度减去右子的高度。因子的值只能是-1、0、1。 下面是一个简单的搜索的例子: ``` 4 / \ 2 5 / \ \ 1 3 6 ``` 这棵搜索满足以下条件: - 对于每个节点,它的左子和右子的高度差都不超过 1。 - 对于节点 4,它的因子是 0。对于节点 2,它的因子是 -1。对于节点 5,它的因子是 1。 搜索的优点在于,它能够保证插入、删除和查找操作的时间复杂度都是 O(logn),这使得它在处理大量数据时能够保证较快的速度。常见的搜索AVL、红黑和Treap。 ### 回答2: 搜索AVL)是一种自的二搜索,它的左右子的高度差不超过1。下面是一个实现搜索的例子: 1. 首先,定义节点类。节点类包含一个键和对应的值,以及左右子的指针。 ``` class Node: def __init__(self, key, value): self.key = key self.value = value self.left = None self.right = None self.height = 1 ``` 2. 创建搜索类,包含插入、删除和查找等方法。在中插入节点时,需要保持。 ``` class AVLTree: def __init__(self): self.root = None # 获取节点的高度 def get_height(self, node): if node is None: return 0 return node.height # 更新节点的高度 def update_height(self, node): node.height = 1 + max(self.get_height(node.left), self.get_height(node.right)) # 获取节点的因子 def get_balance_factor(self, node): if node is None: return 0 return self.get_height(node.left) - self.get_height(node.right) # 向中插入节点 def insert(self, key, value): self.root = self._insert(self.root, key, value) # 插入节点的辅助函数 def _insert(self, node, key, value): if node is None: return Node(key, value) if key < node.key: node.left = self._insert(node.left, key, value) else: node.right = self._insert(node.right, key, value) # 更新节点的高度 self.update_height(node) # balance_factor = self.get_balance_factor(node) if balance_factor > 1: if key < node.left.key: node = self._rotate_right(node) # LL型 else: node.left = self._rotate_left(node.left) # LR型 node = self._rotate_right(node) elif balance_factor < -1: if key > node.right.key: node = self._rotate_left(node) # RR型 else: node.right = self._rotate_right(node.right) # RL型 node = self._rotate_left(node) return node # 左旋转 def _rotate_left(self, node): new_root = node.right node.right = new_root.left new_root.left = node # 更新旋转后节点的高度 self.update_height(node) self.update_height(new_root) return new_root # 右旋转 def _rotate_right(self, node): new_root = node.left node.left = new_root.right new_root.right = node # 更新旋转后节点的高度 self.update_height(node) self.update_height(new_root) return new_root ``` 这样,我们就可以使用AVLTree类来创建并操作搜索了。当插入或删除一个节点时,我们会根据节点的键值进行比较,并保持性。这样可以提高搜索效率,并确保的高度始终保持。 ### 回答3: 搜索(Balanced Binary Search Tree)是一种特殊的二搜索,它的左右子的高度差始终在一个固定的范围内,以确保性和高效性。 构建搜索的常用方法是AVLAVL是一种自搜索,其因子(左右子高度之差)满足条件。 具体构建过程如下: 1. 首先,构建一个空作为起始状态。 2. 从待插入节点集合中选择一个节点作为根节点。 3. 将根节点插入空,并根据的特点进行操作。 4. 从待插入节点中选择一个节点,将其插入中。 5. 检查状态,如果不,则进行相应的旋转操作恢复。 6. 重复步骤4和5,直到所有节点均被插入中。 7. 完成后,搜索构建完成。 在插入节点时,根据具体情况进行左旋、右旋、左右旋或右左旋等操作,以保持。旋转操作会调整节点的位置以及子的链接关系,使保持性。 通过AVL的构建,可以保证的高度始终在一个较小的范围内,从而提高搜索、插入和删除等操作的效率。 总之,构建搜索的过程就是不断插入节点并进行操作的过程。通过合适的旋转操作,保证性,从而提高的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值