示例#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;