示例#1
0
文件: TreeSet.cs 项目: obpositive/yal
 public bool InOrderTreeWalk(TreeWalkAction <T> action)
 {
     if (Root != null)
     {
         Stack <Node> stack = new Stack <Node>(2 * ((int)Math.Log(Count + 1)));
         Node         r     = Root;
         while (r != null)
         {
             stack.Push(r);
             r = r.Left;
         }
         while (stack.Count != 0)
         {
             r = stack.Pop();
             if (!action(r))
             {
                 return(false);
             }
             for (Node node2 = r.Right; node2 != null; node2 = node2.Left)
             {
                 stack.Push(node2);
             }
         }
     }
     return(true);
 }
示例#2
0
        //
        // Do a in order walk on tree and calls the delegate for each node.
        // If the action delegate returns false, stop the walk.
        //
        // Return true if the entire tree has been walked.
        // Otherwise returns false.
        //
        internal bool InOrderTreeWalk(TreeWalkAction <T> action)
        {
            if (root == null)
            {
                return(true);
            }

            // The maximum height of a red-black tree is 2*lg(n+1).
            // See page 264 of "Introduction to algorithms" by Thomas H. Cormen
            Stack <Node> stack   = new Stack <Node>(2 * (int)Math.Log(Count + 1));
            Node         current = root;

            while (current != null)
            {
                stack.Push(current);
                current = current.Left;
            }

            while (stack.Count != 0)
            {
                current = stack.Pop();
                if (!action(current))
                {
                    return(false);
                }

                Node node = current.Right;
                while (node != null)
                {
                    stack.Push(node);
                    node = node.Left;
                }
            }
            return(true);
        }
示例#3
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);
            }
        }
示例#4
0
    internal void InOrderTreeWalk(Node start, TreeWalkAction <Key, Value> action, TreeWalkTerminationPredicate <Key, Value> terminationPredicate)
    {
        Node node = start;

        while (node != null && !terminationPredicate(node))
        {
            action(node);
            node = node.Next;
        }
    }
示例#5
0
        /// <summary>
        /// Performs reverse in-order traversal on the
        /// <see cref="AndersonTree{TKey,TValue}"/>
        /// </summary>
        /// <param name="action">
        /// A method used to perform some action with each node in the tree.
        /// </param>
        /// <remarks>
        /// The <paramref name="action"/> must return <c>true</c> in order to
        /// allows the traversal to continue.
        /// </remarks>
        internal bool ReverseInOrderTreeWalk(TreeWalkAction <TKey, TValue> action)
        {
            if (root_.Level != 0)
            {
                Stack <AndersonTreeNode <TKey, TValue> > stack =
                    new Stack <AndersonTreeNode <TKey, TValue> >
                        (2 * ((int)Math.Log((double)(count_ + 1))));

                AndersonTreeNode <TKey, TValue> node = root_;

                // traverse the left branch of the tree until reach a sentinel_ node.
                while (node.Level != 0)
                {
                    stack.Push(node);
                    node = node.Right;
                }

                while (stack.Count != 0)
                {
                    node = stack.Pop();

                    if (!action(node))
                    {
                        return(false);
                    }

                    // traverse the right branch of the current node and store then into
                    // the execution stack.
                    for (AndersonTreeNode <TKey, TValue> node2 = node.Left;
                         node2.Level != 0;
                         node2 = node2.Right)
                    {
                        stack.Push(node2);
                    }
                }
            }
            return(true);
        }
示例#6
0
 internal bool InOrderTreeWalk(TreeWalkAction <T> action)
 {
     if (this.root != null)
     {
         Stack <TreeSet <T> .Node> stack = new Stack <TreeSet <T> .Node>(2 * (int)System.Math.Log((double)(this.Count + 1)));
         for (TreeSet <T> .Node node = this.root; node != null; node = node.Left)
         {
             stack.Push(node);
         }
         while (stack.Count != 0)
         {
             TreeSet <T> .Node node = stack.Pop();
             if (!action(node))
             {
                 return(false);
             }
             for (TreeSet <T> .Node node2 = node.Right; node2 != null; node2 = node2.Left)
             {
                 stack.Push(node2);
             }
         }
     }
     return(true);
 }