1 / 23

Data Structures – LECTURE Balanced trees

Data Structures – LECTURE Balanced trees. Motivation Types of balanced trees AVL trees Properties Balancing operations. Motivation.

Download Presentation

Data Structures – LECTURE Balanced 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. Data Structures – LECTURE Balanced trees • Motivation • Types of balanced trees • AVL trees • Properties • Balancing operations

  2. Motivation • Binary search trees are useful for efficiently implementing dynamic set operations: Search, Successor, Predecessor, Minimum, Maximum, Insert, Delete in O(h) time, where h is the height of the tree • When the tree is balanced, that is, its height h = O(lg n), the operations are indeed efficient. • However, the Insert and Delete alter the shape of the tree and can result in an unbalanced tree. In the worst case, h = O(n)  no better than a linked list!

  3. Balanced trees • Find a method for keeping the tree always balanced • When an Insert or Delete operation causes an imbalance, we want to correct this in at most O(lg n) time  no complexity overhead. • Add a requirement on the height of subtrees • The most popular balanced tree data structures: • AVL trees: subtree height difference of at most 1 • Red-Black trees: subtree height difference ≤ 2(lg n + 1) • 2-3 B-trees: same subtree height, varying # of children

  4. x h Sh h-1 h-2 Sh-2 Sh-1 Sh=Sh–1+ Sh–2 + 1 Size of tree: AVL trees An AVL tree is a binary tree with one balance property: For any node in the tree, the height difference between its left and right subtrees is at most one. AVL: Adelson-Velsky and Landis, 1962

  5. 12 8 16 4 10 14 2 6 1 Examples 12 8 16 4 10 14 2 6 Not an AVL tree (nodes 8 and 12) An AVL tree

  6. root h = lg n h-1 h-1 or h-2 Tright Tleft AVL tree properties • What is the minimum and maximum height h of an AVL tree with n nodes? • Study the recursion: T(n) = T(n-1)+ T(n-2) + 1 • Minimum height: fill all levels except the last one

  7. x h Sh h-1 h-2 Sh-2 Sh-1 Maximal height of an AVL tree • All levels have a difference of height of 1. • The smallest AVL tree of depth 1 has 1 node. • The smallest AVL tree of depth 2 has 2 nodes. • In general, Sh = Sh-1+ Sh-2+ 1(S1= 1; S2 = 2)

  8. golden ratio! Maximal AVL height: complexity • Study the Fibonacci series recursion T(n) = T(n-1)+ T(n-2) (T1= 0; T2 = 2) • Special case of linear recurrence equation! 2T(n-2) ≤ T(n)≤ 2T(n-1) 2└n/2┘ ≤ T(n)≤ 2n-1 • The solution is T(n) = cn where c is a constant (prove guess!) T(n) = T(n-1)+ T(n-2) cn = cn-1 +cn-2 c2 = c+1  h = lg1.6n O(lg n) S(n) = T(n) +1 same complexity

  9. Balancing AVL trees • Before the operation, the tree is balanced. • After an insertion or deletion operation, the tree might become unbalanced.  fix subtrees that became unbalanced. • The height of any subtree has changed by at most 1. Thus, if a node is not balanced, the difference between its children heights is 2. • Four possible cases with a height difference of 2.

  10. k1 k2 k2 k1 A C B B C A Cases of imbalance (1) Case 1: The left subtree is higher than the right subtree because of the left subtree of the left child Case 4: The right subtree is higher than the left subtree because of the right subtree of the right child

  11. k2 k1 k1 k2 C A A C B B Cases of imbalance (2) Case 2: The left subtree is higher than the right subtree because of the right subtree of the left child Case 3: The right subtree is higher than the left subtree because of the left subtree of the right child

  12. k1 right rotation k2 A B C Fixing Case 1: right rotation k2 • The rotation takes O(1) time and restores the balance • Insertion – The height of subtree A is increased. After the rotation, the tree has the same height as before the insert. • Deletion – The height of subtree C is decreased. After the rotation, the tree has a lower height by 1 from before k1 C B A

  13. 12 k1 4 16 k2 A 2 8 14 B C 1 6 10 Case 1: Insertion example 12 k2 8 16 k1 C 4 10 14 A B 2 6 1 Insert 1

  14. k2 left rotation k1 A B C Fixing Case 4: left rotation k2 k1 k1 k2 A B C Analysis as in Case 1

  15. left rotation k1 k1 delete k2 5 7 7 5 9 k2 k2 k1 9 Case 4: deletion example 5 4 7 9

  16. k1 right rotation k2 A C B Fixing Case 2 – first try … k2 k1 C A B • A single rotation is not enough • A series of rotations on the subtree of k1 to reduce • Case 2 to Case 1!

  17. left rotation on k1 right rotation on k3 k3 k2 k2 k1 k3 k1 C A C B2 B1 B2 A B1 Fixing Case 2: right then left rotation k3 k1 k2 C A B1 B2

  18. 12 12 k3 k2 8 16 6 16 k2 k3 k1 6 10 14 4 8 14 C k1 4 2 5 10 C B1 A 2 5 B1 A left rotation on 4 right rotation on 8 Case 2: insertion example 12 k3 8 16 k1 4 10 14 k2 C 2 6 A B1 5 no B2

  19. right rotation on k3 left rotation on k1 k2 k1 k3 A C B1 B2 Fixing Case 3: right then left rotation k1 k3 k2 A C B1 B2 Analysis as in Case 2

  20. Insert and delete operations (1) • Insert/delete the element as in a regular binary search tree, and then re-balance. • Observation: only nodes on the path from the root to the node that was changed may become unbalanced. • Assume we go left: the right subtree was not changed, and stays balanced. This holds as we descend the tree.

  21. Insert and delete operations (2) • After adding/deleting a leaf, go up, back to the root. • Re-balance every node on the way as necessary. • The path is O(lg n) long, and each node balance takes O(1), thus the total time for every operation is O(lg n). • For the insertion we can do better: when going up, after the first balance, the subtree that was balanced has height as before, so all higher nodes are now balanced again. We can find this node in the pass down to the leaf, so one pass is enough.

  22. Example: deletion requiring two passes 18 12 21 7 13 3 8 10 15 9 11 14 16 17 delete 3

  23. Summary • Efficient dynamic operations on a binary tree require a balance tree whose height is O(lg n) • Balanced height trees: • AVL trees • Red-black trees • B-trees • Insertion and deletion operations might require re-balancing in O(lg n) to restore balanced tree properties • Re-balancing with left and/or right rotations on a case by case basis.

More Related