Binäre Bäume Richard Göbel
Binäre Bäume Jeder Knoten hat höchsten zwei Nachfolger Anwendung: Verwaltung von Daten im Hauptspeicher
Binärer Baum: Definition eines Knotens public class Node { private int value; private Node left; private Node right; // Konstruktoren und Methoden . . . }
Binärer Baum: Ausgabe der Knoten public static void print(Node root, int depth) { for (int i = 0; i < depth; i++) { System.out.print(" "); } if (root == null) { System.out.println("<EMPTY>"); } else { System.out.println("Value: " + root.value); print(root.left, depth+1); print(root.right, depth+1);} } public static void print(Node root) { print(root, 0);
Binärer Baum: Suche nach Werten public Node search(int nValue) { if (newValue == value) return this; if (newValue < value) { if (left == null) { return null; } else { return left.search(newValue); } } if (right == null) { return right.search(newValue);} }}
Binärer Baum: Einfügen von Werten public void insert(int newValue) { if (newValue == value) { // hier Eintrag hinzufuegen } else if (newValue < value) { if (left == null) { left = new Node(newValue); } else { m_left.insert(newValue); }} else // newValue > value { if (right == null) { right = new Node(newValue); } right.insert(newValue);}} }
Binärer Baum: Löschen eines Knotens - Fälle Knoten ohne Nachfolger Knoten mit einem Nachfolger Knoten mit zwei Nachfolgern
Binärer Baum: Löschen eines Knotens - Code I public Node delete(int oldValue) { if (oldValue == value) { if (left == null) return right; if (right == null) return left; if (right.left == null) { value = right.value; right = right.right; return this; } else { value = right.deleteMin(); return this; } . . .
Binärer Baum: Löschen eines Knotens - Code II . . . if (oldValue < value) { if (left != null) { left = left.delete(oldValue); } return this; else { // oldValue > value if (right != null) { right = right.delete(oldValue);
Binärer Baum - Hauptklasse public class BinTree { private Node content = null; private int size = 0; . . . public Node search(int nValue) { . . . } public void insert(int nValue) { . . . } public void delete(int nValue) { . . . } public static void print(BinTree tree) { Node.print(tree.content(), 0); }
Binärer Baum - Weitere Dienstleistungen Sortierte Ausgabe aller Einträge Sortierte Ausgabe aller Einträge größer (kleiner) als ein vorgegebener Wert Sortierte Ausgabe aller Einträge zwischen zwei Werten
Binärer Baum - Sortierte Ausgabe aller Einträge public static void printAll(Node root) { if (root.left != null) { printAll(root.left); } System.out.println(root.value); if (root.right != null) { printAll(root.right);
Binärer Baum - Einträge größer als ein Wert public static void printAbove(Node root, int min) { if (root.value < min) { if (root.right != null) { printAbove(root.right(), min); } else { if (root.left != null) { printAbove(root.left, min); System.out.println(root.value); printAll(root.right); }}
Binärer Baum - Einträge in einem Bereich I public static void printBetween(Node root, int min,int max) { if (root.value < min) { if (root.right != null) { printBetween(root.right, min, max); } else if (root.value > max) { if (root.left != null) { printBetween(root.left, min, max); . . .
Binärer Baum - Einträge in einem Bereich II . . . else { if (root.left != null) { printAbove(root.left, min); } System.out.println(root.value); if (root.right != null) { printBelow(root.right, max);
Problem - Bäume mit langen Ästen
Lösung: (fast) balancierte Bäume Höhendifferenz des linken und rechten Teilbaums für jeden Knoten ist maximal k (k in der Regel 1) Beispiele: Rot-Schwarz-Baum AVL-Baum (G. M. Adel'son-Vel'skii und Y. M. Landis) Idee: Lokale Reorganisation des Baumes möglich
Lokale Transformation eines binären Baumes X Y A B C X Y A B C
Transformationen des AVL-Baumes - Teil1
Transformationen des AVL-Baumes - Teil 2