AVL树

#pragma mark AVL tree(平衡二叉树)

#define avl_node_len(p) (p == NULL ? 0 : (p) -> height)

typedef struct _avl_node{
    
    int key;
    struct _avl_node* left;
    struct _avl_node* right;
    int height;
    
}avl_node;


//根节点的左子树的左子树还有非空子节点 LL
avl_node* left_left_rotate(avl_node* node){
    
    if (!node) {
        return NULL;
    }
    
    avl_node* k = node -> left;
    
    if (!k) {
        return NULL;
    }
    
    node -> left = k -> right;
    k -> right = node;
    
    node -> height = MAX(avl_node_len(node -> left), avl_node_len(node -> right)) + 1;
    k -> height = MAX(avl_node_len(k -> left), node -> height) + 1;
    
    return k;
}

//根节点右子树的右子树还有非空子节点 RR
avl_node* right_right_rotate(avl_node* node){
    
    if (!node) {
        return NULL;
    }
    
    avl_node* k = node -> right;
    
    if (!k) {
        return NULL;
    }
    
    node -> right = k -> left;
    k -> left = node;
    
    node -> height = MAX(avl_node_len(node -> left), avl_node_len(node -> right)) + 1;
    k -> height = MAX(avl_node_len(k -> right), node -> height) + 1;
    
    return k;
}

//根节点的左子树的右子树还有非空节点LR
avl_node* left_right_rotate(avl_node* node){
    
    if (!node) {
        return NULL;
    }
    
    node -> left = right_right_rotate(node -> left);
    
    return left_left_rotate(node);
}

//根节点的右子树的左子树还有非空节点RL
avl_node* right_left_rotate(avl_node* node){
    
    if (!node) {
        return NULL;
    }
    
    node -> right = left_left_rotate(node -> right);
    
    return right_right_rotate(node);
}

void avl_insert_node(avl_node*& tree,int value){
    
    if (tree == NULL) {
        
        tree = new avl_node();
        tree -> left = NULL;
        tree -> right = NULL;
        tree -> height = 0;
        tree -> key = value;
        
    }else if(tree -> key < value){
        
        avl_insert_node(tree -> right, value);
        
        //(RL or RR)
        if (avl_node_len(tree -> right) - avl_node_len(tree -> left) == 2) {
            
            if (tree -> right -> key < value) {
                
               tree = right_right_rotate(tree);
               
            }else {
                
               tree = right_left_rotate(tree);
            }
        }
        
    }else if (tree -> key > value){
        
        avl_insert_node(tree -> left, value);
        
        // LR ,LL
        if(avl_node_len(tree -> left) - avl_node_len(tree -> right) == 2){
            
            if (tree -> left -> key > value) {
               tree = left_left_rotate(tree);
                
            }else{
               tree = left_right_rotate(tree);
            }
        }
        
    }else if (tree -> key == value){
    
        
    }
    
    tree -> height = MAX(avl_node_len(tree -> left), avl_node_len(tree -> right)) + 1;
}

avl_node* max_avl_node(avl_node* tree){
    
    if (!tree) {
        return NULL;
    }
    
    while (tree -> right) {
        tree = tree -> right;
    }
    
    return tree;
}

avl_node* min_avl_node(avl_node* tree){

    if (!tree) {
        return NULL;
    }
    
    while (tree -> left) {
        tree = tree -> left;
    }
    
    return tree;
}


void avl_delete_node(avl_node*& tree,int value){
    
    if (!tree) {
        return;
    }
    
    if (tree -> key < value) {
        
        avl_delete_node(tree -> right, value);
        if (avl_node_len(tree -> left) - avl_node_len(tree -> right) == 2) {
            //LR,LL
            if (avl_node_len(tree -> left -> right) > avl_node_len(tree -> left -> left)) {
                tree = left_right_rotate(tree);
            }else {
                tree = left_left_rotate(tree);
            }
        }
        
    }else if(tree -> key > value){
        
        avl_delete_node(tree -> left, value);
        if (avl_node_len(tree -> right) - avl_node_len(tree -> left) == 2) {
             //RL RR
            if (avl_node_len(tree -> right -> left) > avl_node_len(tree -> right -> right)) {
                tree = right_left_rotate(tree);
            }else{
                tree = right_right_rotate(tree);
            }
        }
        
    }else{
        
        if (tree -> left && tree -> right) {
            
            if(avl_node_len(tree -> left) > avl_node_len(tree -> right)){
                //左子树找最大节点
                avl_node* max_node = max_avl_node(tree -> left);
                tree -> key = max_node -> key;
                avl_delete_node(tree -> left, max_node ->key);
                
            }else{
                
                //右子树找最小节点
                avl_node* min_node = min_avl_node(tree -> right);
                tree -> key = min_node -> key;
                avl_delete_node(tree -> right, min_node -> key);
                
            }
            
        }else{
            
            avl_node* temp = tree;
            tree = tree -> left ? tree -> left: tree -> right;
            delete temp;
        }
        
    }
}

void walk_avl_tree(avl_node* node){
    
    if (!node) {
        return;
    }
    
    queue<avl_node*> queue;
    queue.push(node);
    while (!queue.empty()) {
        
        avl_node* node = queue.front();
        queue.pop();
        
        NSLog(@"%d",node -> key);
        
        if (node -> left) {
            queue.push(node -> left);
        }
        
        if(node -> right){
            queue.push(node -> right);
        }
    }
}

/**
    测试用例
    avl_node* tree = NULL;
    int vec[] = {2,3,6,1,5,7,9,10,21,23};
    for (int i = 0; i < sizeof(vec) / sizeof(int); i++) {
        
        avl_insert_node(tree, vec[i]);
    }
   
    avl_delete_node(tree, 9);
    avl_delete_node(tree, 3);
    walk_avl_tree(tree);
**/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值