1 / 22

Geordnete Binärbäume

Geordnete Binärbäume. Prof. Dr. Martin Wirsing. in Zusammenarbeit mit Gilbert Beyer und Christian Kroiß http://www.pst.ifi.lmu.de/Lehre/wise-09-10/infoeinf/. Geordnete Binärbäume. Motivation Eine Datenstruktur für ein Namensverzeichnis soll implementiert werden

samson
Download Presentation

Geordnete Binärbä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. Geordnete Binärbäume Prof. Dr. Martin Wirsing in Zusammenarbeit mit Gilbert Beyer und Christian Kroiß http://www.pst.ifi.lmu.de/Lehre/wise-09-10/infoeinf/

  2. Geordnete Binärbäume • Motivation • Eine Datenstruktur für ein Namensverzeichnis soll implementiert werden • Die Datenstruktur soll die Operationen find, insert und print anbieten • Mit der Zeit wird die Menge an Namen wachsen (>1000) • Beispiele „Julia“ „Doris“ „Doris“ „Tom“ „Julia“ 0 1 2 3 4 5 „Tom“ „Romeo“ „Harry“ „Julia“ namen[2] = „Julia“ „Romeo“ • Array 2. Liste 3. Geordnete Binärbäume • (balanciert und zu einer Liste entartet)

  3. Geordnete Binärbäume 7 10 5 12 9 3 • Ein Binärbaum b heißt geordnet, wenn • b leer ist oder wenn • folgendes für alle nichtleeren Teilbäume t von b gilt: Der Schlüssel von t ist • größer (oder gleich) als alle Schlüssel des linken Teilbaums von t und • kleiner (oder gleich) als alle Schlüssel des rechten Teilbaums von t > < > < >

  4. Geordnete Binärbäume t = • Nicht geordnet ist der Baum t1: 7 t1 = 8 5 12 9 3 • Beispiel:Geordnet sind: Der leere Baum und der Baum t: 7 (abstrakte Darstellung) 8 5 12 6 3

  5. Suche im geordneten Binärbaum Prinzipieller Ablauf der Berechnung von t.find(6): Vergleiche 6 mit dem Wert der Wurzel; Da 6<7, gehe zum linken Kindknoten; 7 6<7 8 5 12 6 3

  6. Suche im geordneten Binärbaum Prinzipieller Ablauf der Berechnung von t.find(6): Vergleiche 6 mit dem Wert der Wurzel; Da 6<7, gehe zum linken Kindknoten; Vergleiche 6 mit dem Wert dieses Knotens; Da 6>5, gehe zum rechten Kindknoten dieses Knotens; 7 6<7 8 5 6>5 12 6 3

  7. Suche im geordneten Binärbaum Prinzipieller Ablauf der Berechnung von t.find(6): Vergleiche 6 mit dem Wert der Wurzel; Da 6<7, gehe zum linken Kindknoten; Vergleiche 6 mit dem Wert dieses Knotens; Da 6>5, gehe zum rechten Kindknoten dieses Knotens; Vergleiche 6 mit dem Wert dieses Knotens; Da 6==6, gebe Ergebnis true zurück. 7 6<7 8 5 6>5 12 6 3 6==6 return true;

  8. Suche im geordneten Binärbaum (Implementierung) :Node :Node :Node :Node :Node :Node key=6 key=3 key=12 key=7 key=10 key=5 t  • t.find(6): • Suche zunächst in BinTree. • Wenn t nicht leer, delegiere die • Aufgabe an Node durch • Aufruf von anchor.find(6); • Verfahre wie auf den vorgehenden Folien. :BinTree t.anchor 6<7 6>5 6==6 return true

  9. Suche im geordneten Binärbaum public class BinTree { . . . publicboolean find(int key) { if (anchor == null) return false; else return anchor.find(key); } . . . } class Node { . . . boolean find(int key) { Node current = this; while(current.key != key) // solange nicht gefunden, { if (key < current.key) // gehe nach links? current = current.left; else // sonst gehe nach rechts current = current.right; if(current == null) return false; //nicht gefunden! } return true; //gefunden; gib true zurück } Gibt true zurück, wenn key im Baum; sonst wird false zurückgegeben

  10. Suche im geordneten Binärbaum public class BinTree { . . . publicObject findValue(int key) { if (anchor == null) return null; else return anchor.findValue(key); } . . . } class Node { . . . boolean findValue(int key) { Node current = this; while(current.key != key) // solange nicht gefunden, { if (key < current.key) // gehe nach links? current = current.left; else // sonst gehe nach rechts current = current.right; if(current == null) return null; //nicht gefunden! } return current.value; //gefunden; gib true zurück } Gibt value zurück, wenn key im Baum; sonst wird null zurückgegeben

  11. Einfügen in geordneten Binärbaum • Beim Einfügen in einen geordneten Binärbaum wird rekursiv die “richtige” Stelle gesucht, so dass wieder eine geordneter Binärbaum entsteht. • Beispiel: t.insert(8) ergibt: • (Zur Vereinfachung der Darstellung wird hier nur ein Schlüssel und kein Wert eingefügt.) 7 7 t = t = t.insert(8) 10 10 5 5 12 8 12 6 6 3 3

  12. Einfügen in geordneten Binärbaum (Implementierung) :Node :Node :Node :Node :Node :Node key=12 key=6 key=5 key=7 key=10 key=3 this id t   8 t.insert(8) :BinTree . . . Aufruf von t.insert(id):

  13. Einfügen in geordneten Binärbaum (Implementierung) :Node :Node :Node :Node :Node :Node key=12 key=6 key=5 key=7 key=10 key=3 this id t   8 t.insert(8) :BinTree . . . Delegieren der Aufgabe durch Aufruf von achor.insert(id):

  14. :Node :Node :Node :Node :Node :Node key=6 key=12 key=3 key=10 key=7 key=5 this this id t id   8  8 Einfügen in geordneten Binärbaum (Implementierung) anchor.insertKey(8): :BinTree . . . Delegieren der Aufgabe durch Aufruf von anchor.insertKey(id): Durchlauf durch das Node-Geflecht mit zwei Hilfsvariablen current und parent Delegieren der Aufgabe durch Aufruf von head.add(x):

  15. :Node :Node :Node :Node :Node :Node key=10 key=5 key=12 key=3 key=6 key=7 this this t id id 8    8 current  parent  Einfügen in geordneten Binärbaum (Implementierung) anchor.insert(8): Falls id > current.key, gehe nach rechts: if(id > current.key) current = current.right; :BinTree Setze current = this; parent = current; . . . Delegieren der Aufgabe durch Aufruf von anchor.insert(id): Durchlauf durch das Node-Geflecht mit zwei Hilfsvariablen current und parent

  16. :Node :Node :Node :Node :Node :Node key=3 key=6 key=7 key=10 key=5 key=12 this this id id  8  8 current parent   Einfügen in geordneten Binärbaum (Implementierung) anchor.insert(8): Und setze: parent = current; Falls x > current.elem, gehe nach rechts: if(x > current.elem) current = current.right; :BinTree current . . .

  17. :Node :Node :Node :Node :Node :Node key=6 key=7 key=10 key=5 key=12 key=3 this this id id  8  8 current parent   Einfügen in geordneten Binärbaum (Implementierung) anchor.insert(8): Und setze: parent = current; Falls id < current.key, gehe nach links: if(key < current.key) current = current.left; :BinTree current . . .

  18. :Node :Node :Node :Node :Node :Node key=3 key=6 key=7 key=10 key=5 key=12 this this id id  8  8 parent current  null Einfügen in geordneten Binärbaum (Implementierung) anchor.insert(8): Wenn current= null, füge neuen Knoten ein: if(current == null) { parent.left = new Node(null,id,null); return this; } Falls x < current.elem, gehe nach links: if(x < current.elem) current = current.left; :BinTree current . . .

  19. :Node :Node :Node :Node :Node :Node :Node key=3 key=7 key=10 key=5 key=12 key=8 key=6 this this id id  8  8 current parent  null Einfügen in geordneten Binärbaum (Implementierung) Wenn current= null, füge neuen Knoten ein: if(current == null) { parent.left = new Node(null,id,null); return this; } anchor.insert(8): :BinTree current . . .

  20. Einfügen in geordneten Binärbaum Fügt einen neuen Knoten mit Schlüssel id an der richtigen Stelle im geordneten Baum ein public void insert(int id, Object o) { if(anchor==null) // falls kein Knoten im anchor anchor = new Node(null, id, o, null,); // neuer Knoten else anchor = anchor.insert(id, o); } wobeiinsert in class Node folgendermaßen definiert wird:

  21. Einfügen in geordneten Binärbaum (Implementierung) Fügt einen neuen Knoten passend ein Achtung:id darf nicht im Baum vorkommen! Node insert(int id, Object o) { Node current = this; // starte bei this Node parent; while(true) // terminiert intern { parent = current; if(id < current.key) // gehe nach links? { current = current.left; if(current == null) // am Ende füge links ein { parent.left = new Node(null, id, o, null); return this; } } // end if go left else // falls id > current.key, gehe nach rechts { current = current.right; if(current == null) // am Ende füge rechts ein { parent.right = new Node(null, id, o, null); return this; } } // end else go right } // end while }

  22. Zusammenfassung • Binäre Bäume können in Java implementiert werden als Verallgemeinerung der einfach verketteten Listen mit zwei Nachfolgerverweisen • Eine Operation auf binären Bäume mit Knoten (z.B. find, insert ) wird dann definiert durch Delegation der Operation an die Knotenklasse • Im gezeigten Beispiel gibt die Klasse BinTree die Verantwortung für einen Teil ihrer Funktionalität an die Hilfsklasse Node ab • Als weiteres Beispiel kann eine Methode print definiert werden, welche Informationen über den Knotenwert in der Konsole ausgibt

More Related