| Webbrowser | G:\fernuni_hamburg\java\internet\browser\wwwbrowser.java | liest html der Webseite ein |
| TimeServer | G:\fernuni_hamburg\java\internet\netzzeit\zeit.java | Zeit lesen von time.nist.gov |
| Karte zeichnen (Höhenmodell) | G:\fernuni_hamburg\java\bleckede\elbe.java | Karte Bleckede.dgm |
| Bild anzeigen | G:\fernuni_hamburg\java\Applets\Bildappl\Nebel\nebel.java | nebel.jpg |
| Umrechnen | G:\fernuni_hamburg\java\Applets\Eurorechner2\euro.java | |
| Funktion plotten | G:\fernuni_hamburg\java\Applets\funktion3\funktion.java |
Die Methode getMaximum findet das Maximum eines binären Suchbaums iterativ.
Die Methode isSearchTree überprüft, ob es sich um einen Suchbaum handelt, also ob der Baum sortiert ist (bei inorder-Durchlauf).
import java.util.*;
class BinaryTreeNode{
private int entry;
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;
static ArrayList<Integer> liste = new ArrayList<Integer>();
public int getEntry(){
return entry;
}
public BinaryTreeNode getLeft(){
return leftChild;
}
public BinaryTreeNode getRight(){
return rightChild;
}
public void setEntry(int e){
entry=e;
}
public void setLeft(BinaryTreeNode e){
leftChild=e;
}
public void setRight(BinaryTreeNode e){
rightChild=e;
}
/* static public boolean isSearchTree(BinaryTreeNode node){
if (node==null) {
return true;
} // end of if
else {
boolean b1,b2,b3,b4;
b1=true;
if (node.getLeft() != null) {
if (node.getLeft().getEntry()>node.getEntry()) {
b1=false;
} // end of if-else
}
b2=true;
if (node.getRight() != null) {
if (node.getRight().getEntry()<node.getEntry()) {
b2=false;
} // end of if-else
}
b3= isSearchTree(node.getLeft());
b4= isSearchTree(node.getRight());
return b1 && b2 && b3 && b4;
} // end of if-else
} */
static public void inorder(BinaryTreeNode node){
if (node != null) {
inorder(node.getLeft());
System.out.println(node.getEntry());
liste.add(node.getEntry());
inorder(node.getRight());
} // end of if
}
static public boolean isSearchTree(BinaryTreeNode node){
int alt =0;
int neu =0;
inorder(node);
for (Integer wert : liste ) {
neu= wert;
if (alt>neu) {
return false;
} // end of if
System.out.println(wert);
alt=neu;
} // end of for
return true;
}
static public int getMaximum(BinaryTreeNode node){
if (node==null) {
return 0;
} // end of if
while (node.getRight()!= null) {
node=node.getRight();
} // end of while
return node.getEntry();
}
public static void main(String[] args){
BinaryTreeNode root= new BinaryTreeNode();
root.setEntry(5);
BinaryTreeNode neu= new BinaryTreeNode();
neu.setEntry(3);
root.setLeft(neu);
neu.setRight(new BinaryTreeNode());
neu.getRight().setEntry(4);
neu= neu.getRight();
neu.setLeft(new BinaryTreeNode());
neu.getLeft().setEntry(4);
neu.setRight(new BinaryTreeNode());
neu.getRight().setEntry(5);
root.setRight(new BinaryTreeNode());
root.getRight().setEntry(7);
//root=null;
System.out.println(BinaryTreeNode.isSearchTree(root));
System.out.println(BinaryTreeNode.getMaximum(root));
}
}
Realisierung mit einer Methode, die auf inorder-Durchlauf basiert:
import java.util.*;
class BinaryTreeNode2{
private int entry;
private BinaryTreeNode2 leftChild;
private BinaryTreeNode2 rightChild;
static int alt=0;
static int neu=0;
public int getEntry(){
return entry;
}
public BinaryTreeNode2 getLeft(){
return leftChild;
}
public BinaryTreeNode2 getRight(){
return rightChild;
}
public void setEntry(int e){
entry=e;
}
public void setLeft(BinaryTreeNode2 e){
leftChild=e;
}
public void setRight(BinaryTreeNode2 e){
rightChild=e;
}
static public boolean isSearchTree(BinaryTreeNode2 node){
if (node == null) {
return true;
}
if (isSearchTree(node.getLeft())==false) return false;
neu=node.getEntry();
System.out.println(node.getEntry());
if (alt>neu) return false;
alt=neu;
if (isSearchTree(node.getRight())==false) return false;
return true;
}
static public int getMaximum(BinaryTreeNode2 node){
if (node==null) {
return 0;
} // end of if
while (node.getRight()!= null) {
node=node.getRight();
} // end of while
return node.getEntry();
}
public static void main(String[] args){
BinaryTreeNode2 root= new BinaryTreeNode2();
root.setEntry(5);
BinaryTreeNode2 neu= new BinaryTreeNode2();
neu.setEntry(3);
root.setLeft(neu);
neu.setRight(new BinaryTreeNode2());
neu.getRight().setEntry(4);
neu= neu.getRight();
neu.setLeft(new BinaryTreeNode2());
neu.getLeft().setEntry(1);
neu.setRight(new BinaryTreeNode2());
neu.getRight().setEntry(8);
root.setRight(new BinaryTreeNode2());
root.getRight().setEntry(7);
//root=null;
System.out.println(BinaryTreeNode2.isSearchTree(root));
//System.out.println(BinaryTreeNode2.getMaximum(root));
}
}