在BST中插入节点

时间:2014-10-10 10:13:42

标签: java binary-search-tree

我正在尝试在我的自定义BST中插入节点。第一次调用insertData方法时,新节点被正确插入为root。问题出现在第二次和后续调用中。

以下是我的代码:

1.节点类=

package ishan.trees.tree;

class Node implements Comparable<Node> {

private int data;
public int getData() {
    return data;
}

public void setData(int data) {
    this.data = data;
}

public Node getLeftChild() {
    return leftChild;
}

public void setLeftChild(Node leftChild) {
    this.leftChild = leftChild;
}

public Node getRightChild() {
    return rightChild;
}

public void setRightChild(Node rightChild) {
    this.rightChild = rightChild;
}

private Node leftChild;
private Node rightChild;

public Node(int data,Node leftChild,Node rightChild)
{
    this.data=data;
    this.leftChild=leftChild;
    this.rightChild=rightChild;

}

@Override
public int compareTo(Node o) {
    if(o.getData() > this.data)
    return -1;

    if(o.getData() < this.data)
        return 1;

    return 0;
}
}

树类=

package ishan.trees.tree;

public class Tree {

private Node root=null;

public Node getRoot() {
    return root;
}

public void insertData(int data)
{
    Node node=new Node(data,null,null);
    insert(node,this.root);

}

private Node insert(Comparable<Node> node,Node root1)
{
        if(root1==null)
        {//insert as first element ie root
            this.root=new Node(((Node)node).getData(),null,null);
        }
        else if(node.compareTo(root1) <0)
        {
            root1.setLeftChild(insert(node,root1.getLeftChild()));
        }
        else if(node.compareTo(root1) >0)
        {

            root1.setLeftChild(insert(node,root1.getRightChild()));
        }

 return root1;  
}
}

3.Main Class =

package ishan.trees.usage;

import ishan.trees.tree.Tree;

public class Usuage {

public static void main(String a[])
{
    Tree tree=new Tree();
    tree.insertData(10); //---------1
    tree.insertData(15); //---------2
    tree.insertData(9);  //---------3
    tree.insertData(4);  //---------4
}
}

当我调试第二个调用时,它是这样的:

insertData(15){   插入(15,10) }

将插入方法调用为----&gt;

插入(15,NULL)

每次都得到null,这会导致当前节点替换根节点。

我无法弄清楚为什么在调用期间,root1引用为null并且没有指向我的root?

更多信息:

在从insertData()到insert()的调用期间。在我第二次调用insertData(15)时,我调用insert(15,this.root) - &gt; insert(node,root1)。但是这个root1引用结果是null。但是当我检查this.root时它指的是正确的根节点。

谢谢!

2 个答案:

答案 0 :(得分:2)

好的,这是代码的干运行,

插入10。

当您插入第一个元素时,此API insert会根据您的代码为您创建一个新根,并将其值设置为10,

现在第二次插入让它变得有趣,看看发生了什么

栈跟踪

insertData(15);
insert(node,root) // here root is your actuall root, originally initialized when u inserted first

// it goes to last else if inside insert api
root1.setRightChild(insert(node,root1.getRightChild())); // see now, this apis actually calls insert again, coz new node value was greater then root value

// this is how next stack trace will look like, as root right child was null
insert(node,null); // observer second argument is null again

现在根据你的插入代码将最终再次创建root(root1参数为null,执行第一个条件),丢弃先前定义的root。这就是造成你的问题的原因,你一次又一次地覆盖你的根。

答案 1 :(得分:1)

插入第一个节点即root后,左右节点将为null。下次插入左或右子节点时,您没有检查该条件。

private Node insert(Comparable<Node> node,Node root1)
{
    if(root1==null)
    {//insert as first element ie root
        this.root=new Node(((Node)node).getData(),null,null);
    }
    else if(node.compareTo(root1) <0)
    {
        if(root1.getLeftChild()==null)
            root1.setLeftChild(node);
        else 
            root1.setLeftChild(insert(node,root1.getLeftChild()));
    }
    else if(node.compareTo(root1) >0)
    {

        if(root1.getRightChild()==null)
            root1.setRightChild(node);
        else 
            root1.setRightChild(insert(node,root1.getRightChild()));
    }

return root1;  
}