Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/06/2018, 20:47
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

Hola Fuzzylog, primero que todo gracias por responder y segundo hice lo que me dijiste:

Resulta que el 3er elemento se pierde en sí cuando insertas a p5 en el árbol, en el listado te lo conserva pero se pierde en la estructura del árbol, en estos métodos parece que está el truco:

Código Java:
Ver original
  1. private void rbInsertFixup(Entry<K,V> x) {
  2.         x.color = RED;
  3.         while (x.parent != null && x != root && x.parent.color == RED) {
  4.             if (x.parent == x.parent.parent.left) {
  5.                 // x's parent is a left child.
  6.                 Entry<K,V> y = x.parent.parent.right;
  7.                 if (y != null && y.color == RED) {
  8.                     // Case 1: x's uncle y is red.
  9. //                  System.out.println("rbInsertFixup: Case 1 left");
  10.                     x.parent.color = BLACK;
  11.                     y.color = BLACK;
  12.                     x.parent.parent.color = RED;
  13.                     x = x.parent.parent;
  14.                 } else {
  15.                     if (x == x.parent.right) {
  16.                         // Case 2: x's uncle y is black and x is a right child.
  17.                         // System.out.println("rbInsertFixup: Case 2 left");
  18.                         x = x.parent;
  19.                         rotateLeft(x);
  20.                     }
  21.                     // Case 3: x's uncle y is black and x is a left child.
  22.                     //System.out.println("rbInsertFixup: Case 3 left");
  23.                     x.parent.color = BLACK;
  24.                     x.parent.parent.color = RED;
  25.                     rotateRight(x.parent.parent);
  26.                 }
  27.             } else {
  28.                 // x's parent is a right child.  Do the same as when x's
  29.                 // parent is a left child, but exchange "left" and "right."
  30.                 Entry<K,V> y = x.parent.parent.left;
  31.                 if (y != null && y.color == RED) {
  32.                     // Case 1: x's uncle y is red.
  33.                     // System.out.println("rbInsertFixup: Case 1 right");
  34.                     x.parent.color = BLACK;
  35.                     y.color = BLACK;
  36.                     x.parent.parent.color = RED;
  37.                     x = x.parent.parent;
  38.                 } else {
  39.                     if (x == x.parent.left) {
  40.                         // Case 2: x's uncle y is black and x is a left child.
  41.                         // System.out.println("rbInsertFixup: Case 2 right");
  42.                         x = x.parent;
  43.                         rotateRight(x);
  44.                     }
  45.                     // Case 3: x's uncle y is black and x is a right child.
  46.                     // System.out.println("rbInsertFixup: Case 3 right");
  47.                     x.parent.color = BLACK;
  48.                     x.parent.parent.color = RED;
  49.                     rotateLeft(x.parent.parent);
  50.                 }
  51.             }
  52.         }
  53.         // The root is always black.
  54.         root.color = BLACK;
  55.     }
  56.     /**
  57.      * Do a right rotation around this node.
  58.     */
  59.     private void rotateRight(Entry<K,V> y) {
  60.         if(y != null){
  61.             Entry<K,V> x = y.left;
  62.             y.left = x.right;
  63.             if (x.right != null)
  64.                 x.right.parent = y;
  65.             x.parent = y.parent;
  66.             if (y.parent == null) {
  67.                 root = (Entry)x;
  68.             } else if (y == y.parent.right){
  69.                 y.parent.right = x;
  70.             } else {
  71.                 x.parent.left = x;
  72.             }
  73.             x.right = y;
  74.             y.parent = x;
  75.         }
  76.     }
  77.     /**
  78.      * Do a left rotation around this node.
  79.     */
  80.     private void rotateLeft(Entry<K,V> x) {
  81.         if(x != null){
  82.             Entry<K,V> y = x.right;
  83.             x.right = y.left;
  84.             if (y.left != null)
  85.                 y.left.parent = x;
  86.             y.parent = x.parent;
  87.             if (x.parent == null) {
  88.                 root = (Entry)y;
  89.             } else if (x == x.parent.left) {
  90.                 x.parent.left = y;
  91.             } else {
  92.                 x.parent.right = y;
  93.             }
  94.             y.left = x;
  95.             x.parent = y;
  96.         }        
  97.     }

Y ahí es cuando no me doy cuenta en cómo resolverlos. ¿Cómo resuelvo esta falla?

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