sashomaga

Simple Tree

Jun 2nd, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. class Node<T>
  2. {
  3.     public T Value { get; set; }
  4.  
  5.     public List<Node<T>> children { get; set; }
  6.  
  7.     public bool HasParent { get; set; }
  8.  
  9.     public Node()
  10.     {
  11.         this.children = new List<Node<T>>();
  12.     }
  13.  
  14.     public Node(T value)
  15.         : this()
  16.     {
  17.         this.Value = value;
  18.     }
  19. }
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24.  
  25. class Program
  26. {
  27.     static void Main(string[] args)
  28.     {
  29.         int N = int.Parse(Console.ReadLine());
  30.  
  31.         Node<int>[] nodes = new Node<int>[N];
  32.  
  33.         for (int i = 0; i <= N; i++)
  34.         {
  35.             string edgeAsString = Console.ReadLine();
  36.             string[] edgeParts = edgeAsString.Split(' ');
  37.  
  38.             int parent = int.Parse(edgeParts[0]);
  39.             nodes[parent].Value = parent;
  40.             int child = int.Parse(edgeParts[1]);
  41.             nodes[parent].children.Add(new Node<int>(child));
  42.             nodes[child].Value = child;
  43.             nodes[child].HasParent = true;
  44.         }
  45.     }
  46.  
  47.     public Node<int> FindRoot(Node<int>[] nodes)
  48.     {
  49.         foreach (var node in nodes)
  50.         {
  51.             foreach (var child in node.children)
  52.             {
  53.                 if (child.HasParent == false)
  54.                 {
  55.                     return child;
  56.                 }                
  57.             }
  58.         }
  59.  
  60.         return null;
  61.     }  
  62.  
  63.     public List<Node<int>> FindAllLeafs(Node<int>[] nodes)
  64.     {
  65.         List<Node<int>> result = new List<Node<int>>();
  66.         foreach (var node in nodes)
  67.         {
  68.             if (node.children == null)
  69.             {
  70.                 result.Add(node);
  71.             }
  72.         }
  73.  
  74.         return result;
  75.     }
  76.  
  77.     public List<Node<int>> FindMidleNodes(Node<int>[] nodes)
  78.     {
  79.         List<Node<int>> result = new List<Node<int>>();
  80.         foreach (var node in nodes)
  81.         {
  82.             if (node.children.Count > 0 && node.HasParent == true)
  83.             {
  84.                 result.Add(node);
  85.             }
  86.         }
  87.  
  88.         return result;
  89.     }
  90.  
  91.     public int FindLongestPath(Node<int> root)
  92.     {
  93.         if (root.children.Count == 0)
  94.         {
  95.             return 0;
  96.         }
  97.  
  98.         int maxPath = 0;
  99.         foreach (var node in root.children)
  100.         {
  101.             maxPath = Math.Max(maxPath, FindLongestPath(node));
  102.         }
  103.  
  104.         return maxPath + 1;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment