public static void main(String[]args){ String []name = {"Kamel", "Fliege", "Hund", "Katze", "Maus", "Esel",};} System.out.println("Sortierarray");
public class BinBaum{ public static void main(String[]args) { String []name = {"Kamel", "Fliege", "Hund", "Katze", "Maus", "Esel",}; System.out.println("Sortierarray"); for (int i=0; i<name.length; i++) { System.out.println (name[i]); } BinBaum bin = new BinBaum (name[0]); for (int i=1; i<name.length; i++) { bin.anfuegen (name[i]);} bin.ausgabe(); } private Knoten head = null; private BinBaum (String name) { head = new Knoten(name); } public void anfuegen (String name) { Knoten kn = head; int n=0; while (kn != null) { n = name.compareToIgnoreCase (kn.getName()); if (n ==0)return; System.out.println ("xxx"+kn.getName()); if (n<0) { if (kn.haslNext()) { kn=kn.getlLink();} else { kn.setlLink (new Knoten (name)); return; } } else { if (kn.hasrNext()) {kn=kn.getrLink();} else { kn.setrLink (new Knoten (name)); return; } } } kn =new Knoten (name); } public void ausgabe() { System.out.println("\n\nSortiert:"); inorder (head); } public void inorder (Knoten kn) { if (kn != null) { inorder (kn.getlLink()); System.out.println(kn.getName()); inorder (kn.getrLink()); } } private class Knoten { private Knoten lLink=null; private Knoten rLink=null; private String name; public Knoten (String name) { this.name = name; } public void setlLink (Knoten kn) {lLink=kn;} public void setrLink (Knoten kn) {rLink=kn;} public String getName() {return name;} public boolean haslNext() { if (lLink==null) return false; else return true; } public boolean hasrNext() { if (rLink==null) return false; else return true; } public Knoten getlLink() {return lLink;} public Knoten getrLink() {return rLink;} }}