You are on page 1of 20

CSE 326 Lecture 8: Getting to know AVL Trees

! Todays Topics: " Balanced Search Trees # AVL Trees and Rotations # Splay trees ! Covered in Chapter 4 of the text

R. Rao, CSE 326

Recall from Last Time: AVL Trees


! AVL trees are height-balanced binary search trees ! Balance factor of a node =

height(left subtree) - height(right subtree)


! An AVL tree can only have balance factors +1, 0, or 1

at every node
" Height of an empty subtree is defined to be -1
! Implementation: Store current heights in each node and

calculate balance factors when needed from subtrees root nodes.


R. Rao, CSE 326 2

Is this tree AVL?

6 3 Heights 1 4 1 0 9 2 7 1 8 0

R. Rao, CSE 326

Is this tree AVL?

6 3 Heights 1 4 1 0 9 2 7 1 8 0

-1 6 1 4 0 1 9

Balance factors

1-(-1) = 2

7 (-1)-0 = -1 8 0
No! Aint ALV.

R. Rao, CSE 326

Why AVL?
! Can prove: Height of an AVL tree of N nodes is always

O(log N) (see previous lecture and textbook)


! Run time for accessing any node is therefore O(log N)

6 0 0 4 0 1 9 1 5 8 0 0 1 4 Height = O(log N) 0 1

6 0 7 -1 0 9

! One problem: Insert/Remove may upset AVL balance


R. Rao, CSE 326 5

Insert Example
No longer an AVL tree 6 0 1 5 0 4 7 -1 0 9 Insert 3 6 1

2
1 4 0 3

7 -1 0 9

Problem: Insert may cause balance factor to become 2 or 2 for some node on the path from insertion point to root node
R. Rao, CSE 326 6

Restoring Balance
! Idea: After Inserting the new node,

1. Back up to root updating heights along the access path 2. If Balance Factor = 2 or 2, adjust tree by rotation around deepest such node. 6 1

2
1 4 0 3
R. Rao, CSE 326

7 -1 0 9

Rotating to restore Balance: A Simple Example

6 0 1 5 0 4 7 -1 0 9

Insert 3

6 1

Rotate 0 4 0 3

6 0 7 -1 5 0 AVL 9 0

2
1 4 0 3

7 -1 0 9

AVL

Not AVL

R. Rao, CSE 326

Various Cases of Insertion

BF = 1 BF = 1 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0

Tree before insertion (BF = Balance Factor)

R. Rao, CSE 326

Outside Case

BF = ? BF = ? BF = ? BF = ? BF = 0 BF = 0 BF = 0

Tree after insertion

R. Rao, CSE 326

10

Inside Case

BF = BF = BF = BF = BF = 0 BF = BF = BF = BF = BF =

Tree after insertion

R. Rao, CSE 326

11

Insertions in AVL Trees


Let the node that needs rebalancing be !. There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of !. 2. Insertion into right subtree of right child of !. Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of !. 4. Insertion into left subtree of right child of !. Rebalancing is performed through four separate rotation algorithms.
R. Rao, CSE 326 12

Insertions in AVL Trees: Outside Case


Consider a valid AVL subtree

j Z

k X
R. Rao, CSE 326

Y
13

Insertions in AVL Trees: Outside Case


Inserting into X destroys the AVL property

j Z Y

k X
R. Rao, CSE 326 14

Insertions in AVL Trees: Outside Case

j k

Do a right rotation

Z Y X
R. Rao, CSE 326 15

Insertions in AVL Trees: Outside Case

j k

Do a right rotation: 1. Make j point to Y

Z Y X
R. Rao, CSE 326 16

Insertions in AVL Trees: Outside Case

k j X
R. Rao, CSE 326

Do a right rotation: 2. Make k point to j

Z
17

AVL property has been restored!


(Left rotation is mirror symmetric)

Insertions in AVL Trees: Inside Case


Consider a valid AVL subtree

j Z

k X
R. Rao, CSE 326

Y
18

Insertions in AVL Trees: Inside Case


Inserting into Y destroys the AVL property

j k

Does right rotation restore balance?

Z X
R. Rao, CSE 326

Y
19

Insertions in AVL Trees: Inside Case

k j X Z Y
R. Rao, CSE 326

Right rotation dont do nothin fa dis tree

20

Insertions: Inside Case Take 2


Consider the structure of subtree Y

j Z

k X
R. Rao, CSE 326

Y
21

Insertions in AVL Trees: Inside Case


Y = node i and subtrees V and W

j i Z W
22

k X V
R. Rao, CSE 326

Insertions in AVL Trees: Inside Case

j k X V
R. Rao, CSE 326

Lets try a left-right double rotation . . .

i W

23

Insertions in AVL Trees: Inside Case


Steps for Left-Right Double Rotation

1. Left Rotation: Adjust relevant pointers

i k W X
R. Rao, CSE 326

Z V
24

Insertions in AVL Trees: Inside Case


Steps for Left-Right Double Rotation

i j W

k X
R. Rao, CSE 326

2. Right Rotation: Adjust relevant pointers 3. Make i the root

Z
25

Balance has been restored! (Right-left case is mirror-symmetric)

AVL Tree On Board Exercise


! Insert 8, 1, 0, 2 in that order into following AVL tree:

4 3 6

R. Rao, CSE 326

26

Pros and Cons of AVL Trees


Arguments for AVL trees: 1. Search is O(log N) since AVL trees are always balanced. 2. The height balancing adds no more than a constant factor to the speed of insertion. (Why?) Arguments against using AVL trees: 1. Difficult to program & debug; more space for height info. 2. Asymptotically faster but can be slow in practice. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 4. May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (enter Splay Trees) 27 R. Rao, CSE 326

Did someone say spay??

R. Rao, CSE 326

28

Splay Trees
Splay trees are tree structures that: 1. Are not perfectly balanced all the time 2. Allow actual Find operations to balance the tree so that future operations may run faster Based on the heuristic: If X is accessed once, it is likely to be accessed again. - After node X is accessed, perform splaying operations to bring X up to the root of the tree. - Do this in a way that leaves the tree more balanced as a whole.
R. Rao, CSE 326 29

Splaying: A Motivating Example


Initial tree After splaying with R

After Find(R)
Splay Idea: Get R up to the root using rotations

R. Rao, CSE 326

30

Splay Tree Terminology


Let X be a non-root node with " 2 ancestors. Let P be its parent node. Let G be its grandparent node. G P X
R. Rao, CSE 326

G P X P

G P

X
31

Splay Tree Operations


1. Nodes must contain a parent pointer.

element left

right

parent

2. When X is accessed, apply one of six rotation operations: Single Rotations (X has a P but no G) zig-left, zig-right Double Rotations (X has both a P and a G) zig-zig-left, zig-zig-right zig-zag-left, zig-zag-right
R. Rao, CSE 326 32

Splay Trees: Zig operation


! Zig is just a single rotation, as in an AVL tree ! Suppose R was the node that was accessed (e.g. using Find)

Zig-right

! Zig-right moves R to the top


R. Rao, CSE 326

can access R faster next time


33

Splay Trees: Zig operation


! Suppose Q is accessed (e.g. using Find)

Zig-left

! Zig-left moves Q to the top


R. Rao, CSE 326 34

Splay Trees: Zig-Zig operation


! Zig-Zig consists of two single rotations of the same type

(assume R is the node that was accessed):

Zig (Zig-right)

Zig (Zig-right)

! Again, due to zig-zig splaying, R has bubbled to the top! ! Note: Parent-Grandparent rotated first.
R. Rao, CSE 326 35

Splay Trees: Zig-Zag operation


! Zig-Zag consists of two rotations of the opposite type

(assume R is the node that was accessed):

Zig (Zig-left)

Zag (Zig-right)

! Zig-Zag splaying also causes R to move to the top.

R. Rao, CSE 326

36

Splay Trees: Example

R. Rao, CSE 326

37

Splay Trees: Do-It-Yourself Exercise


! Insert the keys 1, 2, , 7 in that order into an empty splay

tree.
! What happens when you access 7?

R. Rao, CSE 326

38

Analysis of Splay Trees: Amortization


Examples suggest that splaying causes tree to get balanced. The actual analysis is rather advanced and is in Chapter 11. Result of Analysis: Any sequence of M operations on a splay tree of size N takes O(M log N) time. So, the amortized running time for one operation is O(log N). This guarantees that even if the depths of some nodes get very large, you cannot get a long sequence of O(N) searches because each search operation causes a rebalance. Without splaying, total time could be O(MN).
R. Rao, CSE 326 39

Next Class: Beyond Binary Trees: B-trees To Do:

Have a great weekend!

Finish Chapter 4 and Start Chapter 6 Homework # 2


R. Rao, CSE 326 40

You might also like