【问题标题】:BST insertion not working using recursion in javaBST插入在java中使用递归不起作用
【发布时间】:2018-03-25 19:42:00
【问题描述】:

我正在尝试编写一种递归方式来使用 java 插入到我的二叉搜索树中,但它无法正常工作并给出空指针异常 我的 node.java 代码是

public class Node
{
public int data;
public Node left;
public Node right;

public Node()
{
    this.data = -1;
    this.left = null;
    this.right = null;
}
public Node(int n)
{
    this.data = n;
    this.left = null;
    this.right = null;
}
}

我在 Tree.java 中的代码是

public class Tree
{
public Node head = new Node();

public void insert(int n , Node m)
{
    if(m == null || m.data == -1)
    {
        m = new Node(n);
    }
    else
    {
        if(m.data > n)
        {
            insert(n,m.left);
        }
        else if(m.data < n)
        {
            insert(n,m.right);
        }
    }
}
public void print()
{
    System.out.println(head.data);
    System.out.println(head.left.data);
    System.out.println(head.right.data);
}
}

而test.java代码是

public class Test
{
public static void main(String[] args)
{
    Tree t = new Tree();
    Node m = new Node();
    t.insert(12,t.head);
    t.insert(11,t.head);
    t.insert(13,t.head);
    t.print();
}
}

当我编译并运行时,它给出了以下错误

-1
Exception in thread "main" java.lang.NullPointerException
at Tree.print(Tree.java:28)
at Test.main(Test.java:10)

【问题讨论】:

  • 您永远不会将子节点添加到树中的 head 节点。你的插入方法没有意义
  • 我递归调用它们,所以在第二步或第三步 m.left 或 m.right 不是节点,但我在堆栈跟踪中创建了它们,所以我认为不需要添加

标签: java recursion data-structures binary-search-tree


【解决方案1】:

编写的代码存在一些问题:

  1. 插入的基本情况(if 块)实际上不会修改树。它只是创建一个新节点并退出而不将新节点链接到树。
  2. t.head 以 data = -1 开头,因此对 t.insert(..., t.head) 的任何后续调用都将命中 insert() 中的 if 块并且什么都不做,如问题 1 中所述。
  3. 在您尝试阅读head.left.data 的t.print() 之前,NullPointerException 不会发生,但由于此时您只有一个节点树,head.left 为空。

核心问题是您的插入方法没有正确添加节点。它实际上需要将新节点添加为当前节点的子树。

这是固定代码(可能会被简化):

public class Tree {
    public Node head = new Node();

    public void insert(int n , Node m)
    {
        if(m == head && head.data == -1) 
        {
            // if we have an empty tree, just change the head
            head.data = n;
        }
        else if(m.data > n) {
            // we should be adding to the left subtree here
            if(m.left == null) {
                // if no left subtree, create a new node and link to m's left
                m.left = new Node(n);
            }
            else {
                // otherwise call insert on left subtree recursively
                insert(n, m.left);
            }
        }
        else
        {
            // otherwise add to right subtree
            if(m.right == null) {
                // no right subtree, so create node and link to m's right
                m.right = new Node(n);
            }
            else {
                // call recursively
                insert(n, m.right);
            }
        }
    }
    public void print()
    {
        System.out.println(head.data);
        System.out.println(head.left.data);
        System.out.println(head.right.data);
    }
}

【讨论】:

  • 是的,这就是其他情况下不会出现的问题,但我认为如果 m == -1 仅在第一种情况下发生,则 m 的值将为空,因为它不是一个节点本身(进一步的步骤)?
猜你喜欢
  • 2017-10-03
  • 2015-11-08
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2014-04-24
  • 2020-07-16
  • 2020-03-05
相关资源
最近更新 更多