6.006 Lecture 06: AVL Trees, AVL Sort

6.006 Lecture 06: AVL Trees, AVL Sort

Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 Lecture 6: Balanced Binary Search Trees Lecture Overview • The importance of being balanced • AVL trees { Definition and balance { Rotations { Insert • Other balanced trees • Data structures in general • Lower bounds Recall: Binary Search Trees (BSTs) • rooted binary tree • each node has { key { left pointer { right pointer { parent pointer See Fig. 1 3 41 2 20 65 1 29 50 0 0 11 1 26 0 Figure 1: Heights of nodes in a BST 1 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 x ≤x ≥x Figure 2: BST property • BST property (see Fig. 2). • height of node = length (# edges) of longest downward path to a leaf (see CLRS B.5 for details). The Importance of Being Balanced: • BSTs support insert, delete, min, max, next-larger, next-smaller, etc. in O(h) time, where h = height of tree (= height of root). • h is between lg n and n: Fig. 3. vs. Perfectly Balanced Path Figure 3: Balancing BSTs • balanced BST maintains h = O(lg n) ) all operations run in O(lg n) time. 2 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 AVL Trees: Adel'son-Vel'skii & Landis 1962 For every node, require heights of left & right children to differ by at most ±1. • treat nil tree as height -1 • each node stores its height (DATA STRUCTURE AUGMENTATION) (like subtree size)(alternatively, can just store difference in heights) This is illustrated in Fig. 4 k-1 k Figure 4: AVL Tree Concept Balance: Worst when every node differs by 1 | let Nh = (min.) # nodes in height-h AVL tree =) Nh = Nh−1 + Nh−2 + 1 > 2Nh−2 h=2 =) Nh > 2 =) h < 2 lg Nh Alternatively: Nh > Fh (nth Fibonacci number) • In fact Nh = Fn+1 − 1 (simple induction) p 'h 1 + 5 • Fh = p rounded to nearest integer where ' = ≈ 1:618 (golden ratio) 5 2 • =) max. h ≈ log' n ≈ 1:440 lg n 3 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 AVL Insert: 1. insert as in simple BST 2. work your way up tree, restoring AVL property (and updating heights as you go). Each Step: • suppose x is lowest node violating AVL • assume x is right-heavy (left case symmetric) • if x's right child is right-heavy or balanced: follow steps in Fig. 5 x y y k-1 k+1 Left-Rotate(x) k x A k C k-1 B C k k-1 A k-1 B x y x k-1 z k+1 Left-Rotate(x) k+1 k A C k B C k k-1 A k B Figure 5: AVL Insert Balancing • else: follow steps in Fig. 6 x y k+1 Right-Rotate(z) x z k k-1 z k+1 Left-Rotate(x) k A k-1 k y k-1 A B or C D k-1 D k-1 k-2 k-1 or C B k-2 Figure 6: AVL Insert Balancing • then continue up to x's grandparent, greatgrandparent . 4 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 Example: An example implementation of the AVL Insert process is illustrated in Fig. 7 Insert(23) x = 29: left-left case 3 41 41 1 1 2 20 65 20 65 50 0 11 50 0 0 11 1 29 0 2 29 26 1 26 0 0 23 Done Insert(55) 3 41 3 41 1 2 1 2 20 65 20 65 50 0 11 50 0 0 11 1 26 0 1 26 0 23 29 0 0 23 29 0 x=65: left-right case Done 41 3 41 2 20 2 65 2 20 1 55 50 1 0 50 0φ65 11 1 26 11 1 26 0 0 55 0 0 23 29 0 0 23 29 0 Figure 7: Illustration of AVL Tree Insert Process Comment 1. In general, process may need several rotations before done with an Insert. Comment 2. Delete(-min) is similar | harder but possible. 5 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 AVL sort: • insert each item into AVL tree Θ(n lg n) Θ(n) • in-order traversal Θ(n lg n) Balanced Search Trees: There are many balanced search trees. AVL Trees Adel'son-Velsii and Landis 1962 B-Trees/2-3-4 Trees Bayer and McCreight 1972 (see CLRS 18) BB[α] Trees Nievergelt and Reingold 1973 Red-black Trees CLRS Chapter 13 (A) | Splay-Trees Sleator and Tarjan 1985 (R) | Skip Lists Pugh 1989 (A) | Scapegoat Trees Galperin and Rivest 1993 (R) | Treaps Seidel and Aragon 1996 (R) = use random numbers to make decisions fast with high probability (A) = \amortized": adding up costs for several operations =) fast on average For example, Splay Trees are a current research topic | see 6.854 (Advanced Algorithms) and 6.851 (Advanced Data Structures) Big Picture: Abstract Data Type(ADT): interface spec. vs. Data Structure (DS): algorithm for each op. There are many possible DSs for one ADT. One example that we will discuss much later in the course is the \heap" priority queue. Priority Queue ADT heap AVL tree Q = new-empty-queue() Θ(1) Θ(1) Q.insert(x) Θ(lg n) Θ(lg n) x = Q.deletemin() Θ(lg n) Θ(lg n) x = Q.findmin() Θ(1) Θ(lg n) ! Θ(1) 6 Lecture 6 Balanced Binary Search Trees 6.006 Fall 2011 Predecessor/Successor ADT heap AVL tree S = new-empty() Θ(1) Θ(1) S.insert(x) Θ(lg n) Θ(lg n) S.delete(x) Θ(lg n) Θ(lg n) y = S.predecessor(x) ! next- Θ(n) Θ(lg n) smaller y = S.successor(x) ! next-larger Θ(n) Θ(lg n) 7 MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Fall 2011 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us