Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. public class Node<T>
  4. {
  5.     public Node(T value, Node<T> parent = null)
  6.     {
  7.         this.Value = value;
  8.         this.Children = new List<Node<T>>();
  9.     }
  10.  
  11.     public T Value { get; set; }
  12.     public List<Node<T>> Children { get; private set; }
  13.  
  14.     // Prints the node and all its children
  15.     public void PrintTree()
  16.     {
  17.         Console.WriteLine("Tree");
  18.         this.Print(string.Empty, true);
  19.     }
  20.  
  21.     private void Print(string prefix, bool isTail)
  22.     {
  23.         Console.WriteLine(prefix + (isTail ? "└── " : "├── ") + this.Value);
  24.         for (int i = 0; i < this.Children.Count - 1; i++)
  25.         {
  26.             this.Children[i].Print(prefix + (isTail ? "    " : "│   "), false);
  27.         }
  28.  
  29.         if (this.Children.Count >= 1)
  30.         {
  31.             this.Children[this.Children.Count - 1].Print(prefix + (isTail ? "    " : "│   "), true);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement