Пример #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);
                    }
                }
            }
        }
Пример #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);
                    }
                }
            }
        }
Пример #3
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));
                    }
                }
            }
        }
Пример #4
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);
                }
            }
        }
Пример #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 <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);
                }
            }
        }