Tree.java
public class Branch {
private String content;
private Branch left = null;
private Branch right = null;
public void setLeft(Branch tree) { this.left = tree; }
public void setRight(Branch tree) { this.right = tree; }
public void setContent(String str) { this.content = str; }
public Branch getLeft() { return this.left; }
public Branch getRight() { return this.right; }
public String getContent() { return this.content; }
public String toString() {
String theString = "";
if (this.getLeft() != null) {
theString = theString + this.getLeft().toString();
}
if (this.getRight() != null) {
theString = theString + this.getRight().toString();
}
theString = theString + getContent();
return theString;
}
public static void main (String args[]) {
Branch a = new Branch();
Branch b = new Branch();
Branch c = new Branch();
Branch d = new Branch();
Branch e = new Branch();
a.setContent("A");
b.setContent("B");
c.setContent("C");
d.setContent("D");
e.setContent("E");
a.setLeft(b);
a.setRight(c);
b.setLeft(d);
c.setRight(e);
Branch f = new Branch();
f.setContent("F");
b.setRight(f);
System.out.println(a.toString());
}
}
Link to this Page