9.5 平衡二叉树AVL树基础算法

#include <cstdio>
#include <algorithm>
using namespace std;
//AVL 树
//1、结点
struct node{
	int v,height;//v为结点权值,height为当前子树高度
	node *lchild,*rchild ;
};

//新建结点
node * newnode(int v)
{
	node* Node=new node;
	Node->v=v;
	Node->height=1; //初始高度为1
	Node->lchild=Node->rchild=NULL;
	return Node; 
 } 

//获取以root为根的子树的当前height
int getheight(node* root)
{
	if(root==NULL)
		return 0;
	else
		return root->height;
}

//计算结点root的平衡因子
int getbalance(node *root)
{
	return getheight(root->lchild)-gethight(root->rchild);
}

//更新结点root 的height
void updateheight(node* root)
{
	root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}

//基本操作
//1 查找
void search(node* root,int x)
{
	if(root==NULL)
	{
		printf("search faild\n");
		return;
	}
	if(x==root->v)
		printf("%d\n",root->v);
	else if(root->v<x)
		search(root->rchild,x);
	else
		search(root->lchild,x);
}

//2 插入操作
//1)左旋
void L(node* &root)
{
	node* temp=root->rchild; //root指向结点A,temp指向结点B
	root->rchild=temp->lchild; //1让B的左子树成为 A的右子树
	temp->lchild=root;//2 让A成为B的左子树
	updateheight(root);//更新结点A的高度 
	updateheight(temp); //更新结点B的高度 
	root=temp; //3、将根节点设定为B 
}

//2)右旋
void R(node * &root)
{
	node*temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	updateheight(root);
	updateheight(temp);
	root=temp;
 }
  
//插入权值为v的结点
void insert(node* &root,int v)
{
	if(root==NULL) //到达空结点 
	{
		root=newnode(v);
		return;
	}
	if(v-<root->v)
	{
		insert(root->lchild,v);//往左子树插入
		updateheight(root);//更新树高
		if(getbalance(root)==2)
		{
			if(getbalance(root->lchild)==1) //LL型
			{
				R(root);//右旋 
			 } 
			else if(getbalance(root->lchild)==-1) //LR型
			{
				L(root->lchild);//先将根节点的左子树左旋 
				R(root); //再将根节点右旋 
			}
		}
	}
	else
	{
		insert(root->rchild,v);//往右子树插入
		updateheight(root);
		if(getbalance(root)==-2)//不平衡,需调整 
		{
			if(getbalance(root->rchild)==-1)//RR型
				L(root);
			else if(getbalance(root->rchild)==1)//RL型
			{
				R(root->rchild);//先将根节点右子树右旋 
				L(root); //再将根节点左旋 
			}
		 } 
	 } 
}

//AVL树的建立

node *create(int data[],int n)
{
	node* root=NULL;//新建空根结点root 
	for(int i=0;i<n;++i)
	{
		insert(root,data[i]);
	 } 
	return root;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值