[英]BST insertion not working using recursion in java
I am trying to write a recursive way to insert to my binary search tree using java but it is not working properly gives null pointer exception my code for node.java is 我正在尝试使用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;
}
}
my code in Tree.java is 我在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);
}
}
and test.java code is 和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();
}
}
when I compile and run, it gives following error 当我编译并运行时,它给出以下错误
-1
Exception in thread "main" java.lang.NullPointerException
at Tree.print(Tree.java:28)
at Test.main(Test.java:10)
A few issues with the code as written: 编写的代码存在一些问题:
if
block) of your insert doesn't actually modify the tree. if
块)实际上并没有修改树。 It simply creates a new node and exits without linking the new node to the tree. t.head
starts with data = -1
, so any subsequent call to t.insert(..., t.head)
will hit the if
block in insert()
and do nothing, as mentioned in issue 1. t.head
以data = -1
开头,因此对t.insert(..., t.head)
任何后续调用都将insert()
的if
块,并且不执行任何操作,如问题1中所述。 t.print()
where you read head.left.data
, but since you only have a single node tree at this point, head.left
is null. t.print()
读取head.left.data
,不会发生NullPointerException,但是由于此时您只有一个节点树,所以head.left
为null。 The core problem is your insert method doesn't add nodes properly. 核心问题是您的insert方法无法正确添加节点。 It needs to actually add the new node as a subtree of the current node.
它实际上需要将新节点添加为当前节点的子树。
Here's the fixed code (could probably be simplified): 这是固定的代码 (可能会简化):
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.