Exemplo n.º 1
0
    public void UpdateAttributes()
    {
        this.MaxDepth = Root.Level;

        var   k = new TraversalListIDS <TNode>(TraversalType.DepthFirst);
        TNode c;
        bool  finished = false;

        k.Add(this.Root);

        while (k.Count > 0 && finished == false)
        {
            c = k.Take();

            foreach (TNode child in c.Children)
            {
                child.Parent = c;
                child.Level  = c.Level + 1;
                if (child.Level > this.MaxDepth)
                {
                    this.MaxDepth = child.Level;
                }

                k.Add(child);
            }
        }
    }
        public static Tree BuildFS(string root_dir, renderNodeFunct renderNode)
        {
            TNode c;
            TNode n;
            FileInfo fi;
            Type ty;

            var di = new DirectoryInfo(root_dir);
            var k = new TraversalListIDS<TNode>(TraversalType.DepthFirst);
            var t = new Tree(new TNode());

            t.MaxDepth = t.Root.Level = 0;
            t.Root.Data = di;
            k.Add(t.Root);

            while (k.Count > 0)
            {
                c = k.Take();
                renderNode(t,c);

                ty = c.Data.GetType();

                if (ty == typeof(DirectoryInfo))
                {
                    di = (DirectoryInfo)c.Data;

                    foreach (var dir in di.GetDirectories())
                    {
                        n = new TNode();
                        n.Level = c.Level + 1;
                        if (n.Level > t.MaxDepth) t.MaxDepth = n.Level;
                        n.Parent = c;
                        c.Children.Add(n);
                        n.Data = dir;
                        k.Add(n);
                    }
                    //foreach (var file in di.GetFiles())
                    //{
                    //    n = new TNode();
                    //    n.Level = c.Level + 1;
                    //    if (n.Level > t.MaxDepth) t.MaxDepth = n.Level;
                    //    n.Parent = c;
                    //    c.Children.Add(n);
                    //    n.Data = file;
                    //}
                }
                else if (ty == typeof(FileInfo))
                {
                    fi = (FileInfo)c.Data;
                }
            }

            return t;
        }
Exemplo n.º 3
0
    public void Traverse(VisitNodeFunct2 visitNode, TraversalType type)
    {
        var ids = new TraversalListIDS <TNode>(type);

        ids.Add(Root);

        while (ids.Count > 0 && visitNode(ids.Take(), ids) == true)
        {
            ;
        }
    }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
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);
                    }
                }
            }
        }
Exemplo n.º 6
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 gen_mechanism,
            int depth,
            int max_breadth,
            int num_first_children = -1,
            int max_nodes = -1)
        {
            Tree t = new Tree { Root = new TNode { Level = 1 } };

            Random rnd = new Random();
            int breadth;
            int i;
            TNode child;
            var k = new TraversalListIDS<TNode>(gen_mechanism);
            var visited = new Hashtable();
            bool finished = false;
            TNode c;

            k.Add(t.Root);

            while (k.Count > 0 && finished == false)
            {
                // generate new level of arbirary children until desired depth is reached

                c = k.Take();

                // finished when reached desired number of nodes or reached desired depth
                if (max_nodes > 0)
                {
                    finished = t.Count >= max_nodes;
                }
                else
                {
                    finished = c.Level == depth + 1;
                }

                if (!finished && (gen_mechanism == TraversalType.DepthFirst || !visited.ContainsKey(c)))
                {
                    if (gen_mechanism == TraversalType.BreadthFirst)
                        visited.Add(c, 0);

                    if (c.Level == 1)
                    {
                        if (num_first_children == -1)
                        {
                            breadth = rnd.Next(1, max_breadth + 1);
                        }
                        else
                        {
                            breadth = num_first_children;
                        }
                    }
                    else
                    {
                        breadth = rnd.Next(1, max_breadth + 1);
                        //if(c.Level==4){
                        //    breadth = 1000;
                        //}

                        //if (c.Level == depth)
                        //{
                        //    breadth = 255;
                        //}
                    }

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

                        if (child.Level > t.MaxDepth) t.MaxDepth = child.Level;

                        t.Count++;
                        c.Children.Add(child);

                        k.Add(child);
                    }
                }
            }

            return t;
        }
Exemplo n.º 7
0
        public void UpdateAttributes()
        {
            this.MaxDepth = Root.Level;

            var k = new TraversalListIDS<TNode>(TraversalType.DepthFirst);
            TNode c;
            bool finished = false;

            k.Add(this.Root);

            while (k.Count > 0 && finished == false)
            {
                c = k.Take();

                foreach (TNode child in c.Children)
                {
                    child.Parent = c;
                    child.Level = c.Level + 1;
                    if (child.Level > this.MaxDepth) this.MaxDepth = child.Level;

                    k.Add(child);
                }
            }
        }
Exemplo n.º 8
0
        public void Traverse(VisitNodeFunct2 visitNode, TraversalType type)
        {
            var ids = new TraversalListIDS<TNode>(type);
            ids.Add(Root);

            while (ids.Count > 0 && visitNode(ids.Take(), ids) == true) ;
        }
Exemplo n.º 9
0
        /// <summary>
        /// precondition: Root position specified
        /// </summary>
        public void LayoutTopDown2D(PointF radius, double edge_length, TraversalType type, bool grow)
        {
            var k = new TraversalListIDS<List<TNode>>(type);

            //Root.Point.X = 0;
            Root.Point.Y = 0;
            Root.angle = 0;
            Root.leftBisector = TREE_MAX_HORIZ;
            Root.rightBisector = 0;
            Root.leftTangent = TREE_MAX_HORIZ;
            Root.rightTangent = 0;

            int j;
            double i;
            double right_limit;
            double left_limit;
            double slice_space; // angle space for each slice in sector
            double level_arc = 2.0; // TWO_PI*.25
            double level_ratio; // derived level ratio
            double arc; // arc length for a level

            /* LAYOUT LEVEL */
            double remaning_angle;
            double prev_angle;
            double prev_gap;
            TNode prev_sibling;
            TNode first_child;
            List<TNode> parents;
            TNode parent;
            List<TNode> work_item;

            parents = new List<TNode>();
            parents.Add(Root);
            k.Add(parents);

            while (k.Count > 0)
            {
                prev_angle = Root.angle;
                prev_sibling = first_child = null;
                parents = new List<TNode>();

                work_item = k.Take();

                for (j = 0; j < work_item.Count; j++)
                {
                    parent = work_item[j];

                    right_limit = parent.RightLimit();
                    left_limit = parent.LeftLimit();

                    slice_space = (left_limit - right_limit) / parent.Children.Count;

                    if (grow) edge_length += 0.01;

                    i = 0.5;
                    foreach (TNode child in parent.Children)
                    {
                        child.angle = right_limit + (i * slice_space);

                        child.Point.X = Root.Point.X + (child.angle * 60);
                        child.Point.Y = Root.Point.Y + (child.Level * 60);

                        if (child.HasChildren) // Is it a parent node?
                        {
                            if (first_child == null) first_child = child;

                            prev_gap = child.angle - prev_angle;
                            child.prev_gap = prev_gap;
                            child.rightBisector = child.angle - (prev_gap / 2.0);

                            if (prev_sibling != null) prev_sibling.leftBisector = child.rightBisector;

                            // setup sector space for potential decendants that are positioned from current node

                            if (child.Level == MaxDepth) level_ratio = 1.0;
                            else level_ratio = child.Level / (child.Level + 1.0);

                            arc = level_arc * (level_ratio * 30.0);

                            child.leftTangent  = child.angle + arc;
                            child.rightTangent = child.angle - arc;

                            prev_angle = child.prev_angle = child.angle;
                            prev_sibling = child;

                            parents.Add(child);
                        }

                        i++;
                    }
                }

                if (first_child != null)
                {
                    // calculate the remaining angle to define the next level/sector

                    remaning_angle = TREE_MAX_HORIZ - prev_sibling.angle;

                    first_child.rightBisector = (first_child.angle - remaning_angle) / 2.0;

                    prev_sibling.leftBisector = first_child.rightBisector + TREE_MAX_ANGLE;
                }

                if (parents.Count > 0)
                    k.Add(parents);
            }
        }
Exemplo n.º 10
0
    /// <summary>
    /// precondition: Root position specified
    /// </summary>
    public void LayoutRadial2D(Vector2 radius, double edge_length, TraversalType type, bool grow)
    {
        var k = new TraversalListIDS <List <TNode> >(type);

        Root.angle         = 0;
        Root.leftBisector  = TREE_MAX_ANGLE;
        Root.rightBisector = 0;
        Root.leftTangent   = TREE_MAX_ANGLE;
        Root.rightTangent  = 0;

        int   j;
        float i;
        float right_limit;
        float left_limit;
        float slice_space;      // angle space for each slice in sector
        float level_arc = 2.0f; // TWO_PI*.25
        float level_ratio;      // derived level ratio
        float arc;              // arc length for a level

        /* LAYOUT LEVEL */
        float        remaning_angle;
        float        prev_angle;
        float        prev_gap;
        TNode        prev_sibling;
        TNode        first_child;
        List <TNode> parents;
        TNode        parent;
        List <TNode> work_item;

        parents = new List <TNode>();
        parents.Add(Root);
        k.Add(parents);

        while (k.Count > 0)
        {
            prev_angle   = Root.angle;
            prev_sibling = first_child = null;
            parents      = new List <TNode>();

            work_item = k.Take();

            for (j = 0; j < work_item.Count; j++)
            {
                parent = work_item[j];

                right_limit = parent.RightLimit();
                left_limit  = parent.LeftLimit();

                slice_space = (left_limit - right_limit) / parent.Children.Count;

                if (grow)
                {
                    edge_length += 0.01;
                }

                i = 0.5f;
                foreach (TNode child in parent.Children)
                {
                    child.angle = right_limit + (i * slice_space);

                    child.Point.X = Root.Point.X + (float)((edge_length * child.Level * radius.X) * Math.Cos(child.angle));
                    child.Point.Y = Root.Point.Y + (float)((edge_length * child.Level * radius.Y) * Math.Sin(child.angle));
                    child.Point.Z = Root.Point.Z;

                    if (child.HasChildren) // Is it a parent node?
                    {
                        if (first_child == null)
                        {
                            first_child = child;
                        }

                        prev_gap            = child.angle - prev_angle;
                        child.prev_gap      = prev_gap;
                        child.rightBisector = child.angle - (prev_gap / 2.0f);

                        if (prev_sibling != null)
                        {
                            prev_sibling.leftBisector = child.rightBisector;
                        }

                        // setup sector space for potential decendants that are positioned from current node

                        if (child.Level == MaxDepth)
                        {
                            level_ratio = 1.0f;
                        }
                        else
                        {
                            level_ratio = (child.Level / (child.Level + 1.0f));
                        }

                        arc = level_arc * (float)Math.Asin(level_ratio);

                        child.leftTangent  = child.angle + arc;
                        child.rightTangent = child.angle - arc;

                        prev_angle   = child.prev_angle = child.angle;
                        prev_sibling = child;

                        parents.Add(child);
                    }
                    i++;
                }
            }

            if (first_child != null)
            {
                // calculate the remaining angle to define the next level/sector

                remaning_angle = TREE_MAX_ANGLE - prev_sibling.angle;

                first_child.rightBisector = (first_child.angle - remaning_angle) / 2.0f;

                prev_sibling.leftBisector = first_child.rightBisector + TREE_MAX_ANGLE;
            }

            if (parents.Count > 0)
            {
                k.Add(parents);
            }
        }
    }