Script Java

Script Java Script Java

29.01.2014 Aufrufe

12.1 Binärer Baum 12Binärer Baum Hashtabelle Die Programmreihe „Family“ illustriert anhand eines Familienstammbaumes die Datenstruktur eines binären Baumes. 12.1 Binärer Baum Das untenstehende Programm ist ein Ausschnitt aus dem Beispielprogramm „Family - Version 3“. public class Person { static Hashtable table = new Hashtable(100); static EmptyPerson empty = new EmptyPerson(); static boolean isKnown(String name) {return table.containsKey(name);} static Person getPerson(String name) {return (Person)table.get(name);} String name; boolean male; Person father, mother; public Person(String string) { name=string; table.put(name, this); } public boolean isEmpty() {return false;} public void setSex(String sex) {male = sex.equals("male");} public void setFather(Person person) {father=person;} public void setMother(Person person) {mother=person;} public String getName() {return name;} public Person getFather() {return father;} public Person getMother() {return mother;} public String getSex() { if (male) return "male"; 246

12.1 Binärer Baum } else return "female"; public String getFathersName() {return getFather().getName();} public String getMothersName() {return getMother().getName();} public String toString() {return name+" ("+father+", "+mother+")";} public int count() {return 1+father.count()+mother.count();} public int maxDepth() { return 1+Math.max(father.maxDepth(), mother.maxDepth()); } public boolean treeContains(Person person) { if (this==person) return true; else return father.treeContains(person)||mother.treeContains(person); } public boolean isRelatedTo(Person person) { if (person.treeContains(this)) return true; else return father.isRelatedTo(person)||mother.isRelatedTo(person); } public void paint(Graphics g, int x, int y, int dist) { g.fillOval(x-3,y-3,6,6); g.drawString(name,x+6,y+3); if((!father.isEmpty())&(y>60)) { g.drawLine(x,y,x-dist,y-50); father.paint(g,x-dist,y-50,dist/2); } if((!mother.isEmpty())&(y>60)) { g.drawLine(x,y,x+dist,y-50); mother.paint(g,x+dist,y-50,dist/2); } } } public String toPostOrder() { return father.toPostOrder()+mother.toPostOrder()+ getName()+"\n"+getSex()+"\n"+getFathersName()+ "\n"+getMothersName()+"\n"; } public class EmptyPerson extends Person{ public EmptyPerson() {super("");} public boolean isEmpty() {return true;} public String getName() {return "";} public Person getFather() {return empty;} public Person getMother() {return empty;} public String toString() {return "";} public int count() {return 0;} public int maxDepth() {return 0;} public boolean treeContains(Person person) {return false;} public boolean isRelatedTo(Person person) {return false;} public void paint(Graphics g, int x, int y, int dist) {return;} public String toPostOrder() {return "";} } 247

12.1 Binärer Baum<br />

}<br />

else return "female";<br />

public String getFathersName() {return getFather().getName();}<br />

public String getMothersName() {return getMother().getName();}<br />

public String toString() {return name+" ("+father+", "+mother+")";}<br />

public int count() {return 1+father.count()+mother.count();}<br />

public int maxDepth() {<br />

return 1+Math.max(father.maxDepth(), mother.maxDepth());<br />

}<br />

public boolean treeContains(Person person) {<br />

if (this==person) return true;<br />

else<br />

return father.treeContains(person)||mother.treeContains(person);<br />

}<br />

public boolean isRelatedTo(Person person) {<br />

if (person.treeContains(this)) return true;<br />

else<br />

return father.isRelatedTo(person)||mother.isRelatedTo(person);<br />

}<br />

public void paint(Graphics g, int x, int y, int dist) {<br />

g.fillOval(x-3,y-3,6,6);<br />

g.drawString(name,x+6,y+3);<br />

if((!father.isEmpty())&(y>60)) {<br />

g.drawLine(x,y,x-dist,y-50);<br />

father.paint(g,x-dist,y-50,dist/2);<br />

}<br />

if((!mother.isEmpty())&(y>60)) {<br />

g.drawLine(x,y,x+dist,y-50);<br />

mother.paint(g,x+dist,y-50,dist/2);<br />

}<br />

}<br />

}<br />

public String toPostOrder() {<br />

return father.toPostOrder()+mother.toPostOrder()+<br />

getName()+"\n"+getSex()+"\n"+getFathersName()+<br />

"\n"+getMothersName()+"\n";<br />

}<br />

public class EmptyPerson extends Person{<br />

public EmptyPerson() {super("");}<br />

public boolean isEmpty() {return true;}<br />

public String getName() {return "";}<br />

public Person getFather() {return empty;}<br />

public Person getMother() {return empty;}<br />

public String toString() {return "";}<br />

public int count() {return 0;}<br />

public int maxDepth() {return 0;}<br />

public boolean treeContains(Person person) {return false;}<br />

public boolean isRelatedTo(Person person) {return false;}<br />

public void paint(Graphics g, int x, int y, int dist) {return;}<br />

public String toPostOrder() {return "";}<br />

}<br />

247

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!