Tuesday 11 March 2014

AVL Trees



Definitions

Trees of height O(log n) are said to be balanced. AVL trees consist of a special case in which the subtrees of each node differ by at most 1 in their height.
Balanced trees can be used to search, insert, and delete arbitrary keys in O(log n) time. In contrast, height-biased leftist trees rely on non-balanced trees to speed-up insertions and deletions in priority queues.

Height

Claim: AVL trees are balanced.
Proof. Let Nh denote the number of nodes in an AVL tree of depth h
Nh> Nh-1 + Nh-2 + 1
> 2Nh-2 + 1
> 1 + 2(1 + 2Nh-4)
= 1 + 2 + 22N h-4
> 1 + 2 + 22 + 23N h-6
...
> 1 + 2 + 22 + 23 + ... + 2h/2
= 2h/2 1
Hence,
2h/2 1< n
h/2< log 2(+ 1)
h< 2 log 2(+ 1)
A more careful analysis, based on Fibonacci numbers theory, implies the tighter bound of 1.44 log 2(+ 2).

 Rotations

LL             ----Ao ------||
          ----        --  |-
         Bo-          - AR - h
      ---   ---       -|  --
   ----|     ---||      |||
  --   --   --   --
h --BL --   --BR --h
   -|||-     ||||-
     Do    |-------Bo---
   -- ||-       -----
h --BL  -         -Ao-
   -| |--       ---  ----
    |||      -|--|    ---||-
     |      --   --   -    -
     Do    h --BR --   - AR - h
             ||||-    -|||--
RR    |-------Ao---
   --  |-       -----
h --AL  -         -Bo-
   -| |--       ---  ----
    |||      -|--|    ---||-
            --   --   -    -
          h --BL --   - BR -h
             |||--    -|||--
                        Do             ----Bo ------||
          ----        --  |-
         Ao-          - BR -h
      ---- ----       -|  --
   ----|     ---||     ||||
  --   --   --   --     |
h --AL  -   --BL --h    Do
   -|||-     |||--
LR                -----Ao ---------||
           ------            --  |-
         --Bo|               --AR  -h
      ----  ||||             -| |--
  ----|-       |||            |||
  -    -         ||
h - BL -       ||Co||
  -|||--     |||    ||||
           --| |-   -- ||
        h- 1 -CL  -   -CR -|h- 1
            |||-    -|||-
             Do         --------Co--------
      --Bo--            --Ao---
  ------    --||    -----    ---||-
  - B  -   --  --   -   -|  --A   -
h -  L -   -CL --h- 1 -CR -   --  R -h
  -|||--    |||-     |||-    -|||-
             Do
RL   |----------Ao-----
  --  |-            -----
h -AR  -               |Bo--
  -|  --             |||   ----
   |||             |||       ---||-
                  ||         -    -
                |Co||        -BR  -h
             |||    ||||     -|||--
           --| |-   -- ||-
        h- 1 -CL  -   -CR -|h- 1
            |||--   -|||-
             Do         --------Co--------
      --Ao--            --Bo---
  ------    --||    -----    ---||-
  - A  -   --  --   -   -|  --B   -
h -  L--   -CL --h- 1 -CR -   --  R -h
  -|||-     |||-     |||-    -|||-
             Do
LL
&
LR
==>
LL
             ----Ao ------||
          ----        --  |-
         Bo-          - AR - h
      ---   ---       -|  --
   ----|     ---||      |||
  --   --   --   --
h --BL --   --BR --h
   -|||-     ||||-
     Do        Eo    |-------Bo---
   -- ||-       -----
h --BL  -         -Ao-
   -| |--       ---  ----
    |||      -|--|    ---||-
     |      --   --   -    -
     Do    h --BR --   - AR - h
             ||||-    -|||--
              Eo

Insertions and Deletions

Insertions and deletions are performed as in binary search trees, and followed by rotations to correct imbalances in the outcome trees. In the case of insertions, one rotation is sufficient. In the case of deletions, O(log n) rotations at most are needed from the first point of discrepancy going up toward the root.
               o
            --5 ---
         ---      ----
      o---           -- o
     |3||            --8---
    ||  ||        ----    ---
  2o     4o      7o-          11o
  |             |           || ||
 ||            ||          ||   ||
1o             6o          10o|     12o
                         ||
                         |
                        9o
Delete 4            --5o---
         ----     ----
      o---           -- o
     |3              --8---
    ||             ---    ---
   o|            o--        --o
  2             |7           11|
 ||            ||          ||  |||
o|             o          o|     |o
1             6          |10      12
                         |
                        9o
Imbalance at ‘3’ implies a LL rotation with ‘2’        ---5o---
      ---     ---
   o---          --o
  |2|            --8--
 || ||        ----    ---
 o   o       o-         --o
1    3      7           |11|
           ||          ||  ||
          o|          o|    |o
          6          10     12
                    ||
                   9o
Imbalance at ‘5’ implies a RR rotation with ‘8’.              --8o---
           ----     ----
        o---           -- o
      |5 ||             |11|
    |||   ||           ||  ||
   o|      ||o        o|    |o
  |2|       7        10     12
 || ||     ||       ||
 o   o    o|        o
1    3    6        9

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS