public void Reset()
 {
     this.dictNodeCurrent = root;
     this.current         = default(KeyValuePair <TKey, TValue>);
     this.currentIndex    = this.count;
     this.prevCurrent     = default(TKey);
     this.rootLeft        = default(TKey);
 }
 public DictNode(KeyValuePair <TKey, TValue> info, DictNode <TKey, TValue> parent, DictNode <TKey, TValue> left = null, DictNode <TKey, TValue> right = null, ColorEnum color = ColorEnum.Red)
 {
     this.Info   = info;
     this.Parent = parent;
     this.Left   = left;
     this.Right  = right;
     this.Color  = color;
 }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TValue this[TKey key]
        {
            get
            {
                DictNode <TKey, TValue> current = this.root;

                int cmp;

                while (current != null)
                {
                    cmp = key.CompareTo(current.Info.Key);

                    if (cmp < 0)
                    {
                        current = current.Left;
                    }

                    if (cmp > 0)
                    {
                        current = current.Right;
                    }

                    return(current.Info.Value);
                }

                throw new KeyNotFoundException();
            }

            set
            {
                DictNode <TKey, TValue> current = this.root;
                int cmp;

                while (current != null)
                {
                    cmp = key.CompareTo(current.Info.Key);

                    if (cmp < 0)
                    {
                        current = current.Left;
                    }
                    else
                    {
                        if (cmp > 0)
                        {
                            current = current.Right;
                        }
                        else
                        {
                            current.Info = new KeyValuePair <TKey, TValue>(key, value);
                            return;
                        }
                    }
                }

                throw new KeyNotFoundException();
            }
        }
Exemple #4
0
        /// <summary>
        /// Balancing red-black tree after insert.
        /// </summary>
        /// <param name="node"></param>
        private void BalansingAfterAdd(DictNode <TKey, TValue> node)
        {
            DictNode <TKey, TValue> uncle;

            while (node != this.root && node.Parent.Color == ColorEnum.Red)
            {
                if (node.Parent == node.Parent.Parent.Left)
                {
                    uncle = node.Parent.Parent.Right;
                    if (uncle?.Color == ColorEnum.Red)
                    {
                        node.Parent.Color        = ColorEnum.Black;
                        uncle.Color              = ColorEnum.Black;
                        node.Parent.Parent.Color = ColorEnum.Red;
                        node = node.Parent.Parent;
                    }
                    else
                    {
                        if (node == node.Parent.Right)
                        {
                            node = node.Parent;
                            LeftRotate(node);
                        }

                        node.Parent.Color        = ColorEnum.Black;
                        node.Parent.Parent.Color = ColorEnum.Red;
                        RightRotate(node.Parent.Parent);
                    }
                }
                else
                {
                    uncle = node.Parent.Parent.Left;
                    if (uncle?.Color == ColorEnum.Red)
                    {
                        node.Parent.Color        = ColorEnum.Black;
                        uncle.Color              = ColorEnum.Black;
                        node.Parent.Parent.Color = ColorEnum.Red;
                        node = node.Parent.Parent;
                    }
                    else
                    {
                        if (node == node.Parent.Left)
                        {
                            node = node.Parent;
                            RightRotate(node);
                        }

                        node.Parent.Color        = ColorEnum.Black;
                        node.Parent.Parent.Color = ColorEnum.Red;
                        LeftRotate(node.Parent.Parent);
                    }
                }
            }

            this.root.Color = ColorEnum.Black;
        }
 public RedBlackTreeEnumerator(DictNode <TKey, TValue> node, int count)
 {
     this.root            = node;
     this.dictNodeCurrent = node;
     this.count           = count;
     this.currentIndex    = count;
     this.current         = default(KeyValuePair <TKey, TValue>);
     this.prevCurrent     = default(TKey);
     this.rootLeft        = default(TKey);
 }
Exemple #6
0
        /// <summary>
        /// Add element in dictionary.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(TKey key, TValue value)
        {
            DictNode <TKey, TValue> node    = null;
            DictNode <TKey, TValue> current = this.root;

            int cmp;

            while (current != null)
            {
                node = current;
                cmp  = key.CompareTo(current.Info.Key);

                if (cmp < 0)
                {
                    current = current.Left;
                }
                if (cmp > 0)
                {
                    current = current.Right;
                    continue;
                }

                throw new Exception("An element with the same key already exists in the dictionary.");
            }

            if (node == null)
            {
                this.root = new DictNode <TKey, TValue>(new KeyValuePair <TKey, TValue>(key, value), null, color: ColorEnum.Black);
            }
            else
            {
                cmp = key.CompareTo(node.Info.Key);
                if (cmp < 0)
                {
                    node.Left = new DictNode <TKey, TValue>(new KeyValuePair <TKey, TValue>(key, value), node);
                    BalansingAfterAdd(node.Left);
                }
                else
                {
                    node.Right = new DictNode <TKey, TValue>(new KeyValuePair <TKey, TValue>(key, value), node);
                    BalansingAfterAdd(node.Right);
                }
            }
            countOfNodes++;
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key">The key of element</param>
        /// <returns></returns>
        public bool ContainsKey(TKey key)
        {
            DictNode <TKey, TValue> current = root;

            while (current != null)
            {
                if (current.Info.Key.CompareTo(key) < 0)
                {
                    current = current.Left;
                    continue;
                }

                if (current.Info.Key.CompareTo(key) > 0)
                {
                    current = current.Right;
                    continue;
                }

                return(true);
            }
            return(false);
        }
        public bool MoveNext()
        {
            while (currentIndex != 0)
            {
                if (dictNodeCurrent.Left != null && !current.Key.Equals(dictNodeCurrent.Left.Info.Key) && !prevCurrent.Equals(dictNodeCurrent.Left.Info.Key) && !rootLeft.Equals(dictNodeCurrent.Left.Info.Key))
                {
                    dictNodeCurrent = dictNodeCurrent.Left;
                }
                else
                {
                    if (dictNodeCurrent.Right != null && !current.Key.Equals(dictNodeCurrent.Right.Info.Key) && !prevCurrent.Equals(dictNodeCurrent.Right.Info.Key))
                    {
                        dictNodeCurrent = dictNodeCurrent.Right;
                    }
                    else
                    {
                        prevCurrent     = current.Key;
                        current         = dictNodeCurrent.Info;
                        dictNodeCurrent = dictNodeCurrent.Parent;
                        currentIndex--;

                        if (rootLeft.Equals(default(TKey)))
                        {
                            if (root.Left != null)
                            {
                                rootLeft = root.Left.Info.Key;
                            }
                            else
                            {
                                rootLeft = default(TKey);
                            }
                        }

                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex)
        {
            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException("arrayIndex is less than 0.");
            }

            if (array.Length - arrayIndex < countOfNodes)
            {
                throw new ArgumentException("The number of elements in the source is greater than the available space from arrayIndex to the end of the destination array.");
            }

            if (array == null)
            {
                throw new ArgumentException("Array is null");
            }

            Stack <DictNode <TKey, TValue> > stack = new Stack <DictNode <TKey, TValue> >();

            DictNode <TKey, TValue> current = this.root;

            while (stack.Count != 0 || current != null)
            {
                if (current != null)
                {
                    stack.Push(current);
                    current = current.Left;
                }
                else
                {
                    current           = stack.Pop();
                    array[arrayIndex] = current.Info;
                    arrayIndex++;
                    current = current.Right;
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// Balancing red-black tree.
        /// </summary>
        /// <param name="current"></param>
        private void Balancing(DictNode <TKey, TValue> current)
        {
            while (current != root && current.Color == ColorEnum.Black)
            {
                DictNode <TKey, TValue> node;

                if (current == current.Parent.Left)
                {
                    node = current.Parent.Right;
                    if (node.Color == ColorEnum.Red)
                    {
                        node.Color           = ColorEnum.Black;
                        current.Parent.Color = ColorEnum.Red;
                        LeftRotate(current.Parent);
                        node = current.Parent.Right;
                    }

                    if (node.Left == null && node.Right == null)
                    {
                        node.Color = ColorEnum.Red;
                        current    = current.Parent;
                    }
                    else
                    {
                        if (node.Left.Color == ColorEnum.Black && node.Right.Color == ColorEnum.Black)
                        {
                            node.Color = ColorEnum.Red;
                            current    = current.Parent;
                        }
                        else
                        {
                            if (node.Right.Color == ColorEnum.Black)
                            {
                                node.Left.Color = ColorEnum.Black;
                                node.Color      = ColorEnum.Red;
                                RightRotate(node);
                                node = current.Parent.Right;
                            }
                            node.Color           = current.Parent.Color;
                            current.Parent.Color = ColorEnum.Black;
                            node.Right.Color     = ColorEnum.Black;
                            LeftRotate(current.Parent);
                            current.Right = null;
                            current       = this.root;
                        }
                    }
                }
                else
                {
                    node = current.Parent.Left;
                    if (node.Color == ColorEnum.Red)
                    {
                        node.Color           = ColorEnum.Black;
                        current.Parent.Color = ColorEnum.Red;
                        RightRotate(current.Parent);
                        node = current.Parent.Left;
                    }
                    if (node.Left == null && node.Right == null)
                    {
                        node.Color = ColorEnum.Red;
                        current    = current.Parent;
                    }
                    else
                    {
                        if (node.Left.Color == ColorEnum.Black && node.Right.Color == ColorEnum.Black)
                        {
                            node.Color = ColorEnum.Red;
                            current    = current.Parent;
                        }
                        else
                        {
                            if (node.Left.Color == ColorEnum.Black)
                            {
                                node.Right.Color = ColorEnum.Black;
                                node.Color       = ColorEnum.Red;
                                LeftRotate(node);
                                node = current.Parent.Right;
                            }
                            node.Color           = current.Parent.Color;
                            current.Parent.Color = ColorEnum.Black;
                            node.Left.Color      = ColorEnum.Black;
                            RightRotate(current.Parent);
                            current.Left = null;
                            current      = this.root;
                        }
                    }
                }
            }
            current.Color = ColorEnum.Black;
        }
Exemple #11
0
 /// <summary>
 /// Clear dictionary.
 /// </summary>
 public void Clear()
 {
     this.root         = null;
     this.countOfNodes = 0;
 }
Exemple #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(TKey key)
        {
            DictNode <TKey, TValue> current = this.root;
            DictNode <TKey, TValue> node;
            DictNode <TKey, TValue> curr;

            int cmp;

            while (current != null)
            {
                cmp = key.CompareTo(current.Info.Key);

                if (cmp < 0)
                {
                    current = current.Left;
                }

                if (cmp > 0)
                {
                    current = current.Right;
                    continue;
                }

                break;
            }

            if (current == null)
            {
                return(false);
            }

            if (current.Left == null || current.Right == null)
            {
                node = current;
            }
            else
            {
                node = current.Left;

                while (node.Right != null)
                {
                    node = node.Right;
                }
            }

            if (node.Left != null)
            {
                curr = node.Left;
            }
            else
            {
                curr = node.Right;
            }

            if (curr != null)
            {
                curr.Parent = node.Parent;

                if (node == node.Parent.Left)
                {
                    node.Parent.Left = curr;
                }
                else
                {
                    node.Parent.Right = curr;
                }
            }
            else
            {
                curr = node;
            }


            if (node.Parent == null)
            {
                this.root = curr;
                return(true);
            }

            if (node != current)
            {
                current.Info = new KeyValuePair <TKey, TValue>(node.Info.Key, node.Info.Value);
            }

            if (node.Color == ColorEnum.Black)
            {
                Balancing(curr);
            }
            countOfNodes--;
            return(true);
        }
Exemple #13
0
 /// <summary>
 /// The parametrless constructor.
 /// </summary>
 public RedBlackDictionary()
 {
     this.root         = null;
     this.countOfNodes = 0;
 }