Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Similar presentations


Presentation on theme: "AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012."— Presentation transcript:

1 AVL Tree Smt Genap

2 Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap

3 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. Smt Genap XX H H-1 H-2

4 AVL Trees Smt Genap

5 AVL Trees Smt Genap

6 Insertion for AVL Tree After insert 1 Smt Genap

7 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 Smt Genap

8 Insertions Causing Imbalance 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 Smt Genap R P Q k1k1 k2k2 Q k2k2 P k1k1 R H P =H Q =H R

9 Single Rotation (case 1) Smt Genap A k2k2 B k1k1 C CB A k1k1 k2k2 H A =H B +1 H B =H C

10 Single Rotation (case 4) Smt Genap C k1k1 B k2k2 A AB C k2k2 k1k1 H A =H B H C =H B +1

11 Problem with Single Rotation Single rotation does not work for case 2 and 3 (inside case) Smt Genap Q k2k2 P k1k1 R R P Q k1k1 k2k2 H Q =H P +1 H P =H R

12 Double Rotation: Step Smt Genap C k3k3 A k1k1 D B k2k2 C k3k3 A k1k1 D B k2k2 H A =H B =H C =H D

13 Double Rotation: Step Smt Genap C k3k3 A k1k1 D B k2k2

14 Double Rotation Smt Genap C k3k3 A k1k1 D B k2k2 C k3k3 A k1k1 D B k2k2 H A =H B =H C =H D

15 Double Rotation Smt Genap B k1k1 D k3k3 A C k2k2 B k1k1 D k3k3 A C k2k2 H A =H B =H C =H D

16 Example Insert 3 into the AVL tree Smt Genap

17 Example Insert 5 into the AVL tree Smt Genap

18 AVL Trees: Exercise Insertion order: ◦ 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 Smt Genap

19 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. Smt Genap

20 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 Smt Genap

21 Delete 55 (case 1) Smt Genap

22 Delete 55 (case 1) Smt Genap

23 Delete 50 (case 2) Smt Genap

24 Delete 50 (case 2) Smt Genap

25 Delete 60 (case 3) Smt Genap prev

26 Delete 60 (case 3) Smt Genap

27 Delete 55 (case 3) Smt Genap prev

28 Delete 55 (case 3) Smt Genap

29 Delete 50 (case 3) Smt Genap prev

30 Delete 50 (case 3) Smt Genap

31 Delete 40 (case 3) Smt Genap prev

32 Delete 40 : Rebalancing Smt Genap Case ?

33 Delete 40: after rebalancing Smt Genap Single rotation is preferred!

34 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 Smt Genap S H-1 S H-2 H H-1 H-2

35 AVL Tree: analysis (2) The depth of AVL Trees is at most logarithmic. So, all of the operations on AVL trees are also logarithmic. Smt Genap

36 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 Smt Genap

37 Further Study Section 19.4 of Weiss book Smt Genap


Download ppt "AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012."

Similar presentations


Ads by Google