예제 #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");
            }
            var 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 <T> GetEnumerator()
        {
            var stack = new VisitableStack <GeneralTree <T> >();

            stack.Push(this);
            while (!stack.IsEmpty)
            {
                var tree = stack.Pop();
                if (tree != null)
                {
                    yield return(tree.Data);

                    for (var i = 0; i < tree.Degree; i++)
                    {
                        stack.Push(tree.GetChild(i));
                    }
                }
            }
        }
예제 #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 <KeyValuePair <TKey, TValue> > GetEnumerator()
        {
            var 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);
                }
            }
        }
예제 #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()
        {
            var stack = new VisitableStack <BinaryTree <T> >();

            stack.Push(this);
            while (!stack.IsEmpty)
            {
                var tree = stack.Pop();
                yield return(tree.Data);

                if (tree.Left != null)
                {
                    stack.Push(tree.Left);
                }
                if (tree.Right != null)
                {
                    stack.Push(tree.Right);
                }
            }
        }
예제 #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()
        {
            if (_tree != null)
            {
                var stack = new VisitableStack <BinaryTree <Association <TKey, TValue> > >();
                stack.Push(_tree);
                while (!stack.IsEmpty)
                {
                    var t = stack.Pop();
                    yield return(t.Data.ToKeyValuePair());

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