Пример #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()));
 }
Пример #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()));
 }
Пример #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()));
 }
Пример #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()));
 }
Пример #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);
 }
Пример #6
0
 public static int CountVertices(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     return(1 + CountVertices(r.GetLeft()) + CountVertices(r.GetRight()));
 }
Пример #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));
 }
Пример #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()));
 }
Пример #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());
     }
 }
Пример #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());
 }
Пример #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);
        }