1 / 14

AVL Trees

AVL Trees. It’s a balancing act. Binary Tree Problems. If you get either sorted or reverse-sorted input, you essentially get a linked list (always following either the right or left child respectively) This can yield searches on the order of n

tate
Télécharger la présentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Trees It’s a balancing act

  2. Binary Tree Problems • If you get either sorted or reverse-sorted input, you essentially get a linked list (always following either the right or left child respectively) • This can yield searches on the order of n • We would seek to take advantage of the log n search time which a balanced tree can provide.

  3. What is a balanced tree? This is not a balanced tree. Clearly the left-sub tree is deeper than the right sub-tree

  4. What is a balanced tree? This is a balanced tree. Clearly the left-sub tree is the same depth as the right sub-tree.

  5. Problems with balanced trees • A perfectly balanced tree is far to strict of a restriction to place on a binary tree

  6. Loosening the rules • Perhaps we could allow the left and right sub-trees to differ by at most one? • This is the solution which was proposed by G. M. Adel'son-Velskii and E. M. Landis (two Russian math guys) • Henceforth known as the AVL Tree

  7. So... Why should we use it? • While the AVL Tree does not yield a true log n search time, we can achieve an order 1.44 log n solution (pretty close) • Relatively easy to implement (there are a maximum of two modifications to the tree after an insert) • You will be tested over it

  8. Overview of AVL Trees • AVL tree is height-balanced: for every node in the tree, the height of the left and right sub-trees differ by at most one. • We will always have to rebalance the tree after each deletion or insertion (if needed) to keep the tree balanced, and we'll perform rotations to do that.

  9. Overview of AVL Trees • Insertion and deletion are handled in the same manner as with an ordinary BST. • A value is kept in each node which denotes the balance condition of that node or the current height of the node (depending on implementation)

  10. The balancing act • We use single and double rotations to keep the balance.

  11. Balancing act (single rotation) A B B AR BL A BL BR C BR AR C This is the case where we just inserted node C into the left sub-tree of node A, causing an imbalance. After performing the single right rotation, we arrive at a balanced tree again. The mirror case is easily derived.

  12. Balancing act (double rotation) A A C B AR C AR B A BL C B CR BL CL CR AR CL CR BL CL D D D A node was inserted into the sub-tree CL, making the tree off balance by 2 at the root. We first make a left rotation around the node B, placing the CL sub-tree into the right child of B. Then we continue with a left rotation around the root which brings node B (together with its children) up a level and sub-tree AR is pushed down a level (together with node A).

  13. Oh Boy… that sounds fun • At this point you may think that the algorithm is going to be really complex, due to all the rotations and manipulations, but in fact, there will be at most one rotation (single or double) per insertion. To see this, imagine that the tree pictured above is a sub-tree of a bigger tree. Since before the insertion the tree was AVL balanced, it must have been that the height of each of its sub-trees differed at most by one. After the insertion the height of the tree above increased by one, but since we made a rotation it decreased back by one, so in the end the tree above remained at its original height and no more changes to the bigger tree will be needed. [http://www.cs.oberlin.edu/classes/dragn/labs/avl/avl5.html]

  14. AVL Applet Java models of AVL, Splay, and Red Black Trees [http://www.seanet.com/users/arsen/avltree.html] Try sequence: 20, 10, 30, 8, 6*, 9+ *will cause a single left rotation (not effecting root) +will cause a double rotation (effects root)

More Related