Exemple #1
0
        /// <summary>
        /// Accepts the specified visitor and allow it to visit every node of the
        /// tree.
        /// </summary>
        /// <param name="visitor">The visitor to accepts.</param>
        /// <param name="reverse">A value indicating if the elements will be visit
        /// in the reverse order or not.</param>
        /// <param name="state">A user-defined object that qualifies or contains
        /// information about the visitor's current state.</param>
        /// <exception cref="ArgumentNullException"><paramref name="visitor"/> is
        /// a null reference</exception>
        public void Accept(InOrderVisitor <TValue> visitor, object state,
                           bool reverse)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            TreeWalkAction <TKey, TValue> on_tree_walk =
                new TreeWalkAction <TKey, TValue>(
                    delegate(AndersonTreeNode <TKey, TValue> node)
            {
                visitor.Visit(node.Value, state);
                return(!visitor.IsCompleted);
            });

            if (reverse)
            {
                ReverseInOrderTreeWalk(on_tree_walk);
            }
            else
            {
                InOrderTreeWalk(on_tree_walk);
            }
        }
        public void ShouldVisitEachNodeInDescendingOrder()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            Stack <int> data = new Stack <int>(10);

            for (int i = 0; i < 10; i++)
            {
                data.Push(i * 10);
                tree.Add(i * 10, i * 10);
            }

            var visitor =
                new InOrderVisitor <int, int>(
                    new Visitor <int, int>(
                        (key, value, state) => Assert.AreEqual(data.Pop(), value)));

            tree.Accept(visitor, true);
        }
        public void ShouldVisitEachNodeInAscendingOrder()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            Queue <int> data = new Queue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                data.Enqueue(i * 10);
                tree.Add(i * 10, i * 10);
            }

            var visitor =
                new InOrderVisitor <int, int>(
                    new Visitor <int, int>(
                        (key, value, state) => Assert.AreEqual(data.Dequeue(), value)));

            tree.Accept(visitor, false);
        }
Exemple #4
0
 /// <summary>
 /// Accepts the specified visitor and allow it to visit every node of the
 /// tree.
 /// </summary>
 /// <param name="visitor">
 /// The visitor to accepts.
 /// </param>
 /// <param name="reverse">
 /// A value indicating if the elements will be visit in the reverse order
 /// or not.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="visitor"/> is a<c>null</c> reference.
 /// </exception>
 public void Accept(InOrderVisitor <TValue> visitor, bool reverse)
 {
     Accept(visitor, null, reverse);
 }