1066 Root of AVL Tree (25 分)

Root of AVL Tree
知识点:平衡二叉树

版本1

BF = 2 LL(1) LR(-1)
BF = -2 RR(-1) RL(1)
getHeight很关键
模板题,但是更新高度容易忘记。

#include<bits/stdc++.h>
using namespace std;
struct node{
	node* left;
	node* right;
	int data,height;
};
int getHeight(node* root){
	if(root == NULL) return 0;
	return root->height; 
}
int getBalanceFactor(node* root){
	return getHeight(root->left) - getHeight(root->right);
}
void updateHeight(node* root){
	root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
void L(node* &root){ //左旋 
	node* tmp = root->right;
	root->right =  tmp->left;
	tmp->left = root;
	updateHeight(root);
	updateHeight(tmp);
	root = tmp;
}
void R(node* &root){ //左旋 
	node* tmp = root->left;
	root->left =  tmp->right;
	tmp->right = root;
	updateHeight(root);
	updateHeight(tmp);
	root = tmp;
}
void insert(node* &r, int v){
	if(r == NULL){
		r = new node;
		r->data = v;
		r->height = 1;
		r->left = r->right = NULL;
	}else if(v < r->data){ //左子树 
		insert(r->left,v);
		updateHeight(r);
		if(getBalanceFactor(r)==2){
			if(getBalanceFactor(r->left)== 1){ //LL型 
				R(r); 
			}else if(getBalanceFactor(r->left)==-1){ //LR型 
				L(r->left);
				R(r);
			}
		} 
	}else { //右子树 
		insert(r->right, v);
		updateHeight(r);
		if(getBalanceFactor(r)==-2){
			if(getBalanceFactor(r->right)==1){ //RL型 
				R(r->right);
				L(r);
			}else if(getBalanceFactor(r->right)==-1){//RR型
				L(r); 
			} 
		}
	}
}

int main(){
	int n,num;
	cin>>n;
	node* root = NULL;
	for(int i=0;i<n;i++){
		cin>>num;
		insert(root, num);
	}	
	cout<<root->data<<endl;
	return 0;
}

版本2

2019.11.29

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue> 
#include <algorithm>
using namespace std;
int n;
struct node{
	int val, height;
	node* left, * right;
	node(int x){
		val = x;
		height = 1;
		left = right = nullptr;
	}
};
//获取树高
int getHeight(node* root){
	if(!root) return 0;
	return root->height;
} 
//更新树高 
void updateHeight(node* root){
	root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
void R(node*& root){
	node* temp = root->left;
	root->left = temp->right;
	temp->right = root; 
	//更新树高 
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}
void L(node*& root){
	node* temp = root->right;
	root->right = temp->left;
	temp->left = root; 
	//更新树高  
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

//返回平衡因子 
int getBF(node* root){
	return getHeight(root->left) - getHeight(root->right);
}
void insert(node* & root, int x){
	if(!root) root = new node(x);
	else if(x > root->val){ //放在右子树上 
		insert(root->right, x);
		updateHeight(root);
		if(getBF(root) == -2){
			if(getBF(root->right) == -1){
				L(root);
			}else {
				R(root->right);
				L(root);
			}
		} 
	}else{ //放在左子树上 
		insert(root->left, x);
		updateHeight(root);
		if(getBF(root) == 2){
			if(getBF(root->left) == 1){
				R(root);
			}else {
				L(root->left);
				R(root);
			}
		} 
	}
} 
int main(){
	int n, x;
	scanf("%d", &n);
	node* root = nullptr;
	while(n--){
		scanf("%d", &x);
		insert(root, x);
	}
	printf("%d\n", root->val);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值