I'm making a BST (Binary Search Tree). I would like some help with my delete method, it seem that when I set that node to null it stills appear when I display it with my display methods (preorder,inorder,postorder).
Here is my node linked list class
public class Node<T>
{
public int value;
public Node leftValue;
public Node rightValue;
public Node(int _value)
{
value=_value;
}
}
Here is my delete method in my 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 ");
}
}
My delete methods also tries to handle assigning a new successor if the node has children(leafs). May I also get some feedback on if I'm doing it correctly? It tries to handle 3 cases. Case 1 without children, Case 2 1 children, Case 3 2 children.
I think the probably is that the line in my delete method that deletes it by setting it to null if it has no children.
if (root.leftValue==null&&root.rightValue==null)
{
root = null;
}
It sets it to null but the root.value still has a int value which makes it still there when I display it with my display method. At least that's what I think the problem is. I would like some help and feedback thanks!
one of my display method
public void printPre(Node root)
{
if (root==null)
{
return;
}
System.out.print(root.value + ", ");
printPre(root.leftValue);
printPre(root.rightValue);
}
Thanks for the help!
When you call a method, and use variables as arguments, the variables are not passed into the method - only their values. That is why this:
class Test1 {
static void addOne(int i) {
i++;
}
public static void main(String[] args) {
int x = 5;
addOne(x);
System.out.println(x);
}
}
prints 5, not 6. To call addOne
, space for the parameter i
is allocated, the value 5 is copied from x
to i
, the method body runs (setting i
to 6), and then the method's local variables are destroyed when it returns. x
is not modified by the call.
Similarly, this:
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);
}
}
does not modify n.leftValue
. As before, the local variable root
is allocated, the value (which is a reference to a Node
object) is copied from n.leftValue
to root
, the method body executes (setting root
to null) and then the method's local variables are destroyed when it returns. n.leftValue
is not modified, only root
.
One alternative is to simply return the new node, since your function currently doesn't return anything. In the above simplified example:
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.