chcę wygenerować drzewo:
public class Binarytree {
private static Node root;
public static String data;
public Binarytree(String data) {
root = new Node(data);
}
public void add(Node parent, Node child, String orientation) {
if (orientation.equals("l")) {
parent.setLeft(child);
} else {
parent.setRight(child);
}
}
public static void main(String args[]) {
Node n1 = new Node("a");
Node n2 = new Node("b");
Node n3 = new Node("c");
Node n4 = new Node("d");
Binarytree tree = new Binarytree("e");
tree.add(root, n1, "l");
tree.add(root, n2, "r");
tree.add(n2, n3, "l");
tree.add(n2, n4, "r");
}
}
class Node {
private String key;
private Node l;
private Node r;
Node(String string) {
this.key = string;
r = null;
l = null;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public void setLeft(Node l) {
this.l = l;
}
public Node getLeft() {
return l;
}
public void setRight(Node r) {
this.r = r;
}
public Node getRight() {
return r;
}
}
tylko teraz nie wiem jak je wyświetlić? :P
Później będę miała jeszcze mesę innych problemów:
mam później odczytać i wyświetlić powstałe słowa typu "dbe"...
mam do tego użyć rekurencji...
niestety dopiero zaczynam... i nie wiem jak to ugryźć