コード例 #1
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
        public void Insert(T key)
        {
            var parent  = _SentryNode;
            var current = _Root;

            while (current != _SentryNode)
            {
                parent = current;
                if (current.Value.CompareTo(key) > 0)
                {
                    current = current.Left;
                }
                else
                {
                    current = current.Right;
                }
            }
            var node = new RBTreeNode <T>(key);

            node.Parent = parent;
            if (parent == _SentryNode)
            {
                _Root = node;
            }
            else
            {
                if (parent.Value.CompareTo(key) > 0)
                {
                    parent.Left = node;
                }
                else
                {
                    parent.Right = node;
                }
            }
            node.Left  = _SentryNode;
            node.Right = _SentryNode;
            node.Color = RBColor.RED;

            RBInsertFixUp(node);
        }
コード例 #2
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
        public RBTreeNode <T> Predecessor(RBTreeNode <T> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException();
            }

            if (node.Left != _SentryNode)
            {
                return(_Maximum(node.Left));
            }

            var current = node;
            var parent  = node.Parent;

            while (parent != _SentryNode && parent.Left == current)
            {
                current = parent;
                parent  = parent.Parent;
            }
            return(parent);
        }
コード例 #3
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
        public RBTreeNode <T> Successor(RBTreeNode <T> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException();
            }

            if (node.Right != _SentryNode)
            {
                return(_Minimum(node.Right));
            }

            var current = node;
            var parent  = current.Parent;

            while (parent != _SentryNode && parent.Right == current)
            {
                current = parent;
                parent  = parent.Parent;
            }
            return(parent);
        }
コード例 #4
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
 public RedBlackTree()
 {
     _SentryNode = new RBTreeNode <T>(RBColor.BLACK);
     _Root       = _SentryNode;
 }
コード例 #5
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
        private void RBDeleteFixUp(RBTreeNode <T> fixNode)
        {
            var node = fixNode;

            while (node != _SentryNode && node.Color == RBColor.BLACK)
            {
                if (node == node.Parent.Left)
                {
                    var right = node.Parent.Right;
                    if (right.Color == RBColor.RED)
                    {
                        right.Color       = RBColor.BLACK;
                        node.Parent.Color = RBColor.RED;
                        LeftRotate(node.Parent);
                        right = node.Parent.Right;
                    }
                    if (right.Left.Color == RBColor.BLACK && right.Right.Color == RBColor.BLACK)
                    {
                        right.Color = RBColor.RED;
                        node        = node.Parent;
                    }
                    else if (right.Right.Color == RBColor.BLACK)
                    {
                        right.Left.Color = RBColor.BLACK;
                        right.Color      = RBColor.RED;
                        RightRotate(right);
                        right = node.Parent.Right;
                    }
                    right.Color       = node.Parent.Color;
                    node.Parent.Color = RBColor.BLACK;
                    right.Right.Color = RBColor.BLACK;
                    LeftRotate(node.Parent);
                    node = _Root;
                }
                else
                {
                    var left = node.Parent.Left;
                    if (left.Color == RBColor.RED)
                    {
                        left.Color        = RBColor.BLACK;
                        node.Parent.Color = RBColor.RED;
                        RightRotate(node.Parent);
                        left = node.Parent.Left;
                    }
                    if (left.Left.Color == RBColor.BLACK && left.Right.Color == RBColor.BLACK)
                    {
                        left.Color = RBColor.RED;
                        node       = node.Parent;
                    }
                    else if (left.Left.Color == RBColor.BLACK)
                    {
                        left.Right.Color = RBColor.BLACK;
                        left.Color       = RBColor.RED;
                        LeftRotate(left);
                        left = node.Parent.Left;
                    }
                    left.Color        = node.Parent.Color;
                    node.Parent.Color = RBColor.BLACK;
                    left.Left.Color   = RBColor.BLACK;
                    RightRotate(node.Parent);
                    node = _Root;
                }
            }
            node.Color = RBColor.BLACK;
        }
コード例 #6
0
ファイル: RedBlackTree.cs プロジェクト: yangzuo0621/Tree
 public RedBlackTree(IEnumerable <T> collections)
 {
     _SentryNode = new RBTreeNode <T>(RBColor.BLACK);
     _Root       = _SentryNode;
     Build(collections);
 }