Willa Posted August 25, 2008 Posted August 25, 2008 It's very frustrating that objects can't be explicitly deleted in Java... I can change the reference and let garbage collection delete it, but this gets tricky when there's more than one reference to the object. Why does Java have this limitation and is there any way to get around it? Specifically, I'm trying to implement a binary search tree. Ultra-specifically, I'm trying to write the function to delete a node. Deleting a leaf should be easy--just get rid of it. But it's turning out to be the hardest type of node to delete because to "just get rid of it," I have to go back and find the parent node, figure out whether the leaf is on the left or right branch, and set either left or right to null. I'm not sure whether my instructor intends for us to simply use the built-in class java.util.TreeSet, but while I'm waiting for him to return my email... might as well learn something new, right?
bascule Posted August 25, 2008 Posted August 25, 2008 It's very frustrating that objects can't be explicitly deleted in Java... I can change the reference and let garbage collection delete it, but this gets tricky when there's more than one reference to the object. Why does Java have this limitation and is there any way to get around it? Most garbage collected languages don't let you explicitly free objects. This is because there may still be outstanding references to the object elsewhere in the application, and if you could explicitly free and object these references would become invalid. Specifically, I'm trying to implement a binary search tree. Ultra-specifically, I'm trying to write the function to delete a node. Deleting a leaf should be easy--just get rid of it. But it's turning out to be the hardest type of node to delete because to "just get rid of it," I have to go back and find the parent node, figure out whether the leaf is on the left or right branch, and set either left or right to null. This should be fairly easy to do with a recursive function which examines the children of a given node. That's where you should be doing your comparisons. If you're 'backtracking' up the tree you're doing something wrong.
Willa Posted August 25, 2008 Author Posted August 25, 2008 Thanks! After a little thought, I figured out how to implement the function without backtracking (although it still seems unnecessarily complicated to me). I guess all I needed was a bit of a rant...disguised as a civilized computer science question.
Willa Posted June 25, 2012 Author Posted June 25, 2012 I just uncovered this post of mine from forever ago, when I was starting to learn programming. 4 years and many real CS courses later, I have no idea what this question means. xD 2
khaled Posted June 27, 2012 Posted June 27, 2012 (edited) To remove\free an object that reserve memory in Java, you need to release all of its references Example: // reference A points at a new object Object A = new Object ( ); // reference B points at the same object Object B = A; // release reference A A = null; // the object is still in memory B = null; // now garbage collector will free the object Another example: class Node { public int data; public Node next; public Node (int d, Node n) { data = d; next = n; } } class DS { Node head = null; public void push (int d) { head = new Node (d, head); } public int pop () { int t = head.data; head = head.next; return (t); } // free the first reference of the chain // and chained objects will be removed // in a sequential order public reset () { head = null; } } Edited June 27, 2012 by khaled
ecoli Posted June 27, 2012 Posted June 27, 2012 I just uncovered this post of mine from forever ago, when I was starting to learn programming. 4 years and many real CS courses later, I have no idea what this question means. xD This is always fun. I have some doozies from my younger days.
Baby Astronaut Posted August 9, 2012 Posted August 9, 2012 Does a way exist for writing a line that instructs the program to ....just find all the references to an object? ....test or model what behaviors shall occur if you'd free all references to a set of chained objects, but without really freeing them?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now