AVL Trees • Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. • Idea: keep the tree balanced after each operation. • Solution:

Download Report

Transcript AVL Trees • Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. • Idea: keep the tree balanced after each operation. • Solution:

AVL Trees

• Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations.

• Idea: keep the tree balanced after each operation.

• Solution: AVL (Adelson-Velskii and Landis) trees.

• AVL tree property: for every node in the tree, the height of the left and right subtrees differs by at most 1.

TCSS 342 AVL Trees v1.0

1

6 1 3 4 5 8 7 AVL tree 11 6 4 8 1 5 7 11 3 not an AVL tree 2 TCSS 342 AVL Trees v1.0

2

AVL trees: find, insert

• AVL tree find is the same as BST find.

• AVL insert: same as BST insert, except that we might have to “fix” the AVL tree after an insert.

• These operations will take time O(

d

), where

d

is the depth of the node being found/inserted.

• What is the maximum height of an

n

-node AVL tree?

TCSS 342 AVL Trees v1.0

3

AVL tree insert

• • Let

x

be the deepest an imbalance occurs.

node where Four cases to consider. The insertion is in the 1. left subtree of the left child of

x.

2. right subtree of the left child of

x.

3. left subtree of the right child of

x.

4. right subtree of the right child of

x.

Idea: Cases 1 & 4 are solved by a single rotation .

Cases 2 & 3 are solved by a double rotation .

TCSS 342 AVL Trees v1.0

4

Single rotation example

15 5 20 3 2 4 10 17 21 1 15 3 1 2 5 4 10 17 20 21 TCSS 342 AVL Trees v1.0

5

Single rotation in general

a height h+2 before insert b Z h h+1 X h Y h  -1 X < b < Y < a < Z b height h+2 after insert a h+1 X h Y TCSS 342 AVL Trees v1.0

Z h 6

Cases 2 & 3

h X a height h+2 before insert b Y h+1 Z h h  -1 b a h X Y h+1 single rotation fails TCSS 342 AVL Trees v1.0

Z h 7

Double rotation, step 1

15 8 17 3 4 4 3 5 6 10 15 16 8 17 6 10 16 5 TCSS 342 AVL Trees v1.0

8

Double rotation, step 2

15 8 17 6 10 16 4 3 5 6 3 4 5 8 10 15 16 17 TCSS 342 AVL Trees v1.0

9

Double rotation in general

height h+2 before insert a b h  0 c h Z h W h h-1 Y X height h+2 after insert W < b

Z h 10

Depth of an AVL tree

Theorem: Any AVL tree with

n

nodes has height less than 1.441 log

n.

Proof: Given an

n

-node AVL tree, we want to find an upper bound on the height of the tree.

Fix

h

. What is the smallest

n

such that there is an AVL tree of height

h

with

n

nodes?

Let S h be the set of all AVL trees of height

h

that have as few nodes as possible.

TCSS 342 AVL Trees v1.0

11

Let w h be the number of nodes in any one of these trees.

w 0 = 1, w 1 = 2 Suppose T  T L and T R S h , where h  2. Let be T’s left and right subtrees. Since T has height

h

, either T L or T R Suppose it’s T R .

has height

h

-1. By definition, both T L AVL trees. In fact, T and T R R  S are h-1 or else it could be replaced by a smaller AVL tree of height

h

-1 to give an AVL tree of height

h

that is smaller than T.

TCSS 342 AVL Trees v1.0

12

Similarly, T L  S h-2 .

Therefore, w h = 1 + w h-2 + w h-1 .

Claim:  For h  0, w h = (1 +  5) / 2    1.6.

h , where Proof: The proof is by induction on

h

.

Basis step

:

h

=0. w 0

h

=1. w 1 = 1 =  0 .

= 2 >  1 .

Induction step

: Suppose the claim is true for 0  m 

h

, where

h

 1.

TCSS 342 AVL Trees v1.0

13

Then w h+1  = 1 + w h-1 1 +  h-1 + + w  h h (by the i.h.) = 1 +  h-1 (1 +  ) = 1 +  h+1 (1+  >  h+1 =  2 ) Thus, the claim is true.

From the claim, in an

n

-node AVL tree of height

h

,

n h

 w h   h (from the Claim)  log 

n

= (log

n

) / (log  ) < 1.441 log

n

TCSS 342 AVL Trees v1.0

14

AVL tree: Running times

• find takes O(log

n

) time, because height of the tree is always O(log

n

).

• insert : O(log

n

) time because we do a find (O(log

n

) time), and then we may have to visit every node on the path back to the root, performing up to 2 single rotations (O(1) time each) to fix the tree.

• remove : O(log

n

) time. Left as an exercise.

TCSS 342 AVL Trees v1.0

15