290 likes | 449 Views
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.
E N D
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 • Both are binary search trees, which is an AVL tree? 5 7 8 2 8 2 1 4 7 1 4 3 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: • 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: • 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 7 8 9 4 3
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 k2 k3 k1 k2 Z X k1 Y Z X X Y
AVL Tree Single Rotation • Symmetric case k2 k1 k1 k3 X k2 Z Z Y k3 Y Z
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 k3 k2 k1 Z k1 D k1 k3 k2 A k2 X A B C D Y C B
Double Rotation – in more detail • 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
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 48 8 24 40 56 28 36 44 52 60 58 62
Deletion Example 32 16 48 24 40 56 28 36 44 52 60 58 62
Deletion Example 32 32 24 48 48 16 16 28 40 56 56 36 44 52 60 58 62
Deletion Example 48 32 56 24 52 60 40 58 62 16 16 28 36 44