Пример #1
0
    private Node rightRotate(Node x)
    {
        Node y = x.left;

        x.left = y.right;
        if (y.right != NIL.NilNode())
        {
            y.right.parent = x;
        }
        y.parent = x.parent;
        if (x.parent == NIL.NilNode())
        {
            root = y;
        }
        else if (x == x.parent.right)
        {
            x.parent.right = y;
        }
        else
        {
            x.parent.left = y;
        }
        y.right  = x;
        x.parent = y;
        return(x);
    }
Пример #2
0
 private static int getHeight(Node n)
 {
     if (n == null || n == NIL.NilNode())
     {
         return(-1);
     }
     return(1 + Max(getHeight(n.left), getHeight(n.right)));
 }
Пример #3
0
 private Node getMax(Node x)
 {
     while (x.right != NIL.NilNode())
     {
         x = x.right;
     }
     return(x);
 }
Пример #4
0
 private Node getMin(Node x)
 {
     while (x.left != NIL.NilNode())
     {
         x = x.left;
     }
     return(x);
 }
Пример #5
0
 private int rightBlackHeight(Node n)
 {
     if (n == NIL.NilNode())
     {
         return(1);
     }
     return(n.color == Color.BLACK ? rightBlackHeight(n.left) + 1 : rightBlackHeight(n.left));
 }
Пример #6
0
    public void delete(int key)
    {
        Node search = find(key);

        if (search != NIL.NilNode())
        {
            delete(search);
        }
    }
Пример #7
0
    public void insert(int key)
    {
        digits = Max(digits, (key.ToString().Length % 2 == 1 ? key.ToString().Length + 1 : key.ToString().Length));
        Node newNode = new Node();

        newNode.key   = key;
        newNode.left  = NIL.NilNode();
        newNode.right = NIL.NilNode();
        insert(newNode);
    }
Пример #8
0
 private bool checktree(Node n)
 {
     if (n == NIL.NilNode())
     {
         return(true);
     }
     if (!(checktree(n.left) && checktree(n.right)))
     {
         WriteLine(n.key + " HEIGHT NOT EVEN");
     }
     return((leftBlackHeight(n.left) == rightBlackHeight(n.right)) && checktree(n.left) && checktree(n.right));
 }
Пример #9
0
 private Node find(Node x, int key)
 {
     if (x == null || x == NIL.NilNode() || key == x.key)
     {
         return(x);
     }
     if (key < x.key)
     {
         return(find(x.left, key));
     }
     else
     {
         return(find(x.right, key));
     }
 }
Пример #10
0
    private void delete(Node z)
    {
        Node y;
        Node x;

        if (z.left == NIL.NilNode() || z.right == NIL.NilNode())
        {
            y = z;
        }
        else
        {
            y = getMin(z.right);
        }

        if (y.left != NIL.NilNode())
        {
            x = y.left;
        }
        else
        {
            x = y.right;
        }
        x.parent = y.parent;
        if (y.parent == NIL.NilNode())
        {
            root = x;
        }
        else
        {
            if (y.parent.left == y)
            {
                y.parent.left = x;
            }
            else
            {
                y.parent.right = x;
            }
        }
        if (y != z)
        {
            z.key = y.key;
        }
        if (y.color == Color.BLACK)
        {
            deleteFixup(x);
        }
    }
Пример #11
0
    private void insert(Node z)
    {
        Node y = NIL.NilNode();
        Node x = root;

        while (x != NIL.NilNode())
        {
            y = x;
            if (z.key < x.key)
            {
                x = x.left;
            }
            else if (z.key > x.key)
            {
                x = x.right;
            }
            else if (z.key == x.key)
            {
                return;
            }
        }
        z.parent = y;
        if (y == NIL.NilNode())
        {
            root = z;
        }
        else if (z.key < y.key)
        {
            y.left = z;
        }
        else if (z.key > y.key)
        {
            y.right = z;
        }
        else if (z.key == y.key)
        {
            return;
        }
        z.left  = NIL.NilNode();
        z.right = NIL.NilNode();
        z.color = Color.RED;
        z       = insertFixup(z);
    }
Пример #12
0
    //===================PRINT===========================
    public void print()
    {
        if (root == null || root == NIL.NilNode())
        {
            WriteLine("Tree is empty");
            return;
        }
        NodeList[] levels = getTreePerLevels();

        int height = getHeight(this.root);

        for (int i = height + 1; i >= 0; i--)
        {
            int padding = formula(i, this.digits);
            int num     = height + 1 - i;
            for (int j = 0; j < Math.Pow(2, num); j++)
            {
                if (levels[num][j] != null)
                {
                    if (levels[num][j] != NIL.NilNode())
                    {
                        encapsulate(padding, j, levels[num][j].key, this.digits, levels[num][j].color);
                    }
                    else
                    {
                        encapsulate(padding, j, int.MinValue, this.digits, Color.BLACK);
                    }
                }
                else
                {
                    encapsulate(padding, j, int.MinValue, this.digits, Color.Blue);
                }
            }
            WriteLine();
        }
    }
Пример #13
0
 static void populate(ref NodeList[] levels, Node n, int level)
 {
     if (n == null)
     {
         levels[level].add(null);
         int j = 1;
         for (int i = level + 1; i < levels.Length; i++)
         {
             for (int k = 1; k <= j; k++)
             {
                 levels[i].add(null);
                 levels[i].add(null);
             }
             j++;
         }
         return;
     }
     if (n == NIL.NilNode())
     {
         levels[level].add(NIL.NilNode());
         int j = 1;
         for (int i = level + 1; i < levels.Length; i++)
         {
             for (int k = 1; k <= j; k++)
             {
                 levels[i].add(null);
                 levels[i].add(null);
             }
             j++;
         }
         return;
     }
     populate(ref levels, n.left, level + 1);
     levels[level].add(n);
     populate(ref levels, n.right, level + 1);
 }