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 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 5 7 8 2 1 4 3 7 8 2 1 4 3 • Both are binary search trees, which is an AVL tree? 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 6 α 8 2 2 7 α 1 5 4 3 7 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 6 α 9 2 2 7 α 1 6 8 9 8 4 5 AVL Tree Single Rotation • 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 Z X k1 X k3 k1 k2 X k2 Y Y Z AVL Tree Single Rotation • Symmetric case k2 k1 k1 k2 X k3 Y Z Z k3 Y Z 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 X k3 k1 Z k2 Y k2 k1 D k2 A B A C k3 B C D Double Rotation – in more detail • A Double Rotation is like 2 single rotations. – Example: Left-right double rotation. k3 Single Left Rotation at K1 k1 k3 k2 D k2 A B D k1 C A B C k2 k1 A k3 B C D 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 16 8 48 24 40 28 36 56 44 52 60 58 62 Deletion Example 32 16 48 24 40 28 36 56 44 52 60 58 62 Deletion Example 32 24 16 48 28 40 36 56 44 52 60 58 62 Deletion Example 48 32 56 24 16 52 40 28 36 44 60 58 62