rimuovere metodo in java BST

voti
1

Io ho una domanda hw ... devo scrivere un metodo di rimozione per un albero binario di ricerca, finora Quello che ho è sotto, ma continuo a ricevere un po 'di errori associati con il mio metodo di rimozione e non so perché. ..would qualcuno si prega di essere in grado di controllare il mio codice. Grazie. Ho anche cercato di creare un metodo find ma sto avendo qualche problema con quello pure ... questo è tutto il senso alla parte inferiore del mio codice di rimozione.

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 );
   }
 */
È pubblicato 10/11/2009 alle 00:16
fonte dall'utente
In altre lingue...                            


2 risposte

voti
1

objè un esempio di Enon TreeNode383<E>quindi non ha getLeft()o getRight()metodo. E anche se lo ha fatto, di aver scritto male.

E che cosa è root? Non riesco a vedere dichiarate per l'ovunque.

Questa sintassi non ha senso neanche:

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

setRight() è un metodo, non un campo (Java non ha immobili come C #) Inoltre è necessario un capitale 'R' nel nome.

Quindi forse che dovrebbe essere

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

cioè, se newDataè stato dichiarato, che non lo è.

Ci sono troppi errori qui per dare un senso di codice. Provare l'attuazione di una funzione alla volta.

Risposto il 10/11/2009 a 00:26
fonte dall'utente

voti
0

ya..there sono alcuni errori ... fondamentalmente, per rimuovere un nodo N da un BST, sostituire N con l'elemento minimo nel sottoalbero destro di N.

Risposto il 14/01/2011 a 13:12
fonte dall'utente

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