Presentation is loading. Please wait.

Presentation is loading. Please wait.

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.

Similar presentations


Presentation on theme: "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27."— Presentation transcript:

1 IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27 th Mar 2007 AVL Tree

2 2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree Definition Properties Operations Outline

3 3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 XX AVL Trees Unbalanced Binary Search Trees are bad. Worst case: operations take O(n). AVL (Adelson-Velskii & Landis) trees maintain balance. For each node in tree, height of left subtree and height of right subtree differ by a maximum of 1. H H-1 H-2

4 4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees 10 5 3 20 2 13 10 5 3 20 1 43 5

5 5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees 12 816 410 26 14

6 6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 12 816 410 26 14 1 Insertion for AVL Tree After insert 1

7 7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Insertion for AVL Tree To ensure balance condition for AVL-tree, after insertion of a new node, we back up the path from the inserted node to root and check the balance condition for each node. If after insertion, the balance condition does not hold in a certain node, we do one of the following rotations: Single rotation Double rotation

8 8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Insertions Causing Imbalance R P Q k1k1 k2k2 Q k2k2 P k1k1 R An insertion into the subtree: P (outside) - case 1 Q (inside) - case 2 An insertion into the subtree: Q (inside) - case 3 R (outside) - case 4 H P =H Q =H R

9 9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 A k2k2 B k1k1 C CB A k1k1 k2k2 Single Rotation (case 1) H A =H B +1 H B =H C

10 10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Single Rotation (case 4) C k1k1 B k2k2 A AB C k2k2 k1k1 H A =H B H C =H B +1

11 11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Q k2k2 P k1k1 R R P Q k1k1 k2k2 Problem with Single Rotation Single rotation does not work for case 2 and 3 (inside case) H Q =H P +1 H P =H R

12 12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 Double Rotation: Step C k3k3 A k1k1 D B k2k2 H A =H B =H C =H D

13 13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 Double Rotation: Step

14 14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 C k3k3 A k1k1 D B k2k2 Double Rotation H A =H B =H C =H D

15 15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 B k1k1 D k3k3 A C k2k2 B k1k1 D k3k3 A C k2k2 Double Rotation H A =H B =H C =H D

16 16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 3 Example Insert 3 into the AVL tree 11 8 20 4 16 27 8 8 11 4 20 3 16 27

17 17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Example Insert 5 into the AVL tree 5 11 8 20 4 16 278 11 5 20 4 16 27 8

18 18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees: Exercise Insertion order: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

19 19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Remove Operation in AVL Tree Removing a node from an AVL Tree is the same as removing from a binary search tree. However, it may unbalance the tree. Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node. Use the appropriate single or double rotation to balance the tree. May need to continue searching for unbalanced nodes all the way to the root.

20 20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Deletion X in AVL Trees Deletion: Case 1: if X is a leaf, delete X Case 2: if X has 1 child, use it to replace X Case 3: if X has 2 children, replace X with its inorder predecessor (and recursively delete it) Rebalancing

21 21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 1) 60 2070 10406585 5153050 8090 55

22 22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 1) 60 2070 10406585 5153050 8090 55

23 23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 2) 60 2070 10406585 5153050 8090 55

24 24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 2) 60 2070 10406585 51530 50 8090 55

25 25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 60 (case 3) 60 2070 10406585 5153050 8090 55 prev

26 26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 60 (case 3) 55 2070 10406585 5153050 8090

27 27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 3) 55 2070 10406585 5153050 8090 prev

28 28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 3) 50 2070 10406585 51530 8090

29 29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 3) 50 2070 10406585 51530 8090 prev

30 30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 3) 40 2070 10306585 515 8090

31 31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40 (case 3) 40 2070 10306585 515 8090 prev

32 32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40 : Rebalancing 30 2070 106585 515 8090 Case ?

33 33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40: after rebalancing 30 7010 206585 5 15 8090 Single rotation is preferred!

34 34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Minimum Element in AVL Tree An AVL Tree of height H has at least F H+3 -1 nodes, where F i is the i-th fibonacci number S 0 = 1 S 1 = 2 S H = S H-1 + S H-2 + 1 S H-1 S H-2 H H-1 H-2

35 35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree: analysis (1)

36 36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree: analysis (2) The depth of AVL Trees is at most logarithmic. So, all of the operations on AVL trees are also logarithmic. The worst-case height is at most 44 percent more than the minimum possible for binary trees.

37 37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Summary Find element, insert element, and remove element operations all have complexity O(log n) for worst case Insert operation: top-down insertion and bottom up balancing

38 38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/resources/animation/data- structure/avl/avltree.html Section 19.4 of Weiss book Further Study


Download ppt "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27."

Similar presentations


Ads by Google