usuwać w sposób Java BST

głosy
1

Mam pytanie ... hw muszę napisać metodę Usuń dla wyszukiwania binarnego drzewa, tak daleko, co mam jest niższa, ale wciąż otrzymuję kilka błędów związanych z moim sposobem usunąć i nie jestem pewien dlaczego. ..would ktoś mógłby być w stanie sprawdzić mój kod. Dziękuję Ci. Próbowałem też stworzyć metodę znaleźć, ale mam pewne problemy z tym równie dobrze ... to aż na dnie mojego kodu usunąć.

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 );
   }
 */
Utwórz 10/11/2009 o 00:16
źródło użytkownik
W innych językach...                            


2 odpowiedzi

głosy
1

objJest to przypadek E, nie TreeNode383<E>tak, że nie ma getLeft()lub getRight()metody. A nawet gdyby tak się stało, to orkisz to źle.

I co root? Nie widzę deklarację dla tego nigdzie.

składnia to nie ma sensu, albo:

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

setRight() to nie metoda pole (Java nie posiada właściwości takich jak C #) Plus trzeba kapitał „R” w nazwie.

Więc może to powinno być

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

to znaczy, jeśli newDatazostało uznane, co nie jest.

Istnieje zbyt wiele błędów tutaj, aby poczucie kodzie. Spróbuj realizacji jednej funkcji na raz.

Odpowiedział 10/11/2009 o 00:26
źródło użytkownik

głosy
0

ya..there są pewne błędy ... w zasadzie, w celu usunięcia węzła N z BST, wymienić N z minimalnego elementu w prawym poddrzewie N.

Odpowiedział 14/01/2011 o 13:12
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more