Advertisement
Guest User

Olostan

a guest
Sep 14th, 2010
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1.         private sealed class MyDumper
  2.         {
  3.             public static string Dump(Node root)
  4.             {
  5.                 var sb = new StringBuilder();
  6.                 var workStack = new Stack<Helper>();
  7.                 workStack.Push(new Helper { Parent = null, CurrentNode = root, ParentIndent = string.Empty });
  8.                 while (workStack.Count > 0)
  9.                 {
  10.                     Helper current = workStack.Pop();
  11.                     if (current.Parent != null)
  12.                     {
  13.                         sb.Append(current.Parent.ParentIndent);
  14.                         current.ParentIndent = current.Parent.ParentIndent +
  15.                                                (current.IsLast ? " " : "│") +
  16.                                                " ";
  17.                         // my indent
  18.                         sb.Append(current.IsLast ? "└" : "├");
  19.                         sb.Append("-");
  20.                     }
  21.                     else current.ParentIndent = "";
  22.                     sb.AppendLine(current.CurrentNode.Text);
  23.                     for (int index = current.CurrentNode.Children.Count - 1; index >= 0; index--)
  24.                     {
  25.                         workStack.Push(new Helper
  26.                                            {
  27.                                                CurrentNode = current.CurrentNode.Children[index],
  28.                                                Parent = current,
  29.                                                IsLast = index == current.CurrentNode.Children.Count - 1
  30.                                            });
  31.                     }
  32.                 }
  33.                 return sb.ToString();
  34.             }
  35.  
  36.             private class Helper
  37.             {
  38.                 public bool IsLast;
  39.                 public Node CurrentNode;
  40.                 public Helper Parent;
  41.                 public string ParentIndent;
  42.             }
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement