views:

348

answers:

1

Hello everyone...I do have a hw question...I have to write a remove method for a binary search tree, so far what I have is below but I keep getting a bunch of errors associated with my remove method and I'm not sure why...would someone please be able to check my code. Thank You. I also tried to create a find method but I'm having some trouble with that as well...that is all the way at the bottom of my remove code.

import java.util.*;

class TreeNode383<E extends Comparable> {

  private E data;

  private TreeNode383<E> left;

  private TreeNode383<E> right;

  private TreeNode383<E> parent;

  public TreeNode383( ) { left = right = parent = null; }

  public TreeNode383( E d, TreeNode383 <E> l, TreeNode383 <E> r,
                     TreeNode383 <E> p) {

    data = d;

    left = l;

    right = r;

    parent = p;

  }

  public  E getData( ) { return data; }

  public void setData(E d) { data = d; }

  public TreeNode383<E> getLeft( ) { return left; }

  public void setLeft(TreeNode383<E> l) { left = l; }

  public TreeNode383<E> getRight( ) { return right; }

  public void setRight(TreeNode383<E> r) { right = r; }

  public TreeNode383<E> getParent( ) { return parent; }

  public void setParent(TreeNode383<E> p) { parent = p; }


  public String toString( ) {

    String answer = "";

    if (left != null) answer += left.toString( );

    answer += data + " ";

    if (right != null) answer += right.toString( );

    return answer;
  }
}

**The start of my remove method**


  boolean remove (E obj)
  {

 if(root == obj)

 return false;


 //when deleting a leaf just delete it

 else if(obj.getleft == NULL && obj.getright == NULL)
  parent = obj = NULL;


 //when deleting an interior node with 1 child
 //replace that node with the child

 else if(obj.getleft == NULL && obj.getright != NULL)
 obj.setright = new TreeNode383<E>(newData, null, null, null);

 else if(obj.getleft != NULL && obj.getright == NULL
 obj.setleft = new TreeNode383<E>(newData, null, null, null);


 //when deleting an interior node with 2 children
 //find left most node in right subtree,
 //promote it to replace the deleted node
 //promote its child to replace where it was



  /*
  private BinaryNode findMin( BinaryNode t )
  {
      if( t == null )
            return null;
      else if( t.left == null )
           return t;
      return findMin( t.left );
   }
 */
A: 

obj is an instance of E not TreeNode383<E> so it has no getLeft() or getRight() method. And even if it did, you spelled it wrong.

And what's root? I can't see a declaration for that anywhere.

This syntax makes no sense either:

obj.setright = new TreeNode383<E>(newData, null, null, null);

setRight() is a method not a field (Java does not have properties like C#) Plus you need a capital 'R' in the name.

So maybe that should be

obj.setRight(new TreeNode383<E>(newData, null, null, null));

that is, if newData was declared, which it isn't.

There are too many errors here to make sense of your code. Try implementing one function at a time.

finnw