示例#1
0
 public static void PrintList(Node start)
 {
     while(start != null)
     {
         Console.Write("{0}-->", start.Value);
         start = start.Next;
     }
 }
示例#2
0
 public void growTree(int maxHeight, String indent)
 {
     //Console.WriteLine(indent + m_weight.ToString());
     if (maxHeight > 0)
     {
         m_leftChild = new Node();
         m_leftChild.growTree(maxHeight - 1, indent + " ");
         m_rightChild = new Node();
         m_rightChild.growTree(maxHeight - 1, indent + " ");
     }
     else
         m_leftChild = m_rightChild = null;
 }
示例#3
0
        public static Node Reverse(Node start, Node originalStart)
        {
            // base case
            if (start.Next == null)
            {
                return start;
            }

            Node next = start.Next;
            Node reverseStart = Reverse(next);
            next.Next = start;

            if (start == originalStart)
            {
                start.Next = null;
            }

            return reverseStart;
        }
示例#4
0
        public static void Test1()
        {
            Node Node1 = new Node {Value = 1};
            Node Node2 = new Node {Value = 2};
            Node Node3 = new Node {Value = 3};
            Node Node4 = new Node {Value = 4};
            Node Node5 = new Node {Value = 5};
            Node Node6 = new Node {Value = 6};

            Node1.Next = Node2;
            Node2.Next = Node3;
            Node3.Next = Node4;
            Node4.Next = Node5;
            Node5.Next = Node6;

            ReverseLL1.PrintList(Node1);

            Node result = ReverseLL1.Reverse(Node1);

            Console.WriteLine();

            ReverseLL1.PrintList(result);
        }
示例#5
0
 public void rotateTree(ref Weight leftWeight, ref Weight rightWeight)
 {
     //Console.WriteLine("rotateTree(" + m_weight.ToString() + ") - begin");
     Node newLeftChild = null, newRightChild = null;
     int objCount = s_objCount;
     if (m_leftChild != null)
     {
         newRightChild = new Node();
         objCount++;
         newRightChild.m_leftChild = m_leftChild.m_leftChild;
         newRightChild.m_rightChild = m_leftChild.m_rightChild;
         newRightChild.m_weight = m_leftChild.m_weight;
     }
     if (m_rightChild != null)
     {
         newLeftChild = new Node();
         objCount++;
         newLeftChild.m_leftChild = m_rightChild.m_leftChild;
         newLeftChild.m_rightChild = m_rightChild.m_rightChild;
         newLeftChild.m_weight = m_rightChild.m_weight;
     }
     m_leftChild = newLeftChild;
     m_rightChild = newRightChild;
     for (int I = 0; I < 1024; I++) { int[] u = new int[1024]; }
     GC.Collect();
     if (m_rightChild != null)
     {
         if (m_rightChild.m_leftChild != null &&
             m_rightChild.m_rightChild != null)
         {
             m_rightChild.rotateTree(
                 ref m_rightChild.m_leftChild.m_weight,
                 ref m_rightChild.m_rightChild.m_weight);
         }
         else
         {
             Weight minus1 = null;
             m_rightChild.rotateTree(ref minus1, ref minus1);
         }
         if (leftWeight != m_rightChild.m_weight)
         {
             Console.WriteLine("left weight do not match.");
             throw new Exception();
         }
     }
     if (m_leftChild != null)
     {
         if (m_leftChild.m_leftChild != null &&
             m_leftChild.m_rightChild != null)
         {
             m_leftChild.rotateTree(
                 ref m_leftChild.m_leftChild.m_weight,
                 ref m_leftChild.m_rightChild.m_weight);
         }
         else
         {
             Weight minus1 = null;
             m_leftChild.rotateTree(ref minus1, ref minus1);
         }
         if (rightWeight != m_leftChild.m_weight)
         {
             Console.WriteLine("right weight do not match.");
             throw new Exception();
         }
     }
     //Console.WriteLine("rotateTree(" + m_weight.ToString() + ") - end");
 }
示例#6
0
 private static int Main()
 {
     Node root = new Node();
     root.growTree(4, "");
     root.rotateTree(ref root.m_leftChild.m_weight, ref root.m_rightChild.m_weight);
     return 100;
 }
示例#7
0
 public static Node Reverse(Node start)
 {
     return Reverse(start, start);
 }
示例#8
0
 private static int Main()
 {
     try
     {
         Node root = new Node();
         root.growTree(6, "").rotateTree(
             ref root.m_leftChild.m_weight,
             ref root.m_rightChild.m_weight);
     }
     catch (Exception)
     {
         Console.WriteLine("*** FAILED ***");
         return 1;
     }
     Console.WriteLine("*** PASSED ***");
     return 100;
 }
示例#9
0
            public virtual void rotateTree(ref int leftWeight, ref int rightWeight)
            {
                //Console.WriteLine("rotateTree(" + m_weight.ToString() + ")");
                VerifyValid();

                //	create node objects for children
                Node newLeftChild = null, newRightChild = null;
                if (m_leftChild != null)
                {
                    newRightChild = new Node();
                    newRightChild.m_leftChild = m_leftChild.m_leftChild;
                    newRightChild.m_rightChild = m_leftChild.m_rightChild;
                    newRightChild.m_weight = m_leftChild.m_weight;
                }
                if (m_rightChild != null)
                {
                    newLeftChild = new Node();
                    newLeftChild.m_leftChild = m_rightChild.m_leftChild;
                    newLeftChild.m_rightChild = m_rightChild.m_rightChild;
                    newLeftChild.m_weight = m_rightChild.m_weight;
                }

                //	replace children
                m_leftChild = newLeftChild;
                m_rightChild = newRightChild;

                for (int I = 0; I < 32; I++) { int[] u = new int[1024]; }

                //	verify all valid
                if (m_rightChild != null)
                {
                    if (m_rightChild.m_leftChild != null &&
                        m_rightChild.m_rightChild != null)
                    {
                        m_rightChild.m_leftChild.VerifyValid();
                        m_rightChild.m_rightChild.VerifyValid();
                        m_rightChild.rotateTree(
                            ref m_rightChild.m_leftChild.m_weight,
                            ref m_rightChild.m_rightChild.m_weight);
                    }
                    else
                    {
                        int minus1 = -1;
                        m_rightChild.rotateTree(ref minus1, ref minus1);
                    }
                    if (leftWeight != m_rightChild.m_weight)
                    {
                        Console.WriteLine("left weight do not match.");
                        throw new Exception();
                    }
                }
                if (m_leftChild != null)
                {
                    if (m_leftChild.m_leftChild != null &&
                        m_leftChild.m_rightChild != null)
                    {
                        m_leftChild.m_leftChild.VerifyValid();
                        m_leftChild.m_rightChild.VerifyValid();
                        m_leftChild.rotateTree(
                            ref m_leftChild.m_leftChild.m_weight,
                            ref m_leftChild.m_rightChild.m_weight);
                    }
                    else
                    {
                        int minus1 = -1;
                        m_leftChild.rotateTree(ref minus1, ref minus1);
                    }
                    if (rightWeight != m_leftChild.m_weight)
                    {
                        Console.WriteLine("right weight do not match.");
                        throw new Exception();
                    }
                }
            }