Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Binary Search Tree - Delete Method

807591Apr 6 2008 — edited Apr 7 2008
I need help filling in the last part of this because I cannot figure out how. It is a case where the node has two kids. I replace the node with its inorder successor, and then delete its inorder successor node. Do I need to make an inOrder method?
public void delete( int myVal )
    {
        delete( root, myVal );
    }
    
private void delete( Tnode node, int value )
    {
        if ( ( node.lkid == null ) && ( node.rkid == null ) )
        {
            node = null;
        }
        else if ( ( node.lkid == null ) && ( node.rkid != null ) ) {
            node = node.rkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid == null ) ) {
            node = node.lkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid != null ) ) {
            // TODO implement
        }
    }

Comments

807591
I'm getting "It is a bad idea to assign a new value to a formal parameter within a method. Consider using a local variable instead." How can I set this up properly also?
800282
BicoloredNote wrote:
I'm getting "It is a bad idea to assign a new value to a formal parameter within a method. Consider using a local variable instead." How can I set this up properly also?
Here's how to delete a node from a BST:
[http://en.wikipedia.org/wiki/Binary_search_tree#Deletion]
807591
Updated code, however it doesn't work too well. Any suggestions?
That wiki didn't help too much.
public void delete( int myVal )
    {
        delete( root, myVal );
    }

private void delete( Tnode node, int value )
    {
        if ( ( node.lkid == null ) && ( node.rkid == null ) )
        {
            node = null;
        }
        else if ( ( node.lkid == null ) && ( node.rkid != null ) )
        {
            node = node.rkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid == null ) )
        {
            node = node.lkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid != null ) )
        {
            if ( node.lkid.val > node.rkid.val )
            {
                node = node.lkid;
                node.lkid = null;
            }
            else if ( node.rkid.val > node.lkid.val )
            {
                node = node.rkid;
                node.rkid = null;
            }
        }
    }
800282
BicoloredNote wrote:
Updated code, however it doesn't work too well. Any suggestions?
That wiki didn't help too much.
...
Sorry to hear that, but I can't make it clearer than on that wiki.
807591
Nevermind, figured it out.
800282
BicoloredNote wrote:
Nevermind, figured it out.
Good to hear it.
Would you mind posting your solution here as well then? I mean if someone else is trying to find the answer to your question and, finds this post after searching the web, s/he doesn't know the answer.
807591
Sure, SOLUTION
public void delete( int myVal )
    {
        delete( root, myVal );
    }


    /**
     * Recursive delete method.
     * 
     * @param node
     *            the node given for recursion
     * @param value
     *            the value given for recursion
     */
    private void delete( Tnode node, int value )
    {
        if ( ( node.lkid == null ) && ( node.rkid == null ) )
        {
            this.root = null;
        }
        else if ( ( node.lkid == null ) && ( node.rkid != null ) )
        {
            this.root = node.rkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid == null ) )
        {
            this.root = node.lkid;
        }
        else if ( ( node.lkid != null ) && ( node.rkid != null ) )
        {
            if ( node.lkid.val > node.rkid.val )
            {
                this.root = node.lkid;
                node.lkid = null;
            }
            else if ( node.rkid.val > node.lkid.val )
            {
                this.root = node.rkid;
                node.rkid = null;
            }
        }
    }
800282
BicoloredNote wrote:
Sure, SOLUTION

...
That's no valid delete(...) method!
You're not making a recursive call anywhere, so you won't be able to get to nodes that are more than one "hop" from the root. And even if you don't need a recursive call, like with the following tree:
-
     3
    / \
   /   \
  2     5
 / \   / \
*   * *   *

// * == null
and you'd call delete(2), then your method would leave the tree like this:
-
  5
 / \
*   *
which can't be correct, right?
807591
Yes, I just realized after testing that that did not work.

Any suggestions on fixing this?
800282
BicoloredNote wrote:
Yes, I just realized after testing that that did not work.

Any suggestions on fixing this?
Like I said: if you can't follow the explanation on the wiki page, I don't think I can help you further. But, this is how a recursive delete method could look like:
public void remove('valueToDelete') {
  // assume the tree is not empty and 'valueToDelete' is present in this tree
  this.remove('valueToDelete', 'root', null);
}
    
private void remove('valueToDelete', 'current', 'parent') {

  'diff' <- the difference between 'valueToDelete' and 'current.value'

  if('diff' is more than 0) {
    // recursively traverse into the right sub-tree of 'current'
  } 
  else if('difff' is less than 0) {
    // recursively traverse into the left sub-tree of 'current'
  } 
  else { // Ok, we found the node.

    'tempNode' <- the replacement of 'current'
    // Special cases: see the wiki page (here you need te assign 'tempNode')  
    if('current' has only one child) {
      // your code
    } 
    else if('current' has two children) {
      // your code
    }
    else { // no children
      // your code
    }
    
    // Rebuild the new tree (replace 'current' with 'tempNode')   
    if('current' has no parent) {
      // your code
    } 
    else if('current' is the left child of 'parent') {
      // your code
    } 
    else { // 'current' is the right child of 'parent'
      // your code
    }
  }
}
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 5 2008
Added on Apr 6 2008
10 comments
1,217 views