- 55 Views
- Uploaded on
- Presentation posted in: General

AVL Trees

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

AVL Trees

CS II – Fall 2010

9/8/2010

- 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.

- 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.
- HW#1 due tonight on Webcourses.

- 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

- 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.

- Requiring balancing at the root is not enough.

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

5

7

8

2

8

2

1

4

7

1

4

3

3

5

- 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.

- Update all the balancing information for the nodes on the path back to the root.
- What if the insertion violated the AVL tree property?
- We do a simple modification to the tree known as a rotation.

- 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.

- 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:
- 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 α.

α

α

α

α

- Outside cases (left-left or right-right), fixed by a single rotation:
- An insertion into the left subtree of the left child of α.
4) An insertion into the right subtree of the right child of α.

- An insertion into the left subtree of the left child of α.

6

6

α

8

2

2

7

α

1

5

7

8

9

4

3

- 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

6

α

9

2

2

7

α

1

6

8

9

8

4

5

- 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

k3

k1

k2

Z

X

k1

Y

Z

X

X

Y

- Symmetric case

k2

k1

k1

k3

X

k2

Z

Z

Y

k3

Y

Z

- 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

k3

k2

k1

Z

k1

D

k1

k3

k2

A

k2

X

A

B

C

D

Y

C

B

- A Double Rotation is like 2 single rotations.
- Example: Left-right double rotation.

k3

Single Left Rotation at K1

k3

k1

D

k2

D

A

k2

k1

C

C

A

B

B

Single Right Rotation at K3

k2

k1

k3

A

B

C

D

- 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.

- 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.

32

16

48

8

24

40

56

28

36

44

52

60

58

62

32

16

48

24

40

56

28

36

44

52

60

58

62

32

32

24

48

48

16

16

28

40

56

56

36

44

52

60

58

62

48

32

56

24

52

60

40

58

62

16

16

28

36

44