Пример #1
0
        /// <summary>
        /// Generates a random tree
        /// </summary>
        /// <param name="depth">desired depth of tree</param>
        /// <returns>
        /// A newly generated Tree of specified degree "maxBreadth"
        /// </returns>
        public static Tree Generate(TraversalType type, 
            int depth, int maxBreadth, int numFirstChildren = -1, int maxNodes = -1)
        {
            Tree t;
            TNode r;
            TNode c;
            Random rnd;
            int breadth;
            int d;
            bool finished;
            int i;
            TNode child;
            TraversalListIDS<TNode> ids=new TraversalListIDS<TNode>(type);
            int count;

            rnd=new Random();
            r=new TNode();
            t=new Tree(r);
            finished=false;
            r.Level=1;
            count=0;

            ids.Add(r);

            while(ids.Count > 0 && finished == false)
            {
                c=ids.Get();

                // generate new level of arbirary children until desired depth is reached

                if(c.Level==1)
                {
                    if(numFirstChildren<=0)
                    {
                        breadth=rnd.Next(maxBreadth)+1;
                    }
                    else
                    {
                        breadth=numFirstChildren;
                    }
                }
                else
                {
                    breadth=rnd.Next(maxBreadth)+1;
                }

                for(i = 0; i < breadth; i++)
                {
                    child=new TNode();
                    child.Parent=c;
                    child.Level=c.Level+1;
                    child.ChildIndex = i;

                    count++;
                    c.Children.Add(child);

                    ids.Add(child);
                }

                // finished when reached desired number of nodes or reached desired depth
                if(maxNodes > 0)
                {
                    finished=count>=maxNodes;
                }
                else
                {
                    finished=c.Level==depth;
                }

            }

            return t;
        }
Пример #2
0
        public void TraversePreorder(VisitNodeFunct visitNode, TraversalType type)
        {
            TraversalListIDS<TNode> ids=new TraversalListIDS<TNode>(type);
            TNode c;
            int i;
            Hashtable visited=new Hashtable();

            root.Parent=null;
            root.Level=0;

            ids.Add(root);

            foreach(TNode rootChild in root.Children)
                ids.Add(rootChild);

            while(ids.Count > 0)
            {
                c=ids.Get();

                if(!visited.ContainsKey(c))
                {
                    visitNode(c);
                    visited.Add(c,0);

                    i=0;
                    foreach(TNode child in c.Children)
                    {
                        child.Parent=c;
                        child.Level=c.Level+1;
                        child.ChildIndex=i;

                        ids.Add(child);
                    }
                }
            }
        }