Exemplo n.º 1
0
 public static Tree GetNodeByValue(int val)
 {
     if( !nodeByValue.ContainsKey(val) ) {
         nodeByValue [val] = new Tree(val);
     }
     return nodeByValue [val];
 }
Exemplo n.º 2
0
 public static void GetFurthestNode(ref Tree FurthestNode, Tree parent, int depth=0)
 {
     if( depth > MaxDepth ) {
         MaxDepth = depth;
         FurthestNode = parent;
     }
     foreach (var child in parent.Children) {
         GetFurthestNode(ref FurthestNode, child, depth+1);
     }
 }
Exemplo n.º 3
0
 public static int GetSumSubtree(int sum, ref List<Tree> CorrectParents, Tree node)
 {
     int SubtreeSum = node.Value;
     foreach (var child in node.Children) {
         SubtreeSum += GetSumSubtree (sum, ref CorrectParents, child);
     }
     if (sum == SubtreeSum) {
         CorrectParents.Add (node);
     }
     return SubtreeSum;
 }
Exemplo n.º 4
0
 public static void GetSumPath(int sum, ref List<Tree> CorrectLeafs, Tree node)
 {
     sum -= node.Value;
     if (IsLeaf( node ) ) {
         if (0 == sum) {
             CorrectLeafs.Add (node);
         }
     } else {
         foreach (var child in node.Children) {
             GetSumPath (sum, ref CorrectLeafs, child);
         }
     }
 }
Exemplo n.º 5
0
 public static bool IsLeaf(Tree node)
 {
     return null != node.Parent && 0 == node.Children.Count;
 }
Exemplo n.º 6
0
 public static int PrintPathTo(Tree node)
 {
     int depth = 0;
     var path = new Stack<int>();
     while (null != node) {
         path.Push (node.Value);
         node = node.Parent;
         depth++;
     }
     while( path.Count > 1 ) {
         Console.Write("{0} -> ", path.Pop());
     }
     Console.WriteLine ("{0}", path.Pop ());
     return depth;
 }
Exemplo n.º 7
0
 public static bool IsRoot(Tree node)
 {
     return null == node.Parent;
 }
Exemplo n.º 8
0
 public static bool IsMiddleNode(Tree node)
 {
     return null != node.Parent && node.Children.Count > 0;
 }