Advertisement
Guest User

Lunedi quiz #1 - soluzione

a guest
Feb 9th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1.     class Node
  2.     {
  3.         public Node Left {get; private set;}
  4.         public Node Right { get; private set; }
  5.         public string Value { get; private set; }
  6.  
  7.         public Node(Node left, Node right, string value)
  8.         {
  9.             this.Left = left;
  10.             this.Right = right;
  11.             this.Value = value;
  12.         }
  13.  
  14.         // l'idea: naviga l'albero e se il primo nodo รจ sul lato opposto del secondo
  15.         // rispetto a root, allora i percorsi divergono, quindi root รจ l'antenato cercato
  16.         //
  17.         // se sono tutti e due a sinistra, scendi ricorsivamente sul nodo sinistro di root
  18.         // altrimenti scendi ricorsivamente sul nodo destro
  19.         public static Node FindCommonAncestor(Node root, Node first, Node second)
  20.         {
  21.             if (root == null)
  22.             {
  23.                 throw new Exception("Cannot find a path from root to children");
  24.             }
  25.  
  26.             if ((root == first) || (root == second))
  27.             {
  28.                 VerifyPathExists(root, first);
  29.                 VerifyPathExists(root, second);
  30.                 return root;
  31.             }
  32.  
  33.             if ((root.Value == first.Value) || (root.Value == second.Value))
  34.             {
  35.                 throw new Exception("Duplicate value found");
  36.             }
  37.  
  38.             var comp1 = string.Compare(root.Value, first.Value);
  39.             var comp2 = string.Compare(root.Value, second.Value);
  40.  
  41.             // i percorsi divergono, root รจ l'antenato cercato
  42.             if ((comp1 * comp2) < 0)
  43.             {
  44.                 VerifyPathExists(root, first);
  45.                 VerifyPathExists(root, second);
  46.  
  47.                 return root;
  48.             }
  49.  
  50.             if (comp1 > 0)
  51.             {
  52.                 // sia first che second sono a sinistra
  53.  
  54.                 return FindCommonAncestor(root.Left, first, second);
  55.             }
  56.             else
  57.             {
  58.                 return FindCommonAncestor(root.Right, first, second);
  59.             }
  60.         }
  61.  
  62.         private static void VerifyPathExists(Node root, Node child)
  63.         {
  64.             if (root == null)
  65.             {
  66.                 throw new Exception("Cannot find a path from root to children");
  67.             }
  68.  
  69.             if (root == child)
  70.             {
  71.                 return;
  72.             }
  73.  
  74.             if (root.Value == child.Value)
  75.             {
  76.                 throw new Exception("Duplicate value found");
  77.             }
  78.  
  79.             if (string.Compare(root.Value, child.Value) < 0)
  80.             {
  81.                 VerifyPathExists(root.Right, child);
  82.             }
  83.             else
  84.             {
  85.                 VerifyPathExists(root.Left, child);
  86.             }
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement