LS 2 /
Informatik
Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)
LS 2 /
Informatik
Organisatorisches
Übungen
Ab der nächsten Heimübung ist die Teilnahme an der Besprechung der
Heimübung nicht mehr verpflichtend
Lernraumbetreuung
Mo 14:30-17:30 in U04 zur C-Betreuung (Praktikum)
Dafür fällt aus: Mo 14:30 – 17:30
Zweiter Test
Aufgabe 1: Nachvollziehen eines Algorithmus oder einer Datenstruktur
Aufgabe 2: Entwurf und Analyse eines Algorithmus zur dynamischen
Programmierung
Aufgabe 3: Entwurf und Analyse eines gierigen Algorithmus
2
LS 2 /
Informatik
Datenstrukturen
Datenstruktur Feld
Platzbedarf Q(max)
Laufzeit Suche: Q(n)
Laufzeit Einfügen/Löschen: Q(1)
Vorteile
Schnelles Einfügen und Löschen
Nachteile
Speicherbedarf abhängig von max (nicht vorhersagbar)
Hohe Laufzeit für Suche
3
LS 2 /
Informatik
Datenstrukturen
Datenstruktur „sortiertes Feld“
Sortiertes Feld A[1,…,max]
Integer n, 1 n max
n bezeichnet Anzahl Elemente in Datenstruktur
2
A
4
6
7
11
13
nil
nil
nil
nil
n
4
LS 2 /
Informatik
Datenstrukturen
Einfügen(s)
1.
2.
3.
4.
5.
6.
2
n n+1
in
while s < A[i-1] do
A[i] A[i-1]
i i -1
A[i] s
4
6
7
11
13
nil
nil
nil
nil
n
Einfügen(10)
5
LS 2 /
Informatik
Datenstrukturen
Einfügen(s)
1.
2.
3.
4.
5.
6.
2
n n+1
in
while s < A[i-1] do
A[i] A[i-1]
i i -1
A[i] s
4
6
7
11
13
nil
nil
nil
nil
n
Einfügen(10)
6
LS 2 /
Informatik
Datenstrukturen
Einfügen(s)
1.
2.
3.
4.
5.
6.
2
n n+1
in
while s < A[i-1] do
A[i] A[i-1]
i i -1
A[i] s
4
6
7
11
13
nil
nil
nil
nil
n
Einfügen(10)
7
LS 2 /
Informatik
Datenstrukturen
Einfügen(s)
1.
2.
3.
4.
5.
6.
2
n n+1
in
while s < A[i-1] do
A[i] A[i-1]
i i -1
A[i] s
4
6
7
11
11
13
nil
nil
nil
n
Einfügen(10)
8
LS 2 /
Informatik
Datenstrukturen
Einfügen(s)
1.
2.
3.
4.
5.
6.
2
Laufzeit O(n)
n n+1
in
while s < A[i-1] do
A[i] A[i-1]
i i -1
A[i] s
4
6
7
10
11
13
nil
nil
nil
n
Einfügen(10)
9
LS 2 /
Informatik
Datenstrukturen
Parameter ist der
Index des zu
löschenden Objekts
Löschen(i)
1.
2.
3.
4.
2
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
4
6
7
11
13
nil
nil
nil
nil
n
10
LS 2 /
Informatik
Datenstrukturen
Parameter ist der
Index des zu
löschenden Objekts
Löschen(i)
1.
2.
3.
4.
2
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
4
6
7
11
13
nil
nil
nil
nil
n
Löschen(2)
11
LS 2 /
Informatik
Datenstrukturen
Parameter ist der
Index des zu
löschenden Objekts
Löschen(i)
1.
2.
3.
4.
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
i
2
4
6
7
11
13
nil
nil
nil
nil
n
Löschen(2)
12
LS 2 /
Informatik
Datenstrukturen
Löschen(i)
1.
2.
3.
4.
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
i
2
4
6
7
11
13
nil
nil
nil
nil
n
Löschen(2)
13
LS 2 /
Informatik
Datenstrukturen
Löschen(i)
1.
2.
3.
4.
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
i
2
6
7
11
13
13
nil
nil
nil
nil
n
Löschen(2)
14
LS 2 /
Informatik
Datenstrukturen
Löschen(i)
1.
2.
3.
4.
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
i
2
6
7
11
13
nil
nil
nil
nil
nil
n
Löschen(2)
15
LS 2 /
Informatik
Datenstrukturen
Löschen(i)
1.
2.
3.
4.
for j i to n-1 do
A[j] A[j+1]
A[n] nil
n n-1
i
2
6
7
11
13
nil
nil
nil
nil
nil
n
Löschen(2)
16
LS 2 /
Informatik
Datenstrukturen
Suchen(x)
Binäre Suche
Laufzeit O(log n)
i
2
6
7
11
13
nil
nil
nil
nil
nil
n
Löschen(2)
17
LS 2 /
Informatik
Datenstrukturen
Datenstruktur sortiertes Feld
Platzbedarf Q(max)
Laufzeit Suche: Q(log n)
Laufzeit Einfügen/Löschen: Q(n)
Vorteile
Schnelles Suchen
Nachteile
Speicherbedarf abhängig von max (nicht vorhersagbar)
Hohe Laufzeit für Einfügen/Löschen
18
LS 2 /
Informatik
Datenstrukturen
Doppelt verkettete Listen
Listenelement x ist Objekt bestehend aus Schlüssel und zwei Zeigern prev
und next
next verweist auf Nachfolger von x
prev verweist auf Vorgänger von x
prev/next sind nil, wenn Vorgänger/Nachfolger nicht existiert
head[L] zeigt auf das erste Element
prev
head[L]
nil
5
next
7
Schlüssel
4 nil
19
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
head[L]
nil
7
4 nil
20
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
x
5
head[L]
nil
7
4 nil
21
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
x
5
head[L]
7
4 nil
22
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
x
head[L]
5
7
4 nil
23
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
x
head[L]
nil
5
7
4 nil
24
LS 2 /
Informatik
Datenstrukturen
Einfügen(L,x)
1. next[x] head[L]
2. if head[L] nil then prev[head[L]] x
3. head[L] x
4. prev[x] nil
Laufzeit
•
O(1)
x
head[L]
nil
5
7
4 nil
25
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
head[L]
nil
5
7
4 nil
26
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
head[L]
nil
5
x
7
4 nil
27
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
head[L]
nil
5
x
7
4 nil
28
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
head[L]
nil
4 nil
5
7
x
29
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
head[L]
nil
5
4 nil
30
LS 2 /
Informatik
Datenstrukturen
Löschen(L,x)
1. if prev[x] nil then next[prev[x]] next[x]
2. else head[L] next[x]
3. if next[x] nil then prev[next[x]] prev[x]
4. delete x
Laufzeit
•
O(1)
head[L]
nil
5
4 nil
31
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
32
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
head[L]
nil
5
4 nil
33
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
x
head[L]
nil
Suche(L,4)
5
4 nil
34
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
head[L]
nil
Suche(L,4)
5
4 nil
35
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
head[L]
nil
Suche(L,4)
5
x
4 nil
36
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
head[L]
nil
Suche(L,4)
5
x
4 nil
37
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
head[L]
nil
Suche(L,4)
5
x
4 nil
38
LS 2 /
Informatik
Datenstrukturen
Suche(L,k)
1. x head[L]
2. while xnil and key[x]k do
3.
x next[x]
4. return x
Laufzeit
•
O(n)
head[L]
nil
Suche(L,4)
5
x
4 nil
39
LS 2 /
Informatik
Datenstrukturen
Datenstruktur Liste:
Platzbedarf Q(n)
Laufzeit Suche: Q(n)
Laufzeit Einfügen/Löschen: Q(1)
Vorteile
Schnelles Einfügen/Löschen
O(n) Speicherbedarf
Nachteile
Hohe Laufzeit für Suche
40
LS 2 /
Informatik
Datenstrukturen
Drei grundlegende Datenstrukturen
Feld
sortiertes Feld
doppelt verkettete Liste
Diskussion
Alle drei Strukturen haben gewichtige Nachteile
Zeiger/Referenzen helfen beim Speichermanagement
Sortierung hilft bei Suche ist aber teuer aufrecht zu erhalten
41
LS 2 /
Informatik
Datenstrukturen
Binärbäume
key
Schlüssel key und ggf. weitere Daten
Vaterzeiger p[v] auf Vater von v (blau)
Zeiger lc[v] (rc[v]) auf linkes (rechtes) Kind von v
Wurzelzeiger root[T]
p[v]
lc[v] rc[v]
root[T]
/
/
/
/
/
/
/
/
/
42
LS 2 /
Informatik
Datenstrukturen
6
Binäre Suchbäume
Verwende Binärbaum
Speichere Schlüssel „geordnet“
7
4
3
7
9
Binäre Suchbaumeigenschaft:
Sei x Knoten im binären Suchbaum
Ist y Knoten im linken Unterbaum von x, dann gilt key[y]key[x]
Ist y Knoten im rechten Unterbaum von x, dann gilt key[y]>key[x]
43
LS 2 /
Informatik
Datenstrukturen
Unterschiedliche Suchbäume
Schlüsselmenge 3,4,6,7,7,9
Wir erlauben mehrfache Vorkommen desselben Schlüssels
3
6
3
6
7
4
7
9
7
4
7
9
44
LS 2 /
Informatik
Datenstrukturen
Ausgabe aller Schlüssel
Gegeben binärer Suchbaum
Wie kann man alle Schlüssel aufsteigend sortiert in Q(n) Zeit ausgeben?
6
7
4
3
7
9
45
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Aufruf über
Inorder-Tree-Walk(root[T])
6
7
4
3
7
9
46
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
6
7
4
3
7
9
47
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
6
7
4
3
7
9
48
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
6
7
4
3
7
9
49
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
6
7
4
3
7
9
50
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
6
7
4
3
7
9
51
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Kein linkes Kind
vorhanden, d.h.
lc[x]=nil
x 3
6
7
4
7
9
52
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Kein linkes Kind
vorhanden, d.h.
lc[x]=nil
x 3
6
7
4
7
9
53
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3
6
7
4
3
7
9
54
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3
6
Kein rechtes Kind
vorhanden, d.h.
rc[x]=nil
7
4
3
7
9
55
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3
6
Kein rechtes Kind
vorhanden, d.h.
rc[x]=nil
7
4
3
7
9
56
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3
6
Knoten
abgearbeitet
3
7
4
7
9
57
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4
6
7
4
3
7
9
58
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4
6
7
4
3
nil
7
9
59
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4
6
7
4
3
nil
7
9
60
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4
6
7
4
3
7
9
61
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
62
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
63
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
64
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
65
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
66
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
67
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6
6
7
4
3
7
9
68
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7
6
7
4
3
7
9
69
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7
6
7
4
3
7
9
70
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7
6
7
4
3
9
7
71
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7
6
7
4
3
7
9
72
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7
6
7
4
3
7
9
73
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7
6
7
4
3
7
9
74
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7
6
7
4
3
7
9
75
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7
6
7
4
3
7
9
76
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7
6
7
4
3
9
7
77
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
78
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
79
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
80
nil
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
81
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
82
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
83
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
84
LS 2 /
Informatik
Datenstrukturen
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Ausgabe:
3, 4, 6, 7, 7, 9
6
7
4
3
7
9
85
LS 2 /
Informatik
Datenstrukturen
Lemma 37
Inorder-Tree-Walk gibt die Schlüssel eines binären Suchbaums in
aufsteigender Reihenfolge aus.
86
LS 2 /
Informatik
Datenstrukturen
„Normale“ Induktion
•
•
Wir wollen zeigen, dass Aussage A(i) für alle natürlichen Zahlen i gilt
Dazu beweisen wir, dass
•
•
(a) A(1) gilt
(b) Wenn A(i) gilt, dann gilt auch A(i+1)
•
•
•
(a) heißt Induktionsanfang
(b) nennt man Induktionsschluss (oder auch Induktionsschritt)
Die Vorraussetzung in (b) (also A(i)) heißt Induktionsvoraussetzung
87
LS 2 /
Informatik
Datenstrukturen
Induktion über die Struktur von Binärbäumen
•
•
•
Wollen zeigen, dass Aussage für alle Binärbäume gilt:
(a) Zeige Induktionsanfang für „kleine Binärbäume“
(b) Setze größere Bäume aus kleinen Binärbäumen zusammen, d.h.
Aussage gilt
für Bäume A
und B
A
B
88
LS 2 /
Informatik
Datenstrukturen
Induktion über die Struktur von Binärbäumen
•
•
•
Wollen zeigen, dass Aussage für alle Binärbäume gilt:
(a) Zeige Induktionsanfang für „kleine Binärbäume“
(b) Setze größere Bäume aus kleinen Binärbäumen zusammen, d.h.
Dann gilt
Aussage auch für
neuen Baum mit
Wurzel v
v
A
B
89
LS 2 /
Informatik
Datenstrukturen
Definition
•
Die Höhe eines Binärbaums mit Wurzel v ist die Länge (Anzahl Kanten)
des längsten einfachen Weges (keine mehrfach vorkommenden Knoten)
von der Wurzel zu einem Blatt.
Beispiel
•
Baum der Höhe 3
v
90
LS 2 /
Informatik
Datenstrukturen
Definition
•
Die Höhe eines Binärbaums mit Wurzel v ist die Länge (Anzahl Kanten)
des längsten einfachen Weges (keine mehrfach vorkommenden Knoten)
von der Wurzel zu einem Blatt.
Beispiel
•
Baum der Höhe 1
v
91
LS 2 /
Informatik
Datenstrukturen
Definition
•
Die Höhe eines Binärbaums mit Wurzel v ist die Länge (Anzahl Kanten)
des längsten einfachen Weges (keine mehrfach vorkommenden Knoten)
von der Wurzel zu einem Blatt.
Beispiel
•
Baum der Höhe 0
v
92
LS 2 /
Informatik
Datenstrukturen
Definition
•
Die Höhe eines Binärbaums mit Wurzel v ist die Länge (Anzahl Kanten)
des längsten einfachen Weges (keine mehrfach vorkommenden Knoten)
von der Wurzel zu einem Blatt.
Beispiel
•
•
v
Übereinkunft: Ein leerer Baum hat Höhe -1
Damit gilt:
Höhe eines Baumes mit Wurzel v und
Teilbäumen A und B ist
1 + max{ Höhe(A), Höhe(B)}
A
B
93
LS 2 /
Informatik
Datenstrukturen
Induktion über die Struktur von Binärbäumen
•
•
•
Wir wollen Aussage A(i) durch Induktion über die Höhe von Bäumen
zeigen
(a) Zeige die Aussage für leere Bäume (Bäume der Höhe -1)
(b) Zeige: Gilt die Aussage für Bäume der Höhe i, so gilt sie auch für
Bäume der Höhe i+1
v
B
A
94
LS 2 /
Informatik
Datenstrukturen
Induktion über die Struktur von Binärbäumen
•
•
•
•
Wir wollen Aussage A(i) durch Induktion über die Höhe von Bäumen
zeigen
(a) Zeige die Aussage für leere Bäume (Bäume der Höhe -1)
(b) Zeige: Gilt die Aussage für Bäume der Höhe i, so gilt sie auch für
Bäume der Höhe i+1
v
Dabei können wir immer annehmen,
dass ein Baum der Höhe i+1 aus einer
Wurzel v und zwei Teilbäumen A,B
besteht, so dass
(1) A und B Höhe maximal i haben und
(2) A oder B Höhe i hat
Höhe
i+1
B
A
95
LS 2 /
Informatik
Datenstrukturen
Induktion über die Struktur von Binärbäumen
•
•
•
•
Wir wollen Aussage A(i) durch Induktion über die Höhe von Bäumen
zeigen
(a) Zeige die Aussage für leere Bäume (Bäume der Höhe -1)
(b) Zeige: Gilt die Aussage für Bäume der Höhe i, so gilt sie auch für
Bäume der Höhe i+1
v
Dabei können wir immer annehmen,
dass ein Baum der Höhe i+1 aus einer
Wurzel v und zwei Teilbäumen A,B
besteht, so dass
(1) A und B Höhe maximal i haben und
(2) A oder B Höhe i hat
Höhe
i
B
A
96
LS 2 /
Informatik
Datenstrukturen
Lemma 37
Inorder-Tree-Walk gibt die Schlüssel
eines binären Suchbaums in aufsteigender
Reihenfolge aus.
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Beweis
(I.A.) Ist die Eingabe ein leerer Baum, so wird
Inorder-Tree-Walk nil übergeben. Damit bricht der
Algorithmus sofort ab. Es gibt also keine Ausgabe,
was auch korrekt ist.
(I.V.) Das Lemma gilt für Bäume der Höhe ≤i.
(I.S.) Wir müssen zeigen, dass das Lemma auch für
Höhe i+1≥0. Dazu betrachten wir den Inorder-Tree-Walk
auf einem solchem Baum.
v
B
A
97
LS 2 /
Informatik
Datenstrukturen
Lemma 37
Inorder-Tree-Walk gibt die Schlüssel
eines binären Suchbaums in aufsteigender
Reihenfolge aus.
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Beweis
(I.S.) Wir müssen zeigen, dass das Lemma auch für
Höhe i+1≥0. Dazu betrachten wir den Inorder-Tree-Walk
auf einem solchem Baum. Da der Baum Höhe mindestens
0 hat, wird der Algorithmus nicht mit nil aufgerufen.
Damit wird Zeile 2 ausgeführt. Dort wird Inorder-Tree-Walk
rekursiv mit dem Teilbaum A der Höhe ≤i ausgeführt.
Nach (I.V.) werden die Schlüssel aus A in aufsteigender
Reihenfolge ausgegeben
v
B
A
98
LS 2 /
Informatik
Datenstrukturen
Lemma 37
Inorder-Tree-Walk gibt die Schlüssel
eines binären Suchbaums in aufsteigender
Reihenfolge aus.
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Beweis
Nach (I.V.) werden die Schlüssel aus A in aufsteigender
Reihenfolge ausgegeben. Dann wird in Zeile 3 key[v]
ausgegeben. Wegen der Suchbaumeigenschaft
ist key[v] größer gleich allen Schlüsseln in A und kleiner
als alle Schlüssel in B. In Zeile 4 wird dann
Inorder-Tree-Walk rekursiv für Teilbaum B aufgerufen.
Die dort gespeicherten Schlüssel werden nach (I.V.)
ebenfalls in aufsteigender Reihenfolge ausgegeben.
v
B
A
99
LS 2 /
Informatik
Datenstrukturen
Lemma 37
Inorder-Tree-Walk gibt die Schlüssel
eines binären Suchbaums in aufsteigender
Reihenfolge aus.
Inorder-Tree-Walk(x)
1. if xnil then
2. Inorder-Tree-Walk(lc[x])
3. Ausgabe key[x]
4. Inorder-Tree-Walk(rc[x])
Beweis
Zusammenfassend werden also erst die Schlüssel in
Teilbaum A in aufsteigender Reihenfolge ausgegeben.
Dann folgt key[v] und dann die Schlüssel aus B in
aufsteigender Reihenfolge. Aufgrund der Suchbaumeigenschaft ist die gesamte Folge aufsteigend
sortiert.
v
B
A
100
LS 2 /
Informatik
Datenstrukturen
Suchen in Binärbäumen
Gegeben ist Schlüssel k
Gesucht ist ein Knoten mit Schlüssel k
6
7
4
3
7
9
101
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
6
7
4
3
7
9
102
LS 2 /
Informatik
Datenstrukturen
Aufruf mit
x=root[T]
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
103
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
104
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
105
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
106
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
107
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
108
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Baumsuche(root[T],9)
6
7
4
3
7
9
109
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Ausgabe: 9
Baumsuche(root[T],9)
6
7
4
3
7
9
110
LS 2 /
Informatik
Datenstrukturen
Baumsuche(x,k)
1. if x=nil or k=key[x] then return x
2. if k<key[x] then return Baumsuche(lc[x],k)
3. else return Baumsuche(rc[x],k)
Laufzeit: O(h)
6
7
4
Höhe h
3
7
9
111
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
6
7
4
3
7
9
112
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Aufruf mit
x=root[T]
6
7
4
3
7
9
113
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Aufruf mit
x=root[T]
Baumsuche(root[T],5)
6
7
4
3
7
9
114
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
x
Baumsuche(root[T],5)
6
7
4
3
7
9
115
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Baumsuche(root[T],5)
6
x
3
7
4
7
9
116
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Baumsuche(root[T],5)
6
x
3
7
4
7
9
117
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Aufruf mit
x=root[T]
Baumsuche(root[T],5)
6
x
3
7
4
7
9
118
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Baumsuche(root[T],5)
6
7
4
3
x nil
7
9
119
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Baumsuche(root[T],5)
6
7
4
3
x nil
7
9
120
LS 2 /
Informatik
Datenstrukturen
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
Baumsuche(root[T],5)
6
7
4
3
x nil
7
9
121
LS 2 /
Informatik
Datenstrukturen
Funktionsweise wie (rekursive)
Baumsuche. Laufzeit ebenfalls O(h).
IterativeBaumsuche(x,k)
1. while xnil and kkey[x] do
2. if k<key[x] then x lc[x]
3. else x rc[x]
4. return x
6
7
4
3
7
9
122
LS 2 /
Informatik
Datenstrukturen
Minimum- und Maximumsuche
Suchbaumeigenschaft:
Alle Knoten im rechten Unterbaum eines Knotens x sind größer key[x]
Alle Knoten im linken Unterbaum von x sind key[x]
6
7
4
3
7
9
123
LS 2 /
Informatik
Datenstrukturen
Wird mit Wurzel
aufgerufen
MinimumSuche(x)
1. while lc[x]nil do x lc[x]
2. return x
6
7
4
3
7
9
124
LS 2 /
Informatik
Datenstrukturen
Wird mit Wurzel
aufgerufen
Laufzeit O(h)
MinimumSuche(x)
1. while lc[x]nil do x lc[x]
2. return x
6
7
4
3
7
9
125
LS 2 /
Informatik
Datenstrukturen
Wird mit Wurzel
aufgerufen
Laufzeit O(h)
MaximumSuche(x)
1. while rc[x]nil do x rc[x]
2. return x
6
7
4
3
7
9
126
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Nachfolger bzgl. Inorder-Tree-Walk
Wenn alle Schlüssel unterschiedlich, dann ist das der nächstgrößere
Schlüssel
6
7
4
3
7
9
127
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 1 (rechter Unterbaum von x nicht leer):
Dann ist der linkeste Knoten im rechten Unterbaum der Nachfolger von x
6
7
4
3
7
9
128
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 1 (rechter Unterbaum von x nicht leer):
Dann ist der linkeste Knoten im rechten Unterbaum der Nachfolger von x
6
7
4
3
7
9
129
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 1 (rechter Unterbaum von x nicht leer):
Dann ist der linkeste Knoten im rechten Unterbaum der Nachfolger von x
6
7
4
3
7
9
130
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 2 (rechter Unterbaum von x leer und x hat Nachfolger y):
Dann ist y der erste Knoten auf dem Pfad zur Wurzel, der größer als x ist
6
7
4
3
5
9
131
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 2 (rechter Unterbaum von x leer und x hat Nachfolger y):
Dann ist y der erste Knoten auf dem Pfad zur Wurzel, der größer als x ist
6
7
4
3
5
9
132
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche
Fall 2 (rechter Unterbaum von x leer und x hat Nachfolger y):
Dann ist y der erste Knoten auf dem Pfad zur Wurzel, der größer als x ist
6
7
4
3
5
9
133
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
6. return y
6
7
4
3
5
9
134
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(6)
6. return y
6
7
4
3
5
9
135
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(6)
6. return y
6
7
4
3
5
9
136
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(6)
6. return y
6
7
4
3
5
9
137
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
7
4
3
5
9
138
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
7
4
3
5
9
139
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
y
3
7
4
5
9
140
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
y
3
7
4
5
9
141
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
y
3
7
4
5 x
9
142
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
6. return y
6
y
3
7
4 x
5
9
143
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
y 6
6. return y
7
4 x
3
5
9
144
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
y 6
6. return y
7
4 x
3
5
9
145
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
Nachfolgersuche(5)
y 6
6. return y
7
4 x
3
5
9
146
LS 2 /
Informatik
Datenstrukturen
Nachfolgersuche(x)
1. if rc[x] nil then return MinimumSuche(rc[x])
2. y p[x]
3. while y nil and x=rc[y] do
4.
xy
5.
y p[y]
6. return y
6
7
4
3
Laufzeit O(h)
5
9
147
LS 2 /
Informatik
Datenstrukturen
Vorgängersuche
Laufzeit O(h)
Symmetrisch zu Nachfolgersuche
Daher ebenfall O(h) Laufzeit
6
7
4
3
5
9
148
LS 2 /
Informatik
Datenstrukturen
Binäre Suchbäume
Aufzählen der Elemente mit Inorder-Tree-Walk in O(n) Zeit
Suche in O(h) Zeit
Minimum/Maximum in O(h) Zeit
Vorgänger/Nachfolger in O(h) Zeit
Dynamische Operationen?
Einfügen und Löschen
Müssen Suchbaumeigenschaft aufrecht erhalten
Auswirkung auf Höhe des Baums?
149
LS 2 /
Informatik
Datenstrukturen
Einfügen
Ähnlich wie Baumsuche: Finde Blatt, an das neuer Knoten angehängt wird
Danach wird nil-Zeiger durch neues
Element ersetzt
6
7
4
3
5
9
150
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
6
7
4
3
5
9
151
LS 2 /
Informatik
Datenstrukturen
y wird Vater des
einzufügenden
Elements
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
x
6
7
4
3
5
9
152
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
x
6
7
4
3
5
9
153
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
x
6
7
4
3
y
5
9
154
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
x
6
7
4
3
y
5
9
155
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y
5
x
9
156
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y
5
x
9
157
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
y 7
4
3
5
x
9
158
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
y 7
4
3
5
x
9
159
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
y 7
4
3
5
9
160
x
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
y 7
4
3
5
9
161
x
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
5
y 9 x
162
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y 9
5
x nil
163
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y 9
5
x nil
164
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y 9
5
x nil
165
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y 9
5
x 8
166
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Einfügen(8)
6
7
4
3
y 9
5
x 8
167
LS 2 /
Informatik
Datenstrukturen
Einfügen(T,z)
1. y nil; x root[T]
2. while xnil do
3.
yx
4.
if key[z] < key[x] then x lc[x]
5.
else x rc[x]
6. p[z] y
7. if y=nil then root[T] z
8. else
9.
if key[z]< key[y] then lc[y] z
10. else rc[y] z
Laufzeit O(h)
6
7
4
3
5
9
8
168
LS 2 /
Informatik
Datenstrukturen
Löschen
3 unterschiedliche Fälle
(a) zu löschendes Element z hat keine Kinder
(b) zu löschendes Element z hat ein Kind
(c) zu löschendes Element z hat zwei Kinder
6
7
4
3
5
9
169
LS 2 /
Informatik
Datenstrukturen
Fall (a)
zu löschendes Element z hat keine Kinder
6
7
4
3
5
9
170
LS 2 /
Informatik
Datenstrukturen
Fall (a)
zu löschendes Element z hat keine Kinder
Entferne Element
6
7
4
3
6
5
4
9
3
7
9
171
LS 2 /
Informatik
Datenstrukturen
Fall (b)
Zu löschendes Element z hat 1 Kind
6
7
4
3
5
9
172
LS 2 /
Informatik
Datenstrukturen
Fall (b)
Zu löschendes Element z hat 1 Kind
6
7
4
3
6
5
9
4
9
3
5
173
LS 2 /
Informatik
Datenstrukturen
Fall (c)
Zu löschendes Element z hat 2 Kinder
6
7
4
3
5
9
174
LS 2 /
Informatik
Datenstrukturen
Fall (c)
Zu löschendes Element z hat 2 Kinder
Schritt 1: Bestimme Nachfolger von z
6
7
4
3
5
9
175
LS 2 /
Informatik
Datenstrukturen
Fall (c)
Nachfolger
hat nur ein
Kind
Zu löschendes Element z hat 2 Kinder
Schritt 1: Bestimme Nachfolger von z
6
7
4
3
6
5
9
4
9
3
5
176
LS 2 /
Informatik
Datenstrukturen
Fall (c)
Zu löschendes Element z hat 2 Kinder
Schritt 1: Bestimme Nachfolger von z
Schritt 2: Entferne Nachfolger
6
7
4
3
6
5
9
4
9
3
5
177
LS 2 /
Informatik
Datenstrukturen
Fall (c)
Zu löschendes Element z hat 2 Kinder
Schritt 1: Bestimme Nachfolger von z
Schritt 2: Entferne Nachfolger
Schritt 3: Ersetze z durch Nachfolger
6
7
4
3
7
5
9
4
9
3
5
178
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
6
7
4
3
5
9
179
LS 2 /
Informatik
Referenz auf z wird
Datenstrukturen
übergeben!
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
6
7
4
3
5
9
180
LS 2 /
Informatik
Bestimme Knoten, der
gelöscht werden soll.
Der Knoten hat nur
einen Nachfolger
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
z 6
7
4
3
5
9
181
LS 2 /
Informatik
Bestimme Knoten, der
gelöscht werden soll.
Der Knoten hat nur
einen Nachfolger
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
z 6
7 y
4
3
5
9
182
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
Bestimme das Kind
1. if lc[z]=nil or rc[z]=nil then y z
von y, falls existent
2. else y NachfolgerSuche(z)
Löschen(6)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
z 6
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
4
8.
else rc[p[y]] x
9. key[z] key[y]
3
5
7 y
9 x
183
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
Aktualisiere
2. else y NachfolgerSuche(z) Vaterzeiger von x Löschen(6)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
z 6
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
4
8.
else rc[p[y]] x
9. key[z] key[y]
3
5
7 y
9 x
184
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
z 6
7 y
4
3
5
9 x
185
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
Löschen(6)
3. if lc[y] nil then x lc[y]
Das rechte Kind von
4. else x rc[y]
z wird x.
z 6
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
4
8.
else rc[p[y]] x
9. key[z] key[y]
3
5
7 y
9 x
186
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
Löschen(6)
3. if lc[y] nil then x lc[y]
Das rechte Kind von
4. else x rc[y]
z wird x.
z 6
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
4
8.
else rc[p[y]] x
9. key[z] key[y]
3
5
7 y
9 x
187
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T]
x
Umkopieren
des
7. else if y=lc[p[y]] then lc[p[y]]
x y nach z
Inhalts
von
8.
else rc[p[y]] x
9. key[z] key[y]
3
Löschen(6)
z 7
4
5
9 x
188
LS 2 /
Informatik
Datenstrukturen
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
z 7
4
3
9
5
189
LS 2 /
Informatik
Datenstrukturen
Laufzeit O(h)
Löschen(T,z)
1. if lc[z]=nil or rc[z]=nil then y z
2. else y NachfolgerSuche(z)
3. if lc[y] nil then x lc[y]
4. else x rc[y]
5. if x nil then p[x] p[y]
6. if p[y]=nil then root[T] x
7. else if y=lc[p[y]] then lc[p[y]] x
8.
else rc[p[y]] x
9. key[z] key[y]
Löschen(6)
z 7
4
3
9
5
190
LS 2 /
Informatik
Datenstrukturen
Binäre Suchbäume
Ausgabe aller Elemente in O(n)
Suche, Minimum, Maximum, Nachfolger in O(h)
Einfügen, Löschen in O(h)
Frage
Wie kann man eine „kleine“ Höhe unter Einfügen und Löschen
garantieren?
191