#include <stdlib.h>
#include <stdio.h>
#include "avl.h"
int get_height(node_t *temp);
int get_height(node_t *);
int get_max(int a, int b);
node_t* avl_insert(node_t *root, int val){
node_t *temp = (node_t*)malloc(sizeof(node_t));
temp -> val = val;
node_t *target, *pre, *prepre, *temp1, *temp2;
int pre_bf = 0, new_bf = 0;
target = root;
if (!root) {
root = (node_t*)malloc(sizeof(node_t));
root->left = root->right = root->parent = NULL;
root->val = val;
root->height = 1;
}
else{
while(true ){
if(target -> val == val){
return NULL;
}
if(target -> val < val && target -> right != NULL){
target = target -> right;
}
if(target -> val < val && target -> right == NULL){
target -> right = temp;
temp -> parent = target;
temp -> left = NULL;
temp -> right = NULL;
temp -> height = 1;
temp1 = temp;
while(temp1 != NULL){
temp1 -> height = get_max(get_height(temp1 -> left), get_height(temp1 -> right)) + 1;
temp1 = temp1 -> parent;
}
break;
}
if(target -> val > val && target -> left != NULL){
target = target -> left;
}
if(target -> val > val && target -> left == NULL){
target -> left = temp;
temp -> parent = target;
temp -> left = NULL;
temp -> right = NULL;
temp -> height = 1;
temp1 = temp;
while(temp1 != NULL){
temp1 -> height = get_max(get_height(temp1 -> left), get_height(temp1 -> right)) + 1;
temp1 = temp1 -> parent;
}
break;
}
}
}
temp = target;
while(temp != NULL){
int i = get_height(temp -> left);
int j = get_height(temp -> right);
if((new_bf = get_height(temp -> left) - get_height(temp -> right)) != 2 && (new_bf = get_height(temp -> left) - get_height(temp -> right)) != -2){
pre_bf = new_bf;
}
else if(new_bf == 2){
if(pre_bf == 1 ){ //LL
pre = temp -> left;
prepre = pre -> left;
temp -> left = pre -> right;
if(pre -> right != NULL)
pre -> right -> parent = temp;
pre -> parent = temp -> parent;
if(temp -> parent != NULL)
{if(temp -> parent -> left == temp){
if(temp -> parent != NULL)
temp -> parent -> left = pre;
}
else{
if(pre -> parent != NULL)
if(temp -> parent != NULL)
temp -> parent -> right = pre;
}}
pre -> right = temp;
temp -> parent = pre;
temp -> height = get_max(get_height(temp -> left), get_height(temp -> right)) + 1;
pre -> height = get_max(get_height(pre -> left), get_height(pre -> right)) + 1;
temp = pre;
pre_bf = get_height(temp -> left) - get_height(temp -> right) ;
}
else if(pre_bf == -1){ //LR
pre = temp -> left;
prepre = pre -> right;
if(temp -> parent != NULL)
{if(temp -> parent -> left == temp){
temp -> parent -> left = prepre;
}
else{
if(temp -> parent != NULL)
temp -> parent -> right = prepre;
}}
prepre -> parent = temp -> parent;
pre -> right = prepre -> left;
if(prepre -> left != NULL)
prepre -> left -> parent = pre;
temp -> left = prepre -> right;
if(prepre -> right != NULL)
prepre -> right -> parent = temp;
prepre -> left = pre;
pre -> parent = prepre;
prepre -> right = temp;
temp -> parent = prepre;
pre -> height = get_max(get_height(pre -> left), get_height(pre -> right)) + 1;
temp -> height = get_max(get_height(temp -> left), get_height(temp -> right)) + 1;
prepre -> height = get_max(get_height(prepre -> left), get_height(prepre -> right)) + 1;
temp = prepre;
pre_bf = get_height(temp -> left) - get_height(temp -> right) ;
}
}
else if(new_bf == -2){
if(pre_bf == 1){ //RL
pre = temp -> right;
prepre = pre -> left;
if(temp -> parent != NULL)
{if(temp -> parent -> left == temp){
temp -> parent -> left = prepre;
}
else{
if(temp -> parent != NULL)
temp -> parent -> right = prepre;
}}
prepre -> parent = temp -> parent;
pre -> left = prepre -> right;
if(prepre -> right != NULL)
prepre -> right -> parent = pre;
temp -> right = prepre -> left;
if(prepre -> left != NULL)
prepre -> left -> parent = temp;
prepre -> left = temp;
temp -> parent = prepre;
prepre -> right = pre;
pre -> parent = prepre;
pre -> height = get_max(get_height(pre -> left), get_height(pre -> right)) + 1;
temp -> height = get_max(get_height(temp -> left), get_height(temp -> right)) + 1;
prepre -> height = get_max(get_height(prepre -> left), get_height(prepre -> right)) + 1;
temp = prepre;
pre_bf = get_height(temp -> left) - get_height(temp -> right) ;
}
else if(pre_bf == -1){ //RR
pre = temp -> right;
temp -> right = pre -> left;
if(pre -> left != NULL)
pre -> left -> parent = temp;
if(temp -> parent != NULL)
{if(temp -> parent -> left == temp){
temp -> parent -> left = pre;
}
else{
if(temp -> parent != NULL)
temp -> parent -> right = pre;
}}
pre -> parent = temp -> parent;
pre -> left = temp;
temp -> parent = pre;
temp -> height = get_max(get_height(temp -> left), get_height(temp -> right)) + 1;
pre -> height = get_max(get_height(pre -> left), get_height(pre -> right)) + 1;
temp = pre;
pre_bf = get_height(temp -> left) - get_height(temp -> right) ;
}
}
temp2 = temp;
temp = temp -> parent;
}
return temp2;
}
int get_height(node_t *root){
int height = 0;
if(root){
height = root -> height;
}
return height;
}
int get_max(int a, int b){
return a > b?a:b;
}
AVL添加
最新推荐文章于 2022-05-27 23:00:42 发布