Advertisement
Guest User

Old school tree display

a guest
Sep 10th, 2010
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. public class Pair<T, U>
  2.     {
  3.         public Pair()
  4.         {
  5.         }
  6.  
  7.         public Pair(T first, U second)
  8.         {
  9.             this.First = first;
  10.             this.Second = second;
  11.         }
  12.  
  13.         public T First { get; set; }
  14.         public U Second { get; set; }
  15.     };
  16.  
  17.     sealed class Dumper
  18.     {
  19.         static public string Dump(Node root)
  20.         {
  21.             StringBuilder fulltree = new StringBuilder();
  22.             if (root == null)
  23.                 return string.Empty;
  24.             fulltree.Append(root.Text + "\n");
  25.  
  26.             Stack<Pair<int, IList<Node>>> browser = new Stack<Pair<int, IList<Node>>>();
  27.             browser.Push(new Pair<int, IList<Node>>(0, root.Children));
  28.  
  29.             // customize tree
  30.             const string branch = "├", lastbranch = "└", vertical = "│", space = " ", horizontal = "─";
  31.             List<string> currPath = new List<string>();
  32.  
  33.             // for each list of children
  34.             while (browser.Count != 0)
  35.             {
  36.                 Pair<int, IList<Node>> current = browser.Peek();
  37.                 if (current.First >= current.Second.Count)
  38.                 {
  39.                     // stop!
  40.                     browser.Pop();
  41.                     if (currPath.Count > 0)
  42.                         currPath.RemoveAt(currPath.Count - 1);
  43.                 }
  44.                 else
  45.                 {
  46.                     if ((current.First + 1) == current.Second.Count)
  47.                     {
  48.                         Node n = current.Second[current.First];
  49.                         string path = string.Join(space, currPath.ToArray());
  50.                         if (!string.IsNullOrEmpty(path))
  51.                             path += space;
  52.                         fulltree.Append(path + lastbranch + horizontal + n.Text + "\n");
  53.                        
  54.                         if (n.Children.Count > 0)
  55.                         {
  56.                             currPath.Add(space);
  57.                             browser.Push(new Pair<int, IList<Node>>(0, n.Children));
  58.                         }
  59.                     }
  60.                     else
  61.                     {
  62.                         Node n = current.Second[current.First];
  63.                         string path = string.Join(space, currPath.ToArray());
  64.                         if (!string.IsNullOrEmpty(path))
  65.                             path += space;
  66.                         fulltree.Append(path + branch + horizontal + n.Text + "\n");
  67.                         if (n.Children.Count > 0)
  68.                         {
  69.                             browser.Push(new Pair<int, IList<Node>>(0, n.Children));
  70.                             currPath.Add(vertical);
  71.                         }
  72.                     }
  73.                     current.First++;
  74.                 }
  75.             }
  76.             return fulltree.ToString();
  77.         }
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement