Binary search tree delete node java code

Binary search tree delete node java code

Posted: alexrulezzz Date of post: 24.05.2017

I made a binary search tree in Java but I'm having troubles whit the deleting nodes part. I managed to erase the node when it has only 1 son, and I have the idea to make the deletion when it has 2 sons, anyways the method I'm using when it has no sons when it's a leaf is not working in Java. That's the portion of the code that takes care of the nulling part.

java - Delete a node in Binary Search Tree - Stack Overflow

When I debug it, it is referencing the correct node and it's assigning it the null value, but when I return to the Frame where I'm calling the delete method for my tree the node is still there. What's the correct way to "null" an object in Java? I thought everything was a pointer in here and therefore this would work, but I think it doesn't. When you're null ing something you just make the reference in the scope you're in null.

It doesn't affect anything outside. The reason is that it's not a pointer, it's a reference. When you're null ing a reference, you just say "make this reference synonym to null". It doesn't mean that the object is deleted, it may still exist in other places.

Error (Forbidden)

There is no way to delete objects in java. All you can do is make sure no other object points to them, and the garbage collector will delete the objects sometime. Sign up for our newsletter and get our top new questions delivered to your inbox see an example. In java any function argument that is an object variable will never change the variable with another object instance.

That was one of the language design decisions like single inheritance. By posting your answer, you agree to the privacy policy and terms of service. By subscribing, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users.

c# - Delete a node from a binary tree - Code Review Stack Exchange

Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7.

binary search tree delete node java code

Join them; it only takes a minute: Let me explain by example. Say you have a method foo: Thanks a lot, that's what I wanted to know. Is there a way I can erase from inside a method to any object that is pointing to that object?

There is no way you can delete for any object that is pointing to that object, that's not the way java works. S - I'd appreciate if you upvote and accept my answer. Did you find this question interesting?

Try our newsletter Sign up for our newsletter and get our top new questions delivered to your inbox see an example. Please click the link in the confirmation email to activate your subscription.

Nothing remains but to reinsert either left or right subtree.

Sign up or log in StackExchange. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers. Stack Overflow works best with JavaScript enabled.

binary search tree delete node java code

MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,9 stars - 802 reviews
inserted by FC2 system