Review: Balance in Binary Search Trees. Minimum Size of AVL Trees. Adelson-Velskii & Landis (AVL) Trees. Class #18: AVL Trees N(8)

Review: Balance in Binary Search Trees BinaryNode remove( T x, BinaryNode t ) ! { ! if( t == null )! return t;! int comp = x.compareTo( t.element );! ...
Author: Guest
0 downloads 0 Views 473KB Size
Review: Balance in Binary Search Trees BinaryNode remove( T x, BinaryNode t ) ! { ! if( t == null )! return t;! int comp = x.compareTo( t.element );! if( comp < 0 )! t.left = remove( x, t.left );! else if( compareResult > 0 )! t.right = remove( x, t.right );! else if( t.left != null && t.right != null )! {! t.element = findMin( t.right ).element;! t.right = remove( t.element, t.right );! }! else if( t.left != null ) ! t = t.left; ! else ! t = t.right;!

Class #18: AVL Trees Software Design III (CS 340): M. Allen, 04 March 16

return t;

! 

Analysis shows average depth of a node in a randomly generated BST with n inserted nodes is O(log n)

! 

However, trees are prone to becoming unbalanced ! 

!

}!

Friday, 4 Mar. 2016"

Adelson-Velskii & Landis (AVL) Trees !  1.  2. 

When deleting internal nodes, since we always take from the right-hand side when we need to replace a 2-child node, trees tend to become “left-heavy”

Software Design III (CS 340)"

2"

Minimum Size of AVL Trees

A simple self-adjusting tree with the properties: A properly ordered BST, with least nodes on left and greatest on right For each node, height of right and left subtrees differ by one at most AVL BST

Non-AVL BST

5

7

2

1

8

4

7

2

1

8

! 

4

! 

3 Friday, 4 Mar. 2016"

3

Suppose we want to build AVL tree of depth 9 that is as small as possible To do so, we must take the smallest such tree of depth 7, along with the smallest of depth 8, and add a root to them ! 

5

Software Design III (CS 340)"

N(8)

N(7)

3"

The total size is thus: N(9) = N(8) + N(7) + 1

Friday, 4 Mar. 2016"

Software Design III (CS 340)"

4"

1

Size and Efficiency of AVL Trees ! 

Size of AVL Trees

As long as the balance property is maintained, it is easy to show that for any height h, the minimum number of nodes in an AVL tree of that size is given by:

For h > 1 we have:

So, if we let i = bh/2c, then:

N (h) = N (h

N (h) > N (h N (h) > 2N (h

This allows us to show that: 1.  For n = N(h) nodes, height of AVL tree is O (log n) 2.  Every operation takes time O (log n)

2) > 2N (h

We use the concept of a rotation: a bottom-up operation to re-balance the tree properly after the insertion is performed

N (h) > 4N (h

N (h) > 2bh/2c ⇥ 2 N (h) > 2bh/2c+1

2)

Taking the logarithm of both sides yields:

4) AND N (h

4) > 2N (h

log2 N (h) > bh/2c + 1

6)

4) AND N (h) > 8N (h

h  2(bh/2c + 1) < 2 log2 N (h)

6)

Therefore, by definition: 2i)

Friday, 4 Mar. 2016"

h = O(log2 N (h)) Software Design III (CS 340)"

6"

! 

The only nodes that can possibly be unbalanced by an insertion are those that are between the new inserted node and the root

! 

We only need to look at the deepest unbalanced node

6

5

! 

2

8

4

Since we have made one insertion, the difference between subtree heights for such an unbalanced node will be exactly 2

! 

There are 4 basic cases, based on where we inserted: 1. 

7

2.  3. 

6

Software Design III (CS 340)"

4. 

7"

The first one we meet while going upward

!  Unbalanced

! 

Friday, 4 Mar. 2016"

2) + 1

Rotations in AVL Trees To insert:

3

2) + N (h

N (h) > 2bh/2c N (1)

And in general:

5"

Software Design III (CS 340)"

1

2)

Which gives us:

Inserting into an AVL Tree

! 

1) > N (h

2bh/2c)

2bh/2c)  1:

And this means that:

N (h) > 2i N (h

Standard BST insertion does not preserve the balance property, since certain branches may become too deep

Which means, since (h

The same argument will show that:

! 

! 

N (h) > 2bh/2c N (h

Therefore, we have:

N (h

Friday, 4 Mar. 2016"

2) + 1

Thus, since N (h) is strictly increasing, N (h) > N (h

N(h) = N(h – 1) + N(h – 2) + 1 with: N(0) = 1 N(1) = 2

1) + N (h

LL: LR: RL: RR:

Left subtree of Left child (“outside”) Right subtree of Left child (“inside”) Left subtree of Right child (“inside”) Right subtree of Right child (“outside”)

For any “outside” case, we do a single rotation, and for any “inside” case, we do a double rotation Friday, 4 Mar. 2016"

Software Design III (CS 340)"

8"

2

Single Rotations for “Outside” Cases ! 

!  !  ! 

! 

Single Rotations for “Outside” Cases

Consider the Left-Left case, and let n1 be deepest imbalanced node Subtree X becomes two levels deeper than the subtree Z Y must be shallower than X (or else tree imbalanced before insertion) Y must be deeper than Z (or n2 would actually be deepest imbalanced)

!  ! 

We can repair the problem by rotating just the two rooting nodes: n1

The same thing works for Right-Right case Since the final product is a tree under n1 that is exactly the same height (d + 1) as the one that was under n2 before the insertion, this means that everything above this part of the tree is now also re-balanced, so no more work will be needed

n2

n1

n2

n1

n2

Z Y

X

Y

d

Y

Z

d+1

X

d+2 Friday, 4 Mar. 2016"

Software Design III (CS 340)"

9"

A Single Rotation in an AVL Tree

1

8

4

3

! 

5

2

7

6

Unbalanced (Left-Left)

2

1

6

n1

n1

Z

3

Software Design III (CS 340)"

n2

n2

8

X

11"

d

X Z

d+1

Y Friday, 4 Mar. 2016"

10"

Consider the Left-Right case: n1 is deepest imbalanced node, but the inserted node ends up in “inner” tree Y A single rotation does not help us in this case

Rotated & Re-balanced

7

4

Software Design III (CS 340)"

Double Rotations for “Inside” Cases

6

5

X

Y

Z

d+2

Friday, 4 Mar. 2016"

! 

To insert:

n1

Z

d d+1

X

n2

Friday, 4 Mar. 2016"

d+2

Y Software Design III (CS 340)"

12"

3

Double Rotations for “Inside” Cases !  ! 

A Double Rotation in an AVL Tree

Since Y has at least one node in it (the inserted node, if nothing else), we can treat it as its own tree, with root n3 We then rotate twice, with n3 becoming the rooting node, and its subtrees linked below the other two n1

n2

Z

X

d d+1

Y1 Friday, 4 Mar. 2016"

Y2

n1

Y1

1

6

3

Y2

A

8

d+2

2

1

C

8

3

11

6

5

A

10 B

9

11

9 Software Design III (CS 340)"

13"

Friday, 4 Mar. 2016"

Software Design III (CS 340)"

14"

Efficiency of Operations (Worst Case)

A similar procedure will allow us to delete nodes: Method!

1. 

Re-balanced

Unbalanced Right-Left

10 B

5

C

Z

Deleting from an AVL Tree ! 

4

2

X

9

4

n3

n2

n3

To insert:

After deletion (in same way as for a BST), there can be imbalanced nodes between the deleted node’s former position and the root

2. 

We go from the deletion upwards, looking for any imbalance and correcting it on the basis of which descendant tree is largest

3. 

Again, the LL and RR cases are handled with single rotation, and the LR and RL cases with double rotation Friday, 4 Mar. 2016"

Software Design III (CS 340)"

Linked List!

AVL Tree!

O(N)!

O(N)!

O(log n)!

delete( T x )!

O(N)!

O(N)!

O(log n)!

contains( T x )!

O(N)!

O(N)!

O(log n)!

size()!

O(1)!

O(1)!

O(1)!

! 

15"

Array List!

insert( T x )!

! 

Remember: Use of BST/AVL tree requires sortable data, and some bounds become worse if we want duplicates If we are going to keep data sorted, we can speed up Array List contains() operation to O(log n), using binary search

Friday, 4 Mar. 2016"

Software Design III (CS 340)"

16"

4

Next Week ! 

Topic: B+ Trees and Bit Vectors

! 

Read: finish Text, Chapter 04

! 

Meetings: normal schedule ! 

Code day Friday (in lab)

! 

Homework 03: Friday, 11 March, 5:00 PM

! 

Office Hours: Wing 210 !  ! 

Tuesday & Thursday: 10:00–11:30 AM Tuesday & Thursday: 4:00–5:30 PM

Friday, 4 Mar. 2016"

Software Design III (CS 340)"

17"

5