Pregunta

/** The following function checks the red black tree black height
* @param n the root node is inputed then a traversal is done to calculate the black-height
* @return Return an error message / mesages informing the user whether or not the black height was maintained
* @author Ferron Smith
*/

public static void getCount (SkaRedBlackTreeNode skaRedBlackTreeNode) {
    VizRedBlackTreeNode n = skaRedBlackTreeNode.getVizRep();
if (validRoot(n))
    {
        static int lcount = leftCount(n);
        static int rcount = rightCount(n);

        if (rcount == lcount) {
            n.displayMsg("Black height maintained");
        }
        else
            // n.displayWarning("rcount " + rcount + " lcount " + lcount);
            n.displayError("Red Black Tree is unbalanced");
    }
}

/** The following function counts all the black node of the left side of the tree
 * @param n the left child is inputed and a traversal is done to count all the black nodes
 * */
public static int leftCount (VizRedBlackTreeNode n)
{
        if (n == null)
        return 0;
        else if (n.getrbtColr() == Color.black) 
                return 1 + leftCount(n.getLeft());
        else 
            leftCount(n.getLeft());    
}

/** The following function counts all the black node of the right side of the tree
 * @param n the right child is inputed and a traversal is done to count all the black nodes
 * */
public static int rightCount (VizRedBlackTreeNode n)
{   
    if (n == null)
    return 0;
    else if (n.getrbtColr() == Color.black) {
            return 1 + rightCount (n.getRight());
    else    
        rightCount(n.getRight());
    }
}

Esta es una nueva redacción, ¿cree que éste va a funcionar, he comprobado en ciertas condiciones y no me falló sin embargo

¿Fue útil?

Solución

Por lo que yo puedo decir, usted está comprobando la altura de negro sólo en los caminos extremos izquierdo y derecho hacia abajo del árbol. La definición de un árbol rojo-negro requiere que la altura negro sea el mismo en todos caminos. Por ejemplo, este árbol no válida no está marcado por su programa:

      B
     / \
    /   \
   /     \
  B       B
 / \     / \
B   R   R   B

Además, no comprueba los ciclos o si las claves están en orden.

Otros consejos

Así que me di cuenta de que está trabajando en Java aquí, pero aquí hay algo de pseudocódigo que puede ayudar:

unsigned int blackHeight()
  height, heightLeft, heightRight = 0
  if black
    height++
  if left
    heightLeft = left->blackHeight()
  else
    heightLeft = 1
  if right
    heightRight = right->blackHeight()
  else
    heightRight = 1
  if heightLeft != heightRight
    //YOU HAVE A PROBLEM!
  height += heightLeft
  return height

Sólo estoy empezando a experimentar con árboles negros rojos a mí mismo, pero creo que este algoritmo debe darle la altura de negro en cualquier nodo desde el cual lo.

Edit: Creo que debería calificar, esto sería código que se encuentra dentro de un nodo no está dentro del árbol. En c ++ sería llamado con un someNode-> blackHeight ().

 //enter code here

    private static void blackHeight(rbnode root) {
        if (root == null)
            return; 

        if (root.color == "black") {
            root.black_count = root.parent.black_count+1;

        } else {
            root.black_count = root.parent.black_count;
        }
        if ((root.left == null) && (root.right == null)) {              
            bh = root.black_count;              
        }               
        blackHeight(root.left);
        blackHeight(root.right);
    } 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top