Example #1
0
        private void PostOrderTraverse(Node <T> root)
        {
            HTStack <Node <T> > stack = new HTStack <Node <T> >();

            while (root != null || !stack.IsEmpty())
            {
                while (root != null)
                {
                    if (root.identity != 2)
                    {
                        root.identity++;
                        stack.Push(root);
                        root = root.leftchild;
                    }
                }
                while (!stack.IsEmpty() && stack.GetTop().identity == 2)
                {
                    root = stack.Pop();
                    Console.WriteLine(root.Data);
                }
                if (!stack.IsEmpty())
                {
                    stack.GetTop().identity++;
                    root = stack.GetTop().rightchild;
                }
            }
        }
Example #2
0
        private void InOrderTraverse(Node <T> root)
        {
            HTStack <Node <T> > stack = new HTStack <Node <T> >();

            while (root != null || !stack.IsEmpty())
            {
                while (root != null)
                {
                    stack.Push(root);
                    root = root.leftchild;
                }
                if (!stack.IsEmpty())
                {
                    root = stack.Pop();
                    Console.WriteLine(root.Data);
                    root = root.rightchild;
                }
            }
        }