[英]Inserting a node in BST
我試圖在我的自定義 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,空)
我每次都得到這個空值,這導致當前節點替換根節點。
我不知道為什么在通話過程中,root1 引用為空而不指向我的根?
更多信息 :
它在從 insertData() 調用到 insert() 期間。 說在我第二次調用 insertData(15) 期間,我調用了 insert(15,this.root) -->insert(node,root1) 。 但是這個 root1 引用結果是空的。但是當我檢查 this.root 時,它指的是正確的根節點。
謝謝!
好的,這里是您代碼的試運行,
插入 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 參數為空,執行第一個條件),丟棄先前定義的根。 這就是導致您的問題一次又一次地覆蓋您的根的原因。
插入第一個節點即根后,左右節點將為空。 下次插入左子節點或右子節點時,您不會檢查該條件。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.