Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 你能帮我打印一个用二叉搜索树做的家谱吗?_Java_Binary Search Tree - Fatal编程技术网

Java 你能帮我打印一个用二叉搜索树做的家谱吗?

Java 你能帮我打印一个用二叉搜索树做的家谱吗?,java,binary-search-tree,Java,Binary Search Tree,我已经开始学习二元搜索树,我做了一个练习,要求我用二元搜索树制作一个家谱 我已经创建了它,但是我遇到了一些问题,所以我不确定它是否正确,它有四个变量:姓名、姓氏、父亲和母亲,这使得这棵树与我已经看到的所有示例完全不同。我将在下面的代码中展示我已经完成的工作: //I have created a class Ancestor public class Ancestor { String name; String surname; Ancestor father;

我已经开始学习二元搜索树,我做了一个练习,要求我用二元搜索树制作一个家谱

我已经创建了它,但是我遇到了一些问题,所以我不确定它是否正确,它有四个变量:姓名、姓氏、父亲和母亲,这使得这棵树与我已经看到的所有示例完全不同。我将在下面的代码中展示我已经完成的工作:

//I have created a class Ancestor
public class Ancestor {
    String name;
    String surname;
    Ancestor father;
    Ancestor mother;

public Ancestor (String name, String surname){
    this.name = name;
    this.surname = surname;
    father = null;
    mother = null;
}

public void printAncestor() {
    System.out.println("Ancestors:"+"");
}

public void postOrder (Ancestor a) {
    if (a == null) {
        return;
    }

    else {
        postOrder(a.mother);
        postOrder(a.father);
        a.printAncestor();
    }
  }
}

//Another class familyTree
public class familyTree {
    static Ancestor root = null;

static void insertAncestor (String n, String s){
    Ancestor temp = root;
    Ancestor prev = null;
    boolean notFound = true;

    while (temp != null && notFound){
        if (temp.name.equals(n) && temp.surname.equals(s)){
            notFound = false;
            break;
        }
        else if (n.compareTo(n)<0 && s.compareTo(s)<0){
            prev = temp;
            temp = temp.mother;
        }
        else {
            prev = temp;
            temp = temp.father;
        }
    }

    if (notFound){
        Ancestor a = new Ancestor(n, s);
        if (prev == null) {
            root = a;
        }
        else if (n.compareTo(n)<0 && s.compareTo(s)<0){
            prev.mother = a;
        }
        else {
            prev.father = a;
        }
     }
  }
}

//And I have tried to create a family tree in the main class
public class Main {

public static void main(String[] args) {
// write your code here
    familyTree f = new familyTree();

    f.insertAncestor("Adam", "H");
    f.insertAncestor("Charles", "B");
    f.insertAncestor("Mary", "C");
    f.insertAncestor("Matthew", "W");
    f.insertAncestor("Jane", "X");

 }
}

但它没有成功。所以我不确定到底是怎么回事。正如我所说,这些变量(姓名、姓氏、父亲、母亲)与互联网和其他材料上的大多数例子不同,这一事实让我感到困惑。无论如何,我要提前感谢大家。

有几点。首先是一个小的样式问题:最好使用更具描述性的变量名。您的方法签名如下所示:

static void insertAncestor (String n, String s)
嗯,
n
s
都不是好的参数名称。我可以从上下文中看出,
n
代表姓名,
s
代表姓氏,但为什么不直接叫他们
name
姓氏

就实际的代码功能而言,这一行立即跳出来:

else if (n.compareTo(n)<0 && s.compareTo(s)<0){

else if(n.与(n)相比)您好,谢谢您的回答。我的一个主要问题就是,如何比较参数,并大体上创建树。我希望母亲在左边,父亲在右边,而我将是根,但我不知道如何做。关于使用的数据结构,只是练习是关于二进制search树。如果练习的重点是创建一个二元搜索树,那么我建议您更改正在使用的数据类型。族树不能作为二元搜索树的一部分很好地工作。BST的思想是每个节点都应该是可比较的,有一个定义良好的概念,即一个节点“大于”或“大于”“不到“另一个节点。每个节点的左子节点应小于该节点,右子节点应大于该节点。对于族谱而言,所有这些都没有意义。如果您想使用名称,可能需要根据每个学生的分数键入一个班级列表?我同意您的意见。谢谢您的回答。我将尝试调整它以获得更合理的解决方案。”。
else if (n.compareTo(n)<0 && s.compareTo(s)<0){