【问题标题】:Why won't my binary tree display what's being inserted?为什么我的二叉树不显示正在插入的内容?
【发布时间】:2017-04-30 12:09:30
【问题描述】:

我想知道为什么我的display() 方法即使在将项目插入到我的二叉树之后也会打印基本情况。我正在尝试比较学生的姓氏。我想要的只是显示我的树,以便我可以确认insert() 有效。

这是我的Node.java 文件:

class Node  {
   Student data;
   Node left;
   Node right;

public Node(Student data) {
    this.data = data;
    left = null;
    right = null;
  }
}

这是我的BinaryTree.java 文件:

public class BinaryTree {
    public Node root;

public void insert(Student s) {
    root.left = new Node(s);
    root.right = new Node(s);
    root = insert(s,root);
}

private Node insert(Student s, Node t) {
    if(t == null) {
        t = new Node(s);
        return t;
    }
    else { 
        if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
            t.left = new Node(s);
            t.left = insert(s,t.left);
            return t.left;
        } else if(s.getLastName().compareTo(t.data.getLastName()) > 0) {
            t.right = new Node(s);
            t.right = insert(s,t.right);         
            return t.right;
        }
    }
    return t;
}

public void display(Node root)  { 
    if(root == null) { 
        System.out.println("Nothing found.");
    } else if(root != null) {
        display(root.right);
        System.out.println(root.data);
        display(root.left);
    }
  }
}

这是我的Student.java 文件:

public class Student {

private String firstName;
    private String lastName;
    private String id;

public Student(String first, String last, String Identification) {
    firstName = first;
    lastName = last;
    id = Identification;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public boolean equals(String studentId) {
    return id.equals(studentId);    
  }
}

这是我的Main.java 文件:

public class Main {
  public static void main(String[] args) {
    Student student = new Student("hi", "bye", "testing");
    Student student2 = new Student("out", "some", "names");

    BinaryTree bt = new BinaryTree();
    bt.insert(student);
    bt.insert(student2);
    bt.display(bt.root);
   }
} 

这是我在控制台中的output:

Nothing found.

【问题讨论】:

  • if(t == null) { root = t; } 您将根设置为null
  • 您的显示逻辑也有问题,因为您在遇到基本情况时调用了System.exit(0)。这将杀死你的整个程序。您可能只想返回。
  • @MariaInesParnisari 但是然后我递归地插入到右边和左边。
  • 由于根始终为空,因此不会进入其他情况。 root = insert(s, root)
  • 我发现插入和遍历都有问题。让插入工作,然后担心显示。

标签: java algorithm data-structures binary-tree nodes


【解决方案1】:

我会说专注于插入逻辑,因为无论如何这都是错误的。

您不需要显示方法来查看树。启动调试器,检查左右Node对象引用

1) 您将立即在空树上遇到 nullpointerexception
2)您只需将根分配到一棵空树上,而不是将您可以到达的每个节点都设置为同一个学生节点

例如,就是这样

public class BinaryTree {
    public Node root;

    public void insert(Student s) {
        root = insert(s,root);
    }

    private Node insert(Student s, Node t) {
        if (root == null) return new Node(s);

        if (...) // compare names 

然后,如果你看这里,你会在任何时候覆盖new Node。

    if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
        t.left = new Node(s);  // this is useless 
        t.left = insert(s,t.left);
        return t.left;

您需要检查何时处于叶节点,然后相应地返回。并且您需要返回当前节点备份递归,而不是子节点。

    if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
        if (t left == null) {
            t.left = new Node(s);
       } else {
            t.left = insert(s,t);  // insert using t as the new root 
       } 
       return t;  // return t with its new child 

对右侧做同样的事情

如果这不起作用,请尝试为小型 3 节点树编写算法,然后为 4-7 节点平衡树编写算法

【讨论】:

  • 这真的很清楚,谢谢!我理解 100% 完全有意义的逻辑,但是当我尝试实现这一点时,我的控制台输出是:Nothing found. Student@4b71bbc9 Nothing found. Student@17dfafd1 Nothing found.
  • 您的递归display() 方法每次在树中遇到null 时都会打印Nothing found。通常会有很多这样的。相反,如果 root 是 null,即在调用递归方法之前(或代替)调用递归方法,则只打印该消息。
  • 不,@nampa。在第一次调用display() 时它是,但是当你递归调用时,root 不是树的绝对根,只是子树的相对根。如果整棵树都为空,这很可能为 null。
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2017-01-25
  • 2016-01-17
  • 2017-09-11
  • 1970-01-01
  • 2020-04-26
相关资源
最近更新 更多