1 / 17

Animationen zu Kap. 4.3: AVL-Bäume

Animationen zu Kap. 4.3: AVL-Bäume. Professor Dr. Petra Mutzel Lehrstuhl für Algorithm Engineering, LS11 Fakultät für Informatik, TU Dortmund. 12. VO DAP2 SS 2008 27. Mai 2008. Petra Mutzel DAP2 SS08. 1. Rotationen (1). Beispiel 1:. bal(7)=0. bal(5)=2. 5.

shakti
Télécharger la présentation

Animationen zu Kap. 4.3: AVL-Bäume

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. Animationen zuKap. 4.3: AVL-Bäume Professor Dr. Petra Mutzel Lehrstuhl für Algorithm Engineering, LS11 Fakultät für Informatik, TU Dortmund 12. VO DAP2 SS 2008 27. Mai 2008 Petra Mutzel DAP2 SS08 1

  2. Rotationen (1) Beispiel 1: bal(7)=0 bal(5)=2 5 5 Rotation nach links an 5 bal(7)=1 7 7 bal(5)=0 8 8 5 ist unbalanciert 5, 7, 8 sind balanciert

  3. Rotationen (2) Beispiel 2: bal(5)=2 5 5 Rotation nach links an 5 bal(7)=0 7 7 6 8 6 8 falsch: die 6 muss mitwandern 5 ist unbalanciert 5, 7, 8 sind balanciert

  4. Rotationen (3) bal(7)=-1 bal(5)=2 5 5 bal(5)=1 Rotation nach links an 5 bal(7)=0 6 7 7 6 8 6 8 so korrekt! Warum eigentlich? falsch: die 6 muss mitwandern 5 ist unbalanciert 5, 7, 8 sind balanciert Die Suchbaumeigenschaft bleibt bei einer Rotation erhalten. Nach einer Rotation entsteht wieder ein binärer Suchbaum Achtung: dies ist nicht so wenn gleiche Schlüssel enthalten sind

  5. Rotationen (4) Beispiel 3: bal(8)=0 bal(5)=2 5 5 Rotation nach links an 5 bal(8)=-1 8 8 bal(5)=0 7 7 5 ist unbalanciert falsch: Das war keine Rotation!!!

  6. Rotationen (5) bal(8)=-2 bal(5)=2 5 5 Rotation nach links an 5 bal(8)=-1 8 8 bal(5)=1 7 7 5 ist unbalanciert Diese Rotation hat nichts genützt!

  7. Rotationen (6) bal(7)=0 bal(5)=2 bal(5)=2 5 5 5 bal(8)=-1 bal(7)=1 8 8 7 bal(8)=0 bal(5)=0 7 7 8 Rotation nach links an 5 Rotation nach rechts an 8 Doppelrotation Rechts-Links notwendig!

  8. u a a d v u d v c b c b Doppelrotationen u v d b a c

  9. Definition • AVL-Ersetzung: Operation (z.B Insert, Delete), die einen Unterbaum T eines Knotens z durch einen modifizierten AVL-Baum ersetzt, dessen Höhe um höchstens 1 von der Höhe von T abweicht.

  10. Rebalancierung • Wir betrachten einen AVL-Baum unmittelbar nach einer AVL-Ersetzung (Einfügung bzw. Entfernens eines Knotens). • Sei u ein unbalancierter Knoten maximaler Tiefe, d.h. bal(u)∈{-2,+2}. • Sei T der maximale Unterbaum mit Wurzel u in T. • Wir unterscheiden vier verschiedene Situationen.

  11. u v v u C A C A B B 1. Fall: bal(u)=-2 • Sei v das linke Kind von u (existiert!) • Fall 1.1: bal(v)∈{-1,0}:Rotation nach rechts anu bal(v)∈{0,1} bal(u)=-2 Rotation nach rechts Wir wissen: h(C)=h(A)-1 und h(B)=h(A) oder h(A)-1 • Suchbaumeigenschaft bleibt erhalten; u und v sind balanciert • Für die Knoten unterhalb hat sich nichts geändert.

  12. bal(u)=-2 w u bal(v)=1 v v u D w A A D B B C C 1. Fall: bal(u)=- 2 ff. • Sei v das linke Kind von u (existiert!) • Fall 1.2: bal(v)=+1: Links-Rechtsrotation anu Links- rotation an v dann Rechts- rotation an u u, v und w sind balanciert h(D)=h(A) und ( h(B)=h(A) oder h(C)=h(A) )

  13. 2 + u v v u C A C A B B 1. Fall: bal(u)= -2 • Sei v das linke Kind von u (existiert!) • Fall 1.1: bal(v)∈{-1,0}:Rotation nach rechts anu rechte 2 {0,1} bal(u)=-2 bal(u)=2 Rotation nach rechts links h(C)=h(A)-1 und h(B)=h(A) oder h(B)=h(A)-1 • Inorder-Reihenfolge bleibt erhalten und u und v sind balanciert • Für die Knoten unterhalb hat sich nichts geändert.

  14. + bal(u)=-2 w u bal(v)=-1 v v u D w A A D B B C C 2 1. Fall: bal(u)= - 2 ff. • Sei v das linke Kind von u (existiert!) • Fall 1.2: bal(v)=+1: Links-Rechtsrotation anu rechte rechte Rechts-Links -1 2 bal(u)=2 bal(u)=2 Rechts- Links- rotation an v dann Rechts- rotation an u Links- u, v und w sind balanciert h(D)=h(A) und ( h(B)=h(A) oder h(C)=h(A) )

  15. Rebalancierung ff. • Seien T der maximale Unterbaum mit Wurzel u in T vor der Rebalancierung, T´ der gleiche Teilbaum nach einer einfachen Rotation und T´´ nach einer Doppelrotation. • Für die einfache Rotation gilt: h(T´)-h(T)∈{-1,0} • Im Falle der Doppelrotation gilt: h(T´)-h(T) ∈{-1,0} • Alle Transformationen (Einfach- und Doppelrotationen) kann man also als eine AVL-Ersetzung auffassen.

  16. Rebalancierung ff. • Nach der AVL-Ersetzung gilt: • Die Unterbäume mit Wurzel u und v sind danach balanciert. Für die Unterbäume unterhalb hat sich die Balancierung nicht geändert. • Bestimme nun den nächsten unbalancierten Knoten maximaler Tiefe. Diese Tiefe ist nun kleiner als vorher. • Wiederhole die Rebalancierung (AVL-Ersetzung) solange, bis alle Knoten balanciert sind. • Das Verfahren konvergiert nach O(h(T)) Iterationen zu einem gültigen AVL-Baum.

  17. Rebalancierung ff. • Die Insert-Operation unterscheidet sich nur insofern von der Delete-Operation, dass hier ein einziger Rebalancierungsschritt genügt. • Bei der Delete-Operation hingegen kann es sein, dass mehrere Rebalancierungsschritte notwendig sind. Implementierungen:

More Related