Esempio n. 1
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());
 }
Esempio n. 2
0
 public static int SumValues(BinTreeNode <int> r)
 {
     if (r == null)
     {
         return(0);
     }
     return(r.GetValue() + SumValues(r.GetLeft()) + SumValues(r.GetRight()));
 }
Esempio n. 3
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));
 }
Esempio n. 4
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()));
 }
Esempio n. 5
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()));
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            /*
             * in order:   H, E, A, J, I, C, B, D, G, F -> [H, E, A, J, I], [B, D, G, F] -> [H, E], [J, I], [G, F] ...
             * post order: E, H, J, I, A, B, F, G, D, C
             * Tree:    C
             *      /   \
             *     A     D
             *    / \   / \
             *   H   I B   G
             \  /       \
             \    E J        F
             */
            BinTreeNode <int> e = new BinTreeNode <int>(5);
            BinTreeNode <int> d = new BinTreeNode <int>(10);
            BinTreeNode <int> c = new BinTreeNode <int>(d, 1, e);
            BinTreeNode <int> b = new BinTreeNode <int>(9);
            BinTreeNode <int> a = new BinTreeNode <int>(b, 2, c); //root

            Console.WriteLine("Page One:");
            Console.WriteLine(IsInTree(a, 5));
            Console.WriteLine(CountVertices(a));
            Console.WriteLine(SumValues(a));
            Console.WriteLine(CountLeaves(a));
            Console.WriteLine(CountFullVertices(a));
            Console.WriteLine(CountPartialVertices(a));
            Console.WriteLine("Page Two:");
            //DivideEvens(a); //Leaving it off for other tests.
            Console.WriteLine(d.GetValue()); //example
            //AddZeroToPartial(a);
            //Console.WriteLine(c.GetLeft().GetValue()); //set c.Left = null and check
            Console.WriteLine(CountEvenVertices(a)); //Note: after running DivideEvens this should be 0 - mark it as a comment for a different result.
            Console.WriteLine(SumRightNodes(a));
            try
            {
                Console.WriteLine(GetParent(a, 1).GetValue());
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("No parent");
            }
            Console.WriteLine(IsAllEven(a));

            Console.WriteLine("Page Three:");
            Node <int> p = TurnToSortedList(null, a);

            //TurnToSortedList(a, null);
            while (p != null)
            {
                Console.WriteLine(p.GetValue());
                p = p.GetNext();
            }
        }
Esempio n. 7
0
 public static void FindNode(BinTreeNode <int> r, ref Node <int> temp, ref Node <int> p)
 {
     while (temp != null)
     {
         if (temp.GetValue() > r.GetValue())
         {
             break;
         }
         else
         {
             p    = temp;
             temp = temp.GetNext();
         }
     }
 }
Esempio n. 8
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);
        }