为什么我的节点设置为null后还是会出现?
Why does my node still appear when I set it to null?
我正在制作 BST(二叉搜索树)。我想要一些关于我的删除方法的帮助,似乎当我将该节点设置为 null 时,当我使用我的显示方法(前序、中序、后序)显示它时,它仍然出现。
这是我的节点链表class
public class Node<T>
{
public int value;
public Node leftValue;
public Node rightValue;
public Node(int _value)
{
value=_value;
}
}
这是我在 BST 中的删除方法 class
public void findDelete(Node root, int value)
{
if (root== null)
{
System.out.println(" Not founded ");
}
else if (value < root.value)
{
findDelete(root.leftValue,value);
}
else if (value > root.value)
{
findDelete(root.rightValue,value);
}
else if (value == root.value)
{
//// checks if have children
if (root.leftValue==null&&root.rightValue==null)
{
root = null;
}
//// one
else if ( root.leftValue==null)
{
root.value=root.rightValue.value;
}
else if ( root.rightValue==null)
{
root.value=root.leftValue.value;
}
//// two
else if ( root.leftValue!=null && root.rightValue!=null)
{
root.value=findMin(root.rightValue);
findDelete(root.rightValue,value);
}
}
else
{
System.out.println(" Not founded ");
}
}
如果节点有 children(叶子),我的删除方法也会尝试处理分配新的后继者。如果我做得正确,我也可以得到一些反馈吗?它试图处理 3 个案例。案例 1 没有 children,案例 2 1 children,案例 3 2 children。
我认为可能是我的 delete 方法中的那一行通过将它设置为空来删除它,如果它没有 children。
if (root.leftValue==null&&root.rightValue==null)
{
root = null;
}
它将它设置为 null 但 root.value 仍然有一个 int 值,当我用我的显示方法显示它时它仍然存在。至少那是我认为的问题所在。我需要一些帮助和反馈,谢谢!
我的一种展示方式
public void printPre(Node root)
{
if (root==null)
{
return;
}
System.out.print(root.value + ", ");
printPre(root.leftValue);
printPre(root.rightValue);
}
感谢您的帮助!
当您调用方法并使用变量作为参数时,变量不会传递到方法中 - 只有它们的值。这就是为什么:
class Test1 {
static void addOne(int i) {
i++;
}
public static void main(String[] args) {
int x = 5;
addOne(x);
System.out.println(x);
}
}
打印 5,而不是 6。要调用 addOne
,分配参数 i
的 space,值 5 从 x
复制到 i
,方法体运行(设置i
为6),然后方法的局部变量在return时被销毁。 x
未被调用修改。
同样,这个:
class Test2 {
static void delete(Node root) {
root = null;
}
public static void main(String[] args) {
Node n = (something); // not actually (something), but something that returns a Node
delete(n.leftValue);
}
}
不修改 n.leftValue
。和以前一样,局部变量 root
被分配,值(它是对 Node
对象的引用)从 n.leftValue
复制到 root
,方法体执行(将 root
设置为 null) 然后方法的局部变量在 return 时被销毁。 n.leftValue
没有修改,只有root
.
一种替代方法是简单地 return 新节点,因为您的函数目前没有 return 任何东西。在上面的简化示例中:
class Test3 {
// returns the new node
static Node delete(Node root) {
// in this example it always deletes the node by replacing it with null;
// obviously your actual code is more complicated than this
return null;
}
public static void main(String[] args) {
Node n = (something); // not actually (something), but something that returns a Node
n.leftValue = delete(n.leftValue); // <-- note the change
}
}
我正在制作 BST(二叉搜索树)。我想要一些关于我的删除方法的帮助,似乎当我将该节点设置为 null 时,当我使用我的显示方法(前序、中序、后序)显示它时,它仍然出现。
这是我的节点链表class
public class Node<T>
{
public int value;
public Node leftValue;
public Node rightValue;
public Node(int _value)
{
value=_value;
}
}
这是我在 BST 中的删除方法 class
public void findDelete(Node root, int value)
{
if (root== null)
{
System.out.println(" Not founded ");
}
else if (value < root.value)
{
findDelete(root.leftValue,value);
}
else if (value > root.value)
{
findDelete(root.rightValue,value);
}
else if (value == root.value)
{
//// checks if have children
if (root.leftValue==null&&root.rightValue==null)
{
root = null;
}
//// one
else if ( root.leftValue==null)
{
root.value=root.rightValue.value;
}
else if ( root.rightValue==null)
{
root.value=root.leftValue.value;
}
//// two
else if ( root.leftValue!=null && root.rightValue!=null)
{
root.value=findMin(root.rightValue);
findDelete(root.rightValue,value);
}
}
else
{
System.out.println(" Not founded ");
}
}
如果节点有 children(叶子),我的删除方法也会尝试处理分配新的后继者。如果我做得正确,我也可以得到一些反馈吗?它试图处理 3 个案例。案例 1 没有 children,案例 2 1 children,案例 3 2 children。
我认为可能是我的 delete 方法中的那一行通过将它设置为空来删除它,如果它没有 children。
if (root.leftValue==null&&root.rightValue==null)
{
root = null;
}
它将它设置为 null 但 root.value 仍然有一个 int 值,当我用我的显示方法显示它时它仍然存在。至少那是我认为的问题所在。我需要一些帮助和反馈,谢谢!
我的一种展示方式
public void printPre(Node root)
{
if (root==null)
{
return;
}
System.out.print(root.value + ", ");
printPre(root.leftValue);
printPre(root.rightValue);
}
感谢您的帮助!
当您调用方法并使用变量作为参数时,变量不会传递到方法中 - 只有它们的值。这就是为什么:
class Test1 {
static void addOne(int i) {
i++;
}
public static void main(String[] args) {
int x = 5;
addOne(x);
System.out.println(x);
}
}
打印 5,而不是 6。要调用 addOne
,分配参数 i
的 space,值 5 从 x
复制到 i
,方法体运行(设置i
为6),然后方法的局部变量在return时被销毁。 x
未被调用修改。
同样,这个:
class Test2 {
static void delete(Node root) {
root = null;
}
public static void main(String[] args) {
Node n = (something); // not actually (something), but something that returns a Node
delete(n.leftValue);
}
}
不修改 n.leftValue
。和以前一样,局部变量 root
被分配,值(它是对 Node
对象的引用)从 n.leftValue
复制到 root
,方法体执行(将 root
设置为 null) 然后方法的局部变量在 return 时被销毁。 n.leftValue
没有修改,只有root
.
一种替代方法是简单地 return 新节点,因为您的函数目前没有 return 任何东西。在上面的简化示例中:
class Test3 {
// returns the new node
static Node delete(Node root) {
// in this example it always deletes the node by replacing it with null;
// obviously your actual code is more complicated than this
return null;
}
public static void main(String[] args) {
Node n = (something); // not actually (something), but something that returns a Node
n.leftValue = delete(n.leftValue); // <-- note the change
}
}