示例#1
0
        static void PrettyPrintTreeWorker <T>([NotNull] T node, [NotNull] GetChildrenAction <T> getChildren,
                                              [NotNull] Func <T, string> toStringFunc, StringBuilder builder, bool last, string indent)
        {
            builder.Append(indent);
            if (last)
            {
                builder.Append("\\-");
                indent += "  ";
            }
            else
            {
                builder.Append("|-");
                indent += "| ";
            }

            builder.AppendLine(toStringFunc(node));

            var lst = getChildren(node).MakeSafe().ToList();

            for (int i = 0; i < lst.Count; i++)
            {
                var child = lst[i];
                PrettyPrintTreeWorker <T>(child, getChildren, toStringFunc, builder, i == lst.Count - 1, indent);
            }
        }
示例#2
0
        private static void PostorderWorker <T>([NotNull] T node, [NotNull] GetChildrenAction <T> getChildren,
                                                [NotNull] List <T> outList)
        {
            foreach (T child in getChildren(node).MakeSafe())
            {
                PostorderWorker(child, getChildren, outList);
            }

            outList.Add(node);
        }
示例#3
0
        /// <summary>
        ///     traverse a tree using the postorder traversal
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="root">The root.</param>
        /// <param name="getChildren">The get children.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        ///     root
        ///     or
        ///     getChildren
        /// </exception>
        public static List <T> Postorder <T>([NotNull] T root, [NotNull] GetChildrenAction <T> getChildren)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (getChildren == null)
            {
                throw new ArgumentNullException(nameof(getChildren));
            }

            var outList = new List <T>();

            PostorderWorker(root, getChildren, outList);
            return(outList);
        }
示例#4
0
        /// <summary>
        /// prints a pretty tree
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="root">The root.</param>
        /// <param name="getChildren">The get children.</param>
        /// <param name="toStringFunc">To string function.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// root
        /// or
        /// getChildren
        /// </exception>
        public static string PrettyPrintTree <T>([NotNull] T root, [NotNull] GetChildrenAction <T> getChildren,
                                                 Func <T, string> toStringFunc = null)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (getChildren == null)
            {
                throw new ArgumentNullException(nameof(getChildren));
            }
            toStringFunc = toStringFunc ?? ((T f) => f.ToString());
            StringBuilder builder = new StringBuilder();
            string        indent  = "";

            PrettyPrintTreeWorker(root, getChildren, toStringFunc, builder, true, indent);
            return(builder.ToString());
        }
示例#5
0
        public static IEnumerable <T> Preorder <T>([NotNull] T root, [NotNull] GetChildrenAction <T> getChildrenAction)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (getChildrenAction == null)
            {
                throw new ArgumentNullException(nameof(getChildrenAction));
            }
            var stack = new Stack <T>();

            stack.Push(root);
            while (stack.Count > 0)
            {
                T node = stack.Pop();
                yield return(node);

                foreach (T child in getChildrenAction(node).MakeSafe())
                {
                    stack.Push(child);
                }
            }
        }