【问题标题】:Inserting a node in BST在 BST 中插入一个节点
【发布时间】:2014-10-10 10:13:42
【问题描述】:

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

下面是我的代码:

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.主类=

包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
}
}

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

插入数据(15){ 插入(15,10) }

调用插入方法为---->

插入(15,null)

我每次都得到这个空值,这会导致当前节点替换根节点。

我无法弄清楚为什么在通话过程中,root1 引用为空且不指向我的根?

更多信息:

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

谢谢!

【问题讨论】:

  • 好的...所以我调试错了。感谢您的帮助:)

标签: java binary-search-tree


【解决方案1】:

好的,这里是你的代码的试运行,

插入 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

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

【讨论】:

    【解决方案2】:

    插入第一个节点即根节点后,左右节点将为空。下次插入左或右子节点时,您不会检查该条件。

    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;  
    }
    

    【讨论】:

    • 感谢您的回答,但这不是问题。它在从 insertData() 到 insert() 的调用期间。在我第二次调用 insertData(15) 期间,我调用了 insert(15,this.root) -->insert(node,root1) 。但是这个 root1 引用结果是空的。但是当我检查 this.root 时,它指的是正确的根节点..
    • 它在insert 中满足if(root1==null) 条件?
    猜你喜欢
    • 2017-06-14
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多