Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 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
  22.                 r = r.right;
  23.             removeReturn = true;
  24.         }
  25.         return r;
  26.     }
  27.  
  28.     private BinaryNode removeFirst(BinaryNode r) {
  29.         if (r != null) {
  30.             if (r.left != null) {
  31.                 BinaryNode first = null;
  32.                 BinaryNode father = r;
  33.                 BinaryNode aux = r.left;
  34.                 while (aux.left != null) {
  35.                     father = aux;
  36.                     aux = aux.left;
  37.                 }
  38.                 father.left = aux.right;
  39.             } else
  40.                 r = r.right;
  41.         }
  42.         return r;
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement