1 6.2.1 AVL-Trees (according to Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search.

Slides:



Advertisements
Ähnliche Präsentationen
Word Order in German Subordiante Clauses
Advertisements

The difference between kein and nicht.
Multi electron atoms Atoms with Z>1 contain >1 electron. This changes the atomic structure considerably because in addition to the electron-nucleus interaction,
Formation of Questions in German
Telling Time in German Deutsch 1 Part 1 Time in German There are two ways to tell time in German. There are two ways to tell time in German. Standard.
Binäre Bäume Richard Göbel.
Priority search queues: Loser trees Advanced Algorithms & Data Structures Lecture Theme 06 Tobias Lauer Summer Semester 2006.
Three minutes presentation I ArbeitsschritteW Seminar I-Prax: Inhaltserschließung visueller Medien, Spree WS 2010/2011 Giving directions.
PageRank 1.What does the graph represent? 2.Describe PageRank. 3.What does PageRank measure in a graph? 4.Which role does PageRank play in IR?
FH-Hof Sortieren mit Binären Bäumen Richard Göbel.
This presentation is timed so you will only need to click on the left mouse button when it is time to move to the next slide. Right click on this screen.
Comparative Adjectives. The term comparison of adjectives is used when two or more persons or things have the same quality (height, size, color, any characteristic)
Die Hausaufgaben: Machen Sie Ü. 7 auf S. 29
Adjektive Endungen von Frau Templeton.
7.1 Externes Suchen Bisherige Algorithmen: geeignet, wenn alle Daten im Hauptspeicher. Große Datenmengen: oft auf externen Speichermedien, z.B. Festplatte.
Zu + Infinitiv : eine Erklärung
Tag um Tag, Jahr um Jahr, Tag um Tag, Jahr um Jahr, Day by day, year by year, Wenn ich durch diese Straßen geh', When I go through these streets, Seh ich.
You need to use your mouse to see this presentation © Heidi Behrens.
die Zeiten (The Tenses) das Aktiv (Active Voice)
Kapitel 4: Die Schule Deutsch 1
Die Zeit (TIME) Germans are on military time which is 1-24
Institut AIFB, Universität Karlsruhe (TH) Forschungsuniversität gegründet 1825 Towards Automatic Composition of Processes based on Semantic.
Montag, den Hausaufgabe für Dienstag den Keine Hausaufgabe!!! Film: 8:00 oder 9:30 Guten Tag!
| DC-IAP/SVC3 | © Bosch Rexroth Pneumatics GmbH This document, as well as the data, specifications and other information set forth in.
Das Perfekt (Present Perfect Tense). Think of 5 things you did in your holidays but think of sentences in the PRESENT TENSE. 1.Am Montag schlafe ich viel.
Plural Forms of Nouns & Wie viel? or Wie viele?
Morphology and Syntax More on sentence structure.
BAS5SE | Fachhochschule Hagenberg | Daniel Khan | S SPR5 MVC Plugin Development SPR6P.
Reflexive Verbs.
Ich möchte ein Eisberg sein. Last time … 3 icebergs Triangels Unique connections Ich möchte ein Eisberg sein
Deutsch 1 G Stunde. Donnerstag, der 25. Oktober 2012 Deutsch 1, G Stunde Heute ist ein B- Tag Unit: Family & homeFamilie & Zuhause Objectives: Phrases.
Deutsch 1 G Stunde. Montag, der 10. September 2012 Deutsch 1 (G Stunde)Heute ist ein D - Tag Unit: Introduction to German & Germany Objectives: Introducing.
Things I really, really need to remember.. AKKUSATIVDATIV DurchAus FürAußer GegenBei OhneMit UmNach BisSeit Von Zu These prepositions always trigger these.
Die Zukunft The future I will.
You need to use your mouse to see this presentation © Heidi Behrens.
You need to use your mouse to see this presentation © Heidi Behrens.
You need to use your mouse to see this presentation © Heidi Behrens.
You need to use your mouse to see this presentation
You need to use your mouse to see this presentation © Heidi Behrens.
You need to use your mouse to see this presentation © Heidi Behrens.
You need to use your mouse to see this presentation © Heidi Behrens.
From “Deutsch: Na Klar! 5th Edition”
Wo warst du? und Was hast du gekauft? HMV. How to say in the The word in can have a strange effect on the word for the that comes after it. When you say.
Verben Wiederholung Deutsch III Notizen.
The Genitive Case This case has three uses: USE #1: To show possession or that something is an integral part of something else. z.B. The mans dog has just.
To Infinitives, and Beyond!
The word,,aber in German is most often used as a coordinating conjunction. Ich wollte nach Bremen fahren aber Mein Auto ist kaputt. Ich mag English aber.
“Weil” und “Denn”.
The most obvious or direct use of auch is to mean also. Ich möchte auch Gitarre lernen. Auch ich möchte Gitarre lernen. I would like to learn Guitar. Someone.
The cheating verbs… (modal verbs). Modal Verb Chart wollenmüssenkönnenmögensollendürfenmöchten Ichwillmusskannmagsolldarfmöchte Duwillstmusstkannstmagstsollstdarfstmöchtest.
External Labels – The rules For all external labels the following rules apply (external labels are all labels which are not inside of a shape) - all labels.
© Boardworks Ltd of 8 Time Manner Place © Boardworks Ltd of 8 This icon indicates that the slide contains activities created in Flash. These.
By: Jade Bowerman. German numbers are quite a bit like our own. You start with one through ten and then you add 20, 30, 40 or 50 to them. For time you.
Negation is when you dont have or dont do something.
Adjectiv Endungen Lite: Adjective following articles and pre-ceeding nouns. Colors and Clothes.
Sentence Structure Subject and verb are always together. Subject and verb are always together. Subject and verb must agree Subject and verb must agree.
DEUTSCHE VERBEN I. REGULAR VERBS.
Separable Verbs Turn to page R22 in your German One Book R22 is in the back of the book There are examples at the top of the page.
Plusquamperfekt The past of the past.
Dyabola Archäologische Bibliographie Römisch-Germanischen Kommission (RGK) Author searches – compound names Bibliotheken Click = next Libraries.
1 Stevens Direct Scaling Methods and the Uniqueness Problem: Empirical Evaluation of an Axiom fundamental to Interval Scale Level.
THE PERFECT TENSE IN GERMAN
Adjective Endings Nominative & Accusative Cases describing auf deutsch The information contained in this document may not be duplicated or distributed.
You need to use your mouse to see this presentation
By Martin L. Loeffler.  The basic sentence has a subject and a verb.  The subject and verb need to be together.  The subject and verb need to agree.
THE CONVERSATIONAL PAST
Data Mining Spectral Clustering Junli Zhu SS 2005.
Adjective Declension in German
Coordinating Conjunctions Why we need them & how to use them deutschdrang.com.
School supplies.
 Präsentation transkript:

AVL-Trees (according to Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst case: (n). Can be better! Idea: Balanced trees. Definition: An AVL-tree is a binary search tree such that for each sub-tree T ' = | h(L) - h(R) | 1 holds (balanced sub-trees is a characteristic of AVL-trees). The balance factor or height is often annotated at each node h(.)+1.

2 |Height(I) – hight(D)| < = 1 This is an AVL tree

3 This is NOT an AVL tree (node * does not hold the required condition)

4 Goals 1. How can the AVL-characteristics be kept when inserting and deleting nodes? 2. We will see that for AVL-trees the complexity of the operations is in the worst case = O(height of the AVL-tree) = O(log n)

5 Preservation of the AVL-characteristics After inserting and deleting nodes from a tree we must procure that new tree preserves the characteristics of an AVL-tree: Re-balancing. How ?: simple and double rotations

Only 2 cases (an their mirrors) Lets analyze the case of insertion – The new element is inserted at the right (left) sub- tree of the right (left) child which was already higher than the left (right) sub-tree by 1 – The new element is inserted at the left (right) sub- tree of the right (left) child which was already higher than the left (right) sub-tree by 1 6

7 Rotation (for the case when the right sub-tree grows too high after an insertion) Is transformed into

8 Double rotation (for the case that the right sub-tree grows too high after an insertion at its left sub-tree) Is transformed into Double rotation

9 W Z a b c x W Z a b c new First rotation Second rotation Y Y X X

10 Re-balancing after insertion: After an insertion the tree might be still balanced or: theorem: After an insertion we need only one rotation of double-rotation at the first node that got unbalanced * in order to re-establish the balance properties of the AVL tree. (* : on the way from the inserted node to the root). Because: after a rotation or double rotation the resulting tree will have the original size of the tree!

The same applies for deleting Only 2 cases (an their mirrors) – The element is deleted at the right (left) sub-tree of which was already smaller than the left (right) sub- tree by 1 – The new element is inserted at the left (right) sub- tree of the right (left) child which was already higher that the left (right) sub-tree by 1 11

The cases Deleted node

13 Re-balancing after deleting: After deleting a node the tree might be still balanced or: Theorem: after deleting we can restore the AVL balance properties of the sub-tree having as root the first* node that got unbalanced with just only one simple rotation or a double rotation. (* : on the way from the deleted note to the root). However: the height of the resulting sub-tree might be shortened by 1, this means more rotations might be (recursively) necessary at the parent nodes, which can affect up to the root of the entire tree.

14 About Implementation While searching for unbalanced sub-tree after an operation it is only necessary to check the parent´s sub-tree only when the son´s sub-tree has changed it height. In order make the checking for unbalanced sub-trees more efficient, it is recommended to put some more information on the nodes, for example: the height of the sub-tree or the balance factor (height(left sub-tree) – height(right sub- tree)) This information must be updated after each operation It is necessary to have an operation that returns the parent of a certain node (for example, by adding a pointer to the parent).

15 Complexity analysis– worst case Be h the height of the AVL-tree. Searching: as in the normal binary search tree O(h). Insert: the insertion is the same as the binary search tree (O(h)) but we must add the cost of one simple or double rotation, which is constant : also O(h). delete: delete as in the binary search tree(O(h)) but we must add the cost of (possibly) one rotation at each node on the way from the deleted node to the root, which is at most the height of the tree: O(h). All operations are O(h).

16 Calculating the height of an AVL tree Be N(h) the minimal number of nodes In an AVL-tree having height h. N(0)=1, N(1)=2, N(h) = 1 + N(h-1) + N(h-2) für h 2. N(3)=4, N(4)=7 remember: Fibonacci-numbers fibo(0)=0, fibo(1)=1, fibo(n) = fibo(n-1) + fibo(n-2) fib(3)=1, fib(4)=2, fib(5)=3, fib(6)=5, fib(7)=8 By calculating we can state: N(h) = fibo(h+3) Principle of construction

17 Be n the number of nodes of an AVL-tree of height h. Then it holds that: n N(h), By making p = (1 + sqrt(5))/2 und q = (1- sqrt(5))/2 we can now write n fibo(h+3)-1 = ( p h+3 – q h+3 ) / sqrt(5) – 1 ( p h+3 /sqrt(5)) – 3/2, thus h+3+log p (1/sqrt(5)) log p (n+3/2), thus there is a constant c with h log p (n) + c = log p (2) log 2 (n) + c = 1.44… log 2 (n) + c = O(log n).

Heapsort Idea: two phases: 1. Construction of the heap 2. Output of the heap For ordering number in an ascending sequence: use a Heap with reverse order: the maximum number should be at the root (not the minimum). Heapsort is an in-situ-Procedure

19 Remembering Heaps: change the definition Heap with reverse order: For each node x and each successor y of x the following holds: m(x) m(y), left-complete, which means the levels are filled starting from the root and each level from left to right, Implementation in an array, where the nodes are set in this order (from left to right).

Externes Suchen Bisherige Algorithmen: geeignet, wenn alle Daten im Hauptspeicher. Große Datenmengen: oft auf externen Speichermedien, z.B. Festplatte. Zugriff: immer gleich auf einen ganzen Block (eine Seite) von Daten, z.B: 4096 Bytes. Effizienz: Zahl der Seitenzugriffe klein halten!

21 Für externes Suchen: Variante von Suchbäumen mit: Knoten = Seite Vielwegsuchbäume!

22 Definition (Vielweg-Suchbaum) Der leere Baum ist ein Vielweg-Suchbaum mit der Schlüsselmenge {}. Seien T 0,..., T n Vielweg-Suchbäume mit Schlüsseln aus einer gemeinsamen Schlüsselmenge S, und sei k 1,...,k n eine Folge von Schlüsseln mit k 1 <...< k n. Dann ist die Folge T 0 k 1 T 1 k 2 T 2 k k n T n ein Vielweg-Suchbaum genau dann, wenn: für alle Schlüssel x aus T 0 gilt: x < k 1 für i=1,...,n-1, für alle Schlüssel x in T i gilt: k i < x < k i +1, für alle Schlüssel x aus T n gilt: k n < x.

23 B-Baum Definition Ein B-Baum der Ordnung m ist ein Vielweg-Suchbaum mit folgenden Eigenschaften 1 #(Schlüssel in Wurzel) 2m und m #(Schlüssel in Knoten) 2m für alle anderen Knoten. Alle Pfade von der Wurzel zu einem Blatt sind gleichlang. Jeder innere Knoten mit s Schlüsseln hat genau s+1 Söhne.

24 Beispiel: Ein B-Baum der Ordnung 2:

25 Abschätzungen zu B-Bäumen Ein minimal gefüllter B-Baum der Ordnung m und Höhe h: Knotenzahl im linken wie im rechten Teilbaum 1 + (m+1) + (m+1) (m+1) h-1 = ( (m+1) h – 1) / m. Die Wurzel hat einen Schlüssel, alle anderen Knoten haben m Schlüssel. Insgesamt: Schlüsselzahl n in einem B-Baum der Höhe h: n 2 (m+1) h – 1 Also gilt für jeden B-Baum der Höhe h mit n Schlüsseln: h log m+1 ((n+1)/2).

26 Beispiel Also gilt für jeden B-Baum der Höhe h mit n Schlüsseln: h log m+1 ((n+1)/2). Beispiel: Bei Seitengröße: 1 KByte und jeder Eintrag nebst Zeiger: 8 Byte, kann m=63 gewählt werden, und bei einer Datenmenge von n= folgt h log < 4 und damit h max = 3.

Externes Suchen Definition Ein B-Baum der Ordnung m ist ein Vielweg-Suchbaum mit folgenden Eigenschaften 1 #(Schlüssel in Wurzel) 2m und m #(Schlüssel in Knoten) 2m für alle anderen Knoten. Alle Pfade von der Wurzel zu einem Blatt sind gleichlang. Jeder innere Knoten mit s Schlüsseln hat genau s+1 Söhne.

28 Beispiel: Ein B-Baum der Ordnung 2:

29 Abschätzungen zu B-Bäumen Ein minimal gefüllter B-Baum der Ordnung m und Höhe h: Knotenzahl im linken wie im rechten Teilbaum 1 + (m+1) + (m+1) (m+1) h-1 = ( (m+1) h – 1) / m. Die Wurzel hat einen Schlüssel, alle anderen Knoten haben m Schlüssel. Insgesamt: Schlüsselzahl n in einem B-Baum der Höhe h: n 2 (m+1) h – 1 Also gilt für jeden B-Baum der Höhe h mit n Schlüsseln: h log m+1 ((n+1)/2).

30 Beispiel Also gilt für jeden B-Baum der Höhe h mit n Schlüsseln: h log m+1 ((n+1)/2). Beispiel: Bei Seitengröße: 1 KByte und jeder Eintrag nebst Zeiger: 8 Byte, kann m=63 gewählt werden, und bei einer Datenmenge von n= folgt h log < 4 und damit h max = 3.

31 Algorithmen zum Einfügen und Löschen von Schlüsseln in B-Bäumen Algorithmus insert (root, x) //füge Schlüssel x in den Baum mit Wurzelknoten root ein suche nach x im Baum mit Wurzel root; wenn x nicht gefunden { sei p Blatt, an dem die Suche endete; füge x an der richtigen Position ein; wenn p nun 2m+1 Schlüssel {overflow(p)} }

32 Algorithmus overflow (p) = split (p) Algorithmus split (p) Erster Fall: p hat einen Vater q. Zerlege den übervollen Knoten. Der mittlere Schlüssel wandert in den Vater. Anmerkung: das Splitting muss evtl. bis zur Wurzel wiederholt werden. Algorithmus Split (1)

33 Algorithmus split (p) Zweiter Fall: p ist die Wurzel. Zerlege den übervollen Knoten. Eröffne eine neue Ebene nach oben mit einer neuen Wurzel mit dem mittleren Schlüssel. Algorithmus Split (2)

34 //entferne Schlüssel x aus dem Baum mit Wurzel root suche nach x im Baum mit Wurzel root; wenn x gefunden { wenn x in einem inneren Knoten liegt { vertausche x mit dem nächstgrößeren Schlüssel x' im Baum // wenn x in einem inneren Knoten liegt, gibt // es einen nächstgrößeren Schlüssel // im Baum, und dieser liegt in einem Blatt } sei p das Blatt, das x enthält; lösche x aus p; wenn p nicht die wurzel ist { wenn p m-1 Schlüssel hat {underflow (p)} } } Algorithmus delete (root,x)

35 Algorithmus underflow (p) // behandle die Unterläufe des Knoten p wenn p einen Nachbarknoten hat mit s>m Knoten { balance (p,p') } anderenfalls // da p nicht die Wurzel sein kann, muss p Nachbarn mit m Schlüsseln haben { sei p' Nachbar mit m Schlüsseln; merge (p,p')}

36 Algorithmus balance (p, p') // balanciere Knoten p mit seinem Nachbarknoten p' (s > m, r = (m+s)/2 -m )

37 Algorithmus merge (p,p') // verschmelze Knoten p mit seinem Nachbarknoten Führe die folgende Operation durch: Anschließend: wenn ( q <> Wurzel) und (q hat m-1 Schlüssel) underflow (q) anderenfalls (wenn (q= Wurzel) und (q leer)) {gib q frei und lasse root auf p^ zeigen}

38 Rekursion Wenn es bei underflow zu merge kommt, muss evtl. underflow eine Ebene höher wiederholt werden. Dies kann sich bis zur Wurzel fortsetzen.

39 Beispiel: B-Baum der Ordnung 2

40 Aufwand Sei m die Ordnung des B-Baums, n die Zahl der Schlüssel. Aufwand für Suchen, Einfügen, Entfernen: O(h) = O(log m+1 ((n+1)/2) ) = O(log m+1 (n)).

41 Anmerkung: B-Bäume auch als interne Speicherstruktur zu gebrauchen: Besonders: B-Bäume der Ordnung 1 (dann nur 1 oder 2 Schlüssel pro Knoten – keine aufwändige Suche innerhalb von Knoten). Aufwand für Suchen, Einfügen, Löschen: O(log n).

42 Anmerkung: Speicherplatzausnutzung: über 50% Grund: die Bedingung: 1/2k #(Schlüssel in Knoten) k Für Knoten Wurzel (k=2m)

43 Noch höhere Speicherplatzausnutzung möglich, z.B. über 66% mit Bedingung: 2/3k #(Schlüssel in Knoten) k für alle Knoten mit Ausnahme der Wurzel und ihrer Kinder. Erreichbar durch 1) modifiziertes Balancieren auch beim Einfügen und 2) split erst, wenn zwei Nachbarn ganz voll. Nachteil: Häufigere Reorganisation beim Einfügen und Löschen notwendig.