100000614 - 《算法笔记》9.5小节——数据结构专题(2)->平衡二叉树(AVL)

9.5小节——数据结构专题(2)->平衡二叉树(AVL)

9.4.1-平衡二叉树的定义

AVL是增加了平衡条件的二叉查找树,即对AVL树的任意结点来说其左子树与右子树高度之差的绝对值不超过1,其中左子树与右子树的高度之差称为该结点的平衡因子。

9.4.2-AVL的基本操作

其基本操作有查找、插入、建树、删除。

1-查找

和二叉查找树的查找一致

//查找
int search(node *root,int v){
	if(root == NULL){//空树,查找失败
//		printf("search failed\n");
		return 0; 
	}
	if(v == root->v){
//		printf("%d\n",root->data);
		return 1;
	}
	else if(v < root->v){
		search(root->lchild,v);
	}
	else{
		search(root->rchild,v);
	}
} 

2-插入

注意为保持平衡而进行的调整操作
左旋(LL)
在这里插入图片描述

//左旋(Left Rotation)
void L(node *&root){
	node *temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp; 
}

右旋(RR)
在这里插入图片描述

//右旋(Right Rotation)
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 v){
	if(root == NULL){
		root = newNode(v);//新建结点,权值为x
		return; 
	}
	if(v < root->v){
		insert(root->lchild,v);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){//LL型
				R(root); 
			}
			else if(getBalanceFactor(root->lchild) == -1){//LR型
				L(root->lchild);
				R(root); 
			}
		}
	}
	else{//v比根结点的权值大 
		insert(root->rchild,v);//往右子树插入 
		updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->lchild) == -1){//RR型
				L(root); 
			}
			else if(getBalanceFactor(root->rchild) == 1){//RL型
				R(root->rchild);
				L(root); 
			}
		}
	}
	
}

3-AVL树的建立

//AVL树的建立
node *Create(int data[],int n){
	node *root = NULL;
	for(int i=0;i < n;i++){
		insert(root,data[i]);
	}
	reurn root;
}

AVL树的基本操作代码集合

//AVL平衡二叉树的基本操作 
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
	int v,height;//v为结点权值,height为当前子树高度 
	struct node *lchild;
	struct node *rchild;
};
//新建结点
node *newNode(int v){
	node *Node = new node;
	Node->v =  v;
	Node->height = 1;
	Node->lchild = NULL;
	Node->rchild = NULL;
	return Node;
}
//计算当前子树高度 
int getHeight(node *root){
	if(root == NULL)	return 0;
	return root->height;
}
//更新结点root的height
void updateHeight(node *root){
	root->height = max(getHeight(root->lchild),getHeight(root->rchild));
} 
//计算平衡因子
int getBalanceFactor(node *root){
	return getHeight(root->lchild) - getHeight(root->rchild);
} 

//查找
void search(node *root,int x){
	if(root == NULL){//空树,查找失败
		printf("search failed\n");
		return; 
	}
	if(x == root->data){
		printf("%d\n",root->data);
	}
	else if(x < root->data){
		search(root->lchild,x);
	}
	else{
		search(root->rchild,x);
	}
} 

//左旋(Left Rotation)
void L(node *&root){
	node *temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp; 
}
//右旋(Right Rotation)
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 v){
	if(root == NULL){
		root = newNode(v);//新建结点,权值为x
		return; 
	}
	if(v < root->v){
		insert(root->lchild,v);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){//LL型
				R(root); 
			}
			else if(getBalanceFactor(root->lchild) == -1){//LR型
				L(root->lchild);
				R(root); 
			}
		}
	}
	else{//v比根结点的权值大 
		insert(root->rchild,v);//往右子树插入 
		updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->lchild) == -1){//RR型
				L(root); 
			}
			else if(getBalanceFactor(root->rchild) == 1){//RL型
				R(root->rchild);
				L(root); 
			}
		}
	}
	
} 
//AVL树的建立
node *Create(int data[],int n){
	node *root = NULL;
	for(int i=0;i < n;i++){
		insert(root,data[i]);
	}
	reurn root;
} 


int main(){
	return 0;
}

Codeup习题

链接: http://codeup.cn/contest.php?cid=100000614

问题 A: 算法9-9~9-12:平衡二叉树的基本操作

链接: http://codeup.cn/problem.php?cid=100000614&pid=0

//问题 A算法9-9~9-12平衡二叉树的基本操作
//问题B二叉搜索树
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
	int v,height;//v为结点权值,height为当前子树高度 
	struct node *lchild;
	struct node *rchild;
};
//新建结点
node *newNode(int v){
	node *Node = new node;
	Node->v =  v;
	Node->height = 1;
	Node->lchild = NULL;
	Node->rchild = NULL;
	return Node;
}
//计算当前子树高度 
int getHeight(node *root){
	if(root == NULL)	return 0;
	return root->height;
}
//更新结点root的height
void updateHeight(node *root){
	root->height = max(getHeight(root->lchild),getHeight(root->rchild));
} 
//计算平衡因子
int getBalanceFactor(node *root){
	return getHeight(root->lchild) - getHeight(root->rchild);
} 

//查找
int search(node *root,int v){
	if(root == NULL){//空树,查找失败
//		printf("search failed\n");
		return 0; 
	}
	if(v == root->v){
//		printf("%d\n",root->data);
		return 1;
	}
	else if(v < root->v){
		search(root->lchild,v);
	}
	else{
		search(root->rchild,v);
	}
} 

//左旋(Left Rotation)
void L(node *&root){
	node *temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp; 
}
//右旋(Right Rotation)
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 v){
	if(root == NULL){
		root = newNode(v);//新建结点,权值为x
		return; 
	}
	if(v < root->v){
		insert(root->lchild,v);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){//LL型
				R(root); 
			}
			else if(getBalanceFactor(root->lchild) == -1){//LR型
				L(root->lchild);
				R(root); 
			}
		}
	}
	else{//v比根结点的权值大 
		insert(root->rchild,v);//往右子树插入 
		updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->lchild) == -1){//RR型
				L(root); 
			}
			else if(getBalanceFactor(root->rchild) == 1){//RL型
				R(root->rchild);
				L(root); 
			}
		}
	}
	
} 
//AVL树的建立
node *Create(int data[],int n){
	node *root = NULL;
	for(int i=0;i < n;i++){
		insert(root,data[i]);
	}
	return root;
} 


int main(){
	int n,k;
	while(cin>>n>>k){
		node *root = NULL;
		for(int i = 0;i < n;i++){
			int num;
			cin>>num;
			insert(root,num);
		}
		for(int i = 0;i < k;i++){
			int searnum;
			cin>>searnum;
		//	int flag = search(root,searnum);
			printf("%d ",search(root,searnum));
		}
	}
	return 0;
}

平衡二叉树小结

平衡二叉树(AVL)是所有结点满足左右子树高度之差(即平衡因子)绝对值小于1的二叉查找树(BST),特别注意插入与删除后对不平衡树的调整RR、LL、RL、LR。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李霁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值