import java.util.LinkedList; import java.util.NoSuchElementException; public class BinTree { private Node root; /** * Konstruiert einen leeren Binaerbaum */ public BinTree() { } /** * Konstruiert einen Binaerbaum, der nur aus einem Knoten besteht * * @param key * @param value */ public BinTree(int key, Object value) { this.root = new Node(key, value); } /** * Konstruiert einen Binaerbaum aus einem linken und rechten Teilbaum, und * dem Schluessel/Wert-Paar fuer die Wurzel. * * @param left * @param key * @param value * @param right */ public BinTree(BinTree left, int key, Object value, BinTree right) { this.root = new Node(left.root, key, value, right.root); } /** * Gibt den linken Teilbaum zurueck * * @return * @throws NoSuchElementException */ public BinTree getLeft() throws NoSuchElementException { if (this.root == null) { throw new NoSuchElementException( "Ein leerer Baum hat keinen linken Teilbaum."); } else { BinTree l = new BinTree(); l.root = this.root.getLeft(); return l; } } /** * Gibt den rechten Teilbaum zurueck * * @return * @throws NoSuchElementException */ public BinTree getRight() throws NoSuchElementException { if (this.root == null) { throw new NoSuchElementException( "Ein leerer Baum hat keinen rechten Teilbaum."); } else { BinTree r = new BinTree(); r.root = this.root.getRight(); return r; } } /** * Gibt true zurueck genau dann wenn der Binaerbaum leer ist * * @return */ public boolean isEmpty() { return this.root == null; } }