1 6.2.1 AVL-Trees (according to Adelson-Velskii & Landis, 1962) In normal search trees, the...

43
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 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 ' = < L, x, R > | 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.

Transcript of 1 6.2.1 AVL-Trees (according to Adelson-Velskii & Landis, 1962) In normal search trees, the...

Page 1: 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.

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 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 ' = < L, x, R > | 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.

Page 2: 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.

2

|Height(I) – hight(D)| < = 1

This is an AVL tree

Page 3: 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.

3

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

Page 4: 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.

4

Goals1. 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)

Page 5: 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.

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

Page 6: 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.

Only 2 cases (an their mirrors)

• Let’s 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

Page 7: 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.

7

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

Is transformed into

Page 8: 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.

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

Page 9: 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.

9

W

Z

a

b

cx

W Z

a

b

c

new

new

First rotation

Second rotation

Y

Y

X

X

Page 10: 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.

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!

Page 11: 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.

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

Page 12: 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.

The cases

12

1

1

1

Deleted node

Page 13: 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.

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.

Page 14: 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.

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).

Page 15: 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.

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).

Page 16: 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.

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-numbersfibo(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)=8By calculating we can state:N(h) = fibo(h+3) - 1

0

12 3

Principle of construction

Page 17: 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.

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))/2we can now writen fibo(h+3)-1 = ( ph+3 – qh+3 ) / sqrt(5) – 1 ( p h+3/sqrt(5)) – 3/2,thush+3+logp(1/sqrt(5)) logp(n+3/2),thus there is a constant c withh logp(n) + c = logp(2) • log2(n) + c = 1.44… • log2(n) + c = O(log n).

Page 18: 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.

18

6.3.1 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

Page 19: 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.

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) .

Page 20: 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.

20

7.1 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!

Page 21: 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.

21

Für externes Suchen: Variante von Suchbäumen mit:Knoten = Seite

Vielwegsuchbäume!

Page 22: 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.

22

Definition (Vielweg-Suchbaum)

Der leere Baum ist ein Vielweg-Suchbaum mit der Schlüsselmenge {}.

Seien T0, ..., Tn Vielweg-Suchbäume mit Schlüsseln aus einer gemeinsamen Schlüsselmenge S, und sei k1,...,kn eine Folge von Schlüsseln mit k1 < ...< kn. Dann ist die Folge

T0 k1 T1 k2 T2 k3 .... kn Tn

ein Vielweg-Suchbaum genau dann, wenn:

• für alle Schlüssel x aus T0 gilt: x < k1 • für i=1,...,n-1, für alle Schlüssel x in Ti gilt: ki < x < ki+1, • für alle Schlüssel x aus Tn gilt: kn < x .

Page 23: 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.

23

B-Baum

Definition 7.1.2

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.

Page 24: 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.

24

Beispiel: Ein B-Baum der Ordnung 2:

Page 25: 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.

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)2 + .... + (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 logm+1 ((n+1)/2) .

Page 26: 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.

26

BeispielAlso gilt für jeden B-Baum der Höhe h mit n Schlüsseln:

h logm+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= 1000 000 folgt

h log 64 500 000.5 < 4 und damit hmax = 3.

Page 27: 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.

27

7.1 Externes Suchen

Definition 7.1.2

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.

Page 28: 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.

28

Beispiel: Ein B-Baum der Ordnung 2:

Page 29: 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.

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)2 + .... + (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 logm+1 ((n+1)/2) .

Page 30: 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.

30

BeispielAlso gilt für jeden B-Baum der Höhe h mit n Schlüsseln:

h logm+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= 1000 000 folgt

h log 64 500 000.5 < 4 und damit hmax = 3.

Page 31: 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.

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)} }

Page 32: 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.

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)

Page 33: 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.

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)

Page 34: 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.

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)

Page 35: 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.

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')}

Page 36: 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.

36

Algorithmus balance (p, p') // balanciere Knoten p mit seinem Nachbarknoten p'

(s > m , r = (m+s)/2 -m )

Page 37: 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.

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}

Page 38: 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.

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.

Page 39: 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.

39

Beispiel:B-Baum derOrdnung 2

Page 40: 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.

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(logm+1 ((n+1)/2) )

= O(logm+1(n)).

Page 41: 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.

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).

Page 42: 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.

42

Anmerkung: Speicherplatzausnutzung:

über 50%Grund: die Bedingung:

1/2•k #(Schlüssel in Knoten) k Für Knoten Wurzel

(k=2m)

Page 43: 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.

43

Noch höhere Speicherplatzausnutzung möglich, z.B. über 66% mit Bedingung:

2/3•k #(Schlüssel in Knoten) kfü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.