#include<iostream>
#include<string>
#include<cmath>
#include<stdlib.h>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
template<class T>
class AVL_tree{
public:
typedef struct node{
T vlue;
int height_balance;//用来记录当前节点的平衡度
struct node * left ,*right;
node(T key){
left = nullptr;
right = nullptr;
height_balance = 0;
vlue = key;
}
} AVL_node;
AVL_node * root = nullptr;
int get_height (AVL_node * root){//用来获得当前这棵树的高度
if(root ==nullptr){
return 0;
}
else{
return max(get_height(root->left),get_height(root->right)) + 1;
}
}
int get_balance(AVL_node * root){//获得当前节点的平衡度
int lef = get_height(root->left);
int rig = get_height(root->right);
return lef - rig ;
}
AVL_node * Rotate_right(AVL_node * cur){//右旋操作
AVL_node * cur_sun = cur->left;
AVL_node * cur_re_sun = cur_sun->right;
cur->left = cur_re_sun ;
cur_sun->right = cur;
cur_sun->height_balance = get_balance(cur_sun);
cur->height_balance = get_balance( cur);
return cur_sun;
}
AVL_node * Rotate_left(AVL_node* cur){//左旋操作
AVL_node * cur_sun = cur->right ;
AVL_node * cur_le_sun= cur_sun->left;
cur->right = cur_le_sun;
cur_sun->left = cur;
cur_sun->height_balance = get_balance(cur_sun);
cur->height_balance = get_balance( cur);
return cur_sun;
}
AVL_node * insert_node(T k ,AVL_node* root){
//先插入节点
if(root==nullptr){
AVL_node * NEW_node = new AVL_node(k);
return NEW_node;
}
if(k ==root->vlue ){
return root;
}
else if(k < root->vlue){
root->left = insert_node(k, root->left);//向左子树找插入位置
}
else if(k> root->vlue){
root->right = insert_node(k,root->right);//向右子树找插入位置
}
root->height_balance =get_balance(root);//
if(root->height_balance > 1 && k<root->left->vlue){//LL 型
return Rotate_right(root) ;
}
else if(root->height_balance <-1 && k>root->right->vlue) {//RR型
return Rotate_left(root);
}
else if(root->height_balance > 1 && k>root->left->vlue){//LR型
root->left = Rotate_left(root->left);
return Rotate_right(root) ;
}
else if(root->height_balance < -1 && k< root->right->vlue){//RL型
root->right = Rotate_right(root->right);
return Rotate_left(root);
}
return root;
}
void mid_dfs(AVL_node* root ){//打印节点
if(root ==nullptr){
return ;
}
mid_dfs(root->left);
cout << root->height_balance<<':' << root->vlue<<endl;
mid_dfs(root->right);
return ;
}
void create_AVL_tree(T k){
root = insert_node(k,root);
return ;
}
void print_AVL(){
mid_dfs(root );
}
void mid_way_print(){
if(root==nullptr){
cout<< "AVL is empty"<<endl;
}
queue<AVL_node *> q;
q.push(root);
while(!q.empty()){
int n =q.size();
for(int i =0 ; i < n ; i++){
AVL_node * tem = q.front();
q.pop();
cout << tem->height_balance << ':' <<tem->vlue<<' ';
if(tem->left!=nullptr){
q.push(tem->left);
}
if(tem->right!=nullptr){
q.push(tem->right);
}
}
cout <<endl;
}
}
int get_later_vlue(AVL_node * root){
AVL_node *cur = root ;
while(cur->right != nullptr){
cur = cur ->right;
}
return cur->vlue;
}
AVL_node * delete_node(int k,AVL_node * root){//删除节点
if(root == nullptr){
return nullptr;
}
if( k < root->vlue ){
root->left = delete_node(k,root->left);
}
else if( k > root->vlue){
root->right = delete_node(k,root->right);
}
else if(k==root->vlue){
if(root->left==nullptr && root->right==nullptr){
return nullptr;
}
else if(root->left!=nullptr && root->right==nullptr){
return root->left;
}
else if(root->right!=nullptr && root->left==nullptr){
return root->right;
}
else if(root->right!=nullptr && root->left !=nullptr){
int need_vlue = get_later_vlue(root->right);
root->vlue = need_vlue;
root->right = delete_node(need_vlue,root->right);
}
int root_balance_degree = get_balance( root);
//删除的情况其实可以考虑插入的情景
// LL 型
if(root_balance_degree > 1 && get_balance(root->left) >=0 ){
return Rotate_right(root);
}
//RR型
else if(root_balance_degree < -1 && get_balance(root->right)<=0 ){
return Rotate_left(root);
}
//LR型
else if(root_balance_degree > 1 && get_balance(root->left ) <0){
Rotate_left(root->left);
return Rotate_right(root);
}
///RL型
else if(root_balance_degree < -1 && get_balance(root->right)>1){
Rotate_right(root->right);
return Rotate_left(root);
}
return root;
}
}
};