Root of AVL Tree

本文详细介绍了AVL树的基本概念,包括其自我平衡的特性,即任意节点的两个子树高度差不超过1。通过一系列旋转操作,如左旋、右旋、左右双旋和右左双旋,确保在插入新节点后AVL树仍保持平衡状态。文章提供了具体的代码实现,演示了如何在AVL树中插入节点并进行必要的平衡调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Root of AVL Tree

  • from PTA 04-树5 Root of AVL Tree

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<cmath>
using namespace std;
 
struct TreeNode
{
	int value;
	TreeNode *left;
	TreeNode *right;
	int height;
	TreeNode(int v):value(v),left(NULL),right(NULL),height(0){}//类的初始化 
	TreeNode():left(NULL),right(NULL){}
};
 
int getHeight(TreeNode *t)
{
	if(t == NULL) return -1;
	else return t->height;
}
 
int max(int a,int b) {return a>b? a:b;}
 
//左左
TreeNode* SingleRotateLeft(TreeNode *t2)
{
	TreeNode *t1;
	t1 = t2->left;
    t2->left = t1->right;
	t1->right = t2;
 
	t2->height = max(getHeight(t2->left),getHeight(t2->right)) + 1;
	t1->height = max(getHeight(t1->left),getHeight(t1->right)) + 1;
	return t1;
}
 
//右右
 TreeNode* SingleRotateRight(TreeNode *t2)
 {
	 TreeNode *t1;
	 t1 = t2->right;
	 t2->right = t1->left;
	 t1->left = t2;
	 
	 t2->height = max(getHeight(t2->left),getHeight(t2->right)) + 1;
	 t1->height = max(getHeight(t1->left),getHeight(t1->right)) + 1;
	 return t1;
 }
 
 //左右
 TreeNode * DoubleRotateLR(TreeNode *t3)
 {
	 t3->left = SingleRotateRight(t3->left);
	 return SingleRotateLeft(t3);
 }
 
 //右左
 TreeNode * DoubleRotateRL(TreeNode *t3)
 {
	 t3->right = SingleRotateLeft(t3->right);
	 return SingleRotateRight(t3);
 }

{ 
// //RL相当于先进行一次左旋,再进行右旋,太妙了 
// TreeNode * DoubleRotateRL(TreeNode *t3)
// {
// 	 TreeNode *t; 
// 	 //左旋 
//	 t = SingleRotateLeft(t3->right);
//	 //右旋 
//	 t3->right=t->left;
//	 t->left=t3;
//	 t3->height = max(getHeight(t3->left),getHeight(t3->right)) + 1;
//	 t->height = max(getHeight(t->left),getHeight(t->right)) + 1;
//	 return t;
// }
 } 
 
 
 bool isBalanced(TreeNode *left,TreeNode *right)
 {
	 return abs(getHeight(left) - getHeight(right)) < 2;
 }
 
 TreeNode* insert(int v, TreeNode *root)
 {
	if(root == NULL)
	{
		root = new TreeNode(v);
		return root;
	}
	if(v > root->value) //节点插入在右子树中
	{
		root->right = insert(v,root->right);
		if(!isBalanced(root->left,root->right)){
			if(v > root->right->value)
				root = SingleRotateRight(root);
			else
				root = DoubleRotateRL(root);
		}
	}else{
		root->left = insert(v,root->left);
		if(!isBalanced(root->left,root->right)){
			if(v < root->left->value)
				root = SingleRotateLeft(root);
			else
				root = DoubleRotateLR(root);
		}
	}
	root->height = max(getHeight(root->left),getHeight(root->right)) + 1;
	return root;
 }
 
 int main()
 {
	 int n;
	 while(cin>>n)	
	 {
		 int t;
		 TreeNode *root = NULL;
		 for(int i=0; i<n; i++)
		 {	
			 cin>>t;
			 root = insert(t,root);			 
		 }
		 cout<<root->value<<endl;
	}
	 return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值