Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can someone verify if this code for deleting a BST works?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone verify if this code for deleting a BST works?

    	/** Clear tree method
    	 * @param a node
    	 */
    	private void clear(Node x) {
    		if(x.left != null) {
    			x = x.left;
    			clear(x);
    		}
    		if(x.right != null) {
    			x = x.right;
    			clear(x);
    		}
    	}

    And relevant code fragment
    			Node iter = root.left;
    			Node iter2 = root.right;
     
    			// delete left side of tree
    			while(iter != null) {
    				clear(iter);
    					System.out.println(""+iter.value);
    				iter = iter.left;
    			}
     
    			// delete right side of tree
    			while(iter2 != null) {
    				clear(iter2);
    				iter = iter.right;
    			}
     
    			// delete root
    			clear(root);
    			root = null;

    Above code is supposed to delete an entire binary search tree. But I don't know if it's doing it right or not.

    Sorry if it's been posted before. If it doesn't work could you point me in the right direction?

    Thanks.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can someone verify if this code for deleting a BST works?

    Assuming your nodes don't have parent references (i.e. you can only descend down the tree, not up), you can easily clear the tree by setting the tree's root to null. This will result in a cascading dereferencing of nodes as their parent nodes get garbage collected.

    However, if this is not the case, your code is a little flawed.
        /** Clear tree method
         * @param a node
         */
        private void clear(Node x) {
            if(x.left != null) {
                x = x.left;
                clear(x);
            }
            if(x.right != null) {
                x = x.right;
                clear(x);
            }
        }
    You're re-assigning the value of x. You shouldn't need to do this (and in fact will ruin the clear method if there is a left and right node).

        /** Clear tree method
         * @param a node
         */
        private void clear(Node x) {
            if(x.left != null) {
                clear(x.left);
            }
            if(x.right != null) {
                clear(x.right);
            }
        }

    There's something else that needs to be taken care of: you actually need to de-reference that node. Simply calling clear doesn't remove that node from the BST. Simply assigning the left and right nodes to null (after clearing them) should do the trick.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone verify if this code for deleting a BST works?

    Can't believe I didn't see that! Yea my tree only moves down. Thanks a million.

Similar Threads

  1. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  2. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 02:58 PM
  3. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM
  4. How database connection pooling works in a application
    By JayVirk in forum JDBC & Databases
    Replies: 0
    Last Post: October 10th, 2009, 07:14 AM

Tags for this Thread