Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public boolean remove(Object item) {//Ejercicio 5
  2.         root = remove(root,(E) item);
  3.         return removeReturn;
  4.     }
  5.    
  6.     private BinaryNode remove(BinaryNode r, E item){
  7.         if(r==null)
  8.             removeReturn=false;
  9.         else if(item.compareTo(r.data)<0)
  10.             r.left=remove(r.left,item);
  11.         else if(item.compareTo(r.data)>0)
  12.             r.right=remove(r.right,item);
  13.         else{
  14.             size--;
  15.             if(r.left!=null && r.right!=null){
  16.                 E min = first(r.right);
  17.                 r.data = min;
  18.                 r.right = removeFirst(r.right);
  19.             }else if(r.left!=null)
  20.                 r=r.left;
  21.             else r=r.right;
  22.         removeReturn=true;
  23.         }
  24.         return r;
  25.     }
  26.    
  27.     private BinaryNode removeFirst(BinaryNode r){
  28.         if(r!=null){
  29.             if(r.left!=null){
  30.                 BinaryNode first=null;
  31.                 BinaryNode father=r;
  32.                 BinaryNode aux=r.left;
  33.                 while(aux.left!=null){
  34.                     father = aux;
  35.                     aux=aux.left;
  36.                 }
  37.                 father.left=aux.right;
  38.             }else
  39.                 r=r.right;
  40.         }
  41.         return r;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement