Exemple #1
0
 public static int SumRightNodes(BinTreeNode <int> r)
 {
     if (r.GetRight() == null)
     {
         return(0);
     }
     return(r.GetRight().GetValue() + SumRightNodes(r.GetRight()) + SumRightNodes(r.GetLeft()));
 }
Exemple #2
0
 public static int CountLeaves(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     if (r.GetRight() == null && r.GetLeft() == null)
     {
         return(1);
     }
     return(CountLeaves(r.GetRight()) + CountLeaves(r.GetLeft()));
 }
Exemple #3
0
 public static int CountEvenVertices(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     if (r.GetValue() % 2 == 0)
     {
         return(1 + CountEvenVertices(r.GetLeft()) + CountEvenVertices(r.GetRight()));
     }
     return(CountEvenVertices(r.GetLeft()) + CountEvenVertices(r.GetRight()));
 }
Exemple #4
0
 public static int CountPartialVertices(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     if ((r.GetRight() != null && r.GetLeft() == null) || r.GetLeft() != null && r.GetRight() == null)
     {
         return(1 + CountPartialVertices(r.GetRight()) + CountPartialVertices(r.GetLeft()));
     }
     return(CountPartialVertices(r.GetRight()) + CountPartialVertices(r.GetLeft()));
 }
Exemple #5
0
 public static BinTreeNode <int> GetParent(BinTreeNode <int> r, int v)
 {
     if (r.GetLeft() == null && r.GetRight() == null)
     {
         return(null);
     }
     if (r.GetLeft().GetValue() == v || r.GetRight().GetValue() == v)
     {
         return(r);
     }
     GetParent(r.GetLeft(), v);
     GetParent(r.GetRight(), v);
     return(null);
 }
Exemple #6
0
 public static int CountVertices(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     return(1 + CountVertices(r.GetLeft()) + CountVertices(r.GetRight()));
 }
Exemple #7
0
 public static bool IsInTree(BinTreeNode <int> r, int v)
 {
     if (r == null)
     {
         return(false);
     }
     if (r.GetValue() == v)
     {
         return(true);
     }
     return(IsInTree(r.GetLeft(), v) || IsInTree(r.GetRight(), v));
 }
Exemple #8
0
 public static bool IsAllEven(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(true);
     }
     if (r.GetValue() % 2 == 1)
     {
         return(false);
     }
     return(IsAllEven(r.GetRight()) && IsAllEven(r.GetLeft()));
 }
Exemple #9
0
 public static void AddZeroToPartial(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return;
     }
     if (r.GetLeft() != null && r.GetRight() == null)
     {
         r.SetRight(new BinTreeNode <int>(0));
         AddZeroToPartial(r.GetLeft());
     }
     else if (r.GetLeft() == null && r.GetRight() != null)
     {
         r.SetLeft(new BinTreeNode <int>(0));
         AddZeroToPartial(r.GetRight());
     }
     else
     {
         AddZeroToPartial(r.GetLeft());
         AddZeroToPartial(r.GetRight());
     }
 }
Exemple #10
0
 public static void DivideEvens(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return;
     }
     if (r.GetValue() % 2 == 0)
     {
         r.SetValue(r.GetValue() / 2);
     }
     DivideEvens(r.GetLeft());
     DivideEvens(r.GetRight());
 }
Exemple #11
0
        public static Node <int> TurnToSortedList(Node <int> h, BinTreeNode <int> r)
        {
            if (r == null)
            {
                return(h);
            }
            h = TurnToSortedList(h, r.GetLeft());
            Node <int> t    = new Node <int>(r.GetValue());
            Node <int> temp = h;
            Node <int> p    = null;

            if (temp == null)
            {
                h = t;
            }
            else
            {
                FindNode(r, ref temp, ref p);
                h = AttachNode(h, t, temp, p);
            }
            h = TurnToSortedList(h, r.GetRight());
            return(h);
        }