private ProgramTreeNode FindRoot(ProgramTreeNode node)
        {
            var ret = node;

            while (ret.Parent != null)
            {
                ret = ret.Parent;
            }
            return(ret);
        }
 private ProgramTreeNode GetImbalancedNode(ProgramTreeNode root, int[] distinctWeights)
 {
     if (root.Children.Count(c => GetSubTreeWeight(c) == distinctWeights.First()) == 1)
     {
         return(root.Children.Single(c => GetSubTreeWeight(c) == distinctWeights.First()));
     }
     else
     {
         return(root.Children.Single(c => c.Weight == distinctWeights.Last()));
     }
 }
 public int GetSubTreeWeight(ProgramTreeNode node)
 {
     if (!node.Children.Any())
     {
         return(node.Weight);
     }
     else
     {
         return(node.Weight + node.Children.Sum(GetSubTreeWeight));
     }
 }
 public bool IsBalanced(ProgramTreeNode root)
 {
     return(root.Children.Select(GetSubTreeWeight).Distinct().Count() == 1);
 }
 private ProgramTreeNode FindUnbalancedChild(ProgramTreeNode root)
 {
     return(root.Children.SingleOrDefault(c => !IsBalanced(c)));
 }
 public void AddChild(ProgramTreeNode child)
 {
     _children.Add(child);
     child.Parent = this;
 }