Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BST的java实现 我研究了C++,但是我对java很陌生。我正在尝试编写一个二叉搜索树(BST)类。这是我的代码: public class binary_tree { public class node { int data; node left , right; node(int data , node left , node right) { this.data = data; this.left = left; this.right = right; } } private node root = null; public void addElement(int x) { addElementNotSeen(x , this.root); //this function allows the user to only give x //as a parameter } private void addElementNotSeen(int x , node curent) { if (curent == null) { curent = new node(x , null , null); } else { if (x > curent.data)addElementNotSeen(x , curent.right); else addElementNotSeen(x , curent.left); } } }_Java_Arrays_Recursion_Data Structures - Fatal编程技术网

BST的java实现 我研究了C++,但是我对java很陌生。我正在尝试编写一个二叉搜索树(BST)类。这是我的代码: public class binary_tree { public class node { int data; node left , right; node(int data , node left , node right) { this.data = data; this.left = left; this.right = right; } } private node root = null; public void addElement(int x) { addElementNotSeen(x , this.root); //this function allows the user to only give x //as a parameter } private void addElementNotSeen(int x , node curent) { if (curent == null) { curent = new node(x , null , null); } else { if (x > curent.data)addElementNotSeen(x , curent.right); else addElementNotSeen(x , curent.left); } } }

BST的java实现 我研究了C++,但是我对java很陌生。我正在尝试编写一个二叉搜索树(BST)类。这是我的代码: public class binary_tree { public class node { int data; node left , right; node(int data , node left , node right) { this.data = data; this.left = left; this.right = right; } } private node root = null; public void addElement(int x) { addElementNotSeen(x , this.root); //this function allows the user to only give x //as a parameter } private void addElementNotSeen(int x , node curent) { if (curent == null) { curent = new node(x , null , null); } else { if (x > curent.data)addElementNotSeen(x , curent.right); else addElementNotSeen(x , curent.left); } } },java,arrays,recursion,data-structures,Java,Arrays,Recursion,Data Structures,但是,我的根似乎没有得到任何值。我已经看到,在Java中,您不需要通过引用传递参数,因此我看不出问题所在。您能帮我吗?这行代码 curent = new node(x , null , null); 在addlementNotSeen之外没有任何效果,因为它只修改本地引用 见: 引用数据类型参数(如对象)也通过值传递给方法。这意味着当方法返回时,传入的引用仍然引用与以前相同的对象。但是,如果对象字段具有适当的访问级别,则可以在方法中更改它们的值 当您检查节点是否为null并创建它时,您实际上是

但是,我的根似乎没有得到任何值。我已经看到,在Java中,您不需要通过引用传递参数,因此我看不出问题所在。您能帮我吗?

这行代码

curent = new node(x , null , null);
addlementNotSeen
之外没有任何效果,因为它只修改本地引用

见:

引用数据类型参数(如对象)也通过值传递给方法。这意味着当方法返回时,传入的引用仍然引用与以前相同的对象。但是,如果对象字段具有适当的访问级别,则可以在方法中更改它们的值


当您检查
节点
是否为
null
并创建它时,您实际上是在为参数的引用创建一个新节点,而不是为传递到函数外部的原始引用创建一个新节点

因此,不要在
addElementNotSeen
中检查
节点
是否为
,而是在
addElement
中检查
是否为
,并直接实例化

public void addElement(int x)
{
if(this.root==null)
this.root=新节点(x,null,null)
其他的
addElementNotSeen(x,this.root)
}
当递归地将数据传递到树下时也是如此。不要冒险将
null
作为参数传递。检查
left
right
是否为空,如果为空,则直接创建它们,如上例中使用
this.root

private void addlementnotseen(int x,节点当前)
{
其他的
{
如果(x>当前数据){
if(curent.right==null)
curent.right=新节点(数据,null,null);
其他的
加法不可见(x,当前右);
}否则{
//左边也一样
}        
}
} 

我还是不明白。。。那么,我应该如何改变它呢?考虑在AdDelMeNETSO中创建新的节点实例,当我将这个.root作为参数时,方法ADDelEntEntDebug被添加到堆栈内存中。“CurTrand”将指向“Hist.root”相同的对象,但是是“Currand”和“root”。实际上是相同的?或者它们在内存中有不同的地址?
curent
将指向与此相同的对象。root,对,但就在您执行
curent=…
,curent将指向与此不同的对象。root,而不是修改同一对象。用C++中的指针来考虑java变量(如果你有经验的话)。它们是引用,所以<代码> = < /Cord>只是修改引用,而不是修改对象。我知道C++相当不错,我正在学习java启动android dev.,谢谢!!