Exemplo n.º 1
0
        /// <summary>
        /// Recupera o próximo elemento.
        /// </summary>
        /// <returns></returns>
        public object NextElement()
        {
            if (_stack.Count == 0)
            {
                throw new RedBlackException("Element not found");
            }
            RedBlackNode node = _stack.Peek();

            if (_ascending)
            {
                if (node.Right == _sentinelNode)
                {
                    for (RedBlackNode node2 = _stack.Pop(); HasMoreElements() && (_stack.Peek().Right == node2); node2 = _stack.Pop())
                    {
                        ;
                    }
                }
                else
                {
                    for (RedBlackNode node3 = node.Right; node3 != _sentinelNode; node3 = node3.Left)
                    {
                        _stack.Push(node3);
                    }
                }
            }
            else if (node.Left == _sentinelNode)
            {
                for (RedBlackNode node4 = _stack.Pop(); this.HasMoreElements() && (_stack.Peek().Left == node4); node4 = _stack.Pop())
                {
                    ;
                }
            }
            else
            {
                for (RedBlackNode node5 = node.Left; node5 != _sentinelNode; node5 = node5.Right)
                {
                    _stack.Push(node5);
                }
            }
            _ordKey   = node.Key;
            _objValue = node.Data;
            return(node.Key);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Cria a instancia com os parametros necessários.
 /// </summary>
 /// <param name="tnode">Nó terminal.</param>
 /// <param name="ascending">True para pecorre em ordem ascendente.</param>
 /// <param name="sentinelNode">Nó sentinela.</param>
 public RedBlackEnumerator(RedBlackNode tnode, bool ascending, RedBlackNode sentinelNode)
 {
     _stack        = new Stack <RedBlackNode>();
     _ascending    = ascending;
     _sentinelNode = sentinelNode;
     if (ascending)
     {
         while (tnode != _sentinelNode)
         {
             _stack.Push(tnode);
             tnode = tnode.Left;
         }
     }
     else
     {
         while (tnode != _sentinelNode)
         {
             _stack.Push(tnode);
             tnode = tnode.Right;
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Verifica se na árvore contem algum nó como a chave informada.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Contains(IComparable key)
        {
            RedBlackNode rbTree = _rbTree;

            while (rbTree != _sentinelNode)
            {
                int num = Compare(rbTree.Key, key);
                if (num == 0)
                {
                    _lastNodeFound = rbTree;
                    return(true);
                }
                if (num > 0)
                {
                    rbTree = rbTree.Left;
                }
                else
                {
                    rbTree = rbTree.Right;
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Recupera um dado da árvore pela chave informada.
        /// </summary>
        /// <param name="key">Chave que será comparada.</param>
        /// <param name="compareType">Tipo de comparação.</param>
        /// <returns></returns>
        public object GetData(IComparable key, COMPARE compareType)
        {
            string str;
            WildcardEnabledRegex  regex;
            ArrayList             list       = new ArrayList();
            RedBlackNode          rbTree     = _rbTree;
            IDictionaryEnumerator enumerator = this.GetEnumerator();
            Hashtable             result     = null;
            Hashtable             hashtable2 = null;

            switch (compareType)
            {
            case COMPARE.EQ:
                while (rbTree != _sentinelNode)
                {
                    int num = Compare(rbTree.Key, key);
                    if (num == 0)
                    {
                        _lastNodeFound = rbTree;
                        list.AddRange(rbTree.Data.Keys);
                        return(list);
                    }
                    if (num > 0)
                    {
                        rbTree = rbTree.Left;
                    }
                    else
                    {
                        rbTree = rbTree.Right;
                    }
                }
                return(list);

            case COMPARE.NE:
                result = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (Compare(((IComparable)enumerator.Key), key) != 0)
                    {
                        IDictionaryEnumerator enumerator2 = (enumerator.Value as Hashtable).GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                            result[enumerator2.Key] = enumerator2.Value;
                        }
                    }
                }
                return(new ArrayList(result.Keys));

            case COMPARE.LT:
                result = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (Compare(((IComparable)enumerator.Key), key) < 0)
                    {
                        IDictionaryEnumerator enumerator4 = (enumerator.Value as Hashtable).GetEnumerator();
                        while (enumerator4.MoveNext())
                        {
                            result[enumerator4.Key] = enumerator4.Value;
                        }
                    }
                }
                return(new ArrayList(result.Keys));

            case COMPARE.GT:
                result = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (Compare(((IComparable)enumerator.Key), key) > 0)
                    {
                        IDictionaryEnumerator enumerator3 = (enumerator.Value as Hashtable).GetEnumerator();
                        while (enumerator3.MoveNext())
                        {
                            result[enumerator3.Key] = enumerator3.Value;
                        }
                    }
                }
                return(new ArrayList(result.Keys));

            case COMPARE.LTEQ:
                result = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (Compare(((IComparable)enumerator.Key), key) > 0)
                    {
                        break;
                    }
                    IDictionaryEnumerator enumerator6 = (enumerator.Value as Hashtable).GetEnumerator();
                    while (enumerator6.MoveNext())
                    {
                        result[enumerator6.Key] = enumerator6.Value;
                    }
                }
                break;

            case COMPARE.GTEQ:
                result = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (Compare(((IComparable)enumerator.Key), key) >= 0)
                    {
                        IDictionaryEnumerator enumerator5 = (enumerator.Value as Hashtable).GetEnumerator();
                        while (enumerator5.MoveNext())
                        {
                            result[enumerator5.Key] = enumerator5.Value;
                        }
                    }
                }
                return(new ArrayList(result.Keys));

            case COMPARE.REGEX:
                result = new Hashtable();
                str    = key as string;
                regex  = new WildcardEnabledRegex(str);
                while (enumerator.MoveNext())
                {
                    if ((enumerator.Key is string) && regex.IsMatch((string)enumerator.Key))
                    {
                        IDictionaryEnumerator enumerator7 = (enumerator.Value as Hashtable).GetEnumerator();
                        while (enumerator7.MoveNext())
                        {
                            result[enumerator7.Key] = enumerator7.Value;
                        }
                    }
                }
                return(new ArrayList(result.Keys));

            case COMPARE.IREGEX:
            {
                result     = new Hashtable();
                str        = key as string;
                regex      = new WildcardEnabledRegex(str);
                hashtable2 = new Hashtable();
                while (enumerator.MoveNext())
                {
                    if (enumerator.Key is string)
                    {
                        if (regex.IsMatch((string)enumerator.Key))
                        {
                            IDictionaryEnumerator enumerator8 = (enumerator.Value as Hashtable).GetEnumerator();
                            while (enumerator8.MoveNext())
                            {
                                hashtable2[enumerator8.Key] = enumerator8.Value;
                            }
                        }
                        else
                        {
                            IDictionaryEnumerator enumerator9 = (enumerator.Value as Hashtable).GetEnumerator();
                            while (enumerator9.MoveNext())
                            {
                                result[enumerator9.Key] = enumerator9.Value;
                            }
                        }
                    }
                }
                ArrayList list2 = new ArrayList(result.Keys);
                for (int i = list2.Count - 1; i >= 0; i--)
                {
                    if (hashtable2.ContainsKey(list2[i]))
                    {
                        list2.RemoveAt(i);
                    }
                }
                return(list2);
            }

            default:
                return(list);
            }
            return(new ArrayList(result.Keys));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Limpa todos os nós da árvore.
 /// </summary>
 public void Clear()
 {
     _rbTree    = _sentinelNode;
     _size      = 0;
     _collision = false;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Restaura o nó depois de apagar.
 /// </summary>
 /// <param name="x">Instancia do nó que foi apagado</param>
 private void RestoreAfterDelete(RedBlackNode x)
 {
     while ((x != _rbTree) && (x.Color == 1))
     {
         RedBlackNode right;
         if (x == x.Parent.Left)
         {
             right = x.Parent.Right;
             if (right.Color == 0)
             {
                 right.Color    = 1;
                 x.Parent.Color = 0;
                 this.RotateLeft(x.Parent);
                 right = x.Parent.Right;
             }
             if ((right.Left.Color == 1) && (right.Right.Color == 1))
             {
                 right.Color = 0;
                 x           = x.Parent;
             }
             else
             {
                 if (right.Right.Color == 1)
                 {
                     right.Left.Color = 1;
                     right.Color      = 0;
                     this.RotateRight(right);
                     right = x.Parent.Right;
                 }
                 right.Color       = x.Parent.Color;
                 x.Parent.Color    = 1;
                 right.Right.Color = 1;
                 this.RotateLeft(x.Parent);
                 x = _rbTree;
             }
         }
         else
         {
             right = x.Parent.Left;
             if (right.Color == 0)
             {
                 right.Color    = 1;
                 x.Parent.Color = 0;
                 this.RotateRight(x.Parent);
                 right = x.Parent.Left;
             }
             if ((right.Right.Color == 1) && (right.Left.Color == 1))
             {
                 right.Color = 0;
                 x           = x.Parent;
             }
             else
             {
                 if (right.Left.Color == 1)
                 {
                     right.Right.Color = 1;
                     right.Color       = 0;
                     this.RotateLeft(right);
                     right = x.Parent.Left;
                 }
                 right.Color      = x.Parent.Color;
                 x.Parent.Color   = 1;
                 right.Left.Color = 1;
                 this.RotateRight(x.Parent);
                 x = _rbTree;
             }
         }
     }
     x.Color = 1;
 }