예제 #1
0
        private void InOrderIterative(BinaryTreeNode <T> node)
        {
            if (node == null)
            {
                return;
            }
            var stack   = new Stack.LinkedListImplementation.Stack <BinaryTreeNode <T> >();
            var pointer = node;

            while (true)
            {
                if (pointer != null)
                {
                    stack.PushInStack(pointer);
                    pointer = pointer.LeftChildNode;
                }
                else
                {
                    if (stack.IsStackEmpty)
                    {
                        break;
                    }
                    pointer = stack.PopFromStack();
                    Console.Write(pointer.Info);
                    pointer = pointer.RightChildNode;
                }
            }
        }
예제 #2
0
        private void PreOrderIterative(BinaryTreeNode <T> node)
        {
            if (node == null)
            {
                return;
            }
            var stack = new Stack.LinkedListImplementation.Stack <BinaryTreeNode <T> >();

            stack.PushInStack(node);
            while (!stack.IsStackEmpty)
            {
                var nodeRemoved = stack.PopFromStack();
                Console.Write(nodeRemoved.Info);
                if (nodeRemoved.RightChildNode != null)
                {
                    stack.PushInStack(nodeRemoved.RightChildNode);
                }
                if (nodeRemoved.LeftChildNode != null)
                {
                    stack.PushInStack(nodeRemoved.LeftChildNode);
                }
            }
        }
예제 #3
0
        private void PostOrderIterative(BinaryTreeNode <T> node)
        {
            if (node == null)
            {
                return;
            }
            var stackOne = new Stack.LinkedListImplementation.Stack <BinaryTreeNode <T> >();
            var stackTwo = new Stack.LinkedListImplementation.Stack <BinaryTreeNode <T> >();

            stackOne.PushInStack(node);
            while (!stackOne.IsStackEmpty)
            {
                Console.WriteLine("============Iteration Started=============");


                Console.WriteLine("\n\n\n========Before Start Check First Stack=====================\n\n\n");
                try
                {
                    Console.WriteLine("\nBefore Pop \n");
                    Console.WriteLine("Stack One: ");
                    stackOne.Display();
                }
                catch
                {
                    Console.WriteLine("Empty Stack");
                }
                Console.WriteLine("\n\n\n========First Stack Check Complete=====================\n\n\n");



                var removedElementFromStackOne = stackOne.PopFromStack();
                Console.WriteLine("\n\n\n========After Pop Check First Stack=====================\n\n\n");
                try
                {
                    Console.WriteLine("\nAfter Pop : \n");
                    Console.WriteLine("Stack One: ");
                    stackOne.Display();
                }
                catch
                {
                    Console.WriteLine("Empty Stack");
                }
                Console.WriteLine("\n\n\n========First Stack Check complete=====================\n\n\n");



                Console.WriteLine("\n\n\n========Going To Push In Second Stack=====================\n\n\n");
                Console.Write("\nRemoved from Stack One : " + removedElementFromStackOne.Info);
                Console.WriteLine("\n");
                Console.ReadKey();
                stackTwo.PushInStack(removedElementFromStackOne);
                Console.WriteLine("\n");
                Console.Write("Inserted In stack Two  " + removedElementFromStackOne.Info);
                Console.ReadKey();
                try
                {
                    Console.WriteLine("\nAfter push : \n");
                    Console.WriteLine("Stack Two: ");
                    stackTwo.Display();
                }
                catch
                {
                    Console.WriteLine("Empty Stack");
                }

                Console.WriteLine("\n\n\n========Push complete Second Stack Checked=====================\n\n\n");



                if (removedElementFromStackOne.LeftChildNode != null)
                {
                    Console.WriteLine("\n\n\n========Going To Push Left Child In First Stack=====================\n\n\n");
                    Console.WriteLine("\n");
                    Console.Write("Left Child Found " + removedElementFromStackOne.LeftChildNode.Info);
                    Console.ReadKey();
                    stackOne.PushInStack(removedElementFromStackOne.LeftChildNode);
                    Console.WriteLine("\n");
                    Console.Write("Left Child inserted in stack One  " + removedElementFromStackOne.LeftChildNode.Info);
                    Console.ReadKey();
                    try
                    {
                        Console.WriteLine("\nAfter Left child push : \n");
                        Console.WriteLine("Stack One: ");
                        stackOne.Display();
                    }
                    catch
                    {
                        Console.WriteLine("Empty Stack");
                    }
                    Console.WriteLine("\n\n\n========Left Child Push Completed =====================\n\n\n");
                }

                if (removedElementFromStackOne.RightChildNode != null)
                {
                    Console.WriteLine("\n\n\n========Going To Push Right Child In First Stack=====================\n\n\n");
                    Console.WriteLine("\n");
                    Console.Write("Right Child Found " + removedElementFromStackOne.RightChildNode.Info);
                    Console.ReadKey();
                    stackOne.PushInStack(removedElementFromStackOne.RightChildNode);
                    Console.WriteLine("\n");
                    Console.Write("Right Child Inserted In Stack One " + removedElementFromStackOne.RightChildNode.Info);
                    Console.ReadKey();
                    try
                    {
                        Console.WriteLine("\nAfter Right child push : \n");
                        Console.WriteLine("Stack One: ");
                        stackOne.Display();
                    }
                    catch
                    {
                        Console.WriteLine("Empty Stack");
                    }
                    Console.WriteLine("\n\n\n========Right Child Push Completed =====================\n\n\n");
                }

                Console.WriteLine("\n\n\n============All Stack Status=============\n\n");
                try
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Stack Two: ");
                    stackTwo.Display();
                }
                catch
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Empty Stack Two");
                }

                try
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Stack One: ");
                    stackOne.Display();
                }
                catch
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Empty Stack One");
                }

                Console.WriteLine("============Iteration Completed=============");
            }
        }