Ver Mensaje Individual
  #16 (permalink)  
Antiguo 07/07/2018, 21:59
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Balanceos con Árboles Hash

Buenas a todos, aquí estoy de vuelta resulta que ahora el eliminar no funciona, me da este error:

Cita:
Exception in thread "main" java.lang.NullPointerException
at treemapsimple.MyTreeMap.fixDown(MyTreeMap.java:258 )
at treemapsimple.MyTreeMap.deleteEntry(MyTreeMap.java :212)
at treemapsimple.MyTreeMap.remove(MyTreeMap.java:182)
at treemapsimple.Cuerpo.checkDelete1(Cuerpo.java:35)
at treemapsimple.Cuerpo.main(Cuerpo.java:43)
/home/detectivejd/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Acá está el código:

Código Java:
Ver original
  1. @Override
  2.     public V remove(Object key) {
  3.         Entry<K,V> p = getEntry(key);
  4.         if (p == null) {
  5.             return null;
  6.         } else {
  7.             V oldValue = p.value;
  8.             deleteEntry(p);              
  9.             return oldValue;
  10.         }
  11.     }
  12.     private void deleteEntry(Entry<K,V> p) {
  13.         size--;
  14.         if (p.left != null && p.right != null) {
  15.             Entry<K,V> s = successor(p);
  16.             p.key = s.key;
  17.             p.value = s.value;
  18.             p = s;
  19.         }
  20.         Entry<K,V> rep = (p.left != null ? p.left : p.right);
  21.         if (rep != null) {
  22.             rep.parent = p.parent;
  23.             if (p.parent == null) {
  24.                 root = rep;
  25.             } else if (p == p.parent.left) {
  26.                 p.parent.left  = rep;
  27.             } else {
  28.                 p.parent.right = rep;
  29.             }
  30.             p.left = p.right = p.parent = null;
  31.             if (p.color == BLACK) {
  32.                 fixDown(rep);
  33.             }
  34.         } else if (p.parent == null) { // return if we are the only node.
  35.             root = null;
  36.         } else { //  No children. Use self as phantom replacement and unlink.
  37.             if (p.color == BLACK) {
  38.                 fixDown(p);
  39.             }
  40.             if (p.parent != null) {
  41.                 if (p == p.parent.left) {
  42.                     p.parent.left = null;
  43.                 } else if (p == p.parent.right) {
  44.                     p.parent.right = null;
  45.                 }
  46.                 p.parent = null;
  47.             }
  48.         }        
  49.     }
  50.     private void fixDown(Entry<K,V>x){
  51.         while(x != root && x.color == BLACK) {
  52.             if(x == x.parent.left) {
  53.                 Entry<K,V> sib = x.parent.right;
  54.                 if(sib.color == RED) {
  55.                     sib.color = BLACK;
  56.                     x.parent.color = RED;
  57.                     rotateLeft(x.parent);
  58.                     sib = x.parent.right;
  59.                 }
  60.                 if(sib.left.color == BLACK && sib.right.color == BLACK) { //acá se detiene
  61.                     sib.color = RED;
  62.                     x = x.parent;
  63.                 } else {
  64.                     if(sib.right.color == BLACK) {
  65.                         sib.left.color = BLACK;
  66.                         sib.color = RED;
  67.                         rotateRight(sib);
  68.                         sib = x.parent.right;
  69.                     }
  70.                     sib.color = x.parent.color;
  71.                     x.parent.color = BLACK;
  72.                     sib.right.color = BLACK;
  73.                     rotateLeft(x.parent);
  74.                     x = root;
  75.                 }
  76.             } else {
  77.                 Entry<K,V> sib = x.parent.left;
  78.                 if(sib.color == RED) {
  79.                     sib.color = BLACK;
  80.                     x.parent.color = RED;
  81.                     rotateRight(x.parent);
  82.                     sib = x.parent.left;
  83.                 }
  84.                 if(sib.right.color == BLACK && sib.left.color == BLACK) { // acá se detiene
  85.                     sib.color = RED;
  86.                     x = x.parent;
  87.                 } else {
  88.                     if(sib.left.color == BLACK) {
  89.                         sib.right.color = BLACK;
  90.                         sib.color = RED;
  91.                         rotateLeft(sib);
  92.                         sib = x.parent.left;
  93.                     }
  94.                     sib.color = x.parent.color;
  95.                     x.parent.color = BLACK;
  96.                     sib.left.color = BLACK;
  97.                     rotateRight(x.parent);
  98.                     x = root;
  99.                 }
  100.             }
  101.         }
  102.         x.color = BLACK;
  103.     }

Mirando en otros sitios el fix del eliminar es muy parecido al que tengo, y no sé dónde está el problema, depurando se me detiene en la línea de los if del lado izquierdo como derecho.

Espero sus respuestas y saludos.
__________________
Si te interesa, visita mi perfil de Linkedin. Gracias