Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees CS II – Fall 2010 9/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.

Similar presentations


Presentation on theme: "AVL Trees CS II – Fall 2010 9/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."— Presentation transcript:

1 AVL Trees CS II – Fall /8/2010

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

3 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

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

5 Bad binary tree Requiring balancing at the root is not enough.

6 Binary Search Trees Both are binary search trees, which is an AVL tree?

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

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

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

10 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 α α α

11 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 α α α

12 AVL Tree Single Rotation k3k3 k2k2 Z Y X X We want X up a level and Z down a level. We want k 2 as the new root. – Pretend the tree is flexible, grab hold of k 2, letting gravity take hold. – k 3 > k 2 (by BST property), so k 3 becomes k 2 ’s right subtree. – X and Z can remain attached to the same point, – and Y becomes k 3 ’s subtree to maintain BST property. k3k3 k2k2 ZY X k1k1 k1k1

13 AVL Tree Single Rotation k1k1 k2k2 X Y Z Symmetric case k2k2 Y Z Z k1k1 k3k3 k3k3

14 Single Rotation Examples

15 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. k3k3 k1k1 Z X Y k2k2 k3k3 k1k1 D A B k2k2 C k1k1 DABC k2k2 k3k3

16 Double Rotation – in more detail A Double Rotation is like 2 single rotations. – Example: Left-right double rotation. k3k3 k1k1 D A B k2k2 C k1k1 DABC k2k2 k3k3 k1k1 A B k2k2 C k3k3 D Single Left Rotation at K 1 Single Right Rotation at K 3

17 Double Rotation Examples

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

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

20 Deletion Example

21 Deletion Example

22 Deletion Example

23 Deletion Example


Download ppt "AVL Trees CS II – Fall 2010 9/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."

Similar presentations


Ads by Google