Pregunta

Tengo un problema de tarea sobre insertar en árboles rojos-negro en C#. Escribí el código a continuación y el programa funciona sin ningún problema para agregar los primeros 3 números. Cuando intento agregar el 4to número, obtengo una NullReferenceException. Estoy tratando de resolver el error durante 2 días, pero no puedo resolverlo.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algoritmalar3b
{
class rbtNode
{
    public int? key;
    public char? color;
    public rbtNode left, right, p;
    public rbtNode(int? key, char? color, rbtNode left, rbtNode right, rbtNode p)
    {
        this.key = key;
        this.color = color;
        this.left = left;
        this.right = right;
        this.p = p;
    }
}

class Program
{
    static rbtNode Tnil = new rbtNode(null, 'B', null, null, null);

    static void Main(string[] args)
    {
        rbtNode root = new rbtNode(null, 'B', Tnil, Tnil, null);

        RB_Insert(root, 7);
        RB_Insert(root, 3);
        RB_Insert(root, 89);
        RB_Insert(root, 4);
        RB_Insert(root, 9);
        RB_Insert(root, 15);
        RB_Insert(root, 35);
        RB_Insert(root, 8);
        RB_Insert(root, 24); 
        preOrderWalk(root);
    }

    static void RB_Insert(rbtNode T, int? deger)
    {
        rbtNode z = new rbtNode(deger, null, Tnil, Tnil, null);

        if (T.key == null)
            T.key = deger;
        else
        {
            rbtNode y = new rbtNode(null, null, Tnil, Tnil, null);
            y = Tnil;
            rbtNode x = new rbtNode(null, null, Tnil, Tnil, null);
            x = T;
            while (x != Tnil)
            {
                y = x;
                if (z.key < x.key)
                    x = x.left;
                else
                    x = x.right;
            }
            z.p = y;
            if (y == Tnil)
                T = z;
            else if (z.key < y.key)
                y.left = z;
            else
                y.right = z;
            z.left = Tnil;
            z.right = Tnil;
            z.color = 'R';
            RB_Insert_Fixup(T, z);
        }
    }

    static void RB_Insert_Fixup(rbtNode T, rbtNode z)
    {
        rbtNode y = new rbtNode(null, null, Tnil, Tnil, null);

        while (z.p.color == 'R')
        {
            if (z.p == z.p.p.left)
            {
                y = z.p.p.right;
                if (y.color == 'R')
                {
                    z.p.color = 'B';
                    y.color = 'B';
                    z.p.p.color = 'R';
                    z = z.p.p;
                }
                else if (z == z.p.right)
                {
                    z = z.p;
                    Left_Rotate(T, z);
                    z.p.color = 'B';
                    z.p.p.color = 'R';
                    Right_Rotate(T, z.p.p);
                }
            }
            else
            {
                y = z.p.p.left;
                if (y.color == 'R')
                {
                    z.p.color = 'B';
                    y.color = 'B';
                    z.p.p.color = 'R';
                    z = z.p.p;
                }
                else if (z == z.p.left)
                {
                    z = z.p;
                    Left_Rotate(T, z);
                    z.p.color = 'B';
                    z.p.p.color = 'R';
                    Right_Rotate(T, z.p.p);
                }
            }
        }
        T.color = 'B';
    }

    static void Left_Rotate(rbtNode T, rbtNode x)
    {
        rbtNode y = new rbtNode(null, null, Tnil, Tnil, null);
        y = x.right;
        x.right = y.left;
        if (y.left != Tnil)
            y.left.p = x;
        y.p = x.p;
        if (x.p == Tnil)
            T = y;
        else if (x == x.p.left)
            x.p.left = y;
        else
            x.p.right = y;
        y.left = x;
        x.p = y; 
    }

    static void Right_Rotate(rbtNode T, rbtNode x)
    {
        rbtNode y = new rbtNode(null, null, Tnil, Tnil, null);
        y = x.left;
        x.left = y.right;
        if (y.right != null)
            y.right.p = x;
        y.p = x.p;
        if (x.p == null)
            T = y;
        else if (x == x.p.right)
            x.p.right = y;
        else
            x.p.left = y;
        y.right = x;
        x.p = y;
    }

    static void preOrderWalk(rbtNode T)
    {
        if (T.color == 'B')
            Console.WriteLine("-{0}", T.key);
        else
            Console.WriteLine("{0}", T.key);
        if (T.left != null)
            preOrderWalk(T.left);
        if (T.right != null)
            preOrderWalk(T.right);
    }
}

}

¿Fue útil?

Solución

Parece que tiene problemas en 3 áreas:

  • El algoritmo de árbol RB. Pero parece que casi estás ahí
  • Depuración. Solo debería tomar 2 minutos retroceder un error como este, tal vez 2 horas si eres nuevo. Pero no 2 días.
  • Haciendo una buena pregunta. Tienes un nullref, pero ¿en qué línea? ¿Y cuáles son los valores de los VAR/campos en esa línea?

Cuando eche un vistazo rápido al método FixUp y al resto del código, sospecho que puede que la referencia AP (matriz) sea nula en el punto equivocado.

Como herramienta de diagnóstico, cambie el miembro .p en una propiedad regular:

class rbtNode
{    
    private rbtNode _parent;

    public rbtNode Parent
    {
         get { return _parent; }
         set
         {
             System.Diagnostics.Debug.Assert(value != null);
             _parent = value;
         }
    }
    ....

Creo que tendrás que permitir _parent = nulo pero solo en el constructor.

Los miembros de Get/Set también le dan un buen lugar para colocar los puntos de interrupción (condicionales).

Otros consejos

Creo que te vas un poco mal en tu arreglar función. Agregue ciertas condiciones en while loop, es decir

while (z != null && z.Getparent() != null && z.Getparent().Getcolor() == 'R')

Además, está cometiendo un gran error en la parte de lo contrario de su bucle While. Cambiar el orden de las funciones de Leftrotate y Rightrotate, debe ser el siguiente:

               else if (z == z.Getparent().Getleft())
                    {
                        z = z.Getparent();
                        **RightRotate(T, z);**
                        z.Getparent().Setcolor('B');
                        z.Getparent().Getparent().Setcolor('R');
                        **LeftRotate(T, z);**
                    }

Editar: también necesitaría agregar algunas condiciones más en ambas funciones de rotación.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top