Ejemplo n.º 1
0
        public static IList <TDest> FromTreeNodes <TSrc, TDest>(
            TSrc rootnode,
            System.Func <TSrc, IEnumerable <TSrc> > enumchildren,
            System.Func <TSrc, TDest> createdstnode,
            System.Action <TDest, TDest> addchild)
        {
            var stack  = new Stack <TDest>();
            var tnodes = new List <TDest>();

            var walk_items = Traversal.Walk <TSrc>(rootnode, input_node => enumchildren(input_node));

            foreach (var walk_item in walk_items)
            {
                if (walk_item.HasEnteredNode)
                {
                    var new_dst_node = createdstnode(walk_item.Node);

                    if (stack.Count > 0)
                    {
                        var parent = stack.Peek();
                        addchild(parent, new_dst_node);
                    }
                    stack.Push(new_dst_node);
                    tnodes.Add(new_dst_node);
                }
                else if (walk_item.HasExitedNode)
                {
                    stack.Pop();
                }
            }

            return(tnodes);
        }
Ejemplo n.º 2
0
 public static IEnumerable <T> PostOrder <T>(T root, EnumerateChildren <T> enum_children)
 {
     foreach (var walkevent in Traversal.Walk <T>(root, enum_children, x => true))
     {
         if (walkevent.HasExitedNode)
         {
             yield return(walkevent.Node);
         }
     }
 }
Ejemplo n.º 3
0
        public static string GetTreeString(IEnumerable <TreeNode <T> > nodes, System.Func <T, string> get_start_delim,
                                           System.Func <T, string> get_end_delim, System.Func <T, string> get_content)
        {
            var sb = new System.Text.StringBuilder();

            foreach (var node in nodes)
            {
                foreach (var walkevent in Traversal.Walk <TreeNode <T> >(node, n => n.Children))
                {
                    if (walkevent.HasEnteredNode)
                    {
                        sb.Append(get_start_delim(walkevent.Node.Data));
                        sb.Append(get_content(walkevent.Node.Data));
                    }
                    else if (walkevent.HasExitedNode)
                    {
                        sb.Append(get_end_delim(walkevent.Node.Data));
                    }
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 4
0
 public IEnumerable <WalkEvent <TreeNode <T> > > Walk()
 {
     return(Traversal.Walk <TreeNode <T> >(this, n => n.Children));
 }