السلام عليكم ورحمة الله
أود الاستفسار إن تكرمتم
أريد بناء Family tree بواسطة الـ Binary tree بشكل مبسط
صالح هو الجد الأكبر - لديه ابنان (أحمد ومحمد ) ولدى محمد ثلاث ابناء(سامي وعلي وعمر)
لا أعلم ماهي المشكلة؟
هذا الكود الذي اعمل عليه
public class BinaryTree {
public static void main(String[] args) {
new BinaryTree().run();
}
static class Node
{
Node left;
Node right;
String value;
public Node(String value) {
this.value = value;
}
}
public void run() {
Node rootnode = new Node("Saleh");
System.out.println("Building tree with rootvalue " + rootnode.value);
System.out.println("=================================");
insert(rootnode, "Ahmad");
insert(rootnode, "Mohammad");
insert(rootnode, "Ali");
insert(rootnode, "Sami");
insert(rootnode, "Omar");
System.out.println("Traversing tree in order");
System.out.println("=================================");
printInOrder(rootnode);
}
public void insert(Node node, String value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of node " + node.value);
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of node " + node.value);
node.right = new Node(value);
}
}
}
public void printInOrder(Node node) {
if (node != null) {
printInOrder(node.left);
System.out.println(" Traversed " + node.value);
printInOrder(node.right);
}
}
}أرشدوني وجزاكم الله خيرا



