예제 #1
0
        public override string ToString()
        {
            string   s = "[";
            Node <T> p = this.first;

            while (p != null)
            {
                s = s + p.GetValue().ToString();
                if (p.GetNext() != null)
                {
                    s = s + ",";
                }
                p = p.GetNext();
            }
            s = s + "]";
            return(s);
        }
예제 #2
0
 public Node <T> Remove(Node <T> p)
 {
     if (first == p)
     {
         first = p.GetNext();
         return(first);
     }
     else
     {
         Node <T> prev = first;
         while (prev.GetNext() != p)
         {
             prev = prev.GetNext();
         }
         prev.SetNext(p.GetNext());
         return(prev.GetNext());
     }
 }
예제 #3
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();
            }
        }
예제 #4
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();
         }
     }
 }
예제 #5
0
        public Node <T> Insert(Node <T> p, T info)
        {
            Node <T> q = new Node <T>(info);

            if (p == null)
            {
                q.SetNext(first);
                first = q;
            }
            else
            {
                q.SetNext(p.GetNext());
                p.SetNext(q);
            }
            return(q);
        }