zoukankan      html  css  js  c++  java
  • AVL-tree

    
    

    //avl.h
    #ifndef __AVL_H__
    #define __AVL_H__

    
    

    typedef int KEY_TYPE;

    
    

    /* struct */
    typedef struct AVL{
     KEY_TYPE key;
     int height;
     struct AVL* lchild;
     struct AVL* rchild;
    }AVL;

    
    

    AVL* New_Node(KEY_TYPE key, AVL* lchild, AVL* rchild, int height = 0);
    inline int GetHeight(AVL* node);

    
    

    AVL* RR_Rotate(AVL* k2);
    AVL* LL_Rotate(AVL* k2);
    AVL* LR_Rotate(AVL* k3);
    AVL* RL_Rotate(AVL* k3);

    
    


    AVL* Insert(AVL* root, KEY_TYPE key);
    AVL* Delete(AVL* root, KEY_TYPE key);
    void InOrder(AVL* root);

    
    


    #endif

    
    
    //avl.cpp
    #include "AVL.h"
    #include<iostream>
    
    AVL* New_Node(KEY_TYPE key, AVL* lchild, AVL* rchild, int height)
    {
        AVL* p_avl = new AVL;
        p_avl->key = key;
        p_avl->lchild = lchild;
        p_avl->rchild = rchild;
        p_avl->height = height;
        return p_avl;
    }
    
    inline int GetHeight(AVL* node)
    {
        return (node==NULL)? -1:node->height;
    }
    
    inline int max(int a, int b)
    {
        return a>b?a:b;
    }
    
    /* RR(Y rotates to the right):
    
            k2                   k1
           /                   /  
          k1   Z     ==>       X   k2
         /                       /  
        X   Y                    Y    Z
    */
    AVL* RR_Rotate(AVL* k2)
    {
        AVL* k1 = k2->lchild;
        k2->lchild = k1->rchild;
        k1->rchild = k2;
        k2->height = max(GetHeight(k2->lchild), GetHeight(k2->rchild)) + 1;
        k1->height = max(GetHeight(k1->lchild), k2->height) + 1;
        return k1;
    }
    
    /* LL(Y rotates to the left):
    
            k2                       k1
           /                       /  
          X    k1         ==>      k2   Z
              /                  /  
             Y    Z              X    Y
     */
    AVL* LL_Rotate(AVL* k2)
    {
        AVL* k1 = k2->rchild;
        k2->rchild = k1->lchild;
        k1->lchild = k2;
        k2->height = max(GetHeight(k2->lchild), GetHeight(k2->rchild)) + 1;
        k1->height = max(GetHeight(k1->rchild), k2->height) + 1;
        return k1;
    }
    
    
    
    /* LR(B rotates to the left, then C rotates to the right):
    
          k3                         k3                       k2
         /                         /                       /  
        k1   D                     k2   D                   k1   k3
       /           ==>           /          ==>          /    / 
      A    k2                    k1   C                   A  B  C   D
          /                    /  
         B    C                A    B
    
    */
    AVL* LR_Rotate(AVL* k3)
    {
        k3->lchild = LL_Rotate(k3->lchild);
        return RR_Rotate(k3);
    }
    
    
    /*
    
           k3                         k3                          k2
          /                         /                          /  
         A    k1                    A    k2                     k3   k1 
             /         ==>             /           ==>       /    / 
            k2   B                     C    k1                A   C D   B
           /                              /  
          C    D                          D    B 
    
     */
    AVL* RL_Rotate(AVL* k3)
    {
        k3->rchild = RR_Rotate(k3->rchild);
        return LL_Rotate(k3);
    }
    
    
    AVL* Insert(AVL* root, KEY_TYPE key)
    {
        if(root == NULL)
            return (root = New_Node(key, NULL, NULL));
        else if(key < root->key)
            root->lchild = Insert(root->lchild, key);
        else 
            root->rchild = Insert(root->rchild, key);
        
        root->height = max(GetHeight(root->lchild), GetHeight(root->rchild)) + 1;
        if(GetHeight(root->lchild) - GetHeight(root->rchild) == 2)
        {
            if(key < root->lchild->key)
                root = RR_Rotate(root);
            else
                root = LR_Rotate(root);
        }
        else if(GetHeight(root->rchild) - GetHeight(root->lchild) == 2)
        {
            if(key < root->rchild->key)
                root = RL_Rotate(root);
            else
                root = LL_Rotate(root);
        }
        return root;
    }
    
    
    AVL* Delete(AVL* root, KEY_TYPE key)
    {
        if(!root)
            return NULL;
        if(key == root->key)
        {
            if(root->rchild == NULL)
            {
                AVL* temp = root;
                root = root->lchild;
                delete(temp);
                return root;
            }
            else
            {
                AVL* temp = root->rchild;
                while(temp->lchild)
                    temp = temp->lchild;
                
                root->key = temp->key;
                
                root->rchild = Delete(root->rchild, temp->key);
            }
        }
        else if(key < root->key)
            root->lchild = Delete(root->lchild, key);
        else
            root->rchild = Delete(root->rchild, key);
        
        root->height = max(GetHeight(root->lchild), GetHeight(root->rchild)) + 1;
        if(GetHeight(root->rchild) - GetHeight(root->lchild) == 2)
        {
            if(GetHeight(root->rchild->rchild) >= GetHeight(root->rchild->lchild))
                root = LL_Rotate(root);
            else
                root = RL_Rotate(root);
        }
        else if(GetHeight(root->lchild) - GetHeight(root->rchild) == 2)
        {
            if(GetHeight(root->lchild->lchild) >= GetHeight(root->lchild->rchild))
                root = RR_Rotate(root);
            else
                root = LR_Rotate(root);
        }
        return root;
    }
    
    
    void InOrder(AVL* root)
    {
        if(root)
        {
            InOrder(root->lchild);
            printf("key: %d height: %d ", root->key, root->height);
            if(root->lchild)
                printf("left child: %d ", root->lchild->key);
            if(root->rchild)
                printf("right child: %d ", root->rchild->key);
            printf("
    ");
            InOrder(root->rchild);
        }
    }
    // main.cpp
    #include<iostream>
    #include "AVL.h"
    
    int main(int argc, char* argv[])
    {
        AVL* root = NULL;
        int vector[] = {15,6,7,17,20,2,4,13,9};
    
        const int length = sizeof(vector)/sizeof(int);
        for(int i = 0; i< length;i++)
            root = Insert(root, vector[i]);
        
        printf("
    InOrder: 
    ");
        InOrder(root);
    
        int input;
        printf("
    please input the value you want to delete: ");
        scanf("%d",&input);
        
        while(1)
        {
            root = Delete(root, input);
            printf("
    After delete %u:
    ",input);
            InOrder(root);
            printf("
    please input another value you want to delete: ");
            scanf("%u",&input);
        }
        printf("
    ");
        return 0;
    }
    当你的才华还撑不起你的野心时,那你就应该静下心来学习。
  • 相关阅读:
    Intellijidea建javaWeb以及Servlet简单实现
    PHP关于文件与文件夹(1) 写入文件 文件权限 三、锁定文件
    mysql 时间戳格式化函数from_unixtime使用说明
    Ansible 2.0公布
    如何获取板子上独有的ID号EXYNOS4412/Imx6ul【转】
    2.7 xargs和exec详解【转】
    __setup 在内核中的作用【转】
    Linux __setup解析【转】
    c语言中 %p的含义【转】
    函数 devm_kzalloc()【转】
  • 原文地址:https://www.cnblogs.com/aceg/p/5964638.html
Copyright © 2011-2022 走看看