You are on page 1of 16

CSE 207: AVL Trees

Dr. Md. Shamsuzzoha Bayzid

Department of Computer Science and Engineering


Bangladesh University of Engineering and Technology
Binary Search Tree (BST)
Usually most operations on BST take time proportional to the
height of the tree

A BST with height h can contain at most 2h+1-1 nodes


 20 + 21+ 22 + ... + 2h = 2h+1 - 1
 n <= 2h+1-1
 h >= log2 n

20

21
log n

22

23
Binary Search Tree (BST)
The height may become larger than log2n

 If the keys come in a sorted way


 The height becomes linear in the number of elements!
 Worst case running time of different operations (insert, delete,
lookup)Θ(n)

9
AVL Trees
Adelson-Velski and Landis, 1962
Dynamically rebalance the tree keeping the ordering invariant

Ordering Invariant
x
At any node with key k in a binary search tree, all keys
of the elements in the left subtree are strictly less than k,
while all keys of the elements in the right subtree are
strictly greater than k.
<x x

Height Invariant

At any node in the tree, the heights of the left


and right subtrees differs by at most 1.
h-2
h-1
AVL Trees: Height Invariant

10

4 16

1 7 13 19

14

15

Height Invariant Violated


AVL trees are balanced
Ah

Ah: Smallest possible AVL tree


with height h
Size of Ak >= ch
Ah-2
c= (1+√5)/2 Ah-1

A0 A1 A2 A3 A4

A1
A2

n(A0) = 1; n(A1) = 2;
n(Ah) = n(Ah-1) + n(Ah-2) + 1
AVL trees are balanced
n: 1 2 4 7 12
A0 A1 A2 A3 A4

1 2 4 7 12

1 1 2 3 5 8 13
Fibonacci Sequence

n(h) = f(h+2) -1
f(h) = O(ch)
c= (1+√5)/2
AVL: Rebalancing strategy
Prototypical example

 Move child of the unbalanced node into parent position


 Parent becomes the child of the node that has been pushed up
 Other subtrees move in ways that BST allows

7 4

4 Rotation 1 7

1
AVL: Rebalancing strategy
Prototypical example
 Rotation may not work sometimes!

7 4

4 Rotation 7

5
1
AVL: Rebalancing strategy
Prototypical example
 Rotation may not work sometimes!

7 4

4 Rotation 7

5
5

7
7
5

4 Rotation Rotation
5
4 7
5 4
AVL: Rebalancing
Rotation

x y
Rotation

y x
Rotation

C
A

C B B A

y≤B<x
AVL: Rebalancing
x y

y x

h/h+1
Rotation

h+2
h
C
h+2

h/h+1

h
C B B A

y≤B<x

 At least one of B and C have height h+1, otherwise y cannot have height h+2
 Since node y is height balanced, the other node has a height of h or h+1
 Assume that h(C) = h+1 and h(B) = h/h+1. Does the rotation make it balanced?
 Assume that h(B) = h+1 and h(C) = h/h+1. Does the rotation make it balanced?
AVL: Rotation

zig
x y

zi g
y x

h/h+1
Rotation
zi g

h+2
za
g

h
C
h+2

h/h+1

h
C B Rotation B A

 Move downward from an unbalanced node, each time moving into the higher
subtree
 See if it is zig-zig or zig-zag
 Single rotation will suffice for zig-zig. Double rotation is needed for zig-zag
AVL: Double Rotation
 Since B has height h+1, it is not empty. So we can expand it one more level
 U and V has height h/h-1 (at least one of them has a height of h)

x x
zig

zi g
y y
Expand
za

za
g

g
h

h
h+2

h+2
A z A
h+1
h

h
C B C

h/h-1
U V
AVL: Double Rotation
x x

y z
Rotate at y

h
h+2

z A y A
h

C V
h/h-1

C U
U V

z C

Rotate at x
y x
h/h-1

h
h

C U V A
Practice

Rotations are not that hard to understand.


Yet, practice some examples to make yourself comfortable

You might also like