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@Override
Entry<K,V> p = getEntry(key);
if (p == null) {
return null;
} else {
V oldValue = p.value;
deleteEntry(p);
return oldValue;
}
}
private void deleteEntry(Entry<K,V> p) {
size--;
if (p.left != null && p.right != null) {
Entry<K,V> s = successor(p);
p.key = s.key;
p.value = s.value;
p = s;
}
Entry<K,V> rep = (p.left != null ? p.left : p.right);
if (rep != null) {
rep.parent = p.parent;
if (p.parent == null) {
root = rep;
} else if (p == p.parent.left) {
p.parent.left = rep;
} else {
p.parent.right = rep;
}
p.left = p.right = p.parent = null;
if (p.color == BLACK) {
fixDown(rep);
}
} else if (p.parent == null) { // return if we are the only node.
root = null;
} else { // No children. Use self as phantom replacement and unlink.
if (p.color == BLACK) {
fixDown(p);
}
if (p.parent != null) {
if (p == p.parent.left) {
p.parent.left = null;
} else if (p == p.parent.right) {
p.parent.right = null;
}
p.parent = null;
}
}
}
private void fixDown(Entry<K,V>x){
while(x != root && x.color == BLACK) {
if(x == x.parent.left) {
Entry<K,V> sib = x.parent.right;
if(sib.color == RED) {
sib.color = BLACK;
x.parent.color = RED;
rotateLeft(x.parent);
sib = x.parent.right;
}
if(sib.left.color == BLACK && sib.right.color == BLACK) { //acá se detiene
sib.color = RED;
x = x.parent;
} else {
if(sib.right.color == BLACK) {
sib.left.color = BLACK;
sib.color = RED;
rotateRight(sib);
sib = x.parent.right;
}
sib.color = x.parent.color;
x.parent.color = BLACK;
sib.right.color = BLACK;
rotateLeft(x.parent);
x = root;
}
} else {
Entry<K,V> sib = x.parent.left;
if(sib.color == RED) {
sib.color = BLACK;
x.parent.color = RED;
rotateRight(x.parent);
sib = x.parent.left;
}
if(sib.right.color == BLACK && sib.left.color == BLACK) { // acá se detiene
sib.color = RED;
x = x.parent;
} else {
if(sib.left.color == BLACK) {
sib.right.color = BLACK;
sib.color = RED;
rotateLeft(sib);
sib = x.parent.left;
}
sib.color = x.parent.color;
x.parent.color = BLACK;
sib.left.color = BLACK;
rotateRight(x.parent);
x = root;
}
}
}
x.color = BLACK;
}
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.