예제 #1
0
        public static void RotateLeft(this RedBlackTree.Node b)
        {
            if (b == null)
            {
                throw new ArgumentException("node doesn't equal null");
            }
            var d = b.Right;

            if (b == null)
            {
                throw new ArgumentException("parent node doesn't equal null");
            }
            if (d.Parent.Right != d)
            {
                throw new ArgumentException("If you use left rotate node must contain right child");
            }

            var high = d.Parent.Parent;
            var c    = d.Left;

            b.SetRight(c);
            if (high?.Left == b)
            {
                high.SetLeft(d);
            }
            else if (high?.Right == b)
            {
                high.SetRight(d);
            }

            d.SetLeft(b);
        }
예제 #2
0
        public static RedBlackTree.Node Uncle(this RedBlackTree.Node nd)
        {
            if (IsLeftChild(nd.Parent))
            {
                return(nd.Parent.Parent.Right);
            }

            if (IsRightChild(nd.Parent))
            {
                return(nd.Parent.Parent.Left);
            }

            throw new Exception("Invalid state");
        }
예제 #3
0
        public static RedBlackTree.Node Sibling(this RedBlackTree.Node nd)
        {
            if (nd.Parent == null)
            {
                throw new InvalidOperationException("parent is null");
            }

            if (nd.Parent.Left == nd)
            {
                return(nd.Parent.Right);
            }
            if (nd.Parent.Right == nd)
            {
                return(nd.Parent.Left);
            }

            throw new ArgumentException("invalid state");
        }
예제 #4
0
        public static bool HasRedUncle(this RedBlackTree.Node nd)
        {
            if (nd?.Parent?.Parent == null)
            {
                return(false);
            }

            if (nd.Parent.Parent.Left == nd.Parent)
            {
                return(nd.Parent.Parent.Right?.Color == RedBlackTree.Color.Red);
            }

            if (nd.Parent.Parent.Right == nd.Parent)
            {
                return(nd.Parent.Parent.Left?.Color == RedBlackTree.Color.Red);
            }

            throw new ArgumentException("state invalid, parent not parent for " + nameof(nd));
        }
예제 #5
0
 public static bool IsLeftChild(this RedBlackTree.Node nd) => nd.Parent.Left == nd;
예제 #6
0
 public static bool IsRightChild(this RedBlackTree.Node nd) => nd.Parent.Right == nd;