-1

I have imlemented so far http://pastebin.com/gXJVXdLS

5. Create a remove method. search the tree if the item is found, then you kill it. you have to then balance the tree in AVL fashion to properly fill the hole that you make. Again, use the wikipedia description (which is pretty good) along with the simulator or even maybe my lecture to get the remove working properly. I won't provide sample data on this one-- you can figure that out yourself at this point. just remember that aside from the general case you have two special cases requiring double rotations (see lecture or play with the simulator for cases where you have a left/right or right/left situation.)

Now I need to implement this function

public void remove(K input)

help me in that. I have done this

public void remove(K input) {
    root = remove(root,input);
}

public AVLNode<K> remove(K x, AVLNode<K> t) {
    if (t==null)    { 
        System.out.println("Sorry but you're mistaken, " + t + " doesn't exist in this tree :)/>\n");
        return null;
    }
    System.out.println("Remove starts... " + t.data + " and " + x);
    if (x.compareTo(t.data) < 0 ) {
        t.left = remove(x,t.left);
        int l = t.left != null ? getHeight(t.left) : 0;

        if((t.right != null) && (getHeight(t.right) - l >= 2)) {
            int rightHeight = t.right.right != null ? getHeight(t.right.right) : 0;
            int leftHeight = t.right.left != null ? getHeight(t.right.left) : 0;

            if(rightHeight >= leftHeight)
                t = rotateLeft(t);              
            else
                t = rotateRight(t);//double
        }
    }
    else if (x.compareTo(t.data) > 0) {
        t.right = remove(x,t.right);
        int r = t.right != null ? getHeight(t.right) : 0;
        if((t.left != null) && getHeight(t.left) - r >= 2) {
            int leftHeight = t.left.left != null ? getHeight(t.left.left) : 0;
            int rightHeight = t.left.right != null ? getHeight(t.left.right) : 0;
            if(leftHeight >= rightHeight)
                t = rotateRight(t);             
            else
                t = rotateLeft(t);//and double
        }
    }
    return t;

} //End of remove...
VeteranLK
  • 717
  • 7
  • 17

1 Answers1

0

nvm found the answer

private AVLNode<K> findMin( AVLNode<K> t )
{
    if( t == null )
        return t;

    while( t.left != null )
        t = t.left;
    return t;
}
public void remove( K x )
{
    root = remove( x, root );
}
private AVLNode<K> remove( K x, AVLNode<K> t )
{
    if( t == null )
        return t;   // Item not found; do nothing

    int compareResult = x.compareTo( t.data );

    if( compareResult < 0 )
        t.left = remove( x, t.left );
    else if( compareResult > 0 )
        t.right = remove( x, t.right );
    else if( t.left != null && t.right != null ) // Two children
    {
        t.data = findMin( t.right ).data;
        t.right = remove( t.data, t.right );
    }
    else
        t = ( t.left != null ) ? t.left : t.right;
    return t;
}
VeteranLK
  • 717
  • 7
  • 17