430 likes | 555 Views
AVL Trees. Drozdek Section 6.7.2 pages 257 -262. 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. Tree Balancing.
E N D
AVL Trees Drozdek Section 6.7.2 pages 257 -262
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.
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)
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)
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.
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.
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.
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.
Examples: AVL Trees Numbers in the circles are the balance factors, not the values stored in the nodes. Nyhoff page 841
Examples: Not AVL Trees Nyhoff page 841
AVL Trees Insert DE • Insertions may require adjustment of the tree to maintain balance. • Given tree • Becomes unbalanced • Requires a right rotation on subtree RI • Producing balanced tree
Observation • Inserting a new item into an AVL tree can result in a subtree having a balance factor (absolute value) greater than 1. • But no more than 2. • Inserting a new item does not necessarily increase the balance factor. • 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.
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.
Add PA Tree is still balanced.
Add DE Tree is now unbalanced. A right rotation of RI will restore balance.
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.
Add GA Tree is still balanced.
Add OH The subtree rooted at DE is now unbalanced. A left rotation of DE will restore balance.
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.
After Left Rotation at GA Now we can do a right rotation at PA obtaining a balanced tree.
Insert IL and MI Tree is still balanced.
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.
After Right Rotation at MA Now we can do a left rotation at GA, restoring balance.
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!
After Left Rotation at IL Now we can do a right rotation at OH, restoring balance.
Insert VT Produces an inbalance at PA, which can be removed with a simple left rotation to yield:
Insert TX and WY This does not unbalance the tree, resulting in the final version.
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.
Rebalancing Rotations • Single right rotationUse when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the left subtree of its left child. • Single left rotationUse when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in right subtree of its right child. • Left-right rotationUse when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the right subtree of its left child. • Right-left rotationUse when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in the left subtree of its right child.
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.
Single Right Rotation Note that the right subtree of B R(B) flips over to become the left subtree of A.
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
Single Left Rotation • Symmetrical with single right rotation. • Exchange “right” and “left” in description of single right rotation.
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
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.
Left-Right Rotation Suppose we insert a new node into the left subtree of C, increasing the height of that subtree. Nyhoff page 851
Left-Right Rotation Do a left rotation at B (left child of first out-of-balance ancestor of added node.)
Left-Right Rotation Note that the left subtree of C L(C) flips over to become the right subtree of B. Now do a right rotation at A.
Left-Right Rotation The right subtree of C R(C) has become the left subtree of A.
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. • Set left link of A to point to C. • Set right link of B equal to left link of C • Set left link of C to point to B Now a right rotation of A and C. • Reset link from parent of A to point to C • Set left link of A equal to right link of C • Set right link of C to A
Right-Left Rotation • The Right-Left Rotation is symmetrical. • In the description of the Left-Right rotation, exchange “right” and “left” .
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
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: • 10 5 1 20 8 15 25 • The resulting tree should be complete and balanced. • Repeat the exercise with the integers added in the following order: • 5 1 10 20 15 8 25 • The final result should be the same, but some intermediate results will be different.