红黑树根旋转

时间:2016-04-12 09:02:27

标签: c# data-structures rotation binary-tree red-black-tree

插入时进行单次旋转,但是,当需要旋转整个树(即从根部旋转)时,不进行旋转。有什么我可能做错了吗?

当树为空并且需要左旋转时(例如插入1,5,2),在左旋转方法中,在x.right = y.left处出现Null Reference Exception;

class RedBlackTree
{
    BNode root;
    int count; //holds number of items

    //test
    BNode insertedNode;

    public RedBlackTree()
    {
        root = null;
    }

    //Left Rotate
    public void LeftRotate(BNode root, BNode x)//violatingNode
    {

            BNode y = x.right;
            x.right = y.left;
            if (y.left != null)
                y.left.parent = x;
        if (x.parent == null)
            root = y;
        else if (x.parent.left == x)
            x.parent.left = y;
        else
            x.parent.right = y;
        y.left = x;
            y.parent = x.parent;
            x.parent = y;
    }

    //Right Rotate
    public void RightRotate(BNode root, BNode x) //BNode violatingNode
    {
            BNode y = x.left;
            x.left = y.right;
            if (y.right != null)
            {
                y.right.parent = x;
            }
            if (x.parent == null)
            {
                root = y;
            }
            else if (x.parent.left == x)
            {
                x.parent.left = y;
            }
            else
                x.parent.right = y;

            y.right = x;
            y.parent = x.parent;
            x.parent = y;
    }

    public void insertRBT(int value)
    {
        root = insertNode(value);
        count += 1;   
    }


    public BNode insertNode(int value)
    {
          BNode newItem = new BNode(value);
          BNode grandParent = null, greatGrandParent=null;
          if (root == null)
          {
              root = newItem;
              root.red = false;
              return newItem;
          }
          BNode Y = null;
          BNode X = root;
          while (X != null)
          {
              Y = X;
              if (newItem.value< X.value)
              {
                  X = X.left;
              }
              else
              {
                  X = X.right;
              }
          }
          newItem.parent = Y;
          if (Y == null)
          {
              root = newItem;
          }
          else if (newItem.value < Y.value)
          {
              Y.left = newItem;
          }
          else
          {
              Y.right = newItem;
          }
          //newItem.parent = Y;
          newItem.left = null;
          newItem.right = null;
          newItem.red = true;//colour the new node red

        insertFix(newItem);//, newItem.parent, grandParent, greatGrandParent);
        return root;
    }

    public void insertFix(BNode child)//, BNode parent, BNode grandParent, BNode greatGrandParent)
    {
        BNode uncle = Uncle(child);
        while (child.parent != root && child.parent!=null && child.parent.red == true)
        {
            if (child.parent == child.parent.parent.left)
            {
                if (uncle != null && uncle.red == true)
                {
                    child.parent.red = false;
                    uncle.red = false;
                    child.parent.parent.red = true;
                    child = child.parent.parent;
                }
                else
                {
                    if (child == child.parent.right)
                    {
                        child = child.parent;
                        LeftRotate(root,child.parent);
                    }
                    child.parent.red = false;
                    child.parent.parent.red = true;
                    RightRotate(root,child.parent.parent);
                }
            }
            else
            {
                if (child.parent == child.parent.parent.right)
                {
                    if (uncle != null && uncle.red == true)
                    {
                        child.parent.red = false;
                        uncle.red = false;
                        child.parent.parent.red = true;
                        child = child.parent.parent;
                    }
                    else
                    {
                        if (child == child.parent.left)
                        {
                            child = child.parent;
                            RightRotate(root, child.parent);
                        }
                        child.parent.red = false;
                        child.parent.parent.red = true;
                        LeftRotate(root, child.parent.parent);
                    }
                }
            }
            root.red = false;
        }
   }

我不知道问题出在旋转方法上。当我插入例如50,75,40,45,45,47,9,8时 - 左右旋转在子树上工作。但是,当我插入44时,整个树无法以44成为根

的方式旋转

有人可以提供帮助吗?感谢

0 个答案:

没有答案