树--平衡二叉树(AVL)

为什么需要AVL树

二叉搜索树的搜索效率与其树的深度相关,而二叉搜索树的组成又与其插入序列相关,在极端情况下,二叉搜索树退化为一条单链(比如插入序列是 1 2 3 … n),使得搜索效率大大降低,为了避免这种情况出现,我们采用二叉平衡树对插入结点进行调整,使得树的深度尽可能小

定义

  • 平衡因子
    BF(T)=hL-hR
    hL和hR分别为左子树和右子树的高度

  • 平衡二叉树
    左右子树高度之差不超过1,即|Bf(t)|<=1

在这里插入图片描述

AVL树四大调整

1. LL单旋
在这里插入图片描述
即要插入的节点为根节点的左子树的左子树,
把b的右节点作为A的左节点
把b换位根节点,

node * LLrotation(node * root){
	node * temp=root->left;
	root->left=temp->right;
	temp->right=root;
	return temp;
} 

2. RR单旋
在这里插入图片描述
道理同上,把B的左子树作为A的右子树,把B作为根节点

node * RRrotation(node *root){
	node *temp=root->right;
	root->right=temp->left;
	temp->left=root;
	return temp;
}

3. LR双旋

在这里插入图片描述
要插入的节点为根节点的左子树的右子树,
先进性RR单旋,把C换为B的父节点,A作为C的父节点,
再进行LL单旋,把C作为A的父节点,C成为根节点

node *LRrotation(node *root){
	root->left=RRrotation(root->left);
	return LLrotation(root);
}

4. RL双旋
在这里插入图片描述
道理相同

node *RLrotation(node *root){
	root->right=LLrotation(root->right);
	return RRrotation(root);
}

练习

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct node{
	node * left;
	node * right;
	int height;
	int data;
}; 
int getHeight(node *root){
	return root==NULL?0:root->height;
}
node * RRrotation(node *root){
	node *temp=root->right;
	root->right=temp->left;
	temp->left=root;
	//跟新高度
	root->height=max(getHeight(root->left),getHeight(root->right))+1;
	temp->height=max(root->height,getHeight(temp->right))+1; 
	return temp;
}
node * LLrotation(node * root){
	node * temp=root->left;
	root->left=temp->right;
	temp->right=root;
	root->height=max(getHeight(root->left),getHeight(root->right))+1;
	temp->height=max(root->height,getHeight(temp->left))+1;
	return temp;
} 
node *LRrotation(node *root){
	root->left=RRrotation(root->left);
	return LLrotation(root);
}
node *RLrotation(node *root){
	root->right=LLrotation(root->right);
	return RRrotation(root);
}
node * insert(node *root,int data){
	if(!root){
		root=new node;
		root->data=data;
		root->left=NULL;
		root->right=NULL;
		root->height=0; 
	}else{
		if(data<root->data){
			root->left=insert(root->left,data);
			if(getHeight(root->left)-getHeight(root->right)==2){
				if(data<root->left->data){
					root=LLrotation(root);
				}else if(data>root->left->data){
					root=LRrotation(root);
				}
			}
			
		}else {
			root->right=insert(root->right,data);
			if(getHeight(root->right)-getHeight(root->left)==2){
				if(data>root->right->data){
					root=RRrotation(root);
				}else if(data<root->right->data){
					root=RLrotation(root);
				}
			}
		}
	}
	//更新树高
	root->height=max(getHeight(root->left),getHeight(root->right))+1;
	return root; 
} 
int main(){
	node *root=NULL;
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int temp;
		cin>>temp;
		root=insert(root,temp);
	}
	cout<<root->data;
}






















平衡二叉树是一种特殊的二叉,它的左右子的高度差不超过1。AVL是一种自平衡的二叉搜索,它的高度始终保持在O(log n)。 下面是C语言实现平衡二叉树AVL)的代码: ``` #include <stdio.h> #include <stdlib.h> /* 定义平衡二叉树节点结构体 */ struct AVLNode { int data; // 存储的数据 int height; // 节点高度 struct AVLNode *leftChild; // 左子 struct AVLNode *rightChild; // 右子 }; /* 获取节点高度 */ int getHeight(struct AVLNode *node) { if (node == NULL) { return -1; } else { return node->height; } } /* 获取节点平衡因子 */ int getBalanceFactor(struct AVLNode *node) { if (node == NULL) { return 0; } else { return getHeight(node->leftChild) - getHeight(node->rightChild); } } /* 更新节点高度 */ void updateHeight(struct AVLNode *node) { node->height = 1 + (getHeight(node->leftChild) > getHeight(node->rightChild) ? getHeight(node->leftChild) : getHeight(node->rightChild)); } /* 右旋操作 */ struct AVLNode *rotateRight(struct AVLNode *node) { struct AVLNode *newRoot = node->leftChild; node->leftChild = newRoot->rightChild; newRoot->rightChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 左旋操作 */ struct AVLNode *rotateLeft(struct AVLNode *node) { struct AVLNode *newRoot = node->rightChild; node->rightChild = newRoot->leftChild; newRoot->leftChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 插入操作 */ struct AVLNode *insert(struct AVLNode *root, int data) { if (root == NULL) { root = (struct AVLNode *) malloc(sizeof(struct AVLNode)); root->data = data; root->height = 0; root->leftChild = NULL; root->rightChild = NULL; } else if (data < root->data) { root->leftChild = insert(root->leftChild, data); if (getHeight(root->leftChild) - getHeight(root->rightChild) == 2) { if (data < root->leftChild->data) { root = rotateRight(root); } else { root->leftChild = rotateLeft(root->leftChild); root = rotateRight(root); } } } else if (data > root->data) { root->rightChild = insert(root->rightChild, data); if (getHeight(root->rightChild) - getHeight(root->leftChild) == 2) { if (data > root->rightChild->data) { root = rotateLeft(root); } else { root->rightChild = rotateRight(root->rightChild); root = rotateLeft(root); } } } updateHeight(root); return root; } /* 中序遍历 */ void inOrderTraversal(struct AVLNode *root) { if (root != NULL) { inOrderTraversal(root->leftChild); printf("%d ", root->data); inOrderTraversal(root->rightChild); } } int main() { struct AVLNode *root = NULL; int data[] = {5, 2, 8, 1, 3, 6, 9}; int len = sizeof(data) / sizeof(data[0]); int i; for (i = 0; i < len; i++) { root = insert(root, data[i]); } inOrderTraversal(root); return 0; } ``` 以上代码实现了平衡二叉树的插入和中序遍历操作。在插入操作中,根据插入节点的值和当前节点的值的大小关系,不断递归向左或向右子进行插入操作,并在递归返回时更新节点高度和进行平衡操作。在平衡操作中,根据节点的平衡因子进行旋转操作,使重新平衡。在中序遍历操作中,按照左子、根节点、右子的顺序遍历中的节点,输出节点的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值