Report

AVL Trees Drozdek Section 6.7.2 pages 257 -262 1 Objectives You will be able to Describe an AVL tree. Describe the operations by which an AVL tree is maintained as nearly balanced as items are added. Add a new node to the diagram of an AVL tree. 2 Tree Balancing Consider a BST of state abbreviations. If the states are entered in the order NY, IL, GA, RI, MA, PA, DE, IN, VT, TX, OH, WY. the tree will be balanced. When tree is nicely balanced, search time is O(log2n) 3 Tree Balancing: AVL Trees If abbreviations entered in increasing order DE, GA, IL, IN, MA, MI, NY, OH, PA, RI, TX, VT, WY BST degenerates into linked list with search time O(n) 4 AVL Tree A height balanced tree Tree is rebalanced after additions and deletions make it skewed beyond a certain limit. Search time is O(log2n) http://en.wikipedia.org/wiki/AVL_tree Named after its two inventors: G.M. Adelson-Velskii and E.M. Landis Adelson-Velskii, G.; E. M. Landis (1962). "An algorithm for the organization of information". Proceedings of the USSR Academy of Sciences 146: 263–266. (Russian) English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962. 5 Balance Factor The Balance Factor of a node is defined as height of its left subtree minus height of its right subtree. Note: Some authors define balance factor as right - left. Drozdek defines balance factor as right - left. Examples in this presentation are from a different book. A balance factor with absolute value greater than 1 is unacceptable for an AVL tree. 6 AVL Tree Definition Defined as Binary search tree Balance factor of each node is 0, 1, or -1 Key is to rebalance the tree whenever an insertion or a deletion creates a balance factor with absolute value greater than 1 at any node. 7 AVL Tree as an ADT The differences between an AVL tree and the Binary Search Tree that we have studied previously are purely internal. As seen from the outside they are identical. 8 Examples: AVL Trees Numbers in the circles are the balance factors, not the values stored in the nodes. Nyhoff page 841 9 Examples: Not AVL Trees Nyhoff page 841 10 AVL Trees Insertions may require adjustment of the tree to maintain balance. Given tree Insert DE Becomes unbalanced Requires a right rotation on subtree RI Producing balanced tree 11 Observation Inserting a new item into an AVL tree can result in a subtree having a balance factor (absolute value) greater than 1. Inserting a new item does not necessarily increase the balance factor. But no more than 2. May leave it unchanged. May decrease it. If the balance factor is unacceptable after an insertion, it can be fixed by one of four possible rebalancing rotations. 12 Examples Let’s look at how we can rebalance the tree while adding states. We will add states to the tree in the order RI, PA, DE, GA, OH, MA, IL, MI, IN, NY, VT, TX, WY Start by adding RI to an empty tree. 13 Add PA Tree is still balanced. 14 Add DE Tree is now unbalanced. A right rotation of RI will restore balance. 15 Rotations AVL tree rotations rearrange links so as to reduce the height of the larger subtree at an unbalanced node. They always preserve the BST invariants: Everything in a node's left subtree is less than or equal to it. Everything in a node's right subtree is greater than or equal to it. 16 Add GA Tree is still balanced. 17 Add OH The subtree rooted at DE is now unbalanced. A left rotation of DE will restore balance. 18 Add MA The subtree rooted at PA is now unbalanced. We need to do a right rotation at PA. But we can’t do that immediately with a simple right rotation. We need to do a left rotation at GA first. This is a left-right rotation. 19 After Left Rotation at GA Now we can do a right rotation at PA obtaining a balanced tree. 20 Insert IL and MI Tree is still balanced. 21 Insert IN The subtree rooted at GA is now unbalanced. Again we need a double rotation to restore balance. Start with a right rotation at MA. 22 After Right Rotation at MA Now we can do a left rotation at GA, restoring balance. 23 Insert NY The subtree rooted at OH is now unbalanced. We will need a left-right double rotation in the left subtree of OH. Notice what happens to IN! 24 After Left Rotation at IL Now we can do a right rotation at OH, restoring balance. 25 Insert VT Produces an inbalance at PA, which can be removed with a simple left rotation to yield: 26 Insert TX and WY This does not unbalance the tree, resulting in the final version. 27 Observations When an unacceptable imbalance was produced, we always did a rotation at the first ancestor of the added node having an unacceptable imbalance. We always rotated that node away from side with the larger subtree height. Its child with the larger subtree height takes its place and it becomes a new child of that node. If the first two steps from the first unblanced ancestor toward the added node were in the same direction, a single rotation restored balance. Otherwise, a double rotation was necessary. 28 Rebalancing Rotations 1. Single right rotation 2. Single left rotation 3. Left-right rotation 4. Right-left rotation Use when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the left subtree of its left child. Use when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in right subtree of its right child. Use when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the right subtree of its left child. Use when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in the left subtree of its right child. 29 Single Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the left subree of its left child. First two steps from ancestor to inserted node are both to the left. Shown graphically on next slide. Described in detail on the slide following that. 30 Single Right Rotation Note that the right subtree of B R(B) flips over to become the left subtree of A. 31 Single Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the left subree of its left child. Let A = nearest out of balance ancestor of inserted item B = left child of A Set link from parent of A to point to B. Set left link of A to point to the right child of B. Set right link of B to point to A 32 Single Left Rotation Symmetrical with single right rotation. Exchange “right” and “left” in description of single right rotation. 33 Single Left Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of -2 (heavy to the right) and the inserted node is in the right subree of its rightchild. Let A = nearest out of balance ancestor of inserted item B = right child of A Set link from parent of A to point to B. Set right link of A to point to the left child of B. Set left link of B to point to A 34 Left-Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the right subree of its left child. First two steps from ancestor to inserted node are in opposite directions (left then right). Shown graphically on following slides Described in detail on slides following them. 35 Left-Right Rotation Suppose we insert a new node into the left subtree of C, increasing the height of that subtree. Nyhoff page 851 36 Left-Right Rotation Do a left rotation at B (left child of first out-of-balance ancestor of added node.) 37 Left-Right Rotation Now do a right rotation at A. Note that the left subtree of C L(C) flips over to become the right subtree of B. 38 Left-Right Rotation The right subtree of C R(C) has become the left subtree of A. 39 Left-Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the right subree of its left child. Let A = nearest ancestor of the inserted item. B = left child of A C = right child of B New item added to subtree rooted at C. First do a left rotation of B and C. 1. Set left link of A to point to C. 2. Set right link of B equal to left link of C 3. Set left link of C to point to B Now a right rotation of A and C. 1. Reset link from parent of A to point to C 2. Set left link of A equal to right link of C 3. Set right link of C to A 40 Right-Left Rotation The Right-Left Rotation is symmetrical. In the description of the Left-Right rotation, exchange “right” and “left” . 41 Summary Adding nodes to a binary tree can result in an unbalanced tree. But we can restore balance with one of four kinds of rotations. Single Left Single Right Left-Right Right-Left 42 Exercise Draw by hand, showing the tree after each step with the balance factor at each node, as the following values are added to an AVL tree of integers: The resulting tree should be complete and balanced. Repeat the exercise with the integers added in the following order: 10 5 1 20 8 15 25 5 1 10 20 15 8 25 The final result should be the same, but some intermediate results will be different. 43