AVL

AVL ํŠธ๋ฆฌ๋Š” Balanced Binary Search Tree์ด๋‹ค. AVL ํŠธ๋ฆฌ๋Š” ๊ฐ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์™ผ์ชฝ, ์˜ค๋ฅธ์กฑ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋†’์ด ์ฐจ์ด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ๊ฐ€์ง€๊ณ  ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋†’์ด ์ฐจ์ด๊ฐ€ 1๋ณด๋‹ค ํฌ์ง€์•Š๋‹ค๋Š” ์„ฑ์งˆ์„ ๊ฐ€์ง„๋‹ค.(์œ„ํ‚คํ”ผ๋””์•„ )

์ฆ‰, ์–‘์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋†’์ด ์ฐจ์ด๊ฐ€ 1์ดํ•˜์ด๋‹ค.

๊ท ํ˜•์žกํžŒ AVL ํŠธ๋ฆฌ๋Š” n๊ฐœ์˜ ์›์†Œ๊ฐ€ ์žˆ์„ ๋•Œ O(logn)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ๊ฒ€์ƒ‰, ์‚ฝ์ž…, ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹œ๊ฐ„ ๋ณต์žก๋„

ํ‰๊ท 

์ตœ์•…์˜ ๊ฒฝ์šฐ

๊ณต๊ฐ„

O(n)

O(n)

๊ฒ€์ƒ‰

O(logn)

O(logn)

์‚ฝ์ž…

O(logn)

O(logn)

์‚ญ์ œ

O(logn)

O(logn)

Rotation

ํŠธ๋ฆฌ์˜ ์žฌ๊ตฌ์„ฑ ์ž‘์—…์„ Rotation(ํšŒ์ „)์ด๋ผ๊ณ  ํ•œ๋‹ค.

LL

         z                                      y 
        / \                                   /   \
       y   T4      Right Rotate (z)          x      z
      / \          - - - - - - - - ->      /  \    /  \ 
     x   T3                               T1  T2  T3  T4
    / \
  T1   T2

y๋Š” z์˜ ์™ผ์ชฝ ์ž์‹, x๋Š” y์˜ ์™ผ์ชฝ ์ž์‹์ด๋‹ค. ์ด๋•Œ T1, T2, T3, T4๋Š” ๊ฐ๊ฐ์˜ ์„œ๋ธŒํŠธ๋ฆฌ์ด๋‹ค.

z์˜ ์˜ค๋ฅธ์กฑ ์ž์‹๊ณผ ์™ผ์ชฝ ์ž์‹์ด ๋ถˆ๊ท ํ˜•ํ•˜๋ฏ€๋กœ right Rotation ํ•œ๋ฒˆ์œผ๋กœ ๊ท ํ˜•์„ ์žก์•„์ค„ ์ˆ˜ ์žˆ๋‹ค.

if (bal > 1 && key < node->left->key) return right_rotate(node);

LR

     z                               z                           x
    / \                            /   \                        /  \ 
   y   T4  Left Rotate (y)        x    T4  Right Rotate(z)    y      z
  / \      - - - - - - - - ->    /  \      - - - - - - - ->  / \    / \
T1   x                          y    T3                    T1  T2 T3  T4
    / \                        / \
  T2   T3                    T1   T2

y๋Š” z์˜ ์™ผ์ชฝ ์ž์‹, x๋Š” y์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ํšŒ์ „์„ ๋‘๋ฒˆ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค. ์šฐ์„  y๋ฅผ ์™ผ์ชฝ ํšŒ์ „์„ ํ•œ๋ฒˆ ์‹œ์ผœ์ฃผ๊ณ  z๋ฅผ ์˜ค๋ฅธ์ชฝ ํšŒ์ „์‹œ์ผœ์ฃผ๋ฉด ๊ท ํ˜•์ด ์žกํžˆ๊ฒŒ ๋œ๋‹ค.

if (bal > 1 && key > node->left->key){
    node->left =  left_rotate(node->left);
    return right_rotate(node);
}

RR

 z                                y
 /  \                            /   \ 
T1   y     Left Rotate(z)       z      x
    /  \   - - - - - - - ->    / \    / \
   T2   x                     T1  T2 T3  T4
       / \
     T3  T4

y๋Š” z์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด๊ณ , x๋Š” y์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋Š” LL๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ๋ฒˆ์˜ rotation์œผ๋กœ ๊ท ํ˜•์„ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.

if (bal < -1 && key > node->right->key) return left_rotate(node);

RL

   z                            z                            x
  / \                          / \                          /  \ 
T1   y   Right Rotate (y)    T1   x      Left Rotate(z)   z      y
    / \  - - - - - - - - ->     /  \   - - - - - - - ->  / \    / \
   x   T4                      T2   y                  T1  T2  T3  T4
  / \                              /  \
T2   T3                           T3   T4

y๋Š” z์˜ ์˜ค๋ฅธ์ชฝ์ž์‹, x๋Š” y์˜ ์™ผ์ชฝ ์ž์‹์ด๋‹ค .์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋Š” ์œ„์˜ LR๊ณผ ์ • ๋ฐ˜๋Œ€์ธ ๊ฒฝ์šฐ์ด๋‹ค. ์ด ๋•Œ๋Š” ์˜ค๋ฅธ์ชฝ ํšŒ์ „ ํ›„ ์™ผ์ชฝ ํšŒ์ „์„ ํ•˜๋ฉด ๊ท ํ˜•์„ ๋งž์ถœ ์ˆ˜ ์žˆ๋‹ค.

if (bal < -1 && key < node->right->key){
    node->right = right_rotate(node->right);
    return left_rotate(node);
}

๊ตฌํ˜„

typedef struct node{
    int key;
    struct node * left, *right;
    int height; // ๋†’์ด
}Node;

Node * new_node(int key){
    Node * new = (Node *)malloc(sizeof(Node));
    new->key = key;
    new->height = 0; //์ƒˆ๋กœ์šด ๋…ธ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹จ๋ง ๋…ธ๋“œ์˜ height๋ฅผ ๊ฐ€์ง„๋‹ค.
    new->left = new->right = NULL;
    return new;
}

height

int height(Node * node){
    if(node == NULL) return -1;
    return node->height;
}

Right Rotation

/*
         z                                      y(l) 
        / \                                   /   \
       y   T4      Right Rotate (z)          x      z(node)
      / \          - - - - - - - - ->      /  \    /  \ 
     x   T3                               T1  T2  T3(r) T4
    / \
  T1   T2

*/
Node * right_rotate(Node * node){
    Node * l = node->left;
    Node * r = l->right;

    l->right = node;
    node->left = r;

    node->height = 1+MAX(height(node->left),height(node->right));
    l->height = 1+MAX(height(l->left),height(l->right));

    return l;
}

Left Rotation

/*
 z                                 y(r)
 /  \                            /     \ 
T1   y     Left Rotate(z)       z(node)  x
    /  \   - - - - - - - ->    / \      / \
   T2   x                     T1 T2(l) T3  T4
       / \
     T3  T4
*/

Node * left_rotate(Node * node){
    Node * r = node->right;
    Node * l = r->left;

    r->left = node;
    node->right =l;

    node->height = 1+MAX(height(node->left),height(node->right));
    r->height = 1+MAX(height(r->left),height(r->right));

    return r;
}

balance

int balance(Node * node){
    if(node == NULL) return 0;
    // ์–‘์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋†’์ด๋ฅผ ๋น„๊ตํ•œ๋‹ค.
    return height(node->left)-height(node->right);
}

์‚ฝ์ž…

์œ„์˜ Rotation์„ ์ด์šฉํ•ด์„œ ์‚ฝ์ž…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ์šฐ์„  BST์˜ ์‚ฝ์ž…๊ณผ ๊ฐ™์ด ์‚ฝ์ž…ํ•œ๋‹ค.

  2. ํ˜„์žฌ์˜ height๊ฐ’์„ updateํ•ด์ค€๋‹ค.

  3. balanceํ•œ์ง€ ํ™•์ธํ•œ๋‹ค.(-1๋ฏธ๋งŒ 1์ดˆ๊ณผ ๋ถˆ๊ท ํ˜•)

  4. ๋ถˆ๊ท ํ˜•ํ•˜๋‹ค๋ฉด LL, LR, RR, RL ์˜ ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ์— ๋งž๊ฒŒ rotationํ•ด์ค€๋‹ค.

Node * insert(Node * node, int key){

    // 1. ๋จผ์ € Binary Search Tree์˜ ์‚ฝ์ž…๊ณผ ๊ฐ™์ด ์‚ฝ์ž…ํ•œ๋‹ค.
    if (node == NULL)
        return(new_node(key));

    if (key < node->key)node->left  = insert(node->left, key);
    else if (key > node->key) node->right = insert(node->right, key);
    else return node; //๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” BST์˜ ์‚ฝ์ž…๊ณผ ๊ฐ™์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

    // 2. height๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.
    node->height = 1 + MAX(height(node->left),height(node->right));

    // 3. balanceํ•œ์ง€ ํ™•์ธ์„ ํ•œ๋‹ค.(-1๋ฏธ๋งŒ 1์ดˆ๊ณผ๋ฉด ๋ถˆ๊ท ํ˜•)
    int bal = balance(node);

    // 4. ๋ถˆ๊ท ํ˜•ํ•œ ๊ฒฝ์šฐ์— rotation!
    // LL
    if (bal > 1 && key < node->left->key) return right_rotate(node);

    // RR
    if (bal < -1 && key > node->right->key) return left_rotate(node);

    // LR ๋‘๋ฒˆํšŒ์ „(left->right)
    if (bal > 1 && key > node->left->key){
        node->left =  left_rotate(node->left);
        return right_rotate(node);
    }

    // RL
    if (bal < -1 && key < node->right->key){
        node->right = right_rotate(node->right);
        return left_rotate(node);
    }

    return node;
}

์‚ญ์ œ

  1. ์šฐ์„  BST์˜ ์‚ญ์ œ์™€ ๊ฐ™์ด ์‚ญ์ œํ•˜๋‹ค.

  2. ํ˜„์žฌ ๋…ธ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์‚ญ์ œ๋…ธ๋“œ์˜ ์กฐ์ƒ๋…ธ๋“œ ์ค‘ ํ•˜๋‚˜์—ฌ์•ผํ•œ๋‹ค.

  3. ํ˜„์žฌ์˜ height๊ฐ’์„ updateํ•ด์ค€๋‹ค.

  4. balanceํ•œ์ง€ ํ™•์ธํ•œ๋‹ค.(-1๋ฏธ๋งŒ 1์ดˆ๊ณผ ๋ถˆ๊ท ํ˜•)

  5. ๋ถˆ๊ท ํ˜•ํ•˜๋‹ค๋ฉด LL, LR, RR, RL ์˜ ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ์— ๋งž๊ฒŒ rotationํ•ด์ค€๋‹ค.

Node* delete(Node* root, int key){
    Node * tmp;

    // 1. BST ์‚ญ์ œ
    if (root == NULL) return root;
    if ( key < root->key )root->left = delete(root->left, key);
    else if( key > root->key ) root->right = delete(root->right, key);
    else{// ํ‚ค๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ทธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ
        if(root->left == NULL || root->right == NULL){
            if(root->left!=NULL)tmp = root->left;
            else tmp =root->right;

            // ์ž์‹์ด ์—†๋Š” ๊ฒฝ์šฐ
            if (tmp == NULL){
                tmp = root;
                root = NULL;
            }
            else *root = *tmp; // ์ž์‹์ด ํ•œ๊ฐœ์ธ ๊ฒฝ์šฐ

            free(tmp);
        }else{//์ž์‹์ด ๋‘๊ฐœ์ธ ๊ฒฝ์šฐ

            tmp = min(root->right);
            root->key = tmp->key;
            root->right = delete(root->right, tmp->key);
        }
    }

    // ์‚ญ์ œ ํ›„์— root๊ฐ€ NULL์ธ๊ฒฝ์šฐ
    if (root == NULL) return root;

    // ํ˜„์žฌ height๋ฅผ updateํ•ด์ค€๋‹ค.
    root->height = 1 + MAX(height(root->left),height(root->right));

    // balance ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
    int bal = balance(root);

    // ๋ถˆ๊ทœ์น™ํ•œ ๊ฒฝ์šฐ
    // LL
    if (bal > 1 && balance(root->left) >= 0)
        return right_rotate(root);

    // LR
    if (bal > 1 && balance(root->left) < 0){
        root->left =  left_rotate(root->left);
        return right_rotate(root);
    }

    // RR
    if (bal < -1 && balance(root->right) <= 0)
        return left_rotate(root);

    // RL
    if (bal < -1 && balance(root->right) > 0){
        root->right = right_rotate(root->right);
        return left_rotate(root);
    }

    return root;
}

Last updated