1 / 33

CISC 220 - Data Structures

CISC 220 - Data Structures. Ben Perry University of Delaware Summer 2011. Quiz. Draw the resulting binary search tree after inserting the following nodes: 9, 13, 2, 5, 12, 15, 1, 4, 7, 11

clint
Download Presentation

CISC 220 - Data Structures

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. CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011

  2. Quiz • Draw the resulting binary search tree after inserting the following nodes:9, 13, 2, 5, 12, 15, 1, 4, 7, 11 • Suppose a binary search tree had 64 nodes, but the layout is unknown. How many nodes will you have to traverse on average to find the minimum node? • How many nodes on worst case would it take to find the minimum?

  3. Reminder • Office hours immediately after this class. • Or by appointment. • Try to complete the lab sooner rather than later • Email me or the TA if you have any questions

  4. Binary Search Tree • Elements are sorted • Simple rule: • Left descendants are less than parent • Right descendants are greater than parent

  5. Binary Search Tree • Absolute worst case: Inserting an already sorted list. • Example: insert 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 • You get a linked list.

  6. AVL trees • Recall • Height = distance to furthest descendant leaf • Depth = path length from root • If we can guarantee balanced trees, our worst case for all operations becomes O(log(n)) instead of O(n). • AVL trees do just that. (Adelson-Velskii and Landis)

  7. AVL trees • An AVL tree is a tree where, for each node, the difference between the height of that node’s left and right subtrees is 1 or 0. • NOT an AVL tree: AVL Tree: 7 6 8 6

  8. Smallest AVL tree with height 9

  9. Insert and Rotation in AVL Trees • Insert operation may cause balance factor to become 2 or –2 for some node • only nodes on the path from insertion point to root node have possibly changed in height • So after the Insert, go back up to the root node by node, updating heights • If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotationaround the node AVL Trees

  10. Single Rotation in an AVL Tree 2 2 6 6 1 2 1 1 4 9 4 8 0 0 0 0 1 0 0 7 9 1 5 8 1 5 0 7 AVL Trees

  11. Insertions in AVL Trees Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. AVL Trees

  12. AVL Insertion: Outside Case j Consider a valid AVL subtree k h Z h h X Y AVL Trees

  13. AVL Insertion: Outside Case j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X AVL Trees

  14. AVL Insertion: Outside Case j Do a “right rotation” k h Z h+1 h Y X AVL Trees

  15. Single right rotation j Do a “right rotation” k h Z h+1 h Y X AVL Trees

  16. Outside Case Completed “Right rotation” done! (“Left rotation” is mirror symmetric) k j h+1 h h X Z Y AVL property has been restored! AVL Trees

  17. AVL Insertion: Inside Case j Consider a valid AVL subtree k h Z h h X Y AVL Trees

  18. AVL Insertion: Inside Case j Inserting into Y destroys the AVL property at node j Does “right rotation” restore balance? k h Z h h+1 X Y AVL Trees

  19. AVL Insertion: Inside Case k “Right rotation” does not restore balance… now k is out of balance j h X h h+1 Z Y AVL Trees

  20. AVL Insertion: Inside Case j Consider the structure of subtree Y… k h Z h h+1 X Y AVL Trees

  21. AVL Insertion: Inside Case j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 W V AVL Trees

  22. AVL Insertion: Inside Case j We will do a left-right “double rotation” . . . k Z i X W V AVL Trees

  23. Double rotation : first rotation j left rotation complete i Z k W V X AVL Trees

  24. Double rotation : second rotation j Now do a right rotation i Z k W V X AVL Trees

  25. Double rotation : second rotation right rotation complete Balance has been restored i j k h h h or h-1 V Z W X AVL Trees

  26. Implementation balance (1,0,-1) key left right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree AVL Trees

  27. Insertion in AVL Trees • Insert at the leaf (as for all BST) • only nodes on the path from insertion point to root node have possibly changed in height • So after the Insert, go back up to the root node by node, updating heights • If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node AVL Trees

  28. Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then {T := new tree; T.data := x; height := 0; return;} case T.data = x : return ; //Duplicate do nothing T.data > x : Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : Insert(T.right, x); code similar to the left case Endcase T.height := max(height(T.left),height(T.right)) +1; return; } AVL Trees

  29. Example of Insertions in an AVL Tree 2 20 Insert 5, 40 0 1 10 30 0 0 35 25 AVL Trees

  30. Example of Insertions in an AVL Tree 2 3 20 20 1 1 1 2 10 30 10 30 0 0 0 1 0 0 5 35 5 35 25 25 0 40 Now Insert 45 AVL Trees

  31. Single rotation (outside case) 3 3 20 20 1 2 1 2 10 30 10 30 0 2 0 0 0 1 5 35 5 40 25 25 0 0 35 45 40 1 Imbalance 0 45 Now Insert 34 AVL Trees

  32. Double rotation (inside case) 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 1 5 Imbalance 40 5 40 25 30 0 25 34 1 0 45 35 45 0 Insertion of 34 0 34 AVL Trees

  33. AVL Tree Deletion • Similar but more complex than insertion • Rotations and double rotations needed to rebalance • Imbalance may propagate upward so that many rotations may be needed. AVL Trees

More Related