AVL添加

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值