Example #1
0
        /// <summary>
        /// Accepts the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public void Accept(IVisitor <KeyValuePair <TKey, TValue> > visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            VisitableStack <RedBlackTreeNode <TKey, TValue> > stack = new VisitableStack <RedBlackTreeNode <TKey, TValue> >();

            stack.Push(root);

            while (!stack.IsEmpty)
            {
                if (!visitor.HasCompleted)
                {
                    RedBlackTreeNode <TKey, TValue> node = stack.Pop();
                    visitor.Visit(new KeyValuePair <TKey, TValue>(node.Key, node.Value));

                    if (node.Left != null)
                    {
                        stack.Push(node.Left);
                    }

                    if (node.Right != null)
                    {
                        stack.Push(node.Right);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
        {
            if (tree != null)
            {
                VisitableStack <BinaryTree <Association <TKey, TValue> > > stack = new VisitableStack <BinaryTree <Association <TKey, TValue> > >();

                stack.Push(tree);

                while (!stack.IsEmpty)
                {
                    BinaryTree <Association <TKey, TValue> > t = stack.Pop();

                    yield return(t.Data.ToKeyValuePair());

                    if (t.Left != null)
                    {
                        stack.Push(t.Left);
                    }

                    if (t.Right != null)
                    {
                        stack.Push(t.Right);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than obj. Zero This instance is equal to obj. Greater than zero This instance is greater than obj.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">obj is not the same type as this instance. </exception>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (obj.GetType() == this.GetType())
            {
                VisitableStack <T> s = obj as VisitableStack <T>;

                return(this.Count.CompareTo(s.Count));
            }
            else
            {
                return(this.GetType().FullName.CompareTo(obj.GetType().FullName));
            }
        }
Example #4
0
        /// <summary>
        /// Accepts the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public void Accept(IVisitor <T> visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            VisitableStack <T> .Enumerator enumerator = this.GetEnumerator();

            while (enumerator.MoveNext())
            {
                visitor.Visit(enumerator.Current);

                if (visitor.HasCompleted)
                {
                    return;
                }
            }
        }
Example #5
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <T> GetEnumerator()
        {
            VisitableStack <GeneralTree <T> > stack = new VisitableStack <GeneralTree <T> >();

            stack.Push(this);

            while (!stack.IsEmpty)
            {
                GeneralTree <T> tree = stack.Pop();

                if (tree != null)
                {
                    yield return(tree.Data);

                    for (int i = 0; i < tree.Degree; i++)
                    {
                        stack.Push(tree.GetChild(i));
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <T> GetEnumerator()
        {
            VisitableStack <BinaryTree <T> > stack = new VisitableStack <BinaryTree <T> >();

            stack.Push(this);

            while (!stack.IsEmpty)
            {
                BinaryTree <T> tree = stack.Pop();

                yield return(tree.Data);

                if (tree.leftSubtree != null)
                {
                    stack.Push(tree.leftSubtree);
                }

                if (tree.rightSubtree != null)
                {
                    stack.Push(tree.rightSubtree);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
        {
            VisitableStack <RedBlackTreeNode <TKey, TValue> > stack = new VisitableStack <RedBlackTreeNode <TKey, TValue> >();

            stack.Push(root);

            while (!stack.IsEmpty)
            {
                RedBlackTreeNode <TKey, TValue> node = stack.Pop();

                yield return(new KeyValuePair <TKey, TValue>(node.Key, node.Value));

                if (node.Left != null)
                {
                    stack.Push(node.Left);
                }

                if (node.Right != null)
                {
                    stack.Push(node.Right);
                }
            }
        }