Script Java
Script Java Script Java
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
- Seite 195 und 196: 7.4 Exception Handling Schlüsselwo
- Seite 197 und 198: 7.5 Zusammenfassung ExceptionHandli
- Seite 199 und 200: 8.1 Abstrakte Methoden und Klassen
- Seite 201 und 202: 8.1 Abstrakte Methoden und Klassen
- Seite 203 und 204: 8.1 Abstrakte Methoden und Klassen
- Seite 205 und 206: 8.1 Abstrakte Methoden und Klassen
- Seite 207 und 208: 8.1 Abstrakte Methoden und Klassen
- Seite 209 und 210: 8.1 Abstrakte Methoden und Klassen
- Seite 211 und 212: 8.1 Abstrakte Methoden und Klassen
- Seite 213 und 214: 9.1 Interfaces und Adapter-Klassen
- Seite 215 und 216: 9.1 Interfaces und Adapter-Klassen
- Seite 217 und 218: 9.1 Interfaces und Adapter-Klassen
- Seite 219 und 220: 9.1 Interfaces und Adapter-Klassen
- Seite 221 und 222: 9.2 Zusammenfassung Muss man zwecks
- Seite 223 und 224: 10.1 Zweidimensionaler Array 10Arra
- Seite 225 und 226: 10.1 Zweidimensionaler Array public
- Seite 227 und 228: 10.1 Zweidimensionaler Array new Ti
- Seite 229 und 230: 11.1 Turtle-Geometrie } (int)Math.r
- Seite 231 und 232: 11.2 Vererbung forward() right() Ab
- Seite 233 und 234: 11.3 Rekursion Abbildung 11-3: Turt
- Seite 235 und 236: 11.3 Rekursion } setSize(400,400);
- Seite 237 und 238: 11.3 Rekursion public void tree(int
- Seite 239 und 240: 11.4 Stack import java.awt.event.*;
- Seite 241 und 242: 11.4 Stack public NumCheckbox(int n
- Seite 243 und 244: 11.4 Stack Abbildung 11-8: Turtle -
- Seite 245: 11.4 Stack forward 30 push() forwar
- Seite 249 und 250: 12.1 Binärer Baum Abbildung 12-2:
- Seite 251 und 252: 13.1 Anhang A: Java Syntax A nhäng
- Seite 253 und 254: 13.1 Anhang A: Java Syntax Prior. O
- Seite 255 und 256: 13.2 Anhang B: Übersicht Klassenbi
- Seite 257 und 258: 13.2 Anhang B: Übersicht Klassenbi
- Seite 259 und 260: 13.4 Anhang D: Prinzipien guten Pro
- Seite 261 und 262: 13.4 Anhang D: Prinzipien guten Pro
- Seite 263 und 264: 13.4 Anhang D: Prinzipien guten Pro
- Seite 265 und 266: 13.4 Anhang D: Prinzipien guten Pro
- Seite 267 und 268: 14.1 Stichwortverzeichnis S chwortv
- Seite 269 und 270: 14.1 Stichwortverzeichnis Logo, Pro
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