Advertisement
sylviapsh

Tree -allLeaves, allMiddleNodes, LongestPath

Jun 23rd, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. namespace Tree
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text;
  6.  
  7.     public class Node<T>
  8.     {
  9.         public Node()
  10.         {
  11.             this.Children = new List<Node<T>>();
  12.         }
  13.  
  14.         public Node(T value) : this()
  15.         {
  16.             this.Value = value;
  17.         }
  18.  
  19.         public T Value { get; set; }
  20.  
  21.         public List<Node<T>> Children { get; set; }
  22.  
  23.         public bool HasParent { get; set; }
  24.     }
  25.  
  26.     public class Tree<T>
  27.     {
  28.         public Node<T>[] Nodes { get; set; }
  29.  
  30.         public Node<T> FindRoot()
  31.         {
  32.             var hasParent = new bool[Nodes.Length];
  33.  
  34.             foreach (var node in Nodes)
  35.             {
  36.                 foreach (var child in node.Children)
  37.                 {
  38.                     hasParent[(Convert.ToInt32(child.Value))] = true;
  39.                 }
  40.             }
  41.  
  42.             for (int i = 0; i < hasParent.Length; i++)
  43.             {
  44.                 if (!hasParent[i])
  45.                 {
  46.                     return Nodes[i];
  47.                 }
  48.             }
  49.  
  50.             throw new ArgumentException("nodes", "The tree has no root.");
  51.         }
  52.  
  53.         public List<Node<T>> FindAllLeaves()
  54.         {
  55.             List<Node<T>> leaves = new List<Node<T>>();
  56.  
  57.             foreach (var node in Nodes)
  58.             {
  59.                 if (node.Children.Count == 0)
  60.                 {
  61.                     leaves.Add(node);
  62.                 }
  63.             }
  64.  
  65.             return leaves;
  66.         }
  67.  
  68.         public List<Node<T>> FindAllMiddleNodes()
  69.         {
  70.             List<Node<T>> middleNodes = new List<Node<T>>();
  71.  
  72.             foreach (var node in Nodes)
  73.             {
  74.                 if (node.HasParent && node.Children.Count > 0)
  75.                 {
  76.                     middleNodes.Add(node);
  77.                 }
  78.             }
  79.  
  80.             return middleNodes;
  81.         }
  82.  
  83.         public int FindLongestPath(Node<T> root)
  84.         {
  85.             if (root.Children.Count == 0)
  86.             {
  87.                 return 0;
  88.             }
  89.  
  90.             int maxPath = 0;
  91.             foreach (var node in root.Children)
  92.             {
  93.                 maxPath = Math.Max(maxPath, FindLongestPath(node));
  94.             }
  95.  
  96.             return maxPath + 1;
  97.         }
  98.     }
  99.  
  100.     public class TreeProgram
  101.     {
  102.         public static void Main()
  103.         {
  104.             var n = int.Parse(Console.ReadLine());
  105.             var nodes = new Node<int>[n];
  106.  
  107.             for (int i = 0; i < n; i++)
  108.             {
  109.                 nodes[i] = new Node<int>(i);
  110.             }
  111.  
  112.             for (int i = 1; i <= n - 1; i++)
  113.             {
  114.                 string edgeAsString = Console.ReadLine();
  115.                 var edgeParts = edgeAsString.Split(' ');
  116.  
  117.                 int parentId = int.Parse(edgeParts[0]);
  118.                 int childId = int.Parse(edgeParts[1]);
  119.  
  120.                 nodes[parentId].Children.Add(nodes[childId]);
  121.                 nodes[childId].HasParent = true;
  122.             }
  123.  
  124.             Tree<int> tree = new Tree<int>();        
  125.             tree.Nodes = nodes;
  126.  
  127.             // 1. Find the root
  128.             var root = tree.FindRoot();
  129.             Console.WriteLine("The root of the tree is: {0}", root.Value);
  130.  
  131.             // 2. Find all leaves
  132.             List<Node<int>> leaves = tree.FindAllLeaves();
  133.             Console.Write("Leaves: ");
  134.             Console.WriteLine(PrintNodes(leaves));
  135.            
  136.             // 3. Find all middle nodes
  137.             var middleNodes = tree.FindAllMiddleNodes();
  138.             Console.Write("Middle nodes: ");
  139.             Console.WriteLine(PrintNodes(middleNodes));
  140.  
  141.             // 4. Find the longest path from the root
  142.             var longestPath = tree.FindLongestPath(tree.FindRoot());
  143.             Console.WriteLine("Number of levels: {0}", longestPath + 1);
  144.         }
  145.  
  146.         private static string PrintNodes(List<Node<int>> nodes)
  147.         {
  148.             StringBuilder output = new StringBuilder();
  149.             foreach (var node in nodes)
  150.             {
  151.                 output.AppendFormat("{0} ", node.Value);
  152.             }
  153.  
  154.             return output.ToString();
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement