objectives
play

Objectives Memahami variant dari Binary Search Tree yang balanced - PowerPoint PPT Presentation

Objectives Memahami variant dari Binary Search Tree yang balanced Struktur Data & Algoritme ( Data Structures & Algorithms ) AVL Tree Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter


  1. Objectives � Memahami variant dari Binary Search Tree yang balanced Struktur Data & Algoritme ( Data Structures & Algorithms ) AVL Tree Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0 - I nternal Use Only SDA/ TOPIC/ V2.0/ 2 Outline Motivation � AVL Tree � Unbalanced Binary Search Tree would make all operations take O(n) operation - worst case. � Definition � Property � operations SDA/ TOPIC/ V2.0/ 3 SDA/ TOPIC/ V2.0/ 4 1

  2. AVL Trees AVL Trees � Untuk setiap node dalam tree, ketinggian subtree di 10 anak kiri dan subtree di anak kanan hanya berbeda 10 maksimum 1. 5 5 20 5 20 X X 43 3 3 H H-2 1 H-1 2 1 3 SDA/ TOPIC/ V2.0/ 5 SDA/ TOPIC/ V2.0/ 6 AVL Trees Insertion for AVL Tree � After insert 1 12 12 8 16 8 16 4 10 14 4 10 14 2 6 2 6 1 SDA/ TOPIC/ V2.0/ 7 SDA/ TOPIC/ V2.0/ 8 2

  3. Insertion for AVL Tree Unbalanced Condition � To ensure balance condition for AVL-tree, after H P = H Q = H R k 2 k 1 insertion of a new node, we back up the path from the inserted node to root and check the balance k 1 k 2 condition for each node. � If after insertion, the balance condition does not hold in a certain node, we do one of the following rotations: R P P Q Q R � Single rotation � Double rotation � An insertion into the subtree: � An insertion into the subtree: � P (outside) - case 1 � Q (inside) - case 3 � Q (inside) - case 2 � R (outside) - case 4 SDA/ TOPIC/ V2.0/ 9 SDA/ TOPIC/ V2.0/ 10 Single Rotation (case 1) Single Rotation (case 4) H A = H B + 1 H A = H B H B = H C H C = H B + 1 k 2 k 1 k 2 k 1 k 1 k 2 k 1 k 2 A C C A A B B B B C C A SDA/ TOPIC/ V2.0/ 11 SDA/ TOPIC/ V2.0/ 12 3

  4. Problem with Single Rotation Double Rotation: Step � Single rotation does not work for case 2 and 3 ( inside case ) k 3 k 3 k 2 k 1 k 1 k 2 k 1 k 2 k 2 k 1 R P D D P R Q Q A C B C B A H Q = H P + 1 H P = H R H A = H B = H C = H D SDA/ TOPIC/ V2.0/ 13 SDA/ TOPIC/ V2.0/ 14 Double Rotation: Step Double Rotation k 3 k 2 k 2 k 1 k 1 k 3 k 1 k 3 k 2 D B C A D A B C A D B C H A = H B = H C = H D SDA/ TOPIC/ V2.0/ 15 SDA/ TOPIC/ V2.0/ 16 4

  5. Double Rotation Example � Insert 3 into the AVL tree k 2 k 1 11 11 k 3 k 1 k 3 k 2 8 8 20 4 20 A A B C D 4 16 27 3 16 D 8 27 B C 3 H A = H B = H C = H D SDA/ TOPIC/ V2.0/ 17 SDA/ TOPIC/ V2.0/ 18 Example AVL Trees: Exercise � Insertion order: � Insert 5 into the AVL tree � 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 11 11 8 8 20 5 20 4 16 27 4 16 8 27 5 SDA/ TOPIC/ V2.0/ 19 SDA/ TOPIC/ V2.0/ 20 5

  6. Remove Operation in AVL Tree Deletion X in AVL Trees � Removing from AVL Tree is same as removing from a � Deletion: binary search tree procedure with this difference that � Case 1: jika X adalah leaf, delete X it may unbalance the tree. � Case 2: jika X punya 1 child, X digantikan oleh child � Similar to insertion, starting from the removed node tsb. we check all the nodes in the path up to the root for � Case 3: jika X punya 2 child, ganti X secara rekursif the first unbalance node. dengan predecessor-nya secara inorder � We use the appropriate single or double rotation to � Rebalancing balance the tree. � We may need to continue searching for unbalanced node all the way to the root. SDA/ TOPIC/ V2.0/ 21 SDA/ TOPIC/ V2.0/ 22 Delete 55 (case 1) Delete 55 (case 1) 60 60 20 70 20 70 10 40 65 85 10 40 65 85 80 90 80 90 5 15 30 50 5 15 30 50 55 55 SDA/ TOPIC/ V2.0/ 23 SDA/ TOPIC/ V2.0/ 24 6

  7. Delete 50 (case 2) Delete 50 (case 2) 60 60 20 70 20 70 10 40 65 85 10 40 65 85 50 80 90 80 90 5 15 30 50 5 15 30 55 55 SDA/ TOPIC/ V2.0/ 25 SDA/ TOPIC/ V2.0/ 26 Delete 60 (case 3) Delete 60 (case 3) 60 55 20 70 20 70 10 40 65 85 10 40 65 85 prev 80 90 80 90 5 15 30 50 5 15 30 50 55 SDA/ TOPIC/ V2.0/ 27 SDA/ TOPIC/ V2.0/ 28 7

  8. Delete 55 (case 3) Delete 55 (case 3) 55 50 20 70 20 70 prev 10 40 65 85 10 40 65 85 80 90 80 90 5 15 30 50 5 15 30 SDA/ TOPIC/ V2.0/ 29 SDA/ TOPIC/ V2.0/ 30 Delete 50 (case 3) Delete 50 (case 3) 50 40 prev 20 70 20 70 10 40 65 85 10 30 65 85 80 90 80 90 5 15 30 5 15 SDA/ TOPIC/ V2.0/ 31 SDA/ TOPIC/ V2.0/ 32 8

  9. Delete 40 (case 3) Delete 40 : Rebalancing 40 30 20 70 20 70 prev Case ? 10 30 65 85 10 65 85 80 90 80 90 5 15 5 15 SDA/ TOPIC/ V2.0/ 33 SDA/ TOPIC/ V2.0/ 34 Delete 40: after rebalancing 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 30 � S 1 = 2 10 70 � S H = S H-1 + S H-2 + 1 20 65 85 5 80 90 15 Single rotation is preferred! H H-2 S H-2 H-1 S H-1 SDA/ TOPIC/ V2.0/ 35 SDA/ TOPIC/ V2.0/ 36 9

  10. AVL Tree: analysis (1) AVL Tree: analysis (2) � The depth of AVL Trees at is at most logarithmic. ≈ i φ / 5 F i � So, all of the operations on AVL trees are also logarithmic. = + ≈ φ (1 5 ) / 2 1 . 618 � The worst-case height is at most 44 percent more than the minimum possible for binary trees. ( ) AVL Tree of height H has at least roughly + H 3 φ / 5 < + − 1 . 44 log( 2 ) 1 . 328 H N SDA/ TOPIC/ V2.0/ 37 SDA/ TOPIC/ V2.0/ 38 Summary Further Study � Find element, insert element, and remove element � http://telaga.cs.ui.ac.id/WebKuliah/IKI101 operations all have complexity O(log n) for worst 00/resources/animation/data- case structure/avl/avltree.html (mirror from � Insert operation: top-down insertion and bottom up http://www.geocities.com/SiliconValley/ balancing Heights/8471/avltree.html ) � Chapter 18 SDA/ TOPIC/ V2.0/ 39 SDA/ TOPIC/ V2.0/ 40 10

  11. What’s Next � B Trees SDA/ TOPIC/ V2.0/ 41 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend