AVL Trees CS II – Fall 2010 9/8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have...

23
AVL Trees CS II – Fall 2010 9/8/2010

Transcript of AVL Trees CS II – Fall 2010 9/8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have...

AVL Trees

CS II – Fall 20109/8/2010

Announcements

• HW#2 is posted– Uses AVL Trees, so you have to implement an AVL

Tree class. Most of the code is provided in the book, but the remove is left out.• Still not an easy assignment.

• HW#1 due tonight on Webcourses.

Summary

• Last time we talked about heaps– Which can be used to implement a priority queue,

or heapsort, and have many applications.– We’ll see more of heaps in HW#3

• Today we’re going to talk about:– AVL Trees AND– Disjoint Sets

AVL Trees

• Invented by Adelson-Velskii and Landis.• A binary search tree with a balance condition.– Easy to maintain.– Ensures the depth is O(log N)

• Balance condition – for every node in the tree, the height of the left and right subtrees can differ by at most 1.

Bad binary tree

• Requiring balancing at the root is not enough.

Binary Search Trees

• Both are binary search trees, which is an AVL tree?

5

82

1 4

3

7

7

82

1 4

3 5

Inserting in an AVL Tree

• When we do an Insertion– Update all the balancing information for the

nodes on the path back to the root.• As we are updating the balance information on the

path up to the root, we may find a node whose new balance violates the AVL condition.

• What if the insertion violated the AVL tree property?– We do a simple modification to the tree known as

a rotation.

Insertion in an AVL Tree

• We will show how to rebalance the tree at the first violated node– Then prove that this guarantees that the entire

tree then satisfies the AVL property.

Insertion in an AVL tree• Let us call the node that must be rebalanced α

– Since any node has at most 2 children, and a height imbalance requires that α’s 2 subtrees’ height differ by 2, there are 4 violation cases:

1) An insertion into the left subtree of the left child of α.

2) An insertion into the right subtree of the left child of α.

3) An insertion into the left subtree of the right child of α.

4) An insertion into the right subtree of the right child of α.

α

α

α

α

Insertion in an AVL tree• Outside cases (left-left or right-right), fixed by a

single rotation:1) An insertion into the left subtree of the left child of α.4) An insertion into the right subtree of the right child of α.

6

82

1 5

4

7

3

6

72

8

9

α

α

Insertion in an AVL tree• Inside cases (right-left or left-right), fixed by a

double rotation:2) An insertion into the right subtree of the left child of α.3) An insertion into the left subtree of the right child of α.

7

92

1 6

4

8

5

6

72

9

8

α

α

AVL Tree Single Rotation

k3

k2 Z

Y

X

X

• We want X up a level and Z down a level.• We want k2 as the new root.

– Pretend the tree is flexible, grab hold of k2, letting gravity take hold.

– k3 > k2 (by BST property), so k3 becomes k2’s right subtree. – X and Z can remain attached to the same point, – and Y becomes k3’s subtree to maintain BST property.

k3

k2

ZYXk1

k1

AVL Tree Single Rotation

k1

k2X

Y

Z

• Symmetric case

k2

YZ

Z

k1

k3

k3

Single Rotation Examples

Double Rotation• Place k2 as the new root.

– This forces k1 to be k2’s left child and k3 to be its right child.– Then the 4 subtrees A,B,C,D fall according to BST rules.

k3

k1

Z

X

Y

k2

k3

k1 D

A

B

k2

C

k1

DA B C

k2

k3

Double Rotation – in more detail• A Double Rotation is like 2 single rotations.

– Example: Left-right double rotation.

k3

k1 D

A

B

k2

C

k1

DA B C

k2

k3

k1

A B

k2

C

k3

D

Single Left Rotation at K1

Single Right

Rotation at K3

Double Rotation Examples

Deletion from an AVL Tree

• In an insert there is at most one node that needs to be rebalanced.– But in a delete there may be multiple nodes to be

rebalanced.– Technically only one rebalance that happens at a

node, but once that happens it may affect the ancestral nodes.

Deletion from an AVL Tree– If the node is a leaf, remove it.

• Retrace back up the tree starting with the parent of deleted node to the root, adjusting the balance factor as needed.

– If it’s not a leaf, replace with the largest in its left subtree (inorder predecessor) and remove that node.• You could also replace with the smallest in its right subtree

(inorder successor), but you should use inorder predecessor in HW #2.

– After deletion, retrace the path back up the tree, starting with the parent of the replacement, to the root, adjusting the balance factor as needed.

Deletion Example

32

4816

8 24

28

40 56

52 6036 44

58 62

Deletion Example

32

4816

24

28

40 56

52 6036 44

58 62

Deletion Example

32

48

40 56

52 6036 44

58 62

16

24

2816

32

48

56

Deletion Example

40 52 60

36 44 58 6216

24

2816

32

48

56