コード例 #1
0
 public virtual IRedBlackLeaf <TKey, TValue> BalancePostInsert(IRedBlackLeaf <TKey, TValue> root,
                                                               bool direction)
 {
     if (root[direction].IsRed())
     {
         if (root[!direction].IsRed())
         {
             root.Color       = LeafColor.RED;
             root.Left.Color  = LeafColor.BLACK;
             root.Right.Color = LeafColor.BLACK;
         }
         else
         {
             if (root[direction][direction].IsRed())
             {
                 root = Rotate(root, !direction);
             }
             else if (root[direction][!direction].IsRed())
             {
                 root = DoubleRotate(root, !direction);
             }
         }
     }
     return(root);
 }
コード例 #2
0
        public virtual TValue GetNearest <T>(T key)
        {
            IRedBlackLeaf <TKey, TValue> leaf = Root;
            IRedBlackLeaf <TKey, TValue> last = null;
            var compareKey = (TKey)Convert.ChangeType(key, typeof(TKey));
            var compare    = CreateLeaf(compareKey, default(TValue));

            while (leaf != null)
            {
                if (leaf.GreaterThan(compare))
                {
                    leaf = leaf.Left;
                }
                else if (leaf.LessThan(compare))
                {
                    last = leaf;
                    leaf = leaf.Right;
                }
                else
                {
                    return(leaf.Value);
                }
            }
            if (last.IsEmpty())
            {
                return(GetMaxLeaf().Value);
            }
            else
            {
                return(last.Value);
            }
        }
コード例 #3
0
        public static int ValidateTree( IRedBlackLeaf<string,string> leaf )
        {
            int lh, rh;
            if( leaf == null )
                return 1;
            else
            {
                var left = leaf.Left;
                var right = leaf.Right;

                if( leaf.IsRed() && ( left.IsRed() || right.IsRed() ) )
                    throw new Exception("Red violation!");

                lh = ValidateTree( left );
                rh = ValidateTree( right );

                if( ( left != null && ( left.GreaterThan( leaf ) || leaf.Key.Equals( left.Key ) ) ) ||
                    ( right != null && ( right.LessThan( leaf ) || leaf.Key.Equals( right.Key ) ) ) )
                {
                    throw new Exception("Invalid binary tree structure!");
                }

                if( lh != 0 && rh != 0 && lh != rh )
                    throw new Exception("Black violation!");

                if( lh != 0 && rh != 0 )
                    return leaf.IsRed() ? lh : lh + 1;
                else
                {
                    return 0;
                }
            }
        }
コード例 #4
0
        public virtual IRedBlackLeaf <TKey, TValue> Remove(IRedBlackLeaf <TKey, TValue> root, TKey key, ref bool done)
        {
            if (root.IsEmpty())
            {
                done = true;
            }
            else
            {
                bool direction;
                if (root.Key.Equals(key))
                {
                    if (root.Left.IsEmpty() || root.Right.IsEmpty())
                    {
                        var save = root[root.Left.IsEmpty()];

                        if (root.IsRed())
                        {
                            done = true;
                        }
                        else if (save.IsRed())
                        {
                            save.Color = LeafColor.BLACK;
                            done       = true;
                        }

                        return(save);
                    }
                    else
                    {
                        var heir = root.Left;
                        while (!heir.Right.IsEmpty())
                        {
                            heir = heir.Right;
                        }

                        root.Value = heir.Value;
                        root.Key   = heir.Key;
                        key        = heir.Key;
                    }
                }

                direction       = root.LessThan(CreateLeaf(key, default(TValue)));
                root[direction] = Remove(root[direction], key, ref done);

                if (!done)
                {
                    root = BalancePostDelete(root, direction, ref done);
                }
            }
            return(root);
        }
コード例 #5
0
 public virtual void Add(TKey key, TValue value)
 {
     Lock.EnterWriteLock();
     try
     {
         var leaf = CreateLeaf(key, value);
         Root       = Insert(Root, leaf);
         Root.Color = LeafColor.BLACK;
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
コード例 #6
0
        public virtual IRedBlackLeaf <TKey, TValue> Rotate(IRedBlackLeaf <TKey, TValue> leaf, bool direction)
        {
            var working = leaf[!direction];

            if (!working.IsEmpty())
            {
                leaf[!direction]   = working[direction];
                working[direction] = leaf;

                leaf.Color    = LeafColor.RED;
                working.Color = LeafColor.BLACK;
                return(working);
            }
            return(leaf);
        }
コード例 #7
0
 public virtual IRedBlackLeaf <TKey, TValue> Insert(IRedBlackLeaf <TKey, TValue> root,
                                                    IRedBlackLeaf <TKey, TValue> leaf)
 {
     if (root.IsEmpty())
     {
         root = leaf;
     }
     else
     {
         leaf.Parent = root;
         var direction = leaf.GreaterThan(root);
         root[direction] = Insert(root[direction], leaf);
         root            = BalancePostInsert(root, direction);
     }
     return(root);
 }
コード例 #8
0
        public static int ValidateTree(IRedBlackLeaf <string, string> leaf)
        {
            int lh, rh;

            if (leaf == null)
            {
                return(1);
            }
            else
            {
                var left  = leaf.Left;
                var right = leaf.Right;

                if (leaf.IsRed() && (left.IsRed() || right.IsRed()))
                {
                    throw new Exception("Red violation!");
                }

                lh = ValidateTree(left);
                rh = ValidateTree(right);

                if ((left != null && (left.GreaterThan(leaf) || leaf.Key.Equals(left.Key))) ||
                    (right != null && (right.LessThan(leaf) || leaf.Key.Equals(right.Key))))
                {
                    throw new Exception("Invalid binary tree structure!");
                }

                if (lh != 0 && rh != 0 && lh != rh)
                {
                    throw new Exception("Black violation!");
                }

                if (lh != 0 && rh != 0)
                {
                    return(leaf.IsRed() ? lh : lh + 1);
                }
                else
                {
                    return(0);
                }
            }
        }
コード例 #9
0
        public virtual TValue Get(TKey key)
        {
            Lock.EnterReadLock();
            IRedBlackLeaf <TKey, TValue> leaf = Root;
            var compare = CreateLeaf(key, default(TValue));

            while (leaf != null)
            {
                if (leaf.GreaterThan(compare))
                {
                    leaf = leaf.Left;
                }
                else if (leaf.LessThan(compare))
                {
                    leaf = leaf.Right;
                }
                else
                {
                    break;
                }
            }
            Lock.ExitReadLock();
            return(leaf.IsEmpty() ? default(TValue) : leaf.Value);
        }
コード例 #10
0
 public bool LessThan(IRedBlackLeaf <TKey, TValue> leaf)
 {
     return(LessThan(leaf.Key));
 }
コード例 #11
0
 public bool GreaterThan(IRedBlackLeaf <TKey, TValue> leaf)
 {
     return(GreaterThan(leaf.Key));
 }
コード例 #12
0
 public static int Validate(this IRedBlackLeaf <string, string> leaf)
 {
     return(ValidateTree(leaf));
 }
コード例 #13
0
        public bool LessThan(IRedBlackLeaf <TKey, TValue> leaf)
        {
            var compare = leaf as HashedRedBlackLeaf <TKey, TValue>;

            return(compare == null ? true : HashKey < compare.HashKey);
        }
コード例 #14
0
 public virtual IRedBlackLeaf <TKey, TValue> DoubleRotate(IRedBlackLeaf <TKey, TValue> leaf, bool direction)
 {
     leaf[!direction] = Rotate(leaf[!direction], !direction);
     return(Rotate(leaf, direction));
 }
コード例 #15
0
        public override IRedBlackLeaf <TKey, TValue> Insert(IRedBlackLeaf <TKey, TValue> root,
                                                            IRedBlackLeaf <TKey, TValue> leaf)
        {
            if (Root.IsEmpty())
            {
                Root = leaf;
            }
            else
            {
                IRedBlackLeaf <TKey, TValue> g, t, p, q, head;
                head = new RedBlackLeaf <TKey, TValue>(default(TKey), default(TValue), null);
                g    = t = q = p = null;
                bool direction, last;
                direction = last = false;

                t = head;
                q = t.Right = Root;

                for ( ;;)
                {
                    if (q.IsEmpty())
                    {
                        p[direction] = q = leaf;
                        if (q.IsEmpty())
                        {
                            return(Root);
                        }
                    }
                    else if (q.Left.IsRed() && q.Right.IsRed())
                    {
                        q.Color       = LeafColor.RED;
                        q.Left.Color  = LeafColor.BLACK;
                        q.Right.Color = LeafColor.BLACK;
                    }

                    if (q.IsRed() && p.IsRed())
                    {
                        var newDirection     = t.Right.Key.Equals(g.Key);
                        var lastIteration    = p[last];
                        var lastIterationKey = lastIteration == null ? default(TKey) : lastIteration.Key;
                        if (q.Key.Equals(lastIterationKey))
                        {
                            t[newDirection] = Rotate(g, !last);
                        }
                        else
                        {
                            t[newDirection] = DoubleRotate(g, !last);
                        }
                    }

                    if (q.Key.Equals(leaf.Key))
                    {
                        break;
                    }

                    last      = direction;
                    direction = q.LessThan(leaf);

                    if (!g.IsEmpty())
                    {
                        t = g;
                    }
                    g = p;
                    p = q;
                    q = q[direction];
                }

                Root = head.Right;
            }

            Root.Color = LeafColor.BLACK;
            return(Root);
        }
コード例 #16
0
        public virtual IRedBlackLeaf <TKey, TValue> BalancePostDelete(IRedBlackLeaf <TKey, TValue> root,
                                                                      bool direction, ref bool done)
        {
            IRedBlackLeaf <TKey, TValue> worker = root;
            IRedBlackLeaf <TKey, TValue> leaf   = root[!direction];

            if (leaf.IsRed())
            {
                root = Rotate(root, direction);
                leaf = worker[!direction];
            }

            if (!leaf.IsEmpty())
            {
                if (leaf.Left.IsBlack() && leaf.Right.IsBlack())
                {
                    if (worker.IsRed())
                    {
                        done = true;
                    }
                    worker.Color = LeafColor.BLACK;
                    leaf.Color   = LeafColor.RED;
                }
                else
                {
                    var save    = worker.IsRed();
                    var newRoot = root.Key.Equals(worker.Key);

                    if (leaf[!direction].IsRed())
                    {
                        worker = Rotate(worker, direction);
                    }
                    else
                    {
                        worker = DoubleRotate(worker, direction);
                    }

                    worker.Color = save ? LeafColor.RED : LeafColor.BLACK;
                    if (!worker.Left.IsEmpty())
                    {
                        worker.Left.Color = LeafColor.BLACK;
                    }
                    if (!worker.Right.IsEmpty())
                    {
                        worker.Right.Color = LeafColor.BLACK;
                    }

                    if (newRoot)
                    {
                        root = worker;
                    }
                    else
                    {
                        root[direction] = worker;
                    }

                    done = true;
                }
            }

            return(root);
        }
コード例 #17
0
 public static bool IsEmpty <TKey, TValue>(this IRedBlackLeaf <TKey, TValue> leaf)
 {
     return(leaf == null); // || leaf.Value.Equals(default(TValue));
 }
コード例 #18
0
 public static bool IsBlack <TKey, TValue>(this IRedBlackLeaf <TKey, TValue> leaf)
 {
     return(!leaf.IsEmpty() && leaf.Color == LeafColor.BLACK);
 }