天天看点

1066 Root of AVL Tree (25 分)

maxmin

1066 Root of AVL Tree (25 分)

题意

给出一组数,把它们插入平衡二叉搜索树中,找出根节点。

思路

板子题,构建avl树即可。

构建avl树

avl树在二叉排序树的基础上通过左右旋改变根节点使得树的左右子树高度差不超过1。

构建avl树

一些列函数:

建立结构体包括:节点高度,节点权值,左右子树

typedef struct node{
	int data,height;
	node *lchild,*rchild;
}node;
           

建立新结点,左右子树为NULL,高度为1

node* newNode(int v)
{
	node* p=new node;
	p->data=v;
	p->height=1;
	p->lchild=p->rchild=NULL;
	return p;
}
           

获取节点高度

int getHeight(node* root)
{
	if(root==NULL){
		return 0;
	}else{
		return root->height;
	}
}
           

更新节点高度

void updateHeight(node* root)
{
	root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
           

获取节点左右子树高度差(平衡因子)

int getBalanceFactor(node* root)
{
	return getHeight(root->lchild)-getHeight(root->rchild);
}
           

左旋

需要左旋是因为右子树树高于左子树超过1,此时把根节点的右孩子设为根节点,根节点右孩子的左孩子设为根节点的右孩子。更新节点高度。

void L(node* &root)
{
	node* temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
}
           

右旋

和左旋对称(左右相反即可)

void R(node* &root)
{
	node* temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
}
           

插入节点

需要左右旋的情况有四种

1.根节点的左子树比右子树高,左子树lchild的左子树比lchild右子树高(LL)

2.根节点的左子树比右子树高,左子树lchild的左子树比lchild右子树低 (LR)

3.根节点的左子树比右子树低,右子树rchild的左子树比rchild右子树高 (RL)

4.根节点的左子树比右子树低,右子树rchild的左子树比rchild右子树低(RR)

void insert(node* &root,int v)
{
	if(root==NULL){
		root=newNode(v);
		return ;	
	}
	if(root->data>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{
		insert(root->rchild,v);
		updateHeight(root);
		if(getBalanceFactor(root)==-2){
			if(getBalanceFactor(root->rchild)==-1){	//RR型
				L(root);
			}else if(getBalanceFactor(root->rchild)==1){		//RL型
				R(root->rchild);
				L(root);
			}
		}
	}
}
           

代码

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef struct node{
	int data,height;
	node *lchild,*rchild;
}node;
node* newNode(int v)	
{
	node* p=new node;
	p->data=v;
	p->height=1;
	p->lchild=p->rchild=NULL;
	return p;
}
int getHeight(node* root)
{
	if(root==NULL){
		return 0;
	}else{
		return root->height;
	}
}
int getBalanceFactor(node* root)
{
	return getHeight(root->lchild)-getHeight(root->rchild);
}
void updateHeight(node* root)
{
	root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
void L(node* &root)
{
	node* temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
}
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);
		return ;	
	}
	if(root->data>v){
		insert(root->lchild,v);
		updateHeight(root);
		if(getBalanceFactor(root)==2){
			if(getBalanceFactor(root->lchild)==1){
				R(root);
			}else if(getBalanceFactor(root->lchild)==-1){
				L(root->lchild);
				R(root);
			}
		}
	}else{
		insert(root->rchild,v);
		updateHeight(root);
		if(getBalanceFactor(root)==-2){
			if(getBalanceFactor(root->rchild)==-1){
				L(root);
			}else if(getBalanceFactor(root->rchild)==1){
				R(root->rchild);
				L(root);
			}
		}
	}
}
int main()
{
	int n,x;
	node *root=NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		insert(root,x);
//		printf("###\n");
	}
	printf("%d\n",root->data);
}
           

继续阅读